From dfae869dceb55bdf5327880d4db7e5d4b8877535 Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Fri, 3 Mar 2017 18:02:08 +0800 Subject: [PATCH] add test cases --- docs/swagger.yaml | 46 +++++++++++++++++++++ src/common/utils/email/mail.go | 17 ++++---- src/common/utils/email/mail_test.go | 42 +++++++++++++++++++ src/ui/api/email.go | 2 +- src/ui/api/email_test.go | 63 +++++++++++++++++++++++++++++ src/ui/api/harborapi_test.go | 9 +++++ 6 files changed, 169 insertions(+), 10 deletions(-) create mode 100644 src/common/utils/email/mail_test.go create mode 100644 src/ui/api/email_test.go diff --git a/docs/swagger.yaml b/docs/swagger.yaml index dcbf73c6b..d2fbfe3dd 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1475,6 +1475,31 @@ paths: description: User does not have permission of admin role. 500: description: Unexpected internal errors. + /email/ping: + post: + summary: Test connection and authentication with email server. + description: | + Test connection and authentication with email server. + parameters: + - name: settings + in: body + description: Email server settings, if some of the settings are not assigned, they will be read from system configuration. + required: false + schema: + $ref: '#/definitions/EmailServerSetting' + tags: + - Products + responses: + 200: + description: Ping email server successfully. + 400: + description: Inviald email server settings. + 401: + description: User need to login first. + 403: + description: Only admin has this authority. + 500: + description: Unexpected internal errors. definitions: Search: type: object @@ -1983,3 +2008,24 @@ definitions: error: type: string description: fail reason. + EmailServerSetting: + type: object + properties: + email_host: + type: string + description: The host of email server. + email_port: + type: string + description: The port of email server. + email_username: + type: string + description: The username of email server. + email_password: + type: string + description: The password of email server. + email_ssl: + type: string + description: Use ssl/tls or not. + email_identity: + type: string + description: The dentity of email server. diff --git a/src/common/utils/email/mail.go b/src/common/utils/email/mail.go index ff1c9e4ce..11d94026e 100644 --- a/src/common/utils/email/mail.go +++ b/src/common/utils/email/mail.go @@ -119,14 +119,13 @@ func sendMailWithTLS(m Mail, auth smtp.Auth, content []byte) error { } // Ping tests the connection and authentication with email server -// If tls is true, a secure connection is established, or the -// connection is insecure, and if starttls is true, Ping trys to -// upgrate the insecure connection to a secure one if email server -// supports it. -// Ping doesn't verify the server's certificate and hostname -// if the parameter insecure is ture when the connection is insecure +// If tls is true, a secure connection is established, or Ping +// trys to upgrate the insecure connection to a secure one if +// email server supports it. +// Ping doesn't verify the server's certificate and hostname when +// needed if the parameter insecure is ture func Ping(addr, identity, username, password string, - timeout int, tls, starttls, insecure bool) (err error) { + timeout int, tls, insecure bool) (err error) { log.Debugf("establishing TCP connection with %s ...", addr) conn, err := net.DialTimeout("tcp", addr, time.Duration(timeout)*time.Second) @@ -161,8 +160,8 @@ func Ping(addr, identity, username, password string, } defer client.Close() - //swith to SSL/TLS - if !tls && starttls { + //try to swith to SSL/TLS + if !tls { if ok, _ := client.Extension("STARTTLS"); ok { log.Debugf("switching the connection with %s to SSL/TLS ...", addr) if err = client.StartTLS(&tlspkg.Config{ diff --git a/src/common/utils/email/mail_test.go b/src/common/utils/email/mail_test.go new file mode 100644 index 000000000..3ece8d748 --- /dev/null +++ b/src/common/utils/email/mail_test.go @@ -0,0 +1,42 @@ +/* + Copyright (c) 2016 VMware, Inc. All Rights Reserved. + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package email + +import ( + "strings" + "testing" +) + +func TestPing(t *testing.T) { + addr := "smtp.gmail.com:465" + identity := "" + username := "wrong_username" + password := "wrong_password" + timeout := 60 + tls := true + insecure := false + + // test secure connection + err := Ping(addr, identity, username, password, + timeout, tls, insecure) + if err == nil { + t.Errorf("there should be an auth error") + } else { + if !strings.Contains(err.Error(), "535") { + t.Errorf("unexpected error: %v", err) + } + } +} diff --git a/src/ui/api/email.go b/src/ui/api/email.go index 7cb3c5c27..4bd6ecc1b 100644 --- a/src/ui/api/email.go +++ b/src/ui/api/email.go @@ -110,7 +110,7 @@ func (e *EmailAPI) Ping() { addr := net.JoinHostPort(settings.Host, strconv.Itoa(settings.Port)) if err := email.Ping( addr, settings.Identity, settings.Username, - settings.Password, pingEmailTimeout, settings.SSL, true, false); err != nil { + settings.Password, pingEmailTimeout, settings.SSL, false); err != nil { log.Debugf("ping %s failed: %v", addr, err) e.CustomAbort(http.StatusBadRequest, err.Error()) } diff --git a/src/ui/api/email_test.go b/src/ui/api/email_test.go new file mode 100644 index 000000000..ac1f29559 --- /dev/null +++ b/src/ui/api/email_test.go @@ -0,0 +1,63 @@ +/* + Copyright (c) 2016 VMware, Inc. All Rights Reserved. + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package api + +import ( + "fmt" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + comcfg "github.com/vmware/harbor/src/common/config" +) + +func TestPingEmail(t *testing.T) { + fmt.Println("Testing ping email server") + assert := assert.New(t) + apiTest := newHarborAPI() + + //case 1: ping email server without admin role + code, _, err := apiTest.PingEmail(*testUser, nil) + if err != nil { + t.Errorf("failed to test ping email server: %v", err) + return + } + + assert.Equal(401, code, "the status code of ping email server with non-admin user should be 401") + + settings := map[string]string{ + comcfg.EmailHost: "smtp.gmail.com", + comcfg.EmailPort: "465", + comcfg.EmailIdentity: "", + comcfg.EmailUsername: "wrong_username", + comcfg.EmailPassword: "wrong_password", + comcfg.EmailSSL: "1", + } + + //case 2: secure connection with admin role + code, body, err := apiTest.PingEmail(*admin, 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") + + if !strings.Contains(body, "535") { + t.Errorf("unexpected error: %s does not contains 535", body) + return + } +} diff --git a/src/ui/api/harborapi_test.go b/src/ui/api/harborapi_test.go index c55fb243c..a5536249a 100644 --- a/src/ui/api/harborapi_test.go +++ b/src/ui/api/harborapi_test.go @@ -100,6 +100,7 @@ func init() { beego.Router("/api/systeminfo/getcert", &SystemInfoAPI{}, "get:GetCert") beego.Router("/api/ldap/ping", &LdapAPI{}, "post:Ping") beego.Router("/api/configurations", &ConfigAPI{}) + beego.Router("/api/email/ping", &EmailAPI{}, "post:Ping") _ = updateInitPassword(1, "Harbor12345") @@ -943,3 +944,11 @@ func (a testapi) PutConfig(authInfo usrInfo, cfg map[string]string) (int, error) return code, err } + +func (a testapi) PingEmail(authInfo usrInfo, settings map[string]string) (int, string, error) { + _sling := sling.New().Base(a.basePath).Post("/api/email/ping").BodyJSON(settings) + + code, body, err := request(_sling, jsonAcceptHeader, authInfo) + + return code, string(body), err +}