Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add e2e test for ssl and consumer #309

Merged
merged 3 commits into from
Jul 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
6 changes: 6 additions & 0 deletions api/go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -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=
52 changes: 52 additions & 0 deletions api/route/base_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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 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))
}
47 changes: 21 additions & 26 deletions api/route/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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
}

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

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

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

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

Expand Down
97 changes: 97 additions & 0 deletions api/route/consumer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* 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"
}`).
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"
}`).
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"
}`).
Expect(t).
Status(http.StatusBadRequest).
End()
}
Loading