2019-04-24 21:52:08 +00:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-01-21 20:15:40 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-04-05 12:37:21 +00:00
|
|
|
|
|
|
|
"github.com/authelia/authelia/internal/configuration/schema"
|
2019-04-24 21:52:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func newDefaultConfig() schema.Configuration {
|
|
|
|
config := schema.Configuration{}
|
2019-12-06 20:45:59 +00:00
|
|
|
config.Host = "127.0.0.1"
|
2019-04-24 21:52:08 +00:00
|
|
|
config.Port = 9090
|
2020-03-09 19:57:53 +00:00
|
|
|
config.LogLevel = "info"
|
2020-11-24 23:46:41 +00:00
|
|
|
config.LogFormat = "text"
|
2020-05-02 16:20:40 +00:00
|
|
|
config.JWTSecret = testJWTSecret
|
2019-04-24 21:52:08 +00:00
|
|
|
config.AuthenticationBackend.File = new(schema.FileAuthenticationBackendConfiguration)
|
|
|
|
config.AuthenticationBackend.File.Path = "/a/path"
|
|
|
|
config.Session = schema.SessionConfiguration{
|
|
|
|
Domain: "example.com",
|
|
|
|
Name: "authelia_session",
|
|
|
|
Secret: "secret",
|
|
|
|
}
|
2020-02-06 02:53:02 +00:00
|
|
|
config.Storage.Local = &schema.LocalStorageConfiguration{
|
|
|
|
Path: "abc",
|
2019-11-16 19:50:58 +00:00
|
|
|
}
|
2020-01-21 20:15:40 +00:00
|
|
|
config.Notifier = &schema.NotifierConfiguration{
|
|
|
|
FileSystem: &schema.FileSystemNotifierConfiguration{
|
|
|
|
Filename: "/tmp/file",
|
|
|
|
},
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldNotUpdateConfig(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultConfig()
|
|
|
|
|
2020-04-23 01:11:32 +00:00
|
|
|
ValidateConfiguration(&config, validator)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2020-01-21 20:15:40 +00:00
|
|
|
require.Len(t, validator.Errors(), 0)
|
2019-04-24 21:52:08 +00:00
|
|
|
assert.Equal(t, 9090, config.Port)
|
2020-03-09 19:57:53 +00:00
|
|
|
assert.Equal(t, "info", config.LogLevel)
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldValidateAndUpdatePort(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultConfig()
|
|
|
|
config.Port = 0
|
|
|
|
|
2020-04-23 01:11:32 +00:00
|
|
|
ValidateConfiguration(&config, validator)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2020-01-21 20:15:40 +00:00
|
|
|
require.Len(t, validator.Errors(), 0)
|
2019-04-24 21:52:08 +00:00
|
|
|
assert.Equal(t, 8080, config.Port)
|
|
|
|
}
|
|
|
|
|
2019-12-06 20:45:59 +00:00
|
|
|
func TestShouldValidateAndUpdateHost(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultConfig()
|
|
|
|
config.Host = ""
|
|
|
|
|
2020-04-23 01:11:32 +00:00
|
|
|
ValidateConfiguration(&config, validator)
|
2019-12-06 20:45:59 +00:00
|
|
|
|
2020-01-21 20:15:40 +00:00
|
|
|
require.Len(t, validator.Errors(), 0)
|
2019-12-06 20:45:59 +00:00
|
|
|
assert.Equal(t, "0.0.0.0", config.Host)
|
|
|
|
}
|
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
func TestShouldValidateAndUpdateLogsLevel(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultConfig()
|
2020-03-09 19:57:53 +00:00
|
|
|
config.LogLevel = ""
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2020-04-23 01:11:32 +00:00
|
|
|
ValidateConfiguration(&config, validator)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2020-01-21 20:15:40 +00:00
|
|
|
require.Len(t, validator.Errors(), 0)
|
2020-03-09 19:57:53 +00:00
|
|
|
assert.Equal(t, "info", config.LogLevel)
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
2020-01-21 20:15:40 +00:00
|
|
|
|
|
|
|
func TestShouldEnsureNotifierConfigIsProvided(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultConfig()
|
|
|
|
|
2020-04-23 01:11:32 +00:00
|
|
|
ValidateConfiguration(&config, validator)
|
2020-01-21 20:15:40 +00:00
|
|
|
require.Len(t, validator.Errors(), 0)
|
|
|
|
|
|
|
|
config.Notifier = nil
|
|
|
|
|
2020-04-23 01:11:32 +00:00
|
|
|
ValidateConfiguration(&config, validator)
|
2020-01-21 20:15:40 +00:00
|
|
|
require.Len(t, validator.Errors(), 1)
|
|
|
|
assert.EqualError(t, validator.Errors()[0], "A notifier configuration must be provided")
|
|
|
|
}
|
2020-02-04 21:18:02 +00:00
|
|
|
|
|
|
|
func TestShouldAddDefaultAccessControl(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultConfig()
|
|
|
|
|
2020-04-23 01:11:32 +00:00
|
|
|
ValidateConfiguration(&config, validator)
|
2020-02-04 21:18:02 +00:00
|
|
|
require.Len(t, validator.Errors(), 0)
|
|
|
|
assert.NotNil(t, config.AccessControl)
|
|
|
|
assert.Equal(t, "deny", config.AccessControl.DefaultPolicy)
|
|
|
|
}
|
2020-03-03 07:18:25 +00:00
|
|
|
|
|
|
|
func TestShouldRaiseErrorWhenTLSCertWithoutKeyIsProvided(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultConfig()
|
2020-05-02 16:20:40 +00:00
|
|
|
config.TLSCert = testTLSCert
|
2020-03-03 07:18:25 +00:00
|
|
|
|
2020-04-23 01:11:32 +00:00
|
|
|
ValidateConfiguration(&config, validator)
|
2020-03-03 07:18:25 +00:00
|
|
|
require.Len(t, validator.Errors(), 1)
|
|
|
|
assert.EqualError(t, validator.Errors()[0], "No TLS key provided, please check the \"tls_key\" which has been configured")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldRaiseErrorWhenTLSKeyWithoutCertIsProvided(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultConfig()
|
2020-05-02 16:20:40 +00:00
|
|
|
config.TLSKey = testTLSKey
|
2020-03-03 07:18:25 +00:00
|
|
|
|
2020-04-23 01:11:32 +00:00
|
|
|
ValidateConfiguration(&config, validator)
|
2020-03-03 07:18:25 +00:00
|
|
|
require.Len(t, validator.Errors(), 1)
|
|
|
|
assert.EqualError(t, validator.Errors()[0], "No TLS certificate provided, please check the \"tls_cert\" which has been configured")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldNotRaiseErrorWhenBothTLSCertificateAndKeyAreProvided(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultConfig()
|
2020-05-02 16:20:40 +00:00
|
|
|
config.TLSCert = testTLSCert
|
|
|
|
config.TLSKey = testTLSKey
|
2020-03-03 07:18:25 +00:00
|
|
|
|
2020-04-23 01:11:32 +00:00
|
|
|
ValidateConfiguration(&config, validator)
|
2020-03-03 07:18:25 +00:00
|
|
|
require.Len(t, validator.Errors(), 0)
|
|
|
|
}
|
2020-04-05 12:37:21 +00:00
|
|
|
|
|
|
|
func TestShouldRaiseErrorWithUndefinedJWTSecretKey(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultConfig()
|
|
|
|
config.JWTSecret = ""
|
|
|
|
|
2020-04-23 01:11:32 +00:00
|
|
|
ValidateConfiguration(&config, validator)
|
2020-04-05 12:37:21 +00:00
|
|
|
require.Len(t, validator.Errors(), 1)
|
|
|
|
assert.EqualError(t, validator.Errors()[0], "Provide a JWT secret using \"jwt_secret\" key")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldRaiseErrorWithBadDefaultRedirectionURL(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultConfig()
|
|
|
|
config.DefaultRedirectionURL = "abc"
|
|
|
|
|
2020-04-23 01:11:32 +00:00
|
|
|
ValidateConfiguration(&config, validator)
|
2020-04-05 12:37:21 +00:00
|
|
|
require.Len(t, validator.Errors(), 1)
|
|
|
|
assert.EqualError(t, validator.Errors()[0], "Unable to parse default redirection url")
|
|
|
|
}
|