138 lines
4.2 KiB
Go
138 lines
4.2 KiB
Go
package validator
|
|
|
|
import (
|
|
"runtime"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/authelia/authelia/internal/configuration/schema"
|
|
)
|
|
|
|
func newDefaultConfig() schema.Configuration {
|
|
config := schema.Configuration{}
|
|
config.Server.Host = loopback
|
|
config.Server.Port = 9090
|
|
config.Logging.Level = "info"
|
|
config.Logging.Format = "text"
|
|
config.JWTSecret = testJWTSecret
|
|
config.AuthenticationBackend.File = &schema.FileAuthenticationBackendConfiguration{
|
|
Path: "/a/path",
|
|
}
|
|
config.AccessControl = schema.AccessControlConfiguration{
|
|
DefaultPolicy: "two_factor",
|
|
}
|
|
config.Session = schema.SessionConfiguration{
|
|
Domain: "example.com",
|
|
Name: "authelia_session",
|
|
Secret: "secret",
|
|
}
|
|
config.Storage.Local = &schema.LocalStorageConfiguration{
|
|
Path: "abc",
|
|
}
|
|
config.Notifier = &schema.NotifierConfiguration{
|
|
FileSystem: &schema.FileSystemNotifierConfiguration{
|
|
Filename: "/tmp/file",
|
|
},
|
|
}
|
|
|
|
return config
|
|
}
|
|
|
|
func TestShouldEnsureNotifierConfigIsProvided(t *testing.T) {
|
|
validator := schema.NewStructValidator()
|
|
config := newDefaultConfig()
|
|
|
|
ValidateConfiguration(&config, validator)
|
|
require.Len(t, validator.Errors(), 0)
|
|
|
|
config.Notifier = nil
|
|
|
|
ValidateConfiguration(&config, validator)
|
|
require.Len(t, validator.Errors(), 1)
|
|
assert.EqualError(t, validator.Errors()[0], "A notifier configuration must be provided")
|
|
}
|
|
|
|
func TestShouldAddDefaultAccessControl(t *testing.T) {
|
|
validator := schema.NewStructValidator()
|
|
config := newDefaultConfig()
|
|
|
|
config.AccessControl.DefaultPolicy = ""
|
|
config.AccessControl.Rules = []schema.ACLRule{
|
|
{
|
|
Policy: "bypass",
|
|
Domains: []string{
|
|
"public.example.com",
|
|
},
|
|
},
|
|
}
|
|
|
|
ValidateConfiguration(&config, validator)
|
|
require.Len(t, validator.Errors(), 0)
|
|
assert.NotNil(t, config.AccessControl)
|
|
assert.Equal(t, "deny", config.AccessControl.DefaultPolicy)
|
|
}
|
|
|
|
func TestShouldRaiseErrorWithUndefinedJWTSecretKey(t *testing.T) {
|
|
validator := schema.NewStructValidator()
|
|
config := newDefaultConfig()
|
|
config.JWTSecret = ""
|
|
|
|
ValidateConfiguration(&config, validator)
|
|
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 = "bad_default_redirection_url"
|
|
|
|
ValidateConfiguration(&config, validator)
|
|
require.Len(t, validator.Errors(), 1)
|
|
assert.EqualError(t, validator.Errors()[0], "Value for \"default_redirection_url\" is invalid: the url 'bad_default_redirection_url' is not absolute because it doesn't start with a scheme like 'http://' or 'https://'")
|
|
}
|
|
|
|
func TestShouldNotOverrideCertificatesDirectoryAndShouldPassWhenBlank(t *testing.T) {
|
|
validator := schema.NewStructValidator()
|
|
config := newDefaultConfig()
|
|
ValidateConfiguration(&config, validator)
|
|
require.Len(t, validator.Errors(), 0)
|
|
|
|
require.Equal(t, "", config.CertificatesDirectory)
|
|
}
|
|
|
|
func TestShouldRaiseErrorOnInvalidCertificatesDirectory(t *testing.T) {
|
|
validator := schema.NewStructValidator()
|
|
config := newDefaultConfig()
|
|
config.CertificatesDirectory = "not-a-real-file.go"
|
|
|
|
ValidateConfiguration(&config, validator)
|
|
|
|
require.Len(t, validator.Errors(), 1)
|
|
|
|
if runtime.GOOS == "windows" {
|
|
assert.EqualError(t, validator.Errors()[0], "Error checking certificate directory: CreateFile not-a-real-file.go: The system cannot find the file specified.")
|
|
} else {
|
|
assert.EqualError(t, validator.Errors()[0], "Error checking certificate directory: stat not-a-real-file.go: no such file or directory")
|
|
}
|
|
|
|
validator = schema.NewStructValidator()
|
|
config.CertificatesDirectory = "const.go"
|
|
ValidateConfiguration(&config, validator)
|
|
|
|
require.Len(t, validator.Errors(), 1)
|
|
assert.EqualError(t, validator.Errors()[0], "The path const.go specified for certificate_directory is not a directory")
|
|
}
|
|
|
|
func TestShouldNotRaiseErrorOnValidCertificatesDirectory(t *testing.T) {
|
|
validator := schema.NewStructValidator()
|
|
config := newDefaultConfig()
|
|
config.CertificatesDirectory = "../../suites/common/ssl"
|
|
|
|
ValidateConfiguration(&config, validator)
|
|
|
|
require.Len(t, validator.Errors(), 0)
|
|
}
|