2020-01-22 03:15:40 +07:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
2021-08-07 10:58:08 +07:00
|
|
|
"fmt"
|
2021-11-30 18:15:21 +07:00
|
|
|
"net/mail"
|
2020-01-22 03:15:40 +07:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
2020-04-05 19:37:21 +07:00
|
|
|
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
2020-01-22 03:15:40 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
type NotifierSuite struct {
|
|
|
|
suite.Suite
|
2022-02-28 10:15:01 +07:00
|
|
|
config schema.NotifierConfiguration
|
|
|
|
validator *schema.StructValidator
|
2020-01-22 03:15:40 +07:00
|
|
|
}
|
|
|
|
|
2021-01-04 17:28:55 +07:00
|
|
|
func (suite *NotifierSuite) SetupTest() {
|
|
|
|
suite.validator = schema.NewStructValidator()
|
2022-02-28 10:15:01 +07:00
|
|
|
suite.config.SMTP = &schema.SMTPNotifierConfiguration{
|
2020-01-22 03:15:40 +07:00
|
|
|
Username: "john",
|
|
|
|
Password: "password",
|
2021-11-30 18:15:21 +07:00
|
|
|
Sender: mail.Address{Name: "Authelia", Address: "authelia@example.com"},
|
2020-01-22 03:15:40 +07:00
|
|
|
Host: "example.com",
|
|
|
|
Port: 25,
|
|
|
|
}
|
2022-02-28 10:15:01 +07:00
|
|
|
suite.config.FileSystem = nil
|
2020-01-22 03:15:40 +07:00
|
|
|
}
|
|
|
|
|
2021-11-30 18:15:21 +07:00
|
|
|
/*
|
|
|
|
Common Tests.
|
|
|
|
*/
|
2021-01-04 17:28:55 +07:00
|
|
|
func (suite *NotifierSuite) TestShouldEnsureAtLeastSMTPOrFilesystemIsProvided() {
|
2022-02-28 10:15:01 +07:00
|
|
|
ValidateNotifier(&suite.config, suite.validator)
|
2021-01-04 17:28:55 +07:00
|
|
|
|
2022-04-04 14:46:55 +07:00
|
|
|
suite.Assert().Len(suite.validator.Warnings(), 0)
|
|
|
|
suite.Assert().Len(suite.validator.Errors(), 0)
|
2021-01-04 17:28:55 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
suite.config.SMTP = nil
|
2020-01-22 03:15:40 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
ValidateNotifier(&suite.config, suite.validator)
|
2020-01-22 03:15:40 +07:00
|
|
|
|
2022-04-04 14:46:55 +07:00
|
|
|
suite.Assert().Len(suite.validator.Warnings(), 0)
|
2021-01-04 17:28:55 +07:00
|
|
|
suite.Require().True(suite.validator.HasErrors())
|
2020-01-22 03:15:40 +07:00
|
|
|
|
2021-01-04 17:28:55 +07:00
|
|
|
suite.Assert().Len(suite.validator.Errors(), 1)
|
2020-01-22 03:15:40 +07:00
|
|
|
|
2021-08-07 10:58:08 +07:00
|
|
|
suite.Assert().EqualError(suite.validator.Errors()[0], errFmtNotifierNotConfigured)
|
2020-01-22 03:15:40 +07:00
|
|
|
}
|
|
|
|
|
2021-01-04 17:28:55 +07:00
|
|
|
func (suite *NotifierSuite) TestShouldEnsureEitherSMTPOrFilesystemIsProvided() {
|
2022-02-28 10:15:01 +07:00
|
|
|
ValidateNotifier(&suite.config, suite.validator)
|
2020-01-22 03:15:40 +07:00
|
|
|
|
2022-04-04 14:46:55 +07:00
|
|
|
suite.Assert().Len(suite.validator.Errors(), 0)
|
2020-01-22 03:15:40 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
suite.config.FileSystem = &schema.FileSystemNotifierConfiguration{
|
2020-01-22 03:15:40 +07:00
|
|
|
Filename: "test",
|
|
|
|
}
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
ValidateNotifier(&suite.config, suite.validator)
|
2020-01-22 03:15:40 +07:00
|
|
|
|
2022-04-04 14:46:55 +07:00
|
|
|
suite.Assert().Len(suite.validator.Warnings(), 0)
|
2021-01-04 17:28:55 +07:00
|
|
|
suite.Require().True(suite.validator.HasErrors())
|
|
|
|
|
|
|
|
suite.Assert().Len(suite.validator.Errors(), 1)
|
2020-01-22 03:15:40 +07:00
|
|
|
|
2021-08-07 10:58:08 +07:00
|
|
|
suite.Assert().EqualError(suite.validator.Errors()[0], errFmtNotifierMultipleConfigured)
|
2021-01-04 17:28:55 +07:00
|
|
|
}
|
2020-01-22 03:15:40 +07:00
|
|
|
|
2021-11-30 18:15:21 +07:00
|
|
|
/*
|
|
|
|
SMTP Tests.
|
|
|
|
*/
|
|
|
|
func (suite *NotifierSuite) TestSMTPShouldSetTLSDefaults() {
|
2022-02-28 10:15:01 +07:00
|
|
|
ValidateNotifier(&suite.config, suite.validator)
|
2021-01-04 17:28:55 +07:00
|
|
|
|
2022-04-04 14:46:55 +07:00
|
|
|
suite.Assert().Len(suite.validator.Warnings(), 0)
|
|
|
|
suite.Assert().Len(suite.validator.Errors(), 0)
|
2021-01-04 17:28:55 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
suite.Assert().Equal("example.com", suite.config.SMTP.TLS.ServerName)
|
|
|
|
suite.Assert().Equal("TLS1.2", suite.config.SMTP.TLS.MinimumVersion)
|
|
|
|
suite.Assert().False(suite.config.SMTP.TLS.SkipVerify)
|
2021-11-30 18:15:21 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *NotifierSuite) TestSMTPShouldDefaultTLSServerNameToHost() {
|
2022-02-28 10:15:01 +07:00
|
|
|
suite.config.SMTP.Host = "google.com"
|
|
|
|
suite.config.SMTP.TLS = &schema.TLSConfig{
|
2021-11-30 18:15:21 +07:00
|
|
|
MinimumVersion: "TLS1.1",
|
|
|
|
}
|
2021-01-04 17:28:55 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
ValidateNotifier(&suite.config, suite.validator)
|
2021-01-04 17:28:55 +07:00
|
|
|
|
2022-04-04 14:46:55 +07:00
|
|
|
suite.Assert().Len(suite.validator.Warnings(), 0)
|
|
|
|
suite.Assert().Len(suite.validator.Errors(), 0)
|
2020-01-22 03:15:40 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
suite.Assert().Equal("google.com", suite.config.SMTP.TLS.ServerName)
|
|
|
|
suite.Assert().Equal("TLS1.1", suite.config.SMTP.TLS.MinimumVersion)
|
|
|
|
suite.Assert().False(suite.config.SMTP.TLS.SkipVerify)
|
2021-01-04 17:28:55 +07:00
|
|
|
}
|
|
|
|
|
2021-11-30 18:15:21 +07:00
|
|
|
func (suite *NotifierSuite) TestSMTPShouldEnsureHostAndPortAreProvided() {
|
2022-02-28 10:15:01 +07:00
|
|
|
suite.config.FileSystem = nil
|
|
|
|
ValidateNotifier(&suite.config, suite.validator)
|
2021-01-04 17:28:55 +07:00
|
|
|
|
2022-04-04 14:46:55 +07:00
|
|
|
suite.Assert().Len(suite.validator.Warnings(), 0)
|
|
|
|
suite.Assert().Len(suite.validator.Errors(), 0)
|
2021-01-04 17:28:55 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
suite.config.SMTP.Host = ""
|
|
|
|
suite.config.SMTP.Port = 0
|
2021-01-04 17:28:55 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
ValidateNotifier(&suite.config, suite.validator)
|
2021-01-04 17:28:55 +07:00
|
|
|
|
2022-04-04 14:46:55 +07:00
|
|
|
suite.Assert().Len(suite.validator.Warnings(), 0)
|
2021-01-04 17:28:55 +07:00
|
|
|
suite.Assert().True(suite.validator.HasErrors())
|
2020-01-22 03:15:40 +07:00
|
|
|
|
2021-01-04 17:28:55 +07:00
|
|
|
errors := suite.validator.Errors()
|
2020-01-22 03:15:40 +07:00
|
|
|
|
2021-01-04 17:28:55 +07:00
|
|
|
suite.Require().Len(errors, 2)
|
2020-01-22 03:15:40 +07:00
|
|
|
|
2021-08-07 10:58:08 +07:00
|
|
|
suite.Assert().EqualError(errors[0], fmt.Sprintf(errFmtNotifierSMTPNotConfigured, "host"))
|
|
|
|
suite.Assert().EqualError(errors[1], fmt.Sprintf(errFmtNotifierSMTPNotConfigured, "port"))
|
2020-01-22 03:15:40 +07:00
|
|
|
}
|
|
|
|
|
2021-11-30 18:15:21 +07:00
|
|
|
func (suite *NotifierSuite) TestSMTPShouldEnsureSenderIsProvided() {
|
2022-02-28 10:15:01 +07:00
|
|
|
suite.config.SMTP.Sender = mail.Address{}
|
2021-11-30 18:15:21 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
ValidateNotifier(&suite.config, suite.validator)
|
2021-11-30 18:15:21 +07:00
|
|
|
|
2022-04-04 14:46:55 +07:00
|
|
|
suite.Assert().Len(suite.validator.Warnings(), 0)
|
2021-11-30 18:15:21 +07:00
|
|
|
suite.Require().True(suite.validator.HasErrors())
|
|
|
|
|
|
|
|
suite.Assert().Len(suite.validator.Errors(), 1)
|
|
|
|
|
|
|
|
suite.Assert().EqualError(suite.validator.Errors()[0], fmt.Sprintf(errFmtNotifierSMTPNotConfigured, "sender"))
|
|
|
|
}
|
2020-01-22 03:15:40 +07:00
|
|
|
|
2021-11-30 18:15:21 +07:00
|
|
|
/*
|
|
|
|
File Tests.
|
|
|
|
*/
|
|
|
|
func (suite *NotifierSuite) TestFileShouldEnsureFilenameIsProvided() {
|
2022-02-28 10:15:01 +07:00
|
|
|
suite.config.SMTP = nil
|
|
|
|
suite.config.FileSystem = &schema.FileSystemNotifierConfiguration{
|
2021-11-30 18:15:21 +07:00
|
|
|
Filename: "test",
|
|
|
|
}
|
2022-02-28 10:15:01 +07:00
|
|
|
ValidateNotifier(&suite.config, suite.validator)
|
2020-01-22 03:15:40 +07:00
|
|
|
|
2022-04-04 14:46:55 +07:00
|
|
|
suite.Assert().Len(suite.validator.Warnings(), 0)
|
|
|
|
suite.Assert().Len(suite.validator.Errors(), 0)
|
2020-01-22 03:15:40 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
suite.config.FileSystem.Filename = ""
|
2020-01-22 03:15:40 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
ValidateNotifier(&suite.config, suite.validator)
|
2021-01-04 17:28:55 +07:00
|
|
|
|
2022-04-04 14:46:55 +07:00
|
|
|
suite.Assert().Len(suite.validator.Warnings(), 0)
|
2021-01-04 17:28:55 +07:00
|
|
|
suite.Require().True(suite.validator.HasErrors())
|
|
|
|
|
|
|
|
suite.Assert().Len(suite.validator.Errors(), 1)
|
|
|
|
|
2021-11-30 18:15:21 +07:00
|
|
|
suite.Assert().EqualError(suite.validator.Errors()[0], errFmtNotifierFileSystemFileNameNotConfigured)
|
2020-01-22 03:15:40 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNotifierSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(NotifierSuite))
|
|
|
|
}
|