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
|
|
|
|
configuration schema.StorageConfiguration
|
2021-01-04 10:28:55 +00:00
|
|
|
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()
|
|
|
|
suite.configuration.Local = &schema.LocalStorageConfiguration{
|
2020-02-06 02:53:02 +00:00
|
|
|
Path: "/this/is/a/path",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
func (suite *StorageSuite) TestShouldValidateOneStorageIsConfigured() {
|
|
|
|
suite.configuration.Local = nil
|
2020-02-06 02:53:02 +00:00
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
ValidateStorage(suite.configuration, suite.validator)
|
2020-02-06 02:53:02 +00:00
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
suite.Assert().False(suite.validator.HasWarnings())
|
|
|
|
suite.Require().Len(suite.validator.Errors(), 1)
|
|
|
|
suite.Assert().EqualError(suite.validator.Errors()[0], "A storage configuration must be provided. It could be 'local', 'mysql' or 'postgres'")
|
2020-02-06 02:53:02 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
func (suite *StorageSuite) TestShouldValidateLocalPathIsProvided() {
|
|
|
|
suite.configuration.Local.Path = ""
|
|
|
|
|
|
|
|
ValidateStorage(suite.configuration, suite.validator)
|
|
|
|
|
|
|
|
suite.Assert().False(suite.validator.HasWarnings())
|
|
|
|
suite.Require().Len(suite.validator.Errors(), 1)
|
|
|
|
|
|
|
|
suite.Assert().EqualError(suite.validator.Errors()[0], "A file path must be provided with key 'path'")
|
2020-02-06 02:53:02 +00:00
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
suite.validator.Clear()
|
|
|
|
suite.configuration.Local.Path = "/myapth"
|
2020-02-06 02:53:02 +00:00
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
ValidateStorage(suite.configuration, suite.validator)
|
2020-02-06 02:53:02 +00:00
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
suite.Assert().False(suite.validator.HasWarnings())
|
|
|
|
suite.Assert().False(suite.validator.HasErrors())
|
2020-02-06 02:53:02 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
func (suite *StorageSuite) TestShouldValidateSQLUsernamePasswordAndDatabaseAreProvided() {
|
|
|
|
suite.configuration.MySQL = &schema.MySQLStorageConfiguration{}
|
|
|
|
ValidateStorage(suite.configuration, suite.validator)
|
2020-02-06 02:53:02 +00:00
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
suite.Require().Len(suite.validator.Errors(), 2)
|
2021-03-22 09:04:09 +00:00
|
|
|
suite.Assert().EqualError(suite.validator.Errors()[0], "the SQL username and password must be provided")
|
|
|
|
suite.Assert().EqualError(suite.validator.Errors()[1], "the SQL database must be provided")
|
2020-02-06 02:53:02 +00:00
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
suite.validator.Clear()
|
|
|
|
suite.configuration.MySQL = &schema.MySQLStorageConfiguration{
|
2020-02-06 02:53:02 +00:00
|
|
|
SQLStorageConfiguration: schema.SQLStorageConfiguration{
|
|
|
|
Username: "myuser",
|
|
|
|
Password: "pass",
|
|
|
|
Database: "database",
|
|
|
|
},
|
|
|
|
}
|
2021-01-04 10:28:55 +00:00
|
|
|
ValidateStorage(suite.configuration, suite.validator)
|
2020-02-06 02:53:02 +00:00
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
suite.Assert().False(suite.validator.HasWarnings())
|
|
|
|
suite.Assert().False(suite.validator.HasErrors())
|
2020-02-06 02:53:02 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
func (suite *StorageSuite) TestShouldValidatePostgresSSLModeIsDisableByDefault() {
|
|
|
|
suite.configuration.PostgreSQL = &schema.PostgreSQLStorageConfiguration{
|
2020-02-06 02:53:02 +00:00
|
|
|
SQLStorageConfiguration: schema.SQLStorageConfiguration{
|
|
|
|
Username: "myuser",
|
|
|
|
Password: "pass",
|
|
|
|
Database: "database",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
ValidateStorage(suite.configuration, suite.validator)
|
|
|
|
|
|
|
|
suite.Assert().False(suite.validator.HasWarnings())
|
|
|
|
suite.Assert().False(suite.validator.HasErrors())
|
|
|
|
|
|
|
|
suite.Assert().Equal("disable", suite.configuration.PostgreSQL.SSLMode)
|
2020-02-06 02:53:02 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
func (suite *StorageSuite) TestShouldValidatePostgresSSLModeMustBeValid() {
|
|
|
|
suite.configuration.PostgreSQL = &schema.PostgreSQLStorageConfiguration{
|
2020-02-06 02:53:02 +00:00
|
|
|
SQLStorageConfiguration: schema.SQLStorageConfiguration{
|
|
|
|
Username: "myuser",
|
|
|
|
Password: "pass",
|
|
|
|
Database: "database",
|
|
|
|
},
|
|
|
|
SSLMode: "unknown",
|
|
|
|
}
|
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
ValidateStorage(suite.configuration, suite.validator)
|
|
|
|
|
|
|
|
suite.Assert().False(suite.validator.HasWarnings())
|
|
|
|
suite.Require().Len(suite.validator.Errors(), 1)
|
|
|
|
suite.Assert().EqualError(suite.validator.Errors()[0], "SSL mode must be 'disable', 'require', 'verify-ca', or 'verify-full'")
|
2020-02-06 02:53:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldRunStorageSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(StorageSuite))
|
|
|
|
}
|