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
|
|
|
|
|
|
|
"github.com/authelia/authelia/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()
|
|
|
|
suite.configuration.Local = &schema.LocalStorageConfiguration{
|
2020-02-06 09:53:02 +07:00
|
|
|
Path: "/this/is/a/path",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 17:28:55 +07:00
|
|
|
func (suite *StorageSuite) TestShouldValidateOneStorageIsConfigured() {
|
|
|
|
suite.configuration.Local = 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-01-04 17:28:55 +07: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 09:53:02 +07:00
|
|
|
}
|
|
|
|
|
2021-01-04 17:28:55 +07: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 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-01-04 17:28:55 +07:00
|
|
|
suite.Assert().False(suite.validator.HasWarnings())
|
|
|
|
suite.Assert().False(suite.validator.HasErrors())
|
2020-02-06 09:53:02 +07:00
|
|
|
}
|
|
|
|
|
2021-01-04 17:28:55 +07:00
|
|
|
func (suite *StorageSuite) TestShouldValidateSQLUsernamePasswordAndDatabaseAreProvided() {
|
|
|
|
suite.configuration.MySQL = &schema.MySQLStorageConfiguration{}
|
|
|
|
ValidateStorage(suite.configuration, suite.validator)
|
2020-02-06 09:53:02 +07:00
|
|
|
|
2021-01-04 17:28:55 +07:00
|
|
|
suite.Require().Len(suite.validator.Errors(), 2)
|
|
|
|
suite.Assert().EqualError(suite.validator.Errors()[0], "Username and password must be provided")
|
|
|
|
suite.Assert().EqualError(suite.validator.Errors()[1], "A database 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{
|
|
|
|
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-01-04 17:28:55 +07:00
|
|
|
suite.Assert().False(suite.validator.HasWarnings())
|
|
|
|
suite.Assert().False(suite.validator.HasErrors())
|
2020-02-06 09:53:02 +07:00
|
|
|
}
|
|
|
|
|
2021-01-04 17:28:55 +07:00
|
|
|
func (suite *StorageSuite) TestShouldValidatePostgresSSLModeIsDisableByDefault() {
|
|
|
|
suite.configuration.PostgreSQL = &schema.PostgreSQLStorageConfiguration{
|
2020-02-06 09:53:02 +07:00
|
|
|
SQLStorageConfiguration: schema.SQLStorageConfiguration{
|
|
|
|
Username: "myuser",
|
|
|
|
Password: "pass",
|
|
|
|
Database: "database",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-01-04 17:28:55 +07: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 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{
|
|
|
|
Username: "myuser",
|
|
|
|
Password: "pass",
|
|
|
|
Database: "database",
|
|
|
|
},
|
|
|
|
SSLMode: "unknown",
|
|
|
|
}
|
|
|
|
|
2021-01-04 17:28:55 +07: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 09:53:02 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldRunStorageSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(StorageSuite))
|
|
|
|
}
|