credential-plugin-incremental-effective.t 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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: test continuous watch etcd changes without APISIX reload
  24. --- config
  25. location /t {
  26. content_by_lua_block {
  27. local t = require("lib.test_admin").test
  28. -- enable key-auth on /hello
  29. 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. ngx.sleep(0.2) -- On some machines, changes may not be instantly watched, so sleep makes the test more robust.
  45. -- request /hello without key-auth should response status 401
  46. local code, body = t('/hello', ngx.HTTP_GET)
  47. assert(code == 401)
  48. -- add a consumer jack
  49. t('/apisix/admin/consumers',
  50. ngx.HTTP_PUT,
  51. [[{
  52. "username":"jack"
  53. }]],
  54. [[{
  55. "key": "/apisix/consumers/jack",
  56. "value":
  57. {
  58. "username":"jack"
  59. }
  60. }]]
  61. )
  62. -- create first credential for consumer jack
  63. t('/apisix/admin/consumers/jack/credentials/the-first-one',
  64. ngx.HTTP_PUT,
  65. [[{
  66. "plugins":{"key-auth":{"key":"p7a3k6r4t9"}}
  67. }]],
  68. [[{
  69. "value":{
  70. "id":"the-first-one",
  71. "plugins":{"key-auth":{"key":"p7a3k6r4t9"}}
  72. },
  73. "key":"/apisix/consumers/jack/credentials/the-first-one"
  74. }]]
  75. )
  76. ngx.sleep(0.2)
  77. -- request /hello with credential a
  78. local headers = {}
  79. headers["apikey"] = "p7a3k6r4t9"
  80. code, body = t('/hello', ngx.HTTP_GET, "", nil, headers)
  81. assert(code == 200)
  82. -- create second credential for consumer jack
  83. t('/apisix/admin/consumers/jack/credentials/the-second-one',
  84. ngx.HTTP_PUT,
  85. [[{
  86. "plugins":{"key-auth":{"key":"v8p3q6r7t9"}}
  87. }]],
  88. [[{
  89. "value":{
  90. "id":"the-second-one",
  91. "plugins":{"key-auth":{"key":"v8p3q6r7t9"}}
  92. },
  93. "key":"/apisix/consumers/jack/credentials/the-second-one"
  94. }]]
  95. )
  96. ngx.sleep(0.2)
  97. -- request /hello with credential b
  98. headers["apikey"] = "v8p3q6r7t9"
  99. code, body = t('/hello', ngx.HTTP_GET, "", nil, headers)
  100. assert(code == 200)
  101. -- delete the first credential
  102. code, body = t('/apisix/admin/consumers/jack/credentials/the-first-one', ngx.HTTP_DELETE)
  103. assert(code == 200)
  104. ngx.sleep(0.2)
  105. -- request /hello with credential a
  106. headers["apikey"] = "p7a3k6r4t9"
  107. code, body = t('/hello', ngx.HTTP_GET, "", nil, headers)
  108. assert(code == 401)
  109. }
  110. }
  111. --- request
  112. GET /t