authelia/internal/configuration/validator/notifier_test.go
James Elliott 8aade7f40e
[MISC] Update durations to notation format and housekeeping (#824)
* added regulation validator
* made regulations find_time and ban_time values duration notation strings
* added DefaultRegulationConfiguration for the validator
* made session expiration and inactivity values duration notation strings
* TOTP period does not need to be converted because adjustment should be discouraged
* moved TOTP defaults to DefaultTOTPConfiguration and removed the consts
* arranged the root config validator in configuration file order
* adjusted tests for the changes
* moved duration notation docs to root of configuration
* added references to duration notation where applicable
* project wide gofmt and goimports:
* run gofmt
* run goimports -local github.com/authelia/authelia -w on all files
* Make jwt_secret error uniform and add tests
* now at 100% coverage for internal/configuration/validator/configuration.go
2020-04-05 22:37:21 +10:00

122 lines
3.1 KiB
Go

package validator
import (
"testing"
"github.com/stretchr/testify/suite"
"github.com/authelia/authelia/internal/configuration/schema"
)
type NotifierSuite struct {
suite.Suite
configuration schema.NotifierConfiguration
}
func (s *NotifierSuite) SetupTest() {
s.configuration.SMTP = &schema.SMTPNotifierConfiguration{
Username: "john",
Password: "password",
Sender: "admin@example.com",
Host: "example.com",
Port: 25,
}
}
func (s *NotifierSuite) TestShouldEnsureAtLeastSMTPOrFilesystemIsProvided() {
validator := schema.NewStructValidator()
ValidateNotifier(&s.configuration, validator)
errors := validator.Errors()
s.Require().Len(errors, 0)
s.configuration.SMTP = nil
ValidateNotifier(&s.configuration, validator)
errors = validator.Errors()
s.Require().Len(errors, 1)
s.Assert().EqualError(errors[0], "Notifier should be either `smtp` or `filesystem`")
}
func (s *NotifierSuite) TestShouldEnsureEitherSMTPOrFilesystemIsProvided() {
validator := schema.NewStructValidator()
ValidateNotifier(&s.configuration, validator)
errors := validator.Errors()
s.Require().Len(errors, 0)
s.configuration.FileSystem = &schema.FileSystemNotifierConfiguration{
Filename: "test",
}
ValidateNotifier(&s.configuration, validator)
errors = validator.Errors()
s.Require().Len(errors, 1)
s.Assert().EqualError(errors[0], "Notifier should be either `smtp` or `filesystem`")
}
func (s *NotifierSuite) TestShouldEnsureFilenameOfFilesystemNotifierIsProvided() {
validator := schema.NewStructValidator()
s.configuration.SMTP = nil
s.configuration.FileSystem = &schema.FileSystemNotifierConfiguration{
Filename: "test",
}
ValidateNotifier(&s.configuration, validator)
errors := validator.Errors()
s.Require().Len(errors, 0)
s.configuration.FileSystem.Filename = ""
ValidateNotifier(&s.configuration, validator)
errors = validator.Errors()
s.Require().Len(errors, 1)
s.Assert().EqualError(errors[0], "Filename of filesystem notifier must not be empty")
}
func (s *NotifierSuite) TestShouldEnsureHostAndPortOfSMTPNotifierAreProvided() {
s.configuration.FileSystem = nil
validator := schema.NewStructValidator()
ValidateNotifier(&s.configuration, validator)
errors := validator.Errors()
s.Require().Len(errors, 0)
s.configuration.SMTP.Host = ""
s.configuration.SMTP.Port = 0
ValidateNotifier(&s.configuration, validator)
errors = validator.Errors()
s.Require().Len(errors, 2)
s.Assert().EqualError(errors[0], "Host of SMTP notifier must be provided")
s.Assert().EqualError(errors[1], "Port of SMTP notifier must be provided")
}
func (s *NotifierSuite) TestShouldEnsureSenderOfSMTPNotifierAreProvided() {
s.configuration.FileSystem = nil
validator := schema.NewStructValidator()
ValidateNotifier(&s.configuration, validator)
errors := validator.Errors()
s.Require().Len(errors, 0)
s.configuration.SMTP.Sender = ""
ValidateNotifier(&s.configuration, validator)
errors = validator.Errors()
s.Require().Len(errors, 1)
s.Assert().EqualError(errors[0], "Sender of SMTP notifier must be provided")
}
func TestNotifierSuite(t *testing.T) {
suite.Run(t, new(NotifierSuite))
}