consumer_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * 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, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package consumer_test
  18. import (
  19. "net/http"
  20. . "github.com/onsi/ginkgo/v2"
  21. "github.com/apisix/manager-api/test/e2e/base"
  22. )
  23. var _ = Describe("Consumer", func() {
  24. DescribeTable("test consumer curd",
  25. func(tc base.HttpTestCase) {
  26. base.RunTestCase(tc)
  27. },
  28. Entry("create consumer", base.HttpTestCase{
  29. Object: base.ManagerApiExpect(),
  30. Method: http.MethodPut,
  31. Path: "/apisix/admin/consumers",
  32. Body: `{
  33. "username": "consumer_1",
  34. "plugins": {
  35. "limit-count": {
  36. "count": 2,
  37. "time_window": 60,
  38. "rejected_code": 503,
  39. "key": "remote_addr",
  40. "policy": "local"
  41. }
  42. },
  43. "desc": "test description"
  44. }`,
  45. Headers: map[string]string{"Authorization": base.GetToken()},
  46. ExpectStatus: http.StatusOK,
  47. ExpectBody: []string{"\"code\":0", "\"username\":\"consumer_1\""},
  48. }),
  49. Entry("get consumer #1", base.HttpTestCase{
  50. Object: base.ManagerApiExpect(),
  51. Method: http.MethodGet,
  52. Path: "/apisix/admin/consumers/consumer_1",
  53. Headers: map[string]string{"Authorization": base.GetToken()},
  54. ExpectStatus: http.StatusOK,
  55. ExpectBody: "\"username\":\"consumer_1\"",
  56. }),
  57. Entry("update consumer", base.HttpTestCase{
  58. Object: base.ManagerApiExpect(),
  59. Method: http.MethodPut,
  60. Path: "/apisix/admin/consumers/consumer_1",
  61. Body: `{
  62. "username": "consumer_1",
  63. "plugins": {
  64. "limit-count": {
  65. "count": 2,
  66. "time_window": 60,
  67. "rejected_code": 504,
  68. "key": "remote_addr",
  69. "policy": "local"
  70. }
  71. },
  72. "desc": "test description"
  73. }`,
  74. Headers: map[string]string{"Authorization": base.GetToken()},
  75. ExpectStatus: http.StatusOK,
  76. ExpectBody: []string{"\"code\":0", "\"username\":\"consumer_1\"", "\"rejected_code\":504"},
  77. }),
  78. Entry("get consumer #2", base.HttpTestCase{
  79. Object: base.ManagerApiExpect(),
  80. Method: http.MethodGet,
  81. Path: "/apisix/admin/consumers/consumer_1",
  82. Headers: map[string]string{"Authorization": base.GetToken()},
  83. ExpectStatus: http.StatusOK,
  84. ExpectBody: "\"rejected_code\":504",
  85. }),
  86. Entry("delete consumer", base.HttpTestCase{
  87. Object: base.ManagerApiExpect(),
  88. Method: http.MethodDelete,
  89. Path: "/apisix/admin/consumers/consumer_1",
  90. Headers: map[string]string{"Authorization": base.GetToken()},
  91. ExpectStatus: http.StatusOK,
  92. ExpectBody: "\"code\":0",
  93. }),
  94. )
  95. DescribeTable("test consumer curd exception",
  96. func(tc base.HttpTestCase) {
  97. base.RunTestCase(tc)
  98. },
  99. Entry("create consumer by POST method", base.HttpTestCase{
  100. Object: base.ManagerApiExpect(),
  101. Method: http.MethodPost,
  102. Path: "/apisix/admin/consumers",
  103. Body: `{
  104. "username": "consumer_1",
  105. "plugins": {
  106. "limit-count": {
  107. "count": 2,
  108. "time_window": 60,
  109. "rejected_code": 503,
  110. "key": "remote_addr",
  111. "policy": "local"
  112. }
  113. },
  114. "desc": "test description"
  115. }`,
  116. Headers: map[string]string{"Authorization": base.GetToken()},
  117. ExpectStatus: http.StatusNotFound,
  118. ExpectBody: "404 page not found",
  119. }),
  120. Entry("create consumer with not exist plugin", base.HttpTestCase{
  121. Object: base.ManagerApiExpect(),
  122. Method: http.MethodPut,
  123. Path: "/apisix/admin/consumers",
  124. Body: `{
  125. "username": "jack",
  126. "plugins": {
  127. "key-authnotexist": {
  128. "key": "auth-one"
  129. }
  130. },
  131. "desc": "test description"
  132. }`,
  133. Headers: map[string]string{"Authorization": base.GetToken()},
  134. ExpectStatus: http.StatusBadRequest,
  135. ExpectBody: "schema validate failed: schema not found, path: plugins.key-authnotexist",
  136. }),
  137. Entry("delete consumer (as delete not exist consumer)", base.HttpTestCase{
  138. Object: base.ManagerApiExpect(),
  139. Method: http.MethodDelete,
  140. Path: "/apisix/admin/consumers/test",
  141. Headers: map[string]string{"Authorization": base.GetToken()},
  142. ExpectStatus: http.StatusNotFound,
  143. }),
  144. )
  145. })