-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix validation for email configuration
- Loading branch information
1 parent
fd85a3a
commit 92a43ee
Showing
4 changed files
with
181 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package conf | ||
|
||
import "testing" | ||
|
||
func TestEmailConfig_Validate(t *testing.T) { | ||
type fields struct { | ||
From string | ||
FromFile string | ||
To []string | ||
ToFile string | ||
SmtpHost string | ||
SmtpPort int | ||
SmtpUsername string | ||
SmtpUsernameFile string | ||
SmtpPassword string | ||
SmtpPasswordFile string | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "valid config, nothing defined", | ||
fields: fields{}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "valid config, no files", | ||
fields: fields{ | ||
From: "dyndns@yourdomain.tld", | ||
To: []string{"you@yourdomain.tld"}, | ||
SmtpHost: "localhost", | ||
SmtpPort: 25, | ||
SmtpUsername: "email", | ||
SmtpPassword: "secret", | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "valid config, use files", | ||
fields: fields{ | ||
FromFile: "from.txt", | ||
ToFile: "to.txt", | ||
SmtpHost: "localhost", | ||
SmtpPort: 25, | ||
SmtpUsernameFile: "user.txt", | ||
SmtpPasswordFile: "secret.txt", | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "valid config, mixed usage of files and actual values", | ||
fields: fields{ | ||
FromFile: "from.txt", | ||
To: []string{"mail@domain.tld"}, | ||
SmtpHost: "localhost", | ||
SmtpPort: 25, | ||
SmtpUsername: "user", | ||
SmtpPasswordFile: "secret.txt", | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "invalid config, missing from", | ||
fields: fields{ | ||
ToFile: "to.txt", | ||
SmtpHost: "localhost", | ||
SmtpPort: 25, | ||
SmtpUsernameFile: "user.txt", | ||
SmtpPasswordFile: "secret.txt", | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
conf := &EmailConfig{ | ||
From: tt.fields.From, | ||
FromFile: tt.fields.FromFile, | ||
To: tt.fields.To, | ||
ToFile: tt.fields.ToFile, | ||
SmtpHost: tt.fields.SmtpHost, | ||
SmtpPort: tt.fields.SmtpPort, | ||
SmtpUsername: tt.fields.SmtpUsername, | ||
SmtpUsernameFile: tt.fields.SmtpUsernameFile, | ||
SmtpPassword: tt.fields.SmtpPassword, | ||
SmtpPasswordFile: tt.fields.SmtpPasswordFile, | ||
} | ||
if err := conf.Validate(); (err != nil) != tt.wantErr { | ||
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
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