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