add test cases

This commit is contained in:
Wenkai Yin 2017-03-03 18:02:08 +08:00
parent 23bf2f0ddf
commit dfae869dce
6 changed files with 169 additions and 10 deletions

View File

@ -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.

View File

@ -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{

View File

@ -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)
}
}
}

View File

@ -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())
}

63
src/ui/api/email_test.go Normal file
View File

@ -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
}
}

View File

@ -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
}