2020-04-23 01:47:27 +00:00
package validator
import (
2021-04-16 01:44:37 +00:00
"fmt"
2020-04-23 01:47:27 +00:00
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2021-08-11 01:04:35 +00:00
"github.com/authelia/authelia/v4/internal/configuration/schema"
"github.com/authelia/authelia/v4/internal/utils"
2020-04-23 01:47:27 +00:00
)
func TestShouldValidateGoodKeys ( t * testing . T ) {
2022-04-16 09:00:39 +00:00
configKeys := schema . Keys
2020-04-23 01:47:27 +00:00
val := schema . NewStructValidator ( )
2021-08-03 09:55:21 +00:00
ValidateKeys ( configKeys , "AUTHELIA_" , val )
2020-04-23 01:47:27 +00:00
require . Len ( t , val . Errors ( ) , 0 )
}
func TestShouldNotValidateBadKeys ( t * testing . T ) {
2022-04-16 09:00:39 +00:00
configKeys := schema . Keys
2020-04-23 01:47:27 +00:00
configKeys = append ( configKeys , "bad_key" )
configKeys = append ( configKeys , "totp.skewy" )
val := schema . NewStructValidator ( )
2021-08-03 09:55:21 +00:00
ValidateKeys ( configKeys , "AUTHELIA_" , val )
2020-04-23 01:47:27 +00:00
errs := val . Errors ( )
require . Len ( t , errs , 2 )
2021-08-03 09:55:21 +00:00
assert . EqualError ( t , errs [ 0 ] , "configuration key not expected: bad_key" )
assert . EqualError ( t , errs [ 1 ] , "configuration key not expected: totp.skewy" )
}
func TestShouldNotValidateBadEnvKeys ( t * testing . T ) {
2022-04-16 09:00:39 +00:00
configKeys := schema . Keys
2021-08-03 09:55:21 +00:00
configKeys = append ( configKeys , "AUTHELIA__BAD_ENV_KEY" )
configKeys = append ( configKeys , "AUTHELIA_BAD_ENV_KEY" )
val := schema . NewStructValidator ( )
ValidateKeys ( configKeys , "AUTHELIA_" , val )
warns := val . Warnings ( )
assert . Len ( t , val . Errors ( ) , 0 )
require . Len ( t , warns , 2 )
assert . EqualError ( t , warns [ 0 ] , "configuration environment variable not expected: AUTHELIA__BAD_ENV_KEY" )
assert . EqualError ( t , warns [ 1 ] , "configuration environment variable not expected: AUTHELIA_BAD_ENV_KEY" )
2020-04-23 01:47:27 +00:00
}
func TestAllSpecificErrorKeys ( t * testing . T ) {
2020-09-04 03:20:17 +00:00
var configKeys [ ] string //nolint:prealloc // This is because the test is dynamic based on the keys that exist in the map.
2020-05-05 19:35:32 +00:00
2020-04-23 01:47:27 +00:00
var uniqueValues [ ] string
2022-04-15 09:30:51 +00:00
// Setup configKeys and uniqueValues expected.
2020-04-23 01:47:27 +00:00
for key , value := range specificErrorKeys {
configKeys = append ( configKeys , key )
2020-05-05 19:35:32 +00:00
2020-04-23 01:47:27 +00:00
if ! utils . IsStringInSlice ( value , uniqueValues ) {
uniqueValues = append ( uniqueValues , value )
}
}
val := schema . NewStructValidator ( )
2021-08-03 09:55:21 +00:00
ValidateKeys ( configKeys , "AUTHELIA_" , val )
2020-04-23 01:47:27 +00:00
errs := val . Errors ( )
// Check only unique errors are shown. Require because if we don't the next test panics.
require . Len ( t , errs , len ( uniqueValues ) )
// Dynamically check all specific errors.
for i , value := range uniqueValues {
assert . EqualError ( t , errs [ i ] , value )
}
}
func TestSpecificErrorKeys ( t * testing . T ) {
configKeys := [ ] string {
2021-04-16 01:44:37 +00:00
"notifier.smtp.trusted_cert" ,
"google_analytics" ,
2020-04-23 01:47:27 +00:00
"authentication_backend.file.password_options.algorithm" ,
"authentication_backend.file.password_options.iterations" , // This should not show another error since our target for the specific error is password_options.
"authentication_backend.file.password_hashing.algorithm" ,
"authentication_backend.file.hashing.algorithm" ,
}
val := schema . NewStructValidator ( )
2021-08-03 09:55:21 +00:00
ValidateKeys ( configKeys , "AUTHELIA_" , val )
2020-04-23 01:47:27 +00:00
errs := val . Errors ( )
require . Len ( t , errs , 5 )
2021-04-16 01:44:37 +00:00
assert . EqualError ( t , errs [ 0 ] , specificErrorKeys [ "notifier.smtp.trusted_cert" ] )
assert . EqualError ( t , errs [ 1 ] , specificErrorKeys [ "google_analytics" ] )
2020-04-23 01:47:27 +00:00
assert . EqualError ( t , errs [ 2 ] , specificErrorKeys [ "authentication_backend.file.password_options.iterations" ] )
assert . EqualError ( t , errs [ 3 ] , specificErrorKeys [ "authentication_backend.file.password_hashing.algorithm" ] )
assert . EqualError ( t , errs [ 4 ] , specificErrorKeys [ "authentication_backend.file.hashing.algorithm" ] )
}
2021-04-16 01:44:37 +00:00
func TestReplacedErrors ( t * testing . T ) {
configKeys := [ ] string {
"authentication_backend.ldap.skip_verify" ,
"authentication_backend.ldap.minimum_tls_version" ,
"notifier.smtp.disable_verify_cert" ,
"logs_file_path" ,
"logs_level" ,
}
val := schema . NewStructValidator ( )
2021-08-03 09:55:21 +00:00
ValidateKeys ( configKeys , "AUTHELIA_" , val )
2021-04-16 01:44:37 +00:00
warns := val . Warnings ( )
errs := val . Errors ( )
assert . Len ( t , warns , 0 )
require . Len ( t , errs , 5 )
assert . EqualError ( t , errs [ 0 ] , fmt . Sprintf ( errFmtReplacedConfigurationKey , "authentication_backend.ldap.skip_verify" , "authentication_backend.ldap.tls.skip_verify" ) )
assert . EqualError ( t , errs [ 1 ] , fmt . Sprintf ( errFmtReplacedConfigurationKey , "authentication_backend.ldap.minimum_tls_version" , "authentication_backend.ldap.tls.minimum_version" ) )
assert . EqualError ( t , errs [ 2 ] , fmt . Sprintf ( errFmtReplacedConfigurationKey , "notifier.smtp.disable_verify_cert" , "notifier.smtp.tls.skip_verify" ) )
2021-06-08 13:15:43 +00:00
assert . EqualError ( t , errs [ 3 ] , fmt . Sprintf ( errFmtReplacedConfigurationKey , "logs_file_path" , "log.file_path" ) )
assert . EqualError ( t , errs [ 4 ] , fmt . Sprintf ( errFmtReplacedConfigurationKey , "logs_level" , "log.level" ) )
2021-04-16 01:44:37 +00:00
}