mirror of
https://github.com/goharbor/harbor.git
synced 2025-01-10 18:07:42 +01:00
update ut cases
This commit is contained in:
parent
b25f5f9692
commit
ca15c0e093
@ -52,35 +52,60 @@ func (e *EmailAPI) Prepare() {
|
|||||||
|
|
||||||
// Ping tests connection and authentication with email server
|
// Ping tests connection and authentication with email server
|
||||||
func (e *EmailAPI) Ping() {
|
func (e *EmailAPI) Ping() {
|
||||||
settings := &struct {
|
var host, username, password, identity string
|
||||||
Host string `json:"email_host"`
|
var port int
|
||||||
Port *int `json:"email_port"`
|
var ssl bool
|
||||||
Username string `json:"email_username"`
|
body := e.Ctx.Input.CopyBody(1 << 32)
|
||||||
Password *string `json:"email_password"`
|
if body == nil || len(body) == 0 {
|
||||||
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 {
|
|
||||||
cfg, err := config.Email()
|
cfg, err := config.Email()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("failed to get email configurations: %v", err)
|
log.Errorf("failed to get email configurations: %v", err)
|
||||||
e.CustomAbort(http.StatusInternalServerError,
|
e.CustomAbort(http.StatusInternalServerError,
|
||||||
http.StatusText(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))
|
addr := net.JoinHostPort(host, strconv.Itoa(port))
|
||||||
if err := email.Ping(
|
if err := email.Ping(addr, identity, username,
|
||||||
addr, settings.Identity, settings.Username,
|
password, pingEmailTimeout, ssl, false); err != nil {
|
||||||
*settings.Password, pingEmailTimeout, settings.SSL, false); err != nil {
|
|
||||||
log.Debugf("ping %s failed: %v", addr, err)
|
log.Debugf("ping %s failed: %v", addr, err)
|
||||||
e.CustomAbort(http.StatusBadRequest, err.Error())
|
e.CustomAbort(http.StatusBadRequest, err.Error())
|
||||||
}
|
}
|
||||||
|
@ -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")
|
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 := `{
|
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_host": "smtp.gmail.com",
|
||||||
"email_port": 465,
|
"email_port": 465,
|
||||||
"email_identity": "",
|
"email_identity": "",
|
||||||
"email_username": "wrong_username",
|
"email_username": "wrong_username",
|
||||||
"email_password": "wrong_password",
|
|
||||||
"email_ssl": true
|
"email_ssl": true
|
||||||
}`
|
}`
|
||||||
|
|
||||||
@ -59,4 +71,13 @@ func TestPingEmail(t *testing.T) {
|
|||||||
t.Errorf("unexpected error: %s does not contains 535", body)
|
t.Errorf("unexpected error: %s does not contains 535", body)
|
||||||
return
|
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")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user