-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhancement: Add ability to attach files to email (#6)
* add attachment to email code & tests * clean * switch test image Co-authored-by: David May <[email protected]>
- Loading branch information
Showing
6 changed files
with
139 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ var ( | |
Username: "username", | ||
Password: "password", | ||
} | ||
|
||
noAttachment = &email.Attachment{ | ||
Filename: "", | ||
} | ||
|
@@ -38,10 +39,11 @@ var ( | |
} | ||
|
||
mockPlugin = &Plugin{ | ||
Email: mockEmail, | ||
SMTPHost: mockSMTPHost, | ||
Attachment: noAttachment, | ||
BuildEnv: mockBuildEnv, | ||
Email: mockEmail, | ||
EmailFilename: "", | ||
SMTPHost: mockSMTPHost, | ||
Attachment: noAttachment, | ||
BuildEnv: mockBuildEnv, | ||
} | ||
) | ||
|
||
|
@@ -68,9 +70,10 @@ func TestValidateSuccess(t *testing.T) { | |
{ | ||
name: "return no errors: single To email", | ||
parameters: Plugin{ | ||
Email: mockEmail, | ||
SMTPHost: mockSMTPHost, | ||
Attachment: noAttachment, | ||
Email: mockEmail, | ||
EmailFilename: "", | ||
SMTPHost: mockSMTPHost, | ||
Attachment: noAttachment, | ||
}, | ||
}, | ||
{ | ||
|
@@ -80,8 +83,9 @@ func TestValidateSuccess(t *testing.T) { | |
To: []string{"[email protected]", "[email protected]"}, | ||
From: "[email protected]", | ||
}, | ||
SMTPHost: mockSMTPHost, | ||
Attachment: noAttachment, | ||
EmailFilename: "", | ||
SMTPHost: mockSMTPHost, | ||
Attachment: noAttachment, | ||
}, | ||
}, | ||
{ | ||
|
@@ -91,6 +95,7 @@ func TestValidateSuccess(t *testing.T) { | |
To: []string{"[email protected]", "[email protected]"}, | ||
From: "[email protected]", | ||
}, | ||
EmailFilename: "", | ||
SMTPHost: &SMTPHost{ | ||
Host: "smtphost.com", | ||
Port: "587", | ||
|
@@ -113,8 +118,9 @@ func TestValidateSuccess(t *testing.T) { | |
Sender: "sender", | ||
ReadReceipt: []string{"idk"}, | ||
}, | ||
SMTPHost: mockSMTPHost, | ||
Attachment: noAttachment, | ||
EmailFilename: "", | ||
SMTPHost: mockSMTPHost, | ||
Attachment: noAttachment, | ||
}, | ||
}, | ||
{ | ||
|
@@ -124,9 +130,32 @@ func TestValidateSuccess(t *testing.T) { | |
To: []string{""}, | ||
From: "", | ||
}, | ||
SMTPHost: mockSMTPHost, | ||
EmailFilename: "testdata/example1.txt", | ||
SMTPHost: mockSMTPHost, | ||
Attachment: &email.Attachment{ | ||
Filename: "testdata/example1.txt", | ||
Filename: "", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "return no errors: add txt attachment to email", | ||
parameters: Plugin{ | ||
Email: mockEmail, | ||
EmailFilename: "", | ||
SMTPHost: mockSMTPHost, | ||
Attachment: &email.Attachment{ | ||
Filename: "", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "return no errors: add png image attachment to email", | ||
parameters: Plugin{ | ||
Email: mockEmail, | ||
EmailFilename: "", | ||
SMTPHost: mockSMTPHost, | ||
Attachment: &email.Attachment{ | ||
Filename: "testdata/vela.png", | ||
}, | ||
}, | ||
}, | ||
|
@@ -153,7 +182,8 @@ func TestValidateErrors(t *testing.T) { | |
Email: &email.Email{ | ||
From: "[email protected]", | ||
}, | ||
Attachment: noAttachment, | ||
EmailFilename: "", | ||
Attachment: noAttachment, | ||
}, | ||
wantErr: ErrorMissingEmailToParam, | ||
}, | ||
|
@@ -163,27 +193,51 @@ func TestValidateErrors(t *testing.T) { | |
Email: &email.Email{ | ||
To: []string{"[email protected]"}, | ||
}, | ||
Attachment: noAttachment, | ||
EmailFilename: "", | ||
Attachment: noAttachment, | ||
}, | ||
wantErr: ErrorMissingEmailFromParam, | ||
}, | ||
{ | ||
name: "Email parameters missing from attachment", | ||
name: "Email parameters missing from file", | ||
parameters: Plugin{ | ||
Email: &email.Email{ | ||
To: []string{""}, | ||
From: "", | ||
}, | ||
SMTPHost: mockSMTPHost, | ||
EmailFilename: "testdata/badattachment.txt", | ||
SMTPHost: mockSMTPHost, | ||
Attachment: &email.Attachment{ | ||
Filename: "testdata/badattachment.txt", | ||
Filename: "", | ||
}, | ||
}, | ||
wantErr: io.EOF, | ||
}, | ||
{ | ||
name: "Email file missing", | ||
parameters: Plugin{ | ||
EmailFilename: "testdata/doesnotexist.txt", | ||
Attachment: &email.Attachment{ | ||
Filename: "", | ||
}, | ||
}, | ||
wantErr: os.ErrNotExist, | ||
}, | ||
{ | ||
name: "Email file empty", | ||
parameters: Plugin{ | ||
EmailFilename: "testdata/empty.txt", | ||
Attachment: &email.Attachment{ | ||
Filename: "", | ||
}, | ||
}, | ||
wantErr: ErrorEmptyFile, | ||
}, | ||
{ | ||
name: "Email attachment missing", | ||
parameters: Plugin{ | ||
Email: mockEmail, | ||
EmailFilename: "", | ||
Attachment: &email.Attachment{ | ||
Filename: "testdata/doesnotexist.txt", | ||
}, | ||
|
@@ -193,16 +247,19 @@ func TestValidateErrors(t *testing.T) { | |
{ | ||
name: "Email attachment empty", | ||
parameters: Plugin{ | ||
Email: mockEmail, | ||
EmailFilename: "", | ||
Attachment: &email.Attachment{ | ||
Filename: "testdata/empty.txt", | ||
}, | ||
}, | ||
wantErr: ErrorEmptyAttach, | ||
wantErr: ErrorEmptyFile, | ||
}, | ||
{ | ||
name: "SMTP host missing", | ||
parameters: Plugin{ | ||
Email: mockEmail, | ||
Email: mockEmail, | ||
EmailFilename: "", | ||
SMTPHost: &SMTPHost{ | ||
Port: "1902", | ||
}, | ||
|
@@ -213,7 +270,8 @@ func TestValidateErrors(t *testing.T) { | |
{ | ||
name: "SMTP port missing", | ||
parameters: Plugin{ | ||
Email: mockEmail, | ||
Email: mockEmail, | ||
EmailFilename: "", | ||
SMTPHost: &SMTPHost{ | ||
Host: "smtphost.com", | ||
}, | ||
|
@@ -252,9 +310,10 @@ func TestInjectEnvSuccess(t *testing.T) { | |
Subject: DefaultSubject, | ||
Text: []byte("This is some text for repo: {{ .VELA_REPO_FULL_NAME }}"), | ||
}, | ||
SMTPHost: mockSMTPHost, | ||
Attachment: noAttachment, | ||
BuildEnv: mockBuildEnv, | ||
EmailFilename: "", | ||
SMTPHost: mockSMTPHost, | ||
Attachment: noAttachment, | ||
BuildEnv: mockBuildEnv, | ||
}, | ||
}, | ||
{ | ||
|
@@ -266,9 +325,10 @@ func TestInjectEnvSuccess(t *testing.T) { | |
Subject: "Commit failure on vela build: {{ .VELA_BUILD_NUMBER }}", | ||
Text: []byte("This is some text for repo: {{ .VELA_REPO_FULL_NAME }}"), | ||
}, | ||
SMTPHost: mockSMTPHost, | ||
Attachment: noAttachment, | ||
BuildEnv: mockBuildEnv, | ||
EmailFilename: "", | ||
SMTPHost: mockSMTPHost, | ||
Attachment: noAttachment, | ||
BuildEnv: mockBuildEnv, | ||
}, | ||
}, | ||
} | ||
|
@@ -319,9 +379,10 @@ func TestInjectEnvBadVar(t *testing.T) { | |
From: "[email protected]", | ||
Subject: "This is a bad subject {{ .SOME_OTHER_VARIABLE }}", | ||
}, | ||
SMTPHost: mockSMTPHost, | ||
Attachment: noAttachment, | ||
BuildEnv: mockBuildEnv, | ||
EmailFilename: "", | ||
SMTPHost: mockSMTPHost, | ||
Attachment: noAttachment, | ||
BuildEnv: mockBuildEnv, | ||
}, | ||
}, | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
From: [email protected] | ||
To: [email protected], [email protected] | ||
Bcc: [email protected] | ||
Cc: [email protected] | ||
Subject: Vela Pipeline for {{ .VELA_REPO_FULL_NAME }} {{ .VELA_BUILD_BRANCH }} | ||
Content-Type: text/plain | ||
|
||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.