2020-02-06 02:53:02 +00:00
package validator
import (
"testing"
"github.com/stretchr/testify/suite"
2020-04-05 12:37:21 +00:00
2021-08-11 01:04:35 +00:00
"github.com/authelia/authelia/v4/internal/configuration/schema"
2020-02-06 02:53:02 +00:00
)
type StorageSuite struct {
suite . Suite
2022-02-28 03:15:01 +00:00
config schema . StorageConfiguration
validator * schema . StructValidator
2020-02-06 02:53:02 +00:00
}
2021-01-04 10:28:55 +00:00
func ( suite * StorageSuite ) SetupTest ( ) {
suite . validator = schema . NewStructValidator ( )
2022-02-28 03:15:01 +00:00
suite . config . EncryptionKey = testEncryptionKey
suite . config . Local = nil
suite . config . PostgreSQL = nil
suite . config . MySQL = nil
2020-02-06 02:53:02 +00:00
}
2021-01-04 10:28:55 +00:00
func ( suite * StorageSuite ) TestShouldValidateOneStorageIsConfigured ( ) {
2022-02-28 03:15:01 +00:00
suite . config . Local = nil
suite . config . PostgreSQL = nil
suite . config . MySQL = nil
2020-02-06 02:53:02 +00:00
2022-02-28 03:15:01 +00:00
ValidateStorage ( suite . config , suite . validator )
2020-02-06 02:53:02 +00:00
2021-12-02 05:36:03 +00:00
suite . Require ( ) . Len ( suite . validator . Warnings ( ) , 0 )
2021-01-04 10:28:55 +00:00
suite . Require ( ) . Len ( suite . validator . Errors ( ) , 1 )
2021-12-02 05:36:03 +00:00
suite . Assert ( ) . EqualError ( suite . validator . Errors ( ) [ 0 ] , "storage: configuration for a 'local', 'mysql' or 'postgres' database must be provided" )
2020-02-06 02:53:02 +00:00
}
2021-01-04 10:28:55 +00:00
func ( suite * StorageSuite ) TestShouldValidateLocalPathIsProvided ( ) {
2022-02-28 03:15:01 +00:00
suite . config . Local = & schema . LocalStorageConfiguration {
2021-12-02 05:36:03 +00:00
Path : "" ,
}
2021-01-04 10:28:55 +00:00
2022-02-28 03:15:01 +00:00
ValidateStorage ( suite . config , suite . validator )
2021-01-04 10:28:55 +00:00
2021-12-02 05:36:03 +00:00
suite . Require ( ) . Len ( suite . validator . Warnings ( ) , 0 )
2021-01-04 10:28:55 +00:00
suite . Require ( ) . Len ( suite . validator . Errors ( ) , 1 )
2022-02-28 03:15:01 +00:00
suite . Assert ( ) . EqualError ( suite . validator . Errors ( ) [ 0 ] , "storage: local: option 'path' is required" )
2020-02-06 02:53:02 +00:00
2021-01-04 10:28:55 +00:00
suite . validator . Clear ( )
2022-02-28 03:15:01 +00:00
suite . config . Local . Path = "/myapth"
2020-02-06 02:53:02 +00:00
2022-02-28 03:15:01 +00:00
ValidateStorage ( suite . config , suite . validator )
2020-02-06 02:53:02 +00:00
2021-12-02 05:36:03 +00:00
suite . Require ( ) . Len ( suite . validator . Warnings ( ) , 0 )
suite . Require ( ) . Len ( suite . validator . Errors ( ) , 0 )
2020-02-06 02:53:02 +00:00
}
2021-12-02 05:36:03 +00:00
func ( suite * StorageSuite ) TestShouldValidateMySQLHostUsernamePasswordAndDatabaseAreProvided ( ) {
2022-02-28 03:15:01 +00:00
suite . config . MySQL = & schema . MySQLStorageConfiguration { }
ValidateStorage ( suite . config , suite . validator )
2020-02-06 02:53:02 +00:00
2021-12-02 05:36:03 +00:00
suite . Require ( ) . Len ( suite . validator . Errors ( ) , 3 )
2022-02-28 03:15:01 +00:00
suite . Assert ( ) . EqualError ( suite . validator . Errors ( ) [ 0 ] , "storage: mysql: option 'host' is required" )
suite . Assert ( ) . EqualError ( suite . validator . Errors ( ) [ 1 ] , "storage: mysql: option 'username' and 'password' are required" )
suite . Assert ( ) . EqualError ( suite . validator . Errors ( ) [ 2 ] , "storage: mysql: option 'database' is required" )
2020-02-06 02:53:02 +00:00
2021-01-04 10:28:55 +00:00
suite . validator . Clear ( )
2022-02-28 03:15:01 +00:00
suite . config . MySQL = & schema . MySQLStorageConfiguration {
2020-02-06 02:53:02 +00:00
SQLStorageConfiguration : schema . SQLStorageConfiguration {
2021-12-02 05:36:03 +00:00
Host : "localhost" ,
Username : "myuser" ,
Password : "pass" ,
Database : "database" ,
} ,
}
2022-02-28 03:15:01 +00:00
ValidateStorage ( suite . config , suite . validator )
2021-12-02 05:36:03 +00:00
suite . Require ( ) . Len ( suite . validator . Warnings ( ) , 0 )
suite . Require ( ) . Len ( suite . validator . Errors ( ) , 0 )
}
func ( suite * StorageSuite ) TestShouldValidatePostgreSQLHostUsernamePasswordAndDatabaseAreProvided ( ) {
2022-02-28 03:15:01 +00:00
suite . config . PostgreSQL = & schema . PostgreSQLStorageConfiguration { }
suite . config . MySQL = nil
ValidateStorage ( suite . config , suite . validator )
2021-12-02 05:36:03 +00:00
suite . Require ( ) . Len ( suite . validator . Errors ( ) , 3 )
2022-02-28 03:15:01 +00:00
suite . Assert ( ) . EqualError ( suite . validator . Errors ( ) [ 0 ] , "storage: postgres: option 'host' is required" )
suite . Assert ( ) . EqualError ( suite . validator . Errors ( ) [ 1 ] , "storage: postgres: option 'username' and 'password' are required" )
suite . Assert ( ) . EqualError ( suite . validator . Errors ( ) [ 2 ] , "storage: postgres: option 'database' is required" )
2021-12-02 05:36:03 +00:00
suite . validator . Clear ( )
2022-02-28 03:15:01 +00:00
suite . config . PostgreSQL = & schema . PostgreSQLStorageConfiguration {
2021-12-02 05:36:03 +00:00
SQLStorageConfiguration : schema . SQLStorageConfiguration {
Host : "postgre" ,
2020-02-06 02:53:02 +00:00
Username : "myuser" ,
Password : "pass" ,
Database : "database" ,
} ,
}
2022-02-28 03:15:01 +00:00
ValidateStorage ( suite . config , suite . validator )
2020-02-06 02:53:02 +00:00
2021-12-02 05:36:03 +00:00
suite . Assert ( ) . Len ( suite . validator . Warnings ( ) , 0 )
suite . Assert ( ) . Len ( suite . validator . Errors ( ) , 0 )
2020-02-06 02:53:02 +00:00
}
2021-12-03 06:29:55 +00:00
func ( suite * StorageSuite ) TestShouldValidatePostgresSSLModeAndSchemaDefaults ( ) {
2022-02-28 03:15:01 +00:00
suite . config . PostgreSQL = & schema . PostgreSQLStorageConfiguration {
2020-02-06 02:53:02 +00:00
SQLStorageConfiguration : schema . SQLStorageConfiguration {
2021-12-02 05:36:03 +00:00
Host : "db1" ,
2020-02-06 02:53:02 +00:00
Username : "myuser" ,
Password : "pass" ,
Database : "database" ,
} ,
}
2022-02-28 03:15:01 +00:00
ValidateStorage ( suite . config , suite . validator )
2021-01-04 10:28:55 +00:00
2021-12-02 05:36:03 +00:00
suite . Assert ( ) . Len ( suite . validator . Warnings ( ) , 0 )
suite . Assert ( ) . Len ( suite . validator . Errors ( ) , 0 )
2021-01-04 10:28:55 +00:00
2022-02-28 03:15:01 +00:00
suite . Assert ( ) . Equal ( "disable" , suite . config . PostgreSQL . SSL . Mode )
suite . Assert ( ) . Equal ( "public" , suite . config . PostgreSQL . Schema )
2021-12-03 06:29:55 +00:00
}
func ( suite * StorageSuite ) TestShouldValidatePostgresDefaultsDontOverrideConfiguration ( ) {
2022-02-28 03:15:01 +00:00
suite . config . PostgreSQL = & schema . PostgreSQLStorageConfiguration {
2021-12-03 06:29:55 +00:00
SQLStorageConfiguration : schema . SQLStorageConfiguration {
Host : "db1" ,
Username : "myuser" ,
Password : "pass" ,
Database : "database" ,
} ,
Schema : "authelia" ,
SSL : schema . PostgreSQLSSLStorageConfiguration {
Mode : "require" ,
} ,
}
2022-02-28 03:15:01 +00:00
ValidateStorage ( suite . config , suite . validator )
2021-12-03 06:29:55 +00:00
suite . Assert ( ) . Len ( suite . validator . Warnings ( ) , 0 )
suite . Assert ( ) . Len ( suite . validator . Errors ( ) , 0 )
2022-02-28 03:15:01 +00:00
suite . Assert ( ) . Equal ( "require" , suite . config . PostgreSQL . SSL . Mode )
suite . Assert ( ) . Equal ( "authelia" , suite . config . PostgreSQL . Schema )
2020-02-06 02:53:02 +00:00
}
2021-01-04 10:28:55 +00:00
func ( suite * StorageSuite ) TestShouldValidatePostgresSSLModeMustBeValid ( ) {
2022-02-28 03:15:01 +00:00
suite . config . PostgreSQL = & schema . PostgreSQLStorageConfiguration {
2020-02-06 02:53:02 +00:00
SQLStorageConfiguration : schema . SQLStorageConfiguration {
2021-12-02 05:36:03 +00:00
Host : "db2" ,
2020-02-06 02:53:02 +00:00
Username : "myuser" ,
Password : "pass" ,
Database : "database" ,
} ,
2021-12-02 05:36:03 +00:00
SSL : schema . PostgreSQLSSLStorageConfiguration {
Mode : "unknown" ,
} ,
2020-02-06 02:53:02 +00:00
}
2022-02-28 03:15:01 +00:00
ValidateStorage ( suite . config , suite . validator )
2021-01-04 10:28:55 +00:00
2021-12-02 05:36:03 +00:00
suite . Assert ( ) . Len ( suite . validator . Warnings ( ) , 0 )
2021-01-04 10:28:55 +00:00
suite . Require ( ) . Len ( suite . validator . Errors ( ) , 1 )
2022-02-28 03:15:01 +00:00
suite . Assert ( ) . EqualError ( suite . validator . Errors ( ) [ 0 ] , "storage: postgres: ssl: option 'mode' must be one of 'disable', 'require', 'verify-ca', 'verify-full' but it is configured as 'unknown'" )
2021-12-02 05:36:03 +00:00
}
2021-11-25 01:56:58 +00:00
func ( suite * StorageSuite ) TestShouldRaiseErrorOnNoEncryptionKey ( ) {
2022-02-28 03:15:01 +00:00
suite . config . EncryptionKey = ""
suite . config . Local = & schema . LocalStorageConfiguration {
2021-12-02 05:36:03 +00:00
Path : "/this/is/a/path" ,
}
2021-11-25 01:56:58 +00:00
2022-02-28 03:15:01 +00:00
ValidateStorage ( suite . config , suite . validator )
2021-11-25 01:56:58 +00:00
2021-12-02 05:36:03 +00:00
suite . Require ( ) . Len ( suite . validator . Warnings ( ) , 0 )
2021-11-25 01:56:58 +00:00
suite . Require ( ) . Len ( suite . validator . Errors ( ) , 1 )
2022-06-28 07:13:47 +00:00
suite . Assert ( ) . EqualError ( suite . validator . Errors ( ) [ 0 ] , "storage: option 'encryption_key' is required" )
2021-11-25 01:56:58 +00:00
}
func ( suite * StorageSuite ) TestShouldRaiseErrorOnShortEncryptionKey ( ) {
2022-02-28 03:15:01 +00:00
suite . config . EncryptionKey = "abc"
suite . config . Local = & schema . LocalStorageConfiguration {
2021-12-02 05:36:03 +00:00
Path : "/this/is/a/path" ,
}
2021-11-25 01:56:58 +00:00
2022-02-28 03:15:01 +00:00
ValidateStorage ( suite . config , suite . validator )
2021-11-25 01:56:58 +00:00
2021-12-02 05:36:03 +00:00
suite . Require ( ) . Len ( suite . validator . Warnings ( ) , 0 )
2021-11-25 01:56:58 +00:00
suite . Require ( ) . Len ( suite . validator . Errors ( ) , 1 )
2022-02-28 03:15:01 +00:00
suite . Assert ( ) . EqualError ( suite . validator . Errors ( ) [ 0 ] , "storage: option 'encryption_key' must be 20 characters or longer" )
2021-11-25 01:56:58 +00:00
}
2020-02-06 02:53:02 +00:00
func TestShouldRunStorageSuite ( t * testing . T ) {
suite . Run ( t , new ( StorageSuite ) )
}