This is an automated email from the ASF dual-hosted git repository.

juzhiyuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-dashboard.git


The following commit(s) were added to refs/heads/master by this push:
     new 2cac5f5  feat: rewrite route-with-jwt-plugin-test using ginkgo (#1802)
2cac5f5 is described below

commit 2cac5f5326cdc3716e7d6560b5925ace344f39df
Author: Bisakh Mondal <bisakhmonda...@gmail.com>
AuthorDate: Mon May 17 06:34:05 2021 +0530

    feat: rewrite route-with-jwt-plugin-test using ginkgo (#1802)
---
 api/test/e2e/route_with_plugin_jwt_test.go         | 290 --------------------
 .../e2enew/route/route_with_plugin_jwt_test.go     | 297 +++++++++++++++++++++
 2 files changed, 297 insertions(+), 290 deletions(-)

diff --git a/api/test/e2e/route_with_plugin_jwt_test.go 
b/api/test/e2e/route_with_plugin_jwt_test.go
deleted file mode 100644
index 4801f14..0000000
--- a/api/test/e2e/route_with_plugin_jwt_test.go
+++ /dev/null
@@ -1,290 +0,0 @@
-/*
- * 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 e2e
-
-import (
-       "io/ioutil"
-       "net/http"
-       "testing"
-       "time"
-
-       "github.com/stretchr/testify/assert"
-)
-
-func TestRoute_With_Jwt_Plugin(t *testing.T) {
-       tests := []HttpTestCase{
-               {
-                       Desc:         "make sure the route is not created ",
-                       Object:       APISIXExpect(t),
-                       Method:       http.MethodGet,
-                       Path:         "/hello",
-                       ExpectStatus: http.StatusNotFound,
-                       ExpectBody:   `{"error_msg":"404 Route Not Found"}`,
-               },
-               {
-                       Desc:   "create route",
-                       Object: ManagerApiExpect(t),
-                       Method: http.MethodPut,
-                       Path:   "/apisix/admin/routes/r1",
-                       Body: `{
-                                "name": "route1",
-                                "uri": "/hello",
-                                "plugins": {
-                                        "jwt-auth": {}
-                                },
-                                "upstream": {
-                                        "type": "roundrobin",
-                                       "nodes": [{
-                                               "host": "172.16.238.20",
-                                               "port": 1981,
-                                               "weight": 1
-                                       }]
-                                }
-                        }`,
-                       Headers:      map[string]string{"Authorization": token},
-                       ExpectStatus: http.StatusOK,
-                       ExpectBody:   `"code":0`,
-               },
-               {
-                       Desc:         "make sure the consumer is not created",
-                       Object:       ManagerApiExpect(t),
-                       Method:       http.MethodGet,
-                       Path:         "/apisix/admin/consumers/jack",
-                       Headers:      map[string]string{"Authorization": token},
-                       ExpectStatus: http.StatusNotFound,
-               },
-               {
-                       Desc:   "create consumer",
-                       Object: ManagerApiExpect(t),
-                       Path:   "/apisix/admin/consumers",
-                       Method: http.MethodPut,
-                       Body: `{
-                               "username": "jack",
-                               "plugins": {
-                                       "jwt-auth": {
-                                               "key": "user-key",
-                                               "secret": "my-secret-key",
-                                               "algorithm": "HS256"
-                                       }
-                               },
-                               "desc": "test description"
-                       }`,
-                       Headers:      map[string]string{"Authorization": token},
-                       ExpectStatus: http.StatusOK,
-               },
-       }
-
-       for _, tc := range tests {
-               testCaseCheck(tc, t)
-       }
-
-       time.Sleep(sleepTime)
-
-       // sign jwt token
-       body, status, err := 
httpGet("http://127.0.0.1:9080/apisix/plugin/jwt/sign?key=user-key";, nil)
-       assert.Nil(t, err)
-       assert.Equal(t, http.StatusOK, status)
-       jwtToken := string(body)
-
-       // sign jwt token with not exists key
-       body, status, err = 
httpGet("http://127.0.0.1:9080/apisix/plugin/jwt/sign?key=not-exist-key";, nil)
-       assert.Nil(t, err)
-       assert.Equal(t, http.StatusNotFound, status)
-
-       // verify token and clean test data
-       tests = []HttpTestCase{
-               {
-                       Desc:         "verify route without jwt token",
-                       Object:       APISIXExpect(t),
-                       Method:       http.MethodGet,
-                       Path:         "/hello",
-                       ExpectStatus: http.StatusUnauthorized,
-                       ExpectBody:   `{"message":"Missing JWT token in 
request"}`,
-                       Sleep:        sleepTime,
-               },
-               {
-                       Desc:         "verify route with correct jwt token",
-                       Object:       APISIXExpect(t),
-                       Method:       http.MethodGet,
-                       Path:         "/hello",
-                       Headers:      map[string]string{"Authorization": 
jwtToken},
-                       ExpectStatus: http.StatusOK,
-                       ExpectBody:   "hello world",
-               },
-               {
-                       Desc:         "verify route with incorrect jwt token",
-                       Object:       APISIXExpect(t),
-                       Method:       http.MethodGet,
-                       Path:         "/hello",
-                       Headers:      map[string]string{"Authorization": 
"invalid-token"},
-                       ExpectStatus: http.StatusUnauthorized,
-                       ExpectBody:   `{"message":"invalid jwt string"}`,
-               },
-               {
-                       Desc:         "delete consumer",
-                       Object:       ManagerApiExpect(t),
-                       Method:       http.MethodDelete,
-                       Path:         "/apisix/admin/consumers/jack",
-                       Headers:      map[string]string{"Authorization": token},
-                       ExpectStatus: http.StatusOK,
-               },
-               {
-                       Desc:         "verify route with the jwt token from 
just deleted consumer",
-                       Object:       APISIXExpect(t),
-                       Method:       http.MethodGet,
-                       Path:         "/hello",
-                       Headers:      map[string]string{"Authorization": 
jwtToken},
-                       ExpectStatus: http.StatusUnauthorized,
-                       ExpectBody:   `{"message":"Missing related consumer"}`,
-                       Sleep:        sleepTime,
-               },
-               {
-                       Desc:         "delete route",
-                       Object:       ManagerApiExpect(t),
-                       Method:       http.MethodDelete,
-                       Path:         "/apisix/admin/routes/r1",
-                       Headers:      map[string]string{"Authorization": token},
-                       ExpectStatus: http.StatusOK,
-               },
-               {
-                       Desc:         "verify the deleted route ",
-                       Object:       APISIXExpect(t),
-                       Method:       http.MethodGet,
-                       Path:         "/hello",
-                       ExpectStatus: http.StatusNotFound,
-                       ExpectBody:   `{"error_msg":"404 Route Not Found"}`,
-                       Sleep:        sleepTime,
-               },
-       }
-
-       for _, tc := range tests {
-               testCaseCheck(tc, t)
-       }
-
-       tests = []HttpTestCase{
-               {
-                       Desc:   "create consumer with jwt (no algorithm)",
-                       Object: ManagerApiExpect(t),
-                       Path:   "/apisix/admin/consumers",
-                       Method: http.MethodPut,
-                       Body: `{
-                               "username":"consumer_1",
-                               "desc": "test description",
-                               "plugins":{
-                                       "jwt-auth":{
-                                               "exp":86400,
-                                               "key":"user-key",
-                                               "secret":"my-secret-key"
-                                       }
-                               }
-                       }`,
-                       Headers:      map[string]string{"Authorization": token},
-                       ExpectStatus: http.StatusOK,
-                       ExpectBody:   "\"code\":0",
-               },
-               {
-                       Desc:         "get the consumer",
-                       Object:       ManagerApiExpect(t),
-                       Path:         "/apisix/admin/consumers/consumer_1",
-                       Method:       http.MethodGet,
-                       Headers:      map[string]string{"Authorization": token},
-                       ExpectStatus: http.StatusOK,
-                       ExpectBody:   "\"username\":\"consumer_1\"",
-                       Sleep:        sleepTime,
-               },
-               {
-                       Desc:   "create the route",
-                       Object: ManagerApiExpect(t),
-                       Method: http.MethodPut,
-                       Path:   "/apisix/admin/routes/r1",
-                       Body: `{
-                               "name": "route1",
-                               "uri": "/hello",
-                               "plugins": {
-                                       "jwt-auth": {}
-                               },
-                               "upstream": {
-                                       "type": "roundrobin",
-                                       "nodes": [{
-                                               "host": "172.16.238.20",
-                                               "port": 1980,
-                                               "weight": 1
-                                       }]
-                               }
-                       }`,
-                       Headers:      map[string]string{"Authorization": token},
-                       ExpectStatus: http.StatusOK,
-               },
-       }
-
-       for _, tc := range tests {
-               testCaseCheck(tc, t)
-       }
-
-       // get the token of jwt
-       basepath := "http://127.0.0.1:9080";
-       request, _ := http.NewRequest("GET", 
basepath+"/apisix/plugin/jwt/sign?key=user-key", nil)
-       request.Header.Add("Authorization", token)
-       resp, err := http.DefaultClient.Do(request)
-       assert.Nil(t, err)
-       defer resp.Body.Close()
-       assert.Equal(t, 200, resp.StatusCode)
-       jwttoken, _ := ioutil.ReadAll(resp.Body)
-
-       tests = []HttpTestCase{
-               {
-                       Desc:         "hit route with jwt token",
-                       Object:       APISIXExpect(t),
-                       Method:       http.MethodGet,
-                       Path:         "/hello",
-                       Headers:      map[string]string{"Authorization": 
string(jwttoken)},
-                       ExpectStatus: http.StatusOK,
-                       ExpectBody:   "hello world",
-                       Sleep:        sleepTime,
-               },
-               {
-                       Desc:         "delete consumer",
-                       Object:       ManagerApiExpect(t),
-                       Path:         "/apisix/admin/consumers/consumer_1",
-                       Method:       http.MethodDelete,
-                       Headers:      map[string]string{"Authorization": token},
-                       ExpectStatus: http.StatusOK,
-                       ExpectBody:   "\"code\":0",
-               },
-               {
-                       Desc:         "after delete consumer verify it again",
-                       Object:       ManagerApiExpect(t),
-                       Method:       http.MethodGet,
-                       Path:         "/apisix/admin/consumers/jack",
-                       Headers:      map[string]string{"Authorization": token},
-                       ExpectStatus: http.StatusNotFound,
-                       Sleep:        sleepTime,
-               },
-               {
-                       Desc:         "delete the route",
-                       Object:       ManagerApiExpect(t),
-                       Method:       http.MethodDelete,
-                       Path:         "/apisix/admin/routes/r1",
-                       Headers:      map[string]string{"Authorization": token},
-                       ExpectStatus: http.StatusOK,
-               },
-       }
-
-       for _, tc := range tests {
-               testCaseCheck(tc, t)
-       }
-}
diff --git a/api/test/e2enew/route/route_with_plugin_jwt_test.go 
b/api/test/e2enew/route/route_with_plugin_jwt_test.go
new file mode 100644
index 0000000..af273bd
--- /dev/null
+++ b/api/test/e2enew/route/route_with_plugin_jwt_test.go
@@ -0,0 +1,297 @@
+/*
+ * 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 (
+       "io/ioutil"
+       "net/http"
+       "time"
+
+       "github.com/onsi/ginkgo"
+       "github.com/onsi/ginkgo/extensions/table"
+       "github.com/onsi/gomega"
+
+       "github.com/apisix/manager-api/test/e2enew/base"
+)
+
+var _ = ginkgo.Describe("route with jwt plugin", func() {
+       var jwtToken string
+
+       table.DescribeTable("create route and consumer with jwt plugin",
+               func(tc base.HttpTestCase) {
+                       base.RunTestCase(tc)
+               },
+               table.Entry("make sure the route is not created ", 
base.HttpTestCase{
+                       Object:       base.APISIXExpect(),
+                       Method:       http.MethodGet,
+                       Path:         "/hello",
+                       ExpectStatus: http.StatusNotFound,
+                       ExpectBody:   `{"error_msg":"404 Route Not Found"}`,
+               }),
+               table.Entry("create route", base.HttpTestCase{
+                       Object: base.ManagerApiExpect(),
+                       Method: http.MethodPut,
+                       Path:   "/apisix/admin/routes/r1",
+                       Body: `{
+                                "name": "route1",
+                                "uri": "/hello",
+                                "plugins": {
+                                        "jwt-auth": {}
+                                },
+                                "upstream": {
+                                       "type": "roundrobin",
+                                       "nodes": {
+                                               "` + base.UpstreamIp + `:1981": 
1
+                                       }
+                                }
+                        }`,
+                       Headers:      map[string]string{"Authorization": 
base.GetToken()},
+                       ExpectStatus: http.StatusOK,
+                       ExpectBody:   []string{`"code":0`, `"id":"r1"`, 
`"uri":"/hello"`, `"name":"route1"`, `"jwt-auth":{}`},
+               }),
+               table.Entry("make sure the consumer is not created", 
base.HttpTestCase{
+                       Object:       base.ManagerApiExpect(),
+                       Method:       http.MethodGet,
+                       Path:         "/apisix/admin/consumers/jack",
+                       Headers:      map[string]string{"Authorization": 
base.GetToken()},
+                       ExpectStatus: http.StatusNotFound,
+               }),
+               table.Entry("create consumer", base.HttpTestCase{
+                       Object: base.ManagerApiExpect(),
+                       Path:   "/apisix/admin/consumers",
+                       Method: http.MethodPut,
+                       Body: `{
+                               "username": "jack",
+                               "plugins": {
+                                       "jwt-auth": {
+                                               "key": "user-key",
+                                               "secret": "my-secret-key",
+                                               "algorithm": "HS256"
+                                       }
+                               },
+                               "desc": "test description"
+                       }`,
+                       Headers:      map[string]string{"Authorization": 
base.GetToken()},
+                       ExpectStatus: http.StatusOK,
+                       ExpectBody:   []string{`"code":0`, `"username":"jack"`, 
`"key":"user-key"`, `"secret":"my-secret-key"`},
+               }),
+       )
+       ginkgo.It("sign jwt token", func() {
+               time.Sleep(base.SleepTime)
+
+               // sign jwt token
+               body, status, err := 
base.HttpGet(base.APISIXHost+"/apisix/plugin/jwt/sign?key=user-key", nil)
+               gomega.Expect(err).To(gomega.BeNil())
+               gomega.Expect(status).To(gomega.Equal(http.StatusOK))
+               jwtToken = string(body)
+               // sign jwt token with not exists key
+               body, status, err = 
base.HttpGet(base.APISIXHost+"/apisix/plugin/jwt/sign?key=not-exist-key", nil)
+               gomega.Expect(err).To(gomega.BeNil())
+               gomega.Expect(status).To(gomega.Equal(http.StatusNotFound))
+       })
+
+       ginkgo.It("verify route with correct jwt token", func() {
+               base.RunTestCase(base.HttpTestCase{
+                       Object:       base.APISIXExpect(),
+                       Method:       http.MethodGet,
+                       Path:         "/hello",
+                       Headers:      map[string]string{"Authorization": 
jwtToken},
+                       ExpectStatus: http.StatusOK,
+                       ExpectBody:   "hello world",
+               })
+       })
+
+       table.DescribeTable("verify token and clean consumer",
+               func(tc base.HttpTestCase) {
+                       base.RunTestCase(tc)
+               },
+               table.Entry("verify route without jwt token", base.HttpTestCase{
+                       Object:       base.APISIXExpect(),
+                       Method:       http.MethodGet,
+                       Path:         "/hello",
+                       ExpectStatus: http.StatusUnauthorized,
+                       ExpectBody:   `{"message":"Missing JWT token in 
request"}`,
+                       Sleep:        base.SleepTime,
+               }),
+               table.Entry("verify route with incorrect jwt token", 
base.HttpTestCase{
+                       Object:       base.APISIXExpect(),
+                       Method:       http.MethodGet,
+                       Path:         "/hello",
+                       Headers:      map[string]string{"Authorization": 
"invalid-token"},
+                       ExpectStatus: http.StatusUnauthorized,
+                       ExpectBody:   `{"message":"invalid jwt string"}`,
+               }),
+               table.Entry("delete consumer", base.HttpTestCase{
+                       Object:       base.ManagerApiExpect(),
+                       Method:       http.MethodDelete,
+                       Path:         "/apisix/admin/consumers/jack",
+                       Headers:      map[string]string{"Authorization": 
base.GetToken()},
+                       ExpectStatus: http.StatusOK,
+               }),
+       )
+
+       ginkgo.It("verify route with the jwt token from just deleted consumer", 
func() {
+               base.RunTestCase(base.HttpTestCase{
+                       Object:       base.APISIXExpect(),
+                       Method:       http.MethodGet,
+                       Path:         "/hello",
+                       Headers:      map[string]string{"Authorization": 
jwtToken},
+                       ExpectStatus: http.StatusUnauthorized,
+                       ExpectBody:   `{"message":"Missing related consumer"}`,
+                       Sleep:        base.SleepTime,
+               })
+       })
+
+       table.DescribeTable("cleanup route and verify",
+               func(tc base.HttpTestCase) {
+                       base.RunTestCase(tc)
+               },
+               table.Entry("delete route", base.HttpTestCase{
+                       Object:       base.ManagerApiExpect(),
+                       Method:       http.MethodDelete,
+                       Path:         "/apisix/admin/routes/r1",
+                       Headers:      map[string]string{"Authorization": 
base.GetToken()},
+                       ExpectStatus: http.StatusOK,
+               }),
+               table.Entry("verify the deleted route", base.HttpTestCase{
+                       Object:       base.APISIXExpect(),
+                       Method:       http.MethodGet,
+                       Path:         "/hello",
+                       ExpectStatus: http.StatusNotFound,
+                       ExpectBody:   `{"error_msg":"404 Route Not Found"}`,
+                       Sleep:        base.SleepTime,
+               }),
+       )
+
+       table.DescribeTable("create route and consumer with jwt (no algorithm)",
+               func(tc base.HttpTestCase) {
+                       base.RunTestCase(tc)
+               },
+               table.Entry("create consumer with jwt (no algorithm)", 
base.HttpTestCase{
+                       Object: base.ManagerApiExpect(),
+                       Path:   "/apisix/admin/consumers",
+                       Method: http.MethodPut,
+                       Body: `{
+                               "username":"consumer_1",
+                               "desc": "test description",
+                               "plugins":{
+                                       "jwt-auth":{
+                                               "exp":86400,
+                                               "key":"user-key",
+                                               "secret":"my-secret-key"
+                                       }
+                               }
+                       }`,
+                       Headers:      map[string]string{"Authorization": 
base.GetToken()},
+                       ExpectStatus: http.StatusOK,
+                       ExpectBody: []string{`"code":0`, 
`"username":"consumer_1"`,
+                               
`"jwt-auth":{"exp":86400,"key":"user-key","secret":"my-secret-key"}`},
+               }),
+               table.Entry("get the consumer", base.HttpTestCase{
+                       Object:       base.ManagerApiExpect(),
+                       Path:         "/apisix/admin/consumers/consumer_1",
+                       Method:       http.MethodGet,
+                       Headers:      map[string]string{"Authorization": 
base.GetToken()},
+                       ExpectStatus: http.StatusOK,
+                       ExpectBody:   `"username":"consumer_1"`,
+                       Sleep:        base.SleepTime,
+               }),
+               table.Entry("create the route", base.HttpTestCase{
+                       Object: base.ManagerApiExpect(),
+                       Method: http.MethodPut,
+                       Path:   "/apisix/admin/routes/r1",
+                       Body: `{
+                               "name": "route1",
+                               "uri": "/hello",
+                               "plugins": {
+                                       "jwt-auth": {}
+                               },
+                               "upstream": {
+                                       "type": "roundrobin",
+                                       "nodes": {
+                                               "` + base.UpstreamIp + `:1980": 
1
+                                       }
+                               }
+                       }`,
+                       Headers:      map[string]string{"Authorization": 
base.GetToken()},
+                       ExpectBody:   []string{`"code":0`, `"id":"r1"`, 
`"uri":"/hello"`, `"name":"route1"`, `"jwt-auth":{}`},
+                       ExpectStatus: http.StatusOK,
+               }),
+       )
+
+       ginkgo.It("get the jwt token", func() {
+               time.Sleep(base.SleepTime)
+
+               request, _ := http.NewRequest("GET", 
base.APISIXHost+"/apisix/plugin/jwt/sign?key=user-key", nil)
+               resp, err := http.DefaultClient.Do(request)
+               gomega.Expect(err).To(gomega.BeNil())
+               defer resp.Body.Close()
+               gomega.Expect(resp.StatusCode).To(gomega.Equal(http.StatusOK))
+               jwtTokenBytes, _ := ioutil.ReadAll(resp.Body)
+               jwtToken = string(jwtTokenBytes)
+       })
+
+       ginkgo.It("hit route with jwt token", func() {
+               base.RunTestCase(base.HttpTestCase{
+                       Object:       base.APISIXExpect(),
+                       Method:       http.MethodGet,
+                       Path:         "/hello",
+                       Headers:      map[string]string{"Authorization": 
jwtToken},
+                       ExpectStatus: http.StatusOK,
+                       ExpectBody:   "hello world",
+                       Sleep:        base.SleepTime,
+               })
+       })
+
+       table.DescribeTable("cleanup consumer and route",
+               func(tc base.HttpTestCase) {
+                       base.RunTestCase(tc)
+               },
+               table.Entry("delete consumer", base.HttpTestCase{
+                       Object:       base.ManagerApiExpect(),
+                       Path:         "/apisix/admin/consumers/consumer_1",
+                       Method:       http.MethodDelete,
+                       Headers:      map[string]string{"Authorization": 
base.GetToken()},
+                       ExpectStatus: http.StatusOK,
+                       ExpectBody:   `"code":0`,
+               }),
+               table.Entry("after delete consumer verify it again", 
base.HttpTestCase{
+                       Object:       base.ManagerApiExpect(),
+                       Method:       http.MethodGet,
+                       Path:         "/apisix/admin/consumers/jack",
+                       Headers:      map[string]string{"Authorization": 
base.GetToken()},
+                       ExpectStatus: http.StatusNotFound,
+                       ExpectBody:   `"message":"data not found"`,
+                       Sleep:        base.SleepTime,
+               }),
+               table.Entry("delete the route", base.HttpTestCase{
+                       Object:       base.ManagerApiExpect(),
+                       Method:       http.MethodDelete,
+                       Path:         "/apisix/admin/routes/r1",
+                       Headers:      map[string]string{"Authorization": 
base.GetToken()},
+                       ExpectStatus: http.StatusOK,
+               }),
+               table.Entry("verify the deleted route", base.HttpTestCase{
+                       Object:       base.APISIXExpect(),
+                       Method:       http.MethodGet,
+                       Path:         "/hello",
+                       ExpectStatus: http.StatusNotFound,
+                       ExpectBody:   `{"error_msg":"404 Route Not Found"}`,
+                       Sleep:        base.SleepTime,
+               }),
+       )
+})

Reply via email to