Skip to content

Commit

Permalink
enhance: emit an error when the Auth method is specified but no creds…
Browse files Browse the repository at this point in the history
… were passed (#35)
  • Loading branch information
colindean authored Jan 19, 2023
1 parent a3a745c commit 50bba83
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmd/vela-email/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ var (

// ErrorMissingSMTPParam is returned when the plugin is missing a smtp host or port parameter.
ErrorMissingSMTPParam = errors.New("missing smtp parameter (host/port)")

// ErrorAuthSpecifiedButCredentialsMissing is returned when the plugin is missing credentials when auth type was specified.
ErrorAuthSpecifiedButCredentialsMissing = errors.New("missing credentials when auth type was specified")
)

// Plugin represents the configuration loaded for the plugin.
Expand Down Expand Up @@ -141,6 +144,10 @@ func (p *Plugin) Validate() error {
return ErrorMissingSMTPParam
}

if len(p.Auth) > 0 && !(len(p.SMTPHost.Username) > 0 && len(p.SMTPHost.Password) > 0) {
return ErrorAuthSpecifiedButCredentialsMissing
}

// set defaults
if len(p.Email.Subject) == 0 {
p.Email.Subject = DefaultSubject
Expand Down
36 changes: 36 additions & 0 deletions cmd/vela-email/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,28 @@ func TestValidateSuccess(t *testing.T) {
Attachment: noAttachment,
},
},
{
name: "return no errors: auth specified with credentials",
parameters: Plugin{
Email: mockEmail,
EmailFilename: "",
SMTPHost: mockSMTPHost,
Attachment: noAttachment,
Auth: "LoginAuth",
},
},
{
name: "return no errors: auth not specified, credentials absent",
parameters: Plugin{
Email: mockEmail,
EmailFilename: "",
SMTPHost: &SMTPHost{
Host: "smtphost.com",
Port: "234234",
},
Attachment: noAttachment,
},
},
{
name: "return no errors: multiple To emails",
parameters: Plugin{
Expand Down Expand Up @@ -279,6 +301,20 @@ func TestValidateErrors(t *testing.T) {
},
wantErr: ErrorMissingSMTPParam,
},
{
name: "SMTP auth supplied but credentials missing",
parameters: Plugin{
Auth: "LoginAuth",
SMTPHost: &SMTPHost{
Host: "smtphost.com",
Port: "234234",
},
Email: mockEmail,
EmailFilename: "",
Attachment: noAttachment,
},
wantErr: ErrorAuthSpecifiedButCredentialsMissing,
},
}

for _, test := range tests {
Expand Down

0 comments on commit 50bba83

Please sign in to comment.