update ut cases

This commit is contained in:
Wenkai Yin 2017-03-16 13:59:29 +08:00
parent b25f5f9692
commit ca15c0e093
2 changed files with 68 additions and 22 deletions

View File

@ -52,35 +52,60 @@ func (e *EmailAPI) Prepare() {
// Ping tests connection and authentication with email server
func (e *EmailAPI) Ping() {
settings := &struct {
Host string `json:"email_host"`
Port *int `json:"email_port"`
Username string `json:"email_username"`
Password *string `json:"email_password"`
SSL bool `json:"email_ssl"`
Identity string `json:"email_identity"`
}{}
e.DecodeJSONReq(&settings)
if len(settings.Host) == 0 || settings.Port == nil {
e.CustomAbort(http.StatusBadRequest, "empty host or port")
}
if settings.Password == nil {
var host, username, password, identity string
var port int
var ssl bool
body := e.Ctx.Input.CopyBody(1 << 32)
if body == nil || len(body) == 0 {
cfg, err := config.Email()
if err != nil {
log.Errorf("failed to get email configurations: %v", err)
e.CustomAbort(http.StatusInternalServerError,
http.StatusText(http.StatusInternalServerError))
}
host = cfg.Host
port = cfg.Port
username = cfg.Username
password = cfg.Password
identity = cfg.Identity
ssl = cfg.SSL
} else {
settings := &struct {
Host string `json:"email_host"`
Port *int `json:"email_port"`
Username string `json:"email_username"`
Password *string `json:"email_password"`
SSL bool `json:"email_ssl"`
Identity string `json:"email_identity"`
}{}
e.DecodeJSONReq(&settings)
*settings.Password = cfg.Password
if len(settings.Host) == 0 || settings.Port == nil {
e.CustomAbort(http.StatusBadRequest, "empty host or port")
}
if settings.Password == nil {
cfg, err := config.Email()
if err != nil {
log.Errorf("failed to get email configurations: %v", err)
e.CustomAbort(http.StatusInternalServerError,
http.StatusText(http.StatusInternalServerError))
}
settings.Password = &cfg.Password
}
host = settings.Host
port = *settings.Port
username = settings.Username
password = *settings.Password
identity = settings.Identity
ssl = settings.SSL
}
addr := net.JoinHostPort(settings.Host, strconv.Itoa(*settings.Port))
if err := email.Ping(
addr, settings.Identity, settings.Username,
*settings.Password, pingEmailTimeout, settings.SSL, false); err != nil {
addr := net.JoinHostPort(host, strconv.Itoa(port))
if err := email.Ping(addr, identity, username,
password, pingEmailTimeout, ssl, false); err != nil {
log.Debugf("ping %s failed: %v", addr, err)
e.CustomAbort(http.StatusBadRequest, err.Error())
}

View File

@ -37,13 +37,25 @@ func TestPingEmail(t *testing.T) {
assert.Equal(401, code, "the status code of ping email server with non-admin user should be 401")
//case 2: secure connection with admin role
//case 2: bad request
settings := `{
"email_host": ""
}`
code, _, err = apiTest.PingEmail(*admin, []byte(settings))
if err != nil {
t.Errorf("failed to test ping email server: %v", err)
return
}
assert.Equal(400, code, "the status code of ping email server should be 400")
//case 3: secure connection with admin role
settings = `{
"email_host": "smtp.gmail.com",
"email_port": 465,
"email_identity": "",
"email_username": "wrong_username",
"email_password": "wrong_password",
"email_ssl": true
}`
@ -59,4 +71,13 @@ func TestPingEmail(t *testing.T) {
t.Errorf("unexpected error: %s does not contains 535", body)
return
}
//case 4: ping email server whose settings are read from config
code, _, err = apiTest.PingEmail(*admin, nil)
if err != nil {
t.Errorf("failed to test ping email server: %v", err)
return
}
assert.Equal(400, code, "the status code of ping email server should be 400")
}