2020-01-21 20:15:40 +00:00
package validator
import (
2022-10-21 08:41:33 +00:00
"crypto/tls"
2021-08-07 03:58:08 +00:00
"fmt"
2021-11-30 11:15:21 +00:00
"net/mail"
2020-01-21 20:15:40 +00:00
"testing"
"github.com/stretchr/testify/suite"
2020-04-05 12:37:21 +00:00
2021-08-11 01:04:35 +00:00
"github.com/authelia/authelia/v4/internal/configuration/schema"
2020-01-21 20:15:40 +00:00
)
type NotifierSuite struct {
suite . Suite
2022-02-28 03:15:01 +00:00
config schema . NotifierConfiguration
validator * schema . StructValidator
2020-01-21 20:15:40 +00:00
}
2021-01-04 10:28:55 +00:00
func ( suite * NotifierSuite ) SetupTest ( ) {
suite . validator = schema . NewStructValidator ( )
2022-02-28 03:15:01 +00:00
suite . config . SMTP = & schema . SMTPNotifierConfiguration {
2020-01-21 20:15:40 +00:00
Username : "john" ,
Password : "password" ,
2021-11-30 11:15:21 +00:00
Sender : mail . Address { Name : "Authelia" , Address : "authelia@example.com" } ,
2022-10-21 08:41:33 +00:00
Host : examplecom ,
2020-01-21 20:15:40 +00:00
Port : 25 ,
}
2022-02-28 03:15:01 +00:00
suite . config . FileSystem = nil
2020-01-21 20:15:40 +00:00
}
2021-11-30 11:15:21 +00:00
/ *
2022-08-07 01:24:00 +00:00
Common Tests .
2021-11-30 11:15:21 +00:00
* /
2021-01-04 10:28:55 +00:00
func ( suite * NotifierSuite ) TestShouldEnsureAtLeastSMTPOrFilesystemIsProvided ( ) {
2022-02-28 03:15:01 +00:00
ValidateNotifier ( & suite . config , suite . validator )
2021-01-04 10:28:55 +00:00
2022-04-04 07:46:55 +00:00
suite . Assert ( ) . Len ( suite . validator . Warnings ( ) , 0 )
suite . Assert ( ) . Len ( suite . validator . Errors ( ) , 0 )
2021-01-04 10:28:55 +00:00
2022-02-28 03:15:01 +00:00
suite . config . SMTP = nil
2020-01-21 20:15:40 +00:00
2022-02-28 03:15:01 +00:00
ValidateNotifier ( & suite . config , suite . validator )
2020-01-21 20:15:40 +00:00
2022-04-04 07:46:55 +00:00
suite . Assert ( ) . Len ( suite . validator . Warnings ( ) , 0 )
2021-01-04 10:28:55 +00:00
suite . Require ( ) . True ( suite . validator . HasErrors ( ) )
2020-01-21 20:15:40 +00:00
2021-01-04 10:28:55 +00:00
suite . Assert ( ) . Len ( suite . validator . Errors ( ) , 1 )
2020-01-21 20:15:40 +00:00
2021-08-07 03:58:08 +00:00
suite . Assert ( ) . EqualError ( suite . validator . Errors ( ) [ 0 ] , errFmtNotifierNotConfigured )
2020-01-21 20:15:40 +00:00
}
2021-01-04 10:28:55 +00:00
func ( suite * NotifierSuite ) TestShouldEnsureEitherSMTPOrFilesystemIsProvided ( ) {
2022-02-28 03:15:01 +00:00
ValidateNotifier ( & suite . config , suite . validator )
2020-01-21 20:15:40 +00:00
2022-04-04 07:46:55 +00:00
suite . Assert ( ) . Len ( suite . validator . Errors ( ) , 0 )
2020-01-21 20:15:40 +00:00
2022-02-28 03:15:01 +00:00
suite . config . FileSystem = & schema . FileSystemNotifierConfiguration {
2020-01-21 20:15:40 +00:00
Filename : "test" ,
}
2022-02-28 03:15:01 +00:00
ValidateNotifier ( & suite . config , suite . validator )
2020-01-21 20:15:40 +00:00
2022-04-04 07:46:55 +00:00
suite . Assert ( ) . Len ( suite . validator . Warnings ( ) , 0 )
2021-01-04 10:28:55 +00:00
suite . Require ( ) . True ( suite . validator . HasErrors ( ) )
suite . Assert ( ) . Len ( suite . validator . Errors ( ) , 1 )
2020-01-21 20:15:40 +00:00
2021-08-07 03:58:08 +00:00
suite . Assert ( ) . EqualError ( suite . validator . Errors ( ) [ 0 ] , errFmtNotifierMultipleConfigured )
2021-01-04 10:28:55 +00:00
}
2020-01-21 20:15:40 +00:00
2021-11-30 11:15:21 +00:00
/ *
2022-08-07 01:24:00 +00:00
SMTP Tests .
2021-11-30 11:15:21 +00:00
* /
func ( suite * NotifierSuite ) TestSMTPShouldSetTLSDefaults ( ) {
2022-02-28 03:15:01 +00:00
ValidateNotifier ( & suite . config , suite . validator )
2021-01-04 10:28:55 +00:00
2022-04-04 07:46:55 +00:00
suite . Assert ( ) . Len ( suite . validator . Warnings ( ) , 0 )
suite . Assert ( ) . Len ( suite . validator . Errors ( ) , 0 )
2021-01-04 10:28:55 +00:00
2022-10-21 08:41:33 +00:00
suite . Assert ( ) . Equal ( examplecom , suite . config . SMTP . TLS . ServerName )
suite . Assert ( ) . Equal ( uint16 ( tls . VersionTLS12 ) , suite . config . SMTP . TLS . MinimumVersion . Value )
2022-02-28 03:15:01 +00:00
suite . Assert ( ) . False ( suite . config . SMTP . TLS . SkipVerify )
2021-11-30 11:15:21 +00:00
}
2022-07-18 00:56:09 +00:00
func ( suite * NotifierSuite ) TestSMTPShouldDefaultStartupCheckAddress ( ) {
suite . Assert ( ) . Equal ( mail . Address { Name : "" , Address : "" } , suite . config . SMTP . StartupCheckAddress )
ValidateNotifier ( & suite . config , suite . validator )
suite . Assert ( ) . Len ( suite . validator . Warnings ( ) , 0 )
suite . Assert ( ) . Len ( suite . validator . Errors ( ) , 0 )
suite . Assert ( ) . Equal ( mail . Address { Name : "Authelia Test" , Address : "test@authelia.com" } , suite . config . SMTP . StartupCheckAddress )
}
2021-11-30 11:15:21 +00:00
func ( suite * NotifierSuite ) TestSMTPShouldDefaultTLSServerNameToHost ( ) {
2022-02-28 03:15:01 +00:00
suite . config . SMTP . Host = "google.com"
suite . config . SMTP . TLS = & schema . TLSConfig {
2022-10-21 08:41:33 +00:00
MinimumVersion : schema . TLSVersion { Value : tls . VersionTLS11 } ,
2021-11-30 11:15:21 +00:00
}
2021-01-04 10:28:55 +00:00
2022-02-28 03:15:01 +00:00
ValidateNotifier ( & suite . config , suite . validator )
2021-01-04 10:28:55 +00:00
2022-04-04 07:46:55 +00:00
suite . Assert ( ) . Len ( suite . validator . Warnings ( ) , 0 )
suite . Assert ( ) . Len ( suite . validator . Errors ( ) , 0 )
2020-01-21 20:15:40 +00:00
2022-02-28 03:15:01 +00:00
suite . Assert ( ) . Equal ( "google.com" , suite . config . SMTP . TLS . ServerName )
2022-10-21 08:41:33 +00:00
suite . Assert ( ) . Equal ( uint16 ( tls . VersionTLS11 ) , suite . config . SMTP . TLS . MinimumVersion . MinVersion ( ) )
2022-02-28 03:15:01 +00:00
suite . Assert ( ) . False ( suite . config . SMTP . TLS . SkipVerify )
2021-01-04 10:28:55 +00:00
}
2022-10-21 08:41:33 +00:00
func ( suite * NotifierSuite ) TestSMTPShouldErrorOnSSL30 ( ) {
suite . config . SMTP . Host = examplecom
suite . config . SMTP . TLS = & schema . TLSConfig {
MinimumVersion : schema . TLSVersion { Value : tls . VersionSSL30 } , //nolint:staticcheck
}
ValidateNotifier ( & suite . config , suite . validator )
suite . Assert ( ) . Len ( suite . validator . Warnings ( ) , 0 )
suite . Require ( ) . Len ( suite . validator . Errors ( ) , 1 )
suite . Assert ( ) . EqualError ( suite . validator . Errors ( ) [ 0 ] , "notifier: smtp: tls: option 'minimum_version' is invalid: minimum version is TLS1.0 but SSL3.0 was configured" )
}
func ( suite * NotifierSuite ) TestSMTPShouldErrorOnTLSMinVerGreaterThanMaxVer ( ) {
suite . config . SMTP . Host = examplecom
suite . config . SMTP . TLS = & schema . TLSConfig {
MinimumVersion : schema . TLSVersion { Value : tls . VersionTLS13 } ,
MaximumVersion : schema . TLSVersion { Value : tls . VersionTLS10 } ,
}
ValidateNotifier ( & suite . config , suite . validator )
suite . Assert ( ) . Len ( suite . validator . Warnings ( ) , 0 )
suite . Require ( ) . Len ( suite . validator . Errors ( ) , 1 )
suite . Assert ( ) . EqualError ( suite . validator . Errors ( ) [ 0 ] , "notifier: smtp: tls: option combination of 'minimum_version' and 'maximum_version' is invalid: minimum version TLS1.3 is greater than the maximum version TLS1.0" )
}
func ( suite * NotifierSuite ) TestSMTPShouldWarnOnDisabledSTARTTLS ( ) {
suite . config . SMTP . Host = examplecom
suite . config . SMTP . DisableStartTLS = true
ValidateNotifier ( & suite . config , suite . validator )
suite . Require ( ) . Len ( suite . validator . Warnings ( ) , 1 )
suite . Assert ( ) . Len ( suite . validator . Errors ( ) , 0 )
suite . Assert ( ) . EqualError ( suite . validator . Warnings ( ) [ 0 ] , "notifier: smtp: option 'disable_starttls' is enabled: opportunistic STARTTLS is explicitly disabled which means all emails will be sent insecurely over plaintext and this setting is only necessary for non-compliant SMTP servers which advertise they support STARTTLS when they actually don't support STARTTLS" )
}
2021-11-30 11:15:21 +00:00
func ( suite * NotifierSuite ) TestSMTPShouldEnsureHostAndPortAreProvided ( ) {
2022-02-28 03:15:01 +00:00
suite . config . FileSystem = nil
ValidateNotifier ( & suite . config , suite . validator )
2021-01-04 10:28:55 +00:00
2022-04-04 07:46:55 +00:00
suite . Assert ( ) . Len ( suite . validator . Warnings ( ) , 0 )
suite . Assert ( ) . Len ( suite . validator . Errors ( ) , 0 )
2021-01-04 10:28:55 +00:00
2022-02-28 03:15:01 +00:00
suite . config . SMTP . Host = ""
suite . config . SMTP . Port = 0
2021-01-04 10:28:55 +00:00
2022-02-28 03:15:01 +00:00
ValidateNotifier ( & suite . config , suite . validator )
2021-01-04 10:28:55 +00:00
2022-04-04 07:46:55 +00:00
suite . Assert ( ) . Len ( suite . validator . Warnings ( ) , 0 )
2021-01-04 10:28:55 +00:00
suite . Assert ( ) . True ( suite . validator . HasErrors ( ) )
2020-01-21 20:15:40 +00:00
2021-01-04 10:28:55 +00:00
errors := suite . validator . Errors ( )
2020-01-21 20:15:40 +00:00
2021-01-04 10:28:55 +00:00
suite . Require ( ) . Len ( errors , 2 )
2020-01-21 20:15:40 +00:00
2021-08-07 03:58:08 +00:00
suite . Assert ( ) . EqualError ( errors [ 0 ] , fmt . Sprintf ( errFmtNotifierSMTPNotConfigured , "host" ) )
suite . Assert ( ) . EqualError ( errors [ 1 ] , fmt . Sprintf ( errFmtNotifierSMTPNotConfigured , "port" ) )
2020-01-21 20:15:40 +00:00
}
2021-11-30 11:15:21 +00:00
func ( suite * NotifierSuite ) TestSMTPShouldEnsureSenderIsProvided ( ) {
2022-02-28 03:15:01 +00:00
suite . config . SMTP . Sender = mail . Address { }
2021-11-30 11:15:21 +00:00
2022-02-28 03:15:01 +00:00
ValidateNotifier ( & suite . config , suite . validator )
2021-11-30 11:15:21 +00:00
2022-04-04 07:46:55 +00:00
suite . Assert ( ) . Len ( suite . validator . Warnings ( ) , 0 )
2021-11-30 11:15:21 +00: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-21 20:15:40 +00:00
2021-11-30 11:15:21 +00:00
/ *
2022-08-07 01:24:00 +00:00
File Tests .
2021-11-30 11:15:21 +00:00
* /
func ( suite * NotifierSuite ) TestFileShouldEnsureFilenameIsProvided ( ) {
2022-02-28 03:15:01 +00:00
suite . config . SMTP = nil
suite . config . FileSystem = & schema . FileSystemNotifierConfiguration {
2021-11-30 11:15:21 +00:00
Filename : "test" ,
}
2022-02-28 03:15:01 +00:00
ValidateNotifier ( & suite . config , suite . validator )
2020-01-21 20:15:40 +00:00
2022-04-04 07:46:55 +00:00
suite . Assert ( ) . Len ( suite . validator . Warnings ( ) , 0 )
suite . Assert ( ) . Len ( suite . validator . Errors ( ) , 0 )
2020-01-21 20:15:40 +00:00
2022-02-28 03:15:01 +00:00
suite . config . FileSystem . Filename = ""
2020-01-21 20:15:40 +00:00
2022-02-28 03:15:01 +00:00
ValidateNotifier ( & suite . config , suite . validator )
2021-01-04 10:28:55 +00:00
2022-04-04 07:46:55 +00:00
suite . Assert ( ) . Len ( suite . validator . Warnings ( ) , 0 )
2021-01-04 10:28:55 +00:00
suite . Require ( ) . True ( suite . validator . HasErrors ( ) )
suite . Assert ( ) . Len ( suite . validator . Errors ( ) , 1 )
2021-11-30 11:15:21 +00:00
suite . Assert ( ) . EqualError ( suite . validator . Errors ( ) [ 0 ] , errFmtNotifierFileSystemFileNameNotConfigured )
2020-01-21 20:15:40 +00:00
}
func TestNotifierSuite ( t * testing . T ) {
suite . Run ( t , new ( NotifierSuite ) )
}