From 6c07cfad34a0f6d635ec9a3304d330fbda3e74e4 Mon Sep 17 00:00:00 2001 From: nic-chen Date: Sun, 12 Jul 2020 13:39:52 +0800 Subject: [PATCH 1/3] test: add e2e test for ssl and consumer --- api/go.mod | 3 +- api/go.sum | 6 +++ api/route/base_test.go | 53 ++++++++++++++++++++ api/route/consumer.go | 47 ++++++++--------- api/route/consumer_test.go | 100 +++++++++++++++++++++++++++++++++++++ api/route/ssl.go | 69 ++++++++++++------------- api/route/ssl_test.go | 41 +++++++++++++++ api/route/zclear_test.go | 37 ++++++++++++++ 8 files changed, 291 insertions(+), 65 deletions(-) create mode 100644 api/route/base_test.go create mode 100644 api/route/consumer_test.go create mode 100644 api/route/ssl_test.go create mode 100644 api/route/zclear_test.go diff --git a/api/go.mod b/api/go.mod index 45372ec421..ab07faaabb 100644 --- a/api/go.mod +++ b/api/go.mod @@ -3,13 +3,14 @@ module github.com/apisix/manager-api go 1.13 require ( + github.com/api7/apitest v1.4.9 github.com/gin-contrib/pprof v1.3.0 github.com/gin-gonic/gin v1.6.3 github.com/go-sql-driver/mysql v1.5.0 github.com/jinzhu/gorm v1.9.12 github.com/satori/go.uuid v1.2.0 github.com/sirupsen/logrus v1.6.0 - github.com/stretchr/testify v1.4.0 + github.com/stretchr/testify v1.6.1 github.com/tidwall/gjson v1.6.0 gopkg.in/resty.v1 v1.12.0 ) diff --git a/api/go.sum b/api/go.sum index 9e3e3e32a6..1daa324a3d 100644 --- a/api/go.sum +++ b/api/go.sum @@ -1,3 +1,5 @@ +github.com/api7/apitest v1.4.9 h1:FYTUQJ1hgeB9UvMFif1jjbfiA+XqHPEBfsjhDskytA8= +github.com/api7/apitest v1.4.9/go.mod h1:YZruZ+jDMFL6rNgMWiuhwCTugNN0mJkLCYCHG3ICYlE= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -55,6 +57,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/tidwall/gjson v1.6.0 h1:9VEQWz6LLMUsUl6PueE49ir4Ka6CzLymOAZDxpFsTDc= github.com/tidwall/gjson v1.6.0/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls= github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc= @@ -87,3 +91,5 @@ gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/api/route/base_test.go b/api/route/base_test.go new file mode 100644 index 0000000000..f07ea03be6 --- /dev/null +++ b/api/route/base_test.go @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 route + +import ( + "github.com/api7/apitest" + "github.com/gin-gonic/gin" + + "github.com/apisix/manager-api/conf" + "github.com/apisix/manager-api/filter" +) + +var rootToken string +var handler *apitest.APITest + +var ( + r = gin.New() + uriPrefix = "/apisix/admin" +) + +func init() { + //init mysql connect + conf.InitializeMysql() + + //filters + r.Use( + filter.CORS(), + filter.RequestId(), + filter.RequestLogHandler(), + filter.RecoverHandler()) + + handler = apitest.New(). + Handler(AppendHealthCheck(r)). + Handler(AppendRoute(r)). + Handler(AppendSsl(r)). + Handler(AppendPlugin(r)). + Handler(AppendConsumer(r)). + Handler(AppendUpstream(r)) +} diff --git a/api/route/consumer.go b/api/route/consumer.go index 8c442b61a8..3f0bfa1482 100644 --- a/api/route/consumer.go +++ b/api/route/consumer.go @@ -39,6 +39,22 @@ func AppendConsumer(r *gin.Engine) *gin.Engine { return r } +func handleServiceError(c *gin.Context, requestId interface{}, err error) { + if httpError, ok := err.(*errno.HttpError); ok { + logger.WithField(conf.RequestId, requestId).Error(err) + c.AbortWithStatusJSON(httpError.Code, httpError.Msg) + return + } + + e := err.(*errno.ManagerError) + status := http.StatusInternalServerError + if e.Status > 0 { + status = e.Status + } + logger.WithField(conf.RequestId, requestId).Error(e.ErrorDetail()) + c.AbortWithStatusJSON(status, e.Response()) +} + func consumerList(c *gin.Context) { requestId, _ := c.Get("X-Request-Id") size, _ := strconv.Atoi(c.DefaultQuery("size", "10")) @@ -49,8 +65,7 @@ func consumerList(c *gin.Context) { count, list, err := service.ConsumerList(page, size, search) if err != nil { - logger.WithField(conf.RequestId, requestId).Error(err.(*errno.ManagerError).ErrorDetail()) - c.AbortWithStatusJSON(http.StatusInternalServerError, err.(*errno.ManagerError).Response()) + handleServiceError(c, requestId, err) return } @@ -66,8 +81,7 @@ func consumerItem(c *gin.Context) { consumer, err := service.ConsumerItem(id) if err != nil { - logger.WithField(conf.RequestId, requestId).Error(err.(*errno.ManagerError).ErrorDetail()) - c.AbortWithStatusJSON(http.StatusInternalServerError, err.(*errno.ManagerError).Response()) + handleServiceError(c, requestId, err) return } @@ -88,13 +102,7 @@ func consumerCreate(c *gin.Context) { } if err := service.ConsumerCreate(param, u4.String()); err != nil { - if httpError, ok := err.(*errno.HttpError); ok { - logger.WithField(conf.RequestId, requestId).Error(err) - c.AbortWithStatusJSON(httpError.Code, httpError.Msg) - return - } - logger.WithField(conf.RequestId, requestId).Error(err.(*errno.ManagerError).ErrorDetail()) - c.AbortWithStatusJSON(http.StatusInternalServerError, err.(*errno.ManagerError).Response()) + handleServiceError(c, requestId, err) return } @@ -115,13 +123,7 @@ func consumerUpdate(c *gin.Context) { } if err := service.ConsumerUpdate(param, id); err != nil { - if httpError, ok := err.(*errno.HttpError); ok { - logger.WithField(conf.RequestId, requestId).Error(err) - c.AbortWithStatusJSON(httpError.Code, httpError.Msg) - return - } - logger.WithField(conf.RequestId, requestId).Error(err.(*errno.ManagerError).ErrorDetail()) - c.AbortWithStatusJSON(http.StatusInternalServerError, err.(*errno.ManagerError).Response()) + handleServiceError(c, requestId, err) return } @@ -133,14 +135,7 @@ func consumerDelete(c *gin.Context) { id := c.Param("id") if err := service.ConsumerDelete(id); err != nil { - if httpError, ok := err.(*errno.HttpError); ok { - logger.WithField(conf.RequestId, requestId).Error(err) - c.AbortWithStatusJSON(httpError.Code, httpError.Msg) - return - } - - logger.WithField(conf.RequestId, requestId).Error(err.(*errno.ManagerError).ErrorDetail()) - c.AbortWithStatusJSON(http.StatusInternalServerError, err.(*errno.ManagerError).Response()) + handleServiceError(c, requestId, err) return } diff --git a/api/route/consumer_test.go b/api/route/consumer_test.go new file mode 100644 index 0000000000..2e63277d24 --- /dev/null +++ b/api/route/consumer_test.go @@ -0,0 +1,100 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 route + +import ( + "net/http" + "testing" + + "github.com/apisix/manager-api/service" +) + +func TestConsumer(t *testing.T) { + // create ok + handler. + Post(uriPrefix + "/consumers"). + JSON(`{ + "username": "e2e_test_consumer1", + "plugins": { + "limit-count": { + "count": 2, + "time_window": 60, + "rejected_code": 503, + "key": "remote_addr" + }, + "basic-auth": { + "username": "foo", + "password": "bar" + } + }, + "desc": "test description" + }`). + Headers(map[string]string{"Authorization": rootToken}). + Expect(t). + Status(http.StatusOK). + End() + + c1, _ := service.GetConsumerByUserName("e2e_test_consumer1") + + //update ok + handler. + Put(uriPrefix + "/consumers/" + c1.ID.String()). + JSON(`{ + "username": "e2e_test_consumer1", + "plugins": { + "limit-count": { + "count": 2, + "time_window": 60, + "rejected_code": 503, + "key": "remote_addr" + }, + "basic-auth": { + "username": "foo", + "password": "bar" + } + }, + "desc": "test desc" + }`). + Headers(map[string]string{"Authorization": rootToken}). + Expect(t). + Status(http.StatusOK). + End() + + // duplicate username + handler. + Post(uriPrefix + "/consumers"). + JSON(`{ + "username": "e2e_test_consumer1", + "plugins": { + "limit-count": { + "count": 2, + "time_window": 60, + "rejected_code": 503, + "key": "remote_addr" + }, + "basic-auth": { + "username": "foo", + "password": "bar" + } + }, + "desc": "test description" + }`). + Headers(map[string]string{"Authorization": rootToken}). + Expect(t). + Status(http.StatusBadRequest). + End() +} diff --git a/api/route/ssl.go b/api/route/ssl.go index 7b62b49588..d962be8d79 100644 --- a/api/route/ssl.go +++ b/api/route/ssl.go @@ -30,6 +30,7 @@ import ( func AppendSsl(r *gin.Engine) *gin.Engine { r.POST("/apisix/admin/check_ssl_cert", sslCheck) + r.POST("/apisix/admin/check_ssl_exists", hostCheck) r.GET("/apisix/admin/ssls", sslList) r.POST("/apisix/admin/ssls", sslCreate) @@ -48,14 +49,14 @@ func sslList(c *gin.Context) { status, _ := strconv.Atoi(c.DefaultQuery("status", "-1")) expireStart, _ := strconv.Atoi(c.DefaultQuery("expire_start", "-1")) expireEnd, _ := strconv.Atoi(c.DefaultQuery("expire_end", "-1")) + sortType := c.DefaultQuery("sort_type", "desc") sni := c.DefaultQuery("sni", "") - count, list, err := service.SslList(page, size, status, expireStart, expireEnd, sni) + count, list, err := service.SslList(page, size, status, expireStart, expireEnd, sni, sortType) if err != nil { - logger.WithField(conf.RequestId, requestId).Error(err.(*errno.ManagerError).ErrorDetail()) - c.AbortWithStatusJSON(http.StatusInternalServerError, err.(*errno.ManagerError).Response()) + handleServiceError(c, requestId, err) return } @@ -71,8 +72,7 @@ func sslItem(c *gin.Context) { ssl, err := service.SslItem(id) if err != nil { - logger.WithField(conf.RequestId, requestId).Error(err.(*errno.ManagerError).ErrorDetail()) - c.AbortWithStatusJSON(http.StatusInternalServerError, err.(*errno.ManagerError).Response()) + handleServiceError(c, requestId, err) return } @@ -92,8 +92,7 @@ func sslCheck(c *gin.Context) { ssl, err := service.SslCheck(param) if err != nil { - logger.WithField(conf.RequestId, requestId).Error(err.(*errno.ManagerError).ErrorDetail()) - c.AbortWithStatusJSON(http.StatusInternalServerError, err.(*errno.ManagerError).Response()) + handleServiceError(c, requestId, err) return } @@ -116,13 +115,7 @@ func sslCreate(c *gin.Context) { } if err := service.SslCreate(param, u4.String()); err != nil { - if httpError, ok := err.(*errno.HttpError); ok { - logger.WithField(conf.RequestId, requestId).Error(err) - c.AbortWithStatusJSON(httpError.Code, httpError.Msg) - return - } - logger.WithField(conf.RequestId, requestId).Error(err.(*errno.ManagerError).ErrorDetail()) - c.AbortWithStatusJSON(http.StatusInternalServerError, err.(*errno.ManagerError).Response()) + handleServiceError(c, requestId, err) return } @@ -132,7 +125,6 @@ func sslCreate(c *gin.Context) { func sslUpdate(c *gin.Context) { requestId, _ := c.Get("X-Request-Id") param, exist := c.Get("requestBody") - id := c.Param("id") if !exist || len(param.([]byte)) < 1 { @@ -143,13 +135,7 @@ func sslUpdate(c *gin.Context) { } if err := service.SslUpdate(param, id); err != nil { - if httpError, ok := err.(*errno.HttpError); ok { - logger.WithField(conf.RequestId, requestId).Error(err) - c.AbortWithStatusJSON(httpError.Code, httpError.Msg) - return - } - logger.WithField(conf.RequestId, requestId).Error(err.(*errno.ManagerError).ErrorDetail()) - c.AbortWithStatusJSON(http.StatusInternalServerError, err.(*errno.ManagerError).Response()) + handleServiceError(c, requestId, err) return } @@ -170,14 +156,7 @@ func sslPatch(c *gin.Context) { } if err := service.SslPatch(param, id); err != nil { - if httpError, ok := err.(*errno.HttpError); ok { - logger.WithField(conf.RequestId, requestId).Error(err) - c.AbortWithStatusJSON(httpError.Code, httpError.Msg) - return - } - - logger.WithField(conf.RequestId, requestId).Error(err.(*errno.ManagerError).ErrorDetail()) - c.AbortWithStatusJSON(http.StatusInternalServerError, err.(*errno.ManagerError).Response()) + handleServiceError(c, requestId, err) return } @@ -189,16 +168,30 @@ func sslDelete(c *gin.Context) { id := c.Param("id") if err := service.SslDelete(id); err != nil { - if httpError, ok := err.(*errno.HttpError); ok { - logger.WithField(conf.RequestId, requestId).Error(err) - c.AbortWithStatusJSON(httpError.Code, httpError.Msg) - return - } - - logger.WithField(conf.RequestId, requestId).Error(err.(*errno.ManagerError).ErrorDetail()) - c.AbortWithStatusJSON(http.StatusInternalServerError, err.(*errno.ManagerError).Response()) + handleServiceError(c, requestId, err) return } c.JSON(http.StatusOK, errno.Succeed()) } + +func hostCheck(c *gin.Context) { + requestId, _ := c.Get("X-Request-Id") + param, exist := c.Get("requestBody") + + if !exist || len(param.([]byte)) < 1 { + err := errno.New(errno.InvalidParam) + logger.WithField(conf.RequestId, requestId).Error(err.ErrorDetail()) + c.AbortWithStatusJSON(http.StatusBadRequest, err.Response()) + return + } + + err := service.CheckSniExists(param) + if err != nil { + handleServiceError(c, requestId, err) + return + } + + c.JSON(http.StatusOK, errno.Succeed()) + +} diff --git a/api/route/ssl_test.go b/api/route/ssl_test.go new file mode 100644 index 0000000000..a4a2308de7 --- /dev/null +++ b/api/route/ssl_test.go @@ -0,0 +1,41 @@ +package route + +import ( + "net/http" + "testing" +) + +func TestSslCreate(t *testing.T) { + // ok + handler. + Post(uriPrefix + "/ssls"). + JSON(`{ + "key": "-----BEGIN PRIVATE KEY-----\nMIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDGO0J9xrOcmvgh\npkqHIYHCw35FTfIT5uXOSzdF49M2ZAKBQwFG0ovYT8bc0glNLB+hpDhJPL531qSP\nl1ZOe0W1ofP1u0T5Zzc9Rub/kn7RMPq0BsSC6J3rF+rQEwh1PM8qUuD8DxZ7jaOL\niMNL6SyuZIPsS1kPPBtsioukdo666tbjNMixhQbI9Wpg55abdXRFh3i7Zu/9siF1\njCGcsskjOaUOY4sYQ3i5WU/HIIRhA82XuIL+Sxd32P8bKi2UT1sqFXRjAVR7KRWo\nIVvkmSLoZb9ucV6MsccDrRYBf6rLbI1t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bAX\n5Q3hEOVrsQIDAQABo3kwdzAOBgNVHQ8BAf8EBAMCBaAwEwYDVR0lBAwwCgYIKwYB\nBQUHAwEwDAYDVR0TAQH/BAIwADAfBgNVHSMEGDAWgBRU+EbJj+Hp62gCrNvb3yQk\nYnPHXDAhBgNVHREEGjAYgglyb3V0ZS5jb22CCyoucm91dGUuY29tMA0GCSqGSIb3\nDQEBCwUAA4IBgQAvKN2GEorAlx5sfUU2uiL49iFmQSMDLZQminQl1RIHTI/h+jz8\nNluZSdxDFmNq8am6B2ofD3VLl6StC/G+G6YuekPz+QrUNK4UB+8ftRmY4YRFGTQ6\nRnFli1wOq2ES9vPjKlIj77cznr8uwVHPHq8JxGbn/rx3oVDVPndXFCkJJ1DDjRT+\n22atHNzHt5bc9ut8Fq5NW61P+nnMMFShKJaPBkmm9Pf2pEOd8Y7OU8Iy1Kj65fsE\nUshGF5+RWoxdv6/9f6/uOQhmq3MEKqneUC3pjVZ8TiBlRvADxxR5krvujQswms0D\nFGpRMtGpPGMWTuptSIMwNcar/luVig7wGIBeV5ZaOlSOx3911le9mlS7+2lLqf5H\n5dsMkP30Sjv/jfrIL+SE1qeK3kjL0iIwA/PPARvhctExs9y2llT9+drbJofZUi+I\nZdYfAfyJT4htbcl7jHN8oY7vzwgTyxCcBxkbqKfBqabneutj0jfX39zP0G696tiZ\ndQFXCS4wkvw0CG0=\n-----END CERTIFICATE-----" + }`). + Expect(t). + Status(http.StatusOK). + End() + + // schema fail + handler. + Post(uriPrefix + "/ssls"). + JSON(`{ + "cert": "-----BEGIN CERTIFICATE-----\nMIIEVzCCAr+gAwIBAgIQITiNM7xmudhg3pK85KDwLDANBgkqhkiG9w0BAQsFADB/\nMR4wHAYDVQQKExVta2NlcnQgZGV2ZWxvcG1lbnQgQ0ExKjAoBgNVBAsMIWp1bnh1\nY2hlbkBqdW54dWRlQWlyIChqdW54dSBjaGVuKTExMC8GA1UEAwwobWtjZXJ0IGp1\nbnh1Y2hlbkBqdW54dWRlQWl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3MEKqneUC3pjVZ8TiBlRvADxxR5krvujQswms0D\nFGpRMtGpPGMWTuptSIMwNcar/luVig7wGIBeV5ZaOlSOx3911le9mlS7+2lLqf5H\n5dsMkP30Sjv/jfrIL+SE1qeK3kjL0iIwA/PPARvhctExs9y2llT9+drbJofZUi+I\nZdYfAfyJT4htbcl7jHN8oY7vzwgTyxCcBxkbqKfBqabneutj0jfX39zP0G696tiZ\ndQFXCS4wkvw0CG0=\n-----END CERTIFICATE-----" + }`). + Expect(t). + Status(http.StatusBadRequest). + End() + + //schema fail 2 + handler. + Post(uriPrefix + "/ssls"). + JSON(`{ + "key": "", + "cert": "-----BEGIN CERTIFICATE-----\nMIIEVzCCAr+gAwIBAgIQITiNM7xmudhg3pK85KDwLDANBgkqhkiG9w0BAQsFADB/\nMR4wHAYDVQQKExVta2NlcnQgZGV2ZWxvcG1lbnQgQ0ExKjAoBgNVBAsMIWp1bnh1\nY2hlbkBqdW54dWRlQWlyIChqdW54dSBjaGVuKTExMC8GA1UEAwwobWtjZXJ0IGp1\nbnh1Y2hlbkBqdW54dWRlQWlyIChqdW54dSBjaGVuKTAeFw0xOTA2MDEwMDAwMDBa\nFw0zMDA3MDgwNzQ4MDJaMFUxJzAlBgNVBAoTHm1rY2VydCBkZXZlbG9wbWVudCBj\nZXJ0aWZpY2F0ZTEqMCgGA1UECwwhanVueHVjaGVuQGp1bnh1ZGVBaXIgKGp1bnh1\nIGNoZW4pMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxjtCfcaznJr4\nIaZKhyGBwsN+RU3yE+blzks3RePTNmQCgUMBRtKL2E/G3NIJTSwfoaQ4STy+d9ak\nj5dWTntFtaHz9btE+Wc3PUbm/5J+0TD6tAbEguid6xfq0BMIdTzPKlLg/A8We42j\ni4jDS+ksrmSD7EtZDzwbbIqLpHaOuurW4zTIsYUGyPVqYOeWm3V0RYd4u2bv/bIh\ndYwhnLLJIzmlDmOLGEN4uVlPxyCEYQPNl7iC/ksXd9j/GyotlE9bKhV0YwFUeykV\nqCFb5Jki6GW/bnFejLHHA60WAX+qy2yNbRY/Zdq2OhkxZU/us+yv2SOtAxov4bAX\n5Q3hEOVrsQIDAQABo3kwdzAOBgNVHQ8BAf8EBAMCBaAwEwYDVR0lBAwwCgYIKwYB\nBQUHAwEwDAYDVR0TAQH/BAIwADAfBgNVHSMEGDAWgBRU+EbJj+Hp62gCrNvb3yQk\nYnPHXDAhBgNVHREEGjAYgglyb3V0ZS5jb22CCyoucm91dGUuY29tMA0GCSqGSIb3\nDQEBCwUAA4IBgQAvKN2GEorAlx5sfUU2uiL49iFmQSMDLZQminQl1RIHTI/h+jz8\nNluZSdxDFmNq8am6B2ofD3VLl6StC/G+G6YuekPz+QrUNK4UB+8ftRmY4YRFGTQ6\nRnFli1wOq2ES9vPjKlIj77cznr8uwVHPHq8JxGbn/rx3oVDVPndXFCkJJ1DDjRT+\n22atHNzHt5bc9ut8Fq5NW61P+nnMMFShKJaPBkmm9Pf2pEOd8Y7OU8Iy1Kj65fsE\nUshGF5+RWoxdv6/9f6/uOQhmq3MEKqneUC3pjVZ8TiBlRvADxxR5krvujQswms0D\nFGpRMtGpPGMWTuptSIMwNcar/luVig7wGIBeV5ZaOlSOx3911le9mlS7+2lLqf5H\n5dsMkP30Sjv/jfrIL+SE1qeK3kjL0iIwA/PPARvhctExs9y2llT9+drbJofZUi+I\nZdYfAfyJT4htbcl7jHN8oY7vzwgTyxCcBxkbqKfBqabneutj0jfX39zP0G696tiZ\ndQFXCS4wkvw0CG0=\n-----END CERTIFICATE-----" + }`). + Expect(t). + Status(http.StatusBadRequest). + End() + +} diff --git a/api/route/zclear_test.go b/api/route/zclear_test.go new file mode 100644 index 0000000000..e44101bd34 --- /dev/null +++ b/api/route/zclear_test.go @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 route + +import ( + "net/http" + "testing" + + "github.com/apisix/manager-api/service" +) + +func TestClearTestData(t *testing.T) { + // delete consumers + c1, _ := service.GetConsumerByUserName("e2e_test_consumer1") + handler. + Delete(uriPrefix + "/consumers/" + c1.ID.String()). + Expect(t). + Status(http.StatusOK). + End() + + //delete test ssl + service.DeleteTestSslData() +} From 85ae04091e36159c6f4a10d12f2d98cbb096c994 Mon Sep 17 00:00:00 2001 From: nic-chen Date: Sun, 12 Jul 2020 15:26:56 +0800 Subject: [PATCH 2/3] fix: change assert to avoid the mutual influence of route and service test --- api/service/consumer_test.go | 4 ++-- api/service/ssl_test.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/api/service/consumer_test.go b/api/service/consumer_test.go index ea1a077aeb..cd0e3d94e2 100644 --- a/api/service/consumer_test.go +++ b/api/service/consumer_test.go @@ -125,8 +125,8 @@ func TestConsumerCurd(t *testing.T) { err = ConsumerDelete(c1.String()) assert.Nil(err) - count2, _, err := ConsumerList(2, 1, "") - assert.Equal(count2, count-1) + _, err = ConsumerItem(c1.String()) + assert.Equal(errno.DBReadError.Code, err.(*errno.ManagerError).Code) err = ConsumerDelete(c2.String()) assert.Nil(err) diff --git a/api/service/ssl_test.go b/api/service/ssl_test.go index 46ecbb973a..2134bb1be7 100644 --- a/api/service/ssl_test.go +++ b/api/service/ssl_test.go @@ -344,8 +344,8 @@ func TestSslCurd(t *testing.T) { err = SslDelete(u1.String()) assert.Nil(err) - count2, _, err := SslList(2, 1, -1, 0, 0, "", "desc") - assert.Equal(count2, count-1) + _, err = SslItem(u1.String()) + assert.Equal(errno.DBReadError.Code, err.(*errno.ManagerError).Code) err = SslDelete(u2.String()) assert.Nil(err) From 2ec1dcf70c4a2da076f4bf21390af8490fd393f3 Mon Sep 17 00:00:00 2001 From: nic-chen Date: Sun, 12 Jul 2020 15:27:13 +0800 Subject: [PATCH 3/3] remove useless code --- api/route/base_test.go | 1 - api/route/consumer_test.go | 3 --- 2 files changed, 4 deletions(-) diff --git a/api/route/base_test.go b/api/route/base_test.go index f07ea03be6..7135db0c78 100644 --- a/api/route/base_test.go +++ b/api/route/base_test.go @@ -24,7 +24,6 @@ import ( "github.com/apisix/manager-api/filter" ) -var rootToken string var handler *apitest.APITest var ( diff --git a/api/route/consumer_test.go b/api/route/consumer_test.go index 2e63277d24..60066bb7fa 100644 --- a/api/route/consumer_test.go +++ b/api/route/consumer_test.go @@ -43,7 +43,6 @@ func TestConsumer(t *testing.T) { }, "desc": "test description" }`). - Headers(map[string]string{"Authorization": rootToken}). Expect(t). Status(http.StatusOK). End() @@ -69,7 +68,6 @@ func TestConsumer(t *testing.T) { }, "desc": "test desc" }`). - Headers(map[string]string{"Authorization": rootToken}). Expect(t). Status(http.StatusOK). End() @@ -93,7 +91,6 @@ func TestConsumer(t *testing.T) { }, "desc": "test description" }`). - Headers(map[string]string{"Authorization": rootToken}). Expect(t). Status(http.StatusBadRequest). End()