mirror of
https://github.com/prometheus/alertmanager
synced 2025-02-16 10:37:09 +00:00
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:
parent
632ddb8463
commit
fe0622ab14
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user