credential-plugin-multi-credentials.t 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. use t::APISIX 'no_plan';
  18. repeat_each(1);
  19. no_long_string();
  20. no_root_location();
  21. run_tests;
  22. __DATA__
  23. === TEST 1: enable key-auth plugin on /hello
  24. --- config
  25. location /t {
  26. content_by_lua_block {
  27. local t = require("lib.test_admin").test
  28. -- basic-auth on route 1
  29. local code, body = t('/apisix/admin/routes/1',
  30. ngx.HTTP_PUT,
  31. [[{
  32. "plugins": {
  33. "key-auth": {}
  34. },
  35. "upstream": {
  36. "nodes": {
  37. "127.0.0.1:1980": 1
  38. },
  39. "type": "roundrobin"
  40. },
  41. "uri": "/hello"
  42. }]]
  43. )
  44. if code >= 300 then
  45. ngx.status = code
  46. end
  47. ngx.say(body)
  48. }
  49. }
  50. --- request
  51. GET /t
  52. --- response_body
  53. passed
  54. === TEST 2: create a consumer
  55. --- config
  56. location /t {
  57. content_by_lua_block {
  58. local t = require("lib.test_admin").test
  59. local code, body = t('/apisix/admin/consumers',
  60. ngx.HTTP_PUT,
  61. [[{
  62. "username": "jack"
  63. }]]
  64. )
  65. if code >= 300 then
  66. ngx.status = code
  67. end
  68. ngx.say(body)
  69. }
  70. }
  71. --- request
  72. GET /t
  73. --- response_body
  74. passed
  75. === TEST 3: create the first credential with the key-auth plugin enabled for the consumer
  76. --- config
  77. location /t {
  78. content_by_lua_block {
  79. local t = require("lib.test_admin").test
  80. local code, body = t('/apisix/admin/consumers/jack/credentials/the-first-one',
  81. ngx.HTTP_PUT,
  82. [[{
  83. "plugins": {
  84. "key-auth": {"key": "p7a3k6r4t9"}
  85. }
  86. }]],
  87. [[{
  88. "value":{
  89. "id":"the-first-one",
  90. "plugins":{
  91. "key-auth": {"key": "fsFPtg7BtXMXkvSnS9e1zw=="}
  92. }
  93. },
  94. "key":"/apisix/consumers/jack/credentials/the-first-one"
  95. }]]
  96. )
  97. ngx.status = code
  98. ngx.say(body)
  99. }
  100. }
  101. --- request
  102. GET /t
  103. --- response_body
  104. passed
  105. === TEST 4: create the second credential with the key-auth plugin enabled for the consumer
  106. --- config
  107. location /t {
  108. content_by_lua_block {
  109. local t = require("lib.test_admin").test
  110. local code, body = t('/apisix/admin/consumers/jack/credentials/the-second-one',
  111. ngx.HTTP_PUT,
  112. [[{
  113. "plugins": {
  114. "key-auth": {"key": "v8p3q6r7t9"}
  115. }
  116. }]],
  117. [[{
  118. "value":{
  119. "id":"the-second-one",
  120. "plugins":{
  121. "key-auth": {"key": "QwGua2GjZjOiq+Mj3Mef2g=="}
  122. }
  123. },
  124. "key":"/apisix/consumers/jack/credentials/the-second-one"
  125. }]]
  126. )
  127. ngx.status = code
  128. ngx.say(body)
  129. }
  130. }
  131. --- request
  132. GET /t
  133. --- response_body
  134. passed
  135. === TEST 5: request /hello with the key of the first credential: should be OK
  136. --- request
  137. GET /hello
  138. --- more_headers
  139. apikey: p7a3k6r4t9
  140. --- response_body
  141. hello world
  142. === TEST 6: request /hello with the key of second credential: should be OK
  143. --- request
  144. GET /hello
  145. --- more_headers
  146. apikey: v8p3q6r7t9
  147. --- response_body
  148. hello world
  149. === TEST 7: delete the first credential
  150. --- config
  151. location /t {
  152. content_by_lua_block {
  153. local t = require("lib.test_admin").test
  154. local code, body = t('/apisix/admin/consumers/jack/credentials/the-first-one', ngx.HTTP_DELETE)
  155. ngx.status = code
  156. ngx.say(body)
  157. }
  158. }
  159. --- request
  160. GET /t
  161. --- response_body
  162. passed
  163. === TEST 8: request /hello with the key of the first credential: should be not OK
  164. --- request
  165. GET /hello
  166. --- more_headers
  167. apikey: p7a3k6r4t9
  168. --- error_code: 401
  169. === TEST 9: request /hello with the key of the second credential: should be OK
  170. --- request
  171. GET /hello
  172. --- more_headers
  173. apikey: v8p3q6r7t9
  174. --- response_body
  175. hello world
  176. === TEST 10: delete the second credential
  177. --- config
  178. location /t {
  179. content_by_lua_block {
  180. local t = require("lib.test_admin").test
  181. local code, body = t('/apisix/admin/consumers/jack/credentials/the-second-one', ngx.HTTP_DELETE)
  182. ngx.status = code
  183. ngx.say(body)
  184. }
  185. }
  186. --- request
  187. GET /t
  188. --- response_body
  189. passed
  190. === TEST 11: request /hello with the key of the second credential: should be not OK
  191. --- request
  192. GET /hello
  193. --- more_headers
  194. apikey: v8p3q6r7t9
  195. --- error_code: 401