notify/email: refactor tests (#3078)

We don't need to test `getPassword()` separately as it can be covered
with the tests already checking the various authentication methods.

Signed-off-by: Simon Pasquier <spasquie@redhat.com>

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
Simon Pasquier 2022-09-30 13:32:25 +02:00 committed by GitHub
parent 632ddb8463
commit fe0622ab14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -513,6 +513,16 @@ func TestEmailNotifyWithAuthentication(t *testing.T) {
errMsg: "Invalid username or password",
retry: true,
},
{
title: "wrong credentials (missing password file)",
updateCfg: func(cfg *config.EmailConfig) {
cfg.AuthUsername = c.Username
cfg.AuthPasswordFile = "/does/not/exist"
},
errMsg: "could not read",
retry: true,
},
{
title: "no credentials",
errMsg: "authentication Required",
@ -633,59 +643,3 @@ func TestEmailNoUsernameStillOk(t *testing.T) {
require.NoError(t, err)
require.Nil(t, a)
}
func TestEmailGetPassword(t *testing.T) {
passwordFile, err := os.CreateTemp("", "smtp-password")
require.NoError(t, err, "creating temp file failed")
_, err = passwordFile.WriteString("secret")
require.NoError(t, err, "writing to temp file failed")
for _, tc := range []struct {
title string
updateCfg func(*config.EmailConfig)
errMsg string
}{
{
title: "password from field",
updateCfg: func(cfg *config.EmailConfig) {
cfg.AuthPassword = "secret"
cfg.AuthPasswordFile = ""
},
},
{
title: "password from file field",
updateCfg: func(cfg *config.EmailConfig) {
cfg.AuthPassword = ""
cfg.AuthPasswordFile = passwordFile.Name()
},
},
{
title: "password file path incorrect",
updateCfg: func(cfg *config.EmailConfig) {
cfg.AuthPassword = ""
cfg.AuthPasswordFile = "/does/not/exist"
},
errMsg: "could not read",
},
} {
tc := tc
t.Run(tc.title, func(t *testing.T) {
email := &Email{
conf: &config.EmailConfig{},
}
tc.updateCfg(email.conf)
password, err := email.getPassword()
if len(tc.errMsg) > 0 {
require.Error(t, err)
require.Contains(t, err.Error(), tc.errMsg)
require.Equal(t, "", password)
} else {
require.Nil(t, err)
require.Equal(t, "secret", password)
}
})
}
}