2019-04-25 04:52:08 +07:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-12-24 09:14:52 +07:00
|
|
|
"github.com/authelia/authelia/internal/configuration/schema"
|
2019-04-25 04:52:08 +07:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-01-22 03:15:40 +07:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-04-25 04:52:08 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
func newDefaultConfig() schema.Configuration {
|
|
|
|
config := schema.Configuration{}
|
2019-12-07 03:45:59 +07:00
|
|
|
config.Host = "127.0.0.1"
|
2019-04-25 04:52:08 +07:00
|
|
|
config.Port = 9090
|
|
|
|
config.LogsLevel = "info"
|
|
|
|
config.JWTSecret = "a_secret"
|
|
|
|
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 09:53:02 +07:00
|
|
|
config.Storage.Local = &schema.LocalStorageConfiguration{
|
|
|
|
Path: "abc",
|
2019-11-17 02:50:58 +07:00
|
|
|
}
|
2020-01-22 03:15:40 +07:00
|
|
|
config.Notifier = &schema.NotifierConfiguration{
|
|
|
|
FileSystem: &schema.FileSystemNotifierConfiguration{
|
|
|
|
Filename: "/tmp/file",
|
|
|
|
},
|
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldNotUpdateConfig(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultConfig()
|
|
|
|
|
|
|
|
Validate(&config, validator)
|
|
|
|
|
2020-01-22 03:15:40 +07:00
|
|
|
require.Len(t, validator.Errors(), 0)
|
2019-04-25 04:52:08 +07:00
|
|
|
assert.Equal(t, 9090, config.Port)
|
|
|
|
assert.Equal(t, "info", config.LogsLevel)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldValidateAndUpdatePort(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultConfig()
|
|
|
|
config.Port = 0
|
|
|
|
|
|
|
|
Validate(&config, validator)
|
|
|
|
|
2020-01-22 03:15:40 +07:00
|
|
|
require.Len(t, validator.Errors(), 0)
|
2019-04-25 04:52:08 +07:00
|
|
|
assert.Equal(t, 8080, config.Port)
|
|
|
|
}
|
|
|
|
|
2019-12-07 03:45:59 +07:00
|
|
|
func TestShouldValidateAndUpdateHost(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultConfig()
|
|
|
|
config.Host = ""
|
|
|
|
|
|
|
|
Validate(&config, validator)
|
|
|
|
|
2020-01-22 03:15:40 +07:00
|
|
|
require.Len(t, validator.Errors(), 0)
|
2019-12-07 03:45:59 +07:00
|
|
|
assert.Equal(t, "0.0.0.0", config.Host)
|
|
|
|
}
|
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
func TestShouldValidateAndUpdateLogsLevel(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultConfig()
|
|
|
|
config.LogsLevel = ""
|
|
|
|
|
|
|
|
Validate(&config, validator)
|
|
|
|
|
2020-01-22 03:15:40 +07:00
|
|
|
require.Len(t, validator.Errors(), 0)
|
2019-04-25 04:52:08 +07:00
|
|
|
assert.Equal(t, "info", config.LogsLevel)
|
|
|
|
}
|
2020-01-22 03:15:40 +07:00
|
|
|
|
|
|
|
func TestShouldEnsureNotifierConfigIsProvided(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultConfig()
|
|
|
|
|
|
|
|
Validate(&config, validator)
|
|
|
|
require.Len(t, validator.Errors(), 0)
|
|
|
|
|
|
|
|
config.Notifier = nil
|
|
|
|
|
|
|
|
Validate(&config, validator)
|
|
|
|
require.Len(t, validator.Errors(), 1)
|
|
|
|
assert.EqualError(t, validator.Errors()[0], "A notifier configuration must be provided")
|
|
|
|
}
|
2020-02-05 04:18:02 +07:00
|
|
|
|
|
|
|
func TestShouldAddDefaultAccessControl(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultConfig()
|
|
|
|
|
|
|
|
Validate(&config, validator)
|
|
|
|
require.Len(t, validator.Errors(), 0)
|
|
|
|
assert.NotNil(t, config.AccessControl)
|
|
|
|
assert.Equal(t, "deny", config.AccessControl.DefaultPolicy)
|
|
|
|
}
|
2020-03-03 14:18:25 +07:00
|
|
|
|
|
|
|
func TestShouldRaiseErrorWhenTLSCertWithoutKeyIsProvided(t *testing.T) {
|
|
|
|
validator := schema.NewStructValidator()
|
|
|
|
config := newDefaultConfig()
|
|
|
|
config.TLSCert = "/tmp/cert.pem"
|
|
|
|
|
|
|
|
Validate(&config, validator)
|
|
|
|
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()
|
|
|
|
config.TLSKey = "/tmp/key.pem"
|
|
|
|
|
|
|
|
Validate(&config, validator)
|
|
|
|
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()
|
|
|
|
config.TLSCert = "/tmp/cert.pem"
|
|
|
|
config.TLSKey = "/tmp/key.pem"
|
|
|
|
|
|
|
|
Validate(&config, validator)
|
|
|
|
require.Len(t, validator.Errors(), 0)
|
|
|
|
}
|