From a99cd4a0f0de096ac54370220314e46c080d4403 Mon Sep 17 00:00:00 2001 From: idbeta Date: Mon, 4 Jan 2021 15:23:17 +0800 Subject: [PATCH 1/8] test: backend e2e test for create consumer with jwt --- api/test/e2e/consumer_test.go | 66 +++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/api/test/e2e/consumer_test.go b/api/test/e2e/consumer_test.go index 6c26170ec8..ee26da262e 100644 --- a/api/test/e2e/consumer_test.go +++ b/api/test/e2e/consumer_test.go @@ -536,3 +536,69 @@ func TestConsumer_with_createtime_updatetime(t *testing.T) { testCaseCheck(tc, t) } } + +func TestConsumer_with_jwt(t *testing.T) { + tests := []HttpTestCase{ + { + Desc: "check consumer is not exist", + Object: ManagerApiExpect(t), + Path: "/apisix/admin/consumers/consumer_1", + Method: http.MethodGet, + Headers: map[string]string{"Authorization": token}, + ExpectStatus: http.StatusNotFound, + ExpectBody: "data not found", + }, + { + Desc: "create consumer by PUT method", + 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: "get the token of jwt ", + Object: APISIXExpect(t), + Path: "/apisix/plugin/jwt/sign", + Query: "key=user-key", + Method: http.MethodGet, + ExpectStatus: http.StatusOK, + }, + { + 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", + }, + } + + for _, tc := range tests { + testCaseCheck(tc, t) + } +} From b48b40c489bae599d6318fa35e04246b9e7e54c1 Mon Sep 17 00:00:00 2001 From: idbeta Date: Mon, 4 Jan 2021 15:25:16 +0800 Subject: [PATCH 2/8] chore: add sleeptime --- api/test/e2e/consumer_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/api/test/e2e/consumer_test.go b/api/test/e2e/consumer_test.go index ee26da262e..77affa3ef6 100644 --- a/api/test/e2e/consumer_test.go +++ b/api/test/e2e/consumer_test.go @@ -586,6 +586,7 @@ func TestConsumer_with_jwt(t *testing.T) { Query: "key=user-key", Method: http.MethodGet, ExpectStatus: http.StatusOK, + Sleep: sleepTime, }, { Desc: "delete consumer", From f3b72a1e47a34d592a44f2ac4462e4d073115b62 Mon Sep 17 00:00:00 2001 From: idbeta Date: Tue, 5 Jan 2021 12:26:53 +0800 Subject: [PATCH 3/8] fixed: test route with jwt-auth plugin --- api/test/e2e/consumer_test.go | 73 +++++++++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 12 deletions(-) diff --git a/api/test/e2e/consumer_test.go b/api/test/e2e/consumer_test.go index 77affa3ef6..b4cb042dd4 100644 --- a/api/test/e2e/consumer_test.go +++ b/api/test/e2e/consumer_test.go @@ -539,15 +539,6 @@ func TestConsumer_with_createtime_updatetime(t *testing.T) { func TestConsumer_with_jwt(t *testing.T) { tests := []HttpTestCase{ - { - Desc: "check consumer is not exist", - Object: ManagerApiExpect(t), - Path: "/apisix/admin/consumers/consumer_1", - Method: http.MethodGet, - Headers: map[string]string{"Authorization": token}, - ExpectStatus: http.StatusNotFound, - ExpectBody: "data not found", - }, { Desc: "create consumer by PUT method", Object: ManagerApiExpect(t), @@ -578,14 +569,55 @@ func TestConsumer_with_jwt(t *testing.T) { ExpectBody: "\"username\":\"consumer_1\"", Sleep: sleepTime, }, + { + Desc: "create the route", + Object: ManagerApiExpect(t), + Method: http.MethodPut, + Path: "/apisix/admin/routes/r1", + Body: `{ + "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) + if err != nil { + fmt.Printf("server not responding %s", err.Error()) + } + defer resp.Body.Close() + assert.Equal(t, 200, resp.StatusCode) + respBody, _ := ioutil.ReadAll(resp.Body) + + tests = []HttpTestCase{ { - Desc: "get the token of jwt ", + Desc: "hit route with jwt token", Object: APISIXExpect(t), - Path: "/apisix/plugin/jwt/sign", - Query: "key=user-key", Method: http.MethodGet, + Path: "/hello", + Headers: map[string]string{"Authorization": string(respBody)}, ExpectStatus: http.StatusOK, + ExpectBody: "hello world", Sleep: sleepTime, }, { @@ -597,6 +629,23 @@ func TestConsumer_with_jwt(t *testing.T) { 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 { From dbf96e3235b85243e472272220422624f2a19dfe Mon Sep 17 00:00:00 2001 From: idbeta Date: Tue, 5 Jan 2021 16:41:18 +0800 Subject: [PATCH 4/8] move the new case to route_with_auth_plugin_test.go --- api/test/e2e/consumer_test.go | 116 ------------------- api/test/e2e/route_with_auth_plugin_test.go | 118 +++++++++++++++++++- 2 files changed, 117 insertions(+), 117 deletions(-) diff --git a/api/test/e2e/consumer_test.go b/api/test/e2e/consumer_test.go index b4cb042dd4..6c26170ec8 100644 --- a/api/test/e2e/consumer_test.go +++ b/api/test/e2e/consumer_test.go @@ -536,119 +536,3 @@ func TestConsumer_with_createtime_updatetime(t *testing.T) { testCaseCheck(tc, t) } } - -func TestConsumer_with_jwt(t *testing.T) { - tests := []HttpTestCase{ - { - Desc: "create consumer by PUT method", - 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: `{ - "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) - if err != nil { - fmt.Printf("server not responding %s", err.Error()) - } - defer resp.Body.Close() - assert.Equal(t, 200, resp.StatusCode) - respBody, _ := 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(respBody)}, - 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/e2e/route_with_auth_plugin_test.go b/api/test/e2e/route_with_auth_plugin_test.go index ef5e290eb4..ce8e9efcc4 100644 --- a/api/test/e2e/route_with_auth_plugin_test.go +++ b/api/test/e2e/route_with_auth_plugin_test.go @@ -17,6 +17,8 @@ package e2e import ( + "fmt" + "io/ioutil" "net/http" "testing" "time" @@ -24,7 +26,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestRoute_With_Auth_Plugin(t *testing.T) { +func TestRoute_With_Jwt_Plugin(t *testing.T) { tests := []HttpTestCase{ { Desc: "make sure the route is not created ", @@ -173,4 +175,118 @@ func TestRoute_With_Auth_Plugin(t *testing.T) { testCaseCheck(tc, t) } + tests = []HttpTestCase{ + { + Desc: "create consumer with jwt but without 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: `{ + "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) + if err != nil { + fmt.Printf("server not responding %s", err.Error()) + } + defer resp.Body.Close() + assert.Equal(t, 200, resp.StatusCode) + respBody, _ := 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(respBody)}, + 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) + } + } From 12f271300dbacf498eee1ce0fa98f107518152d6 Mon Sep 17 00:00:00 2001 From: idbeta Date: Tue, 5 Jan 2021 16:43:48 +0800 Subject: [PATCH 5/8] chore: modify case desc --- api/test/e2e/route_with_auth_plugin_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/test/e2e/route_with_auth_plugin_test.go b/api/test/e2e/route_with_auth_plugin_test.go index ce8e9efcc4..240af7b0cf 100644 --- a/api/test/e2e/route_with_auth_plugin_test.go +++ b/api/test/e2e/route_with_auth_plugin_test.go @@ -177,7 +177,7 @@ func TestRoute_With_Jwt_Plugin(t *testing.T) { tests = []HttpTestCase{ { - Desc: "create consumer with jwt but without algorithm", + Desc: "create consumer with jwt (no algorithm)", Object: ManagerApiExpect(t), Path: "/apisix/admin/consumers", Method: http.MethodPut, From b68719f1b93c09a20ed72793587eb0894d207fc1 Mon Sep 17 00:00:00 2001 From: idbeta Date: Wed, 6 Jan 2021 18:12:22 +0800 Subject: [PATCH 6/8] chore: modify jwt token name --- api/test/e2e/route_with_auth_plugin_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/test/e2e/route_with_auth_plugin_test.go b/api/test/e2e/route_with_auth_plugin_test.go index 240af7b0cf..488c36be64 100644 --- a/api/test/e2e/route_with_auth_plugin_test.go +++ b/api/test/e2e/route_with_auth_plugin_test.go @@ -244,7 +244,7 @@ func TestRoute_With_Jwt_Plugin(t *testing.T) { } defer resp.Body.Close() assert.Equal(t, 200, resp.StatusCode) - respBody, _ := ioutil.ReadAll(resp.Body) + jwttoken, _ := ioutil.ReadAll(resp.Body) tests = []HttpTestCase{ { @@ -252,7 +252,7 @@ func TestRoute_With_Jwt_Plugin(t *testing.T) { Object: APISIXExpect(t), Method: http.MethodGet, Path: "/hello", - Headers: map[string]string{"Authorization": string(respBody)}, + Headers: map[string]string{"Authorization": string(jwttoken)}, ExpectStatus: http.StatusOK, ExpectBody: "hello world", Sleep: sleepTime, From b19c9daebbb40c08a26975715fd909237fe90c0f Mon Sep 17 00:00:00 2001 From: idbeta Date: Fri, 8 Jan 2021 11:49:10 +0800 Subject: [PATCH 7/8] modify method for get the token of jwt --- api/test/e2e/route_with_auth_plugin_test.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/api/test/e2e/route_with_auth_plugin_test.go b/api/test/e2e/route_with_auth_plugin_test.go index 488c36be64..4745d1be31 100644 --- a/api/test/e2e/route_with_auth_plugin_test.go +++ b/api/test/e2e/route_with_auth_plugin_test.go @@ -17,7 +17,6 @@ package e2e import ( - "fmt" "io/ioutil" "net/http" "testing" @@ -239,9 +238,7 @@ func TestRoute_With_Jwt_Plugin(t *testing.T) { request, _ := http.NewRequest("GET", basepath+"/apisix/plugin/jwt/sign?key=user-key", nil) request.Header.Add("Authorization", token) resp, err := http.DefaultClient.Do(request) - if err != nil { - fmt.Printf("server not responding %s", err.Error()) - } + assert.Nil(t, err) defer resp.Body.Close() assert.Equal(t, 200, resp.StatusCode) jwttoken, _ := ioutil.ReadAll(resp.Body) From 64ce139276d71feaa128a22f712d9465e31863fb Mon Sep 17 00:00:00 2001 From: idbeta Date: Tue, 12 Jan 2021 10:52:24 +0800 Subject: [PATCH 8/8] modify file name --- ...te_with_auth_plugin_test.go => route_with_plugin_jwt_test.go} | 1 - 1 file changed, 1 deletion(-) rename api/test/e2e/{route_with_auth_plugin_test.go => route_with_plugin_jwt_test.go} (99%) diff --git a/api/test/e2e/route_with_auth_plugin_test.go b/api/test/e2e/route_with_plugin_jwt_test.go similarity index 99% rename from api/test/e2e/route_with_auth_plugin_test.go rename to api/test/e2e/route_with_plugin_jwt_test.go index 4745d1be31..9ceec91d07 100644 --- a/api/test/e2e/route_with_auth_plugin_test.go +++ b/api/test/e2e/route_with_plugin_jwt_test.go @@ -285,5 +285,4 @@ func TestRoute_With_Jwt_Plugin(t *testing.T) { for _, tc := range tests { testCaseCheck(tc, t) } - }