2020-01-22 03:56:44 +07:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
2021-12-01 19:11:29 +07:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2020-01-22 03:56:44 +07:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-04-05 19:37:21 +07:00
|
|
|
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
2020-01-22 03:56:44 +07:00
|
|
|
)
|
|
|
|
|
2020-03-25 08:48:20 +07:00
|
|
|
func TestShouldSetDefaultTOTPValues(t *testing.T) {
|
2020-01-22 03:56:44 +07:00
|
|
|
validator := schema.NewStructValidator()
|
2021-12-01 19:11:29 +07:00
|
|
|
config := &schema.Configuration{
|
|
|
|
TOTP: &schema.TOTPConfiguration{},
|
|
|
|
}
|
2020-01-22 03:56:44 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
ValidateTOTP(config, validator)
|
2020-01-22 03:56:44 +07:00
|
|
|
|
|
|
|
require.Len(t, validator.Errors(), 0)
|
2021-12-01 19:11:29 +07:00
|
|
|
assert.Equal(t, "Authelia", config.TOTP.Issuer)
|
|
|
|
assert.Equal(t, schema.DefaultTOTPConfiguration.Algorithm, config.TOTP.Algorithm)
|
|
|
|
assert.Equal(t, schema.DefaultTOTPConfiguration.Skew, config.TOTP.Skew)
|
|
|
|
assert.Equal(t, schema.DefaultTOTPConfiguration.Period, config.TOTP.Period)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldNormalizeTOTPAlgorithm(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
|
|
|
|
config := &schema.Configuration{
|
|
|
|
TOTP: &schema.TOTPConfiguration{
|
|
|
|
Algorithm: "sha1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ValidateTOTP(config, validator)
|
|
|
|
|
|
|
|
assert.Len(t, validator.Errors(), 0)
|
|
|
|
assert.Equal(t, "SHA1", config.TOTP.Algorithm)
|
2020-03-25 08:48:20 +07:00
|
|
|
}
|
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
func TestShouldRaiseErrorWhenInvalidTOTPAlgorithm(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
|
|
|
|
config := &schema.Configuration{
|
|
|
|
TOTP: &schema.TOTPConfiguration{
|
|
|
|
Algorithm: "sha3",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ValidateTOTP(config, validator)
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
require.Len(t, validator.Errors(), 1)
|
2022-02-28 10:15:01 +07:00
|
|
|
assert.EqualError(t, validator.Errors()[0], fmt.Sprintf(errFmtTOTPInvalidAlgorithm, strings.Join(schema.TOTPPossibleAlgorithms, "', '"), "SHA3"))
|
2021-12-01 19:11:29 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldRaiseErrorWhenInvalidTOTPValues(t *testing.T) {
|
2020-03-25 08:48:20 +07:00
|
|
|
validator := schema.NewStructValidator()
|
2021-12-01 19:11:29 +07:00
|
|
|
config := &schema.Configuration{
|
|
|
|
TOTP: &schema.TOTPConfiguration{
|
|
|
|
Period: 5,
|
|
|
|
Digits: 20,
|
|
|
|
},
|
2020-03-25 08:48:20 +07:00
|
|
|
}
|
2021-12-01 19:11:29 +07:00
|
|
|
|
|
|
|
ValidateTOTP(config, validator)
|
|
|
|
|
|
|
|
require.Len(t, validator.Errors(), 2)
|
|
|
|
assert.EqualError(t, validator.Errors()[0], fmt.Sprintf(errFmtTOTPInvalidPeriod, 5))
|
|
|
|
assert.EqualError(t, validator.Errors()[1], fmt.Sprintf(errFmtTOTPInvalidDigits, 20))
|
2020-01-22 03:56:44 +07:00
|
|
|
}
|