@@ -19,6 +19,12 @@ import axios from "axios";
19
19
import YAML from "yaml" ;
20
20
21
21
const ENDPOINT = "/apisix/admin/configs" ;
22
+ const clientConfig = {
23
+ baseURL : "http://localhost:1984" ,
24
+ headers : {
25
+ "X-API-KEY" : "edd1c9f034335f136f87ad84b625c8f1" ,
26
+ } ,
27
+ } ;
22
28
const config1 = {
23
29
routes : [
24
30
{
@@ -65,12 +71,60 @@ const routeWithModifiedIndex = {
65
71
} ,
66
72
] ,
67
73
} ;
68
- const clientConfig = {
69
- baseURL : "http://localhost:1984" ,
70
- headers : {
71
- "X-API-KEY" : "edd1c9f034335f136f87ad84b625c8f1" ,
72
- } ,
73
- } ;
74
+ const routeWithKeyAuth = {
75
+ routes : [
76
+ {
77
+ id : "r1" ,
78
+ uri : "/r1" ,
79
+ upstream : {
80
+ nodes : { "127.0.0.1:1980" : 1 } ,
81
+ type : "roundrobin" ,
82
+ } ,
83
+ plugins : {
84
+ "proxy-rewrite" : { uri : "/hello" } ,
85
+ "key-auth" : { } ,
86
+ } ,
87
+ } ,
88
+ ]
89
+ }
90
+ const consumerWithModifiedIndex = {
91
+ routes : routeWithKeyAuth . routes ,
92
+ consumers : [
93
+ {
94
+ modifiedIndex : 10 ,
95
+ username : "jack" ,
96
+ plugins : {
97
+ "key-auth" : {
98
+ key : "jack-key" ,
99
+ }
100
+ } ,
101
+ } ,
102
+ ] ,
103
+ }
104
+ const credential1 = {
105
+ routes : routeWithKeyAuth . routes ,
106
+ consumers : [
107
+ {
108
+ "username" : "john"
109
+ } ,
110
+ {
111
+ "id" : "john/credentials/a" ,
112
+ "plugins" : {
113
+ "key-auth" : {
114
+ "key" : "auth-a"
115
+ }
116
+ }
117
+ } ,
118
+ {
119
+ "id" : "john/credentials/b" ,
120
+ "plugins" : {
121
+ "key-auth" : {
122
+ "key" : "auth-b"
123
+ }
124
+ }
125
+ }
126
+ ]
127
+ }
74
128
75
129
describe ( "Admin - Standalone" , ( ) => {
76
130
const client = axios . create ( clientConfig ) ;
@@ -192,14 +246,15 @@ describe("Admin - Standalone", () => {
192
246
it ( 'check route "r2"' , ( ) =>
193
247
expect ( client . get ( "/r2" ) ) . rejects . toThrow (
194
248
"Request failed with status code 404"
195
- ) ) ;
249
+ ) ) ;
196
250
197
251
it ( "only set routes_conf_version" , async ( ) => {
198
252
const resp = await client . put (
199
253
ENDPOINT ,
200
254
YAML . stringify ( { routes_conf_version : 15 } ) ,
201
- { headers : { "Content-Type" : "application/yaml" } ,
202
- } ) ;
255
+ {
256
+ headers : { "Content-Type" : "application/yaml" } ,
257
+ } ) ;
203
258
expect ( resp . status ) . toEqual ( 202 ) ;
204
259
205
260
const resp_1 = await client . get ( ENDPOINT ) ;
@@ -213,8 +268,9 @@ describe("Admin - Standalone", () => {
213
268
const resp2 = await client . put (
214
269
ENDPOINT ,
215
270
YAML . stringify ( { routes_conf_version : 17 } ) ,
216
- { headers : { "Content-Type" : "application/yaml" } ,
217
- } ) ;
271
+ {
272
+ headers : { "Content-Type" : "application/yaml" } ,
273
+ } ) ;
218
274
expect ( resp2 . status ) . toEqual ( 202 ) ;
219
275
220
276
const resp2_1 = await client . get ( ENDPOINT ) ;
@@ -269,6 +325,48 @@ describe("Admin - Standalone", () => {
269
325
const resp3_2 = await client . get ( "/r2" ) ;
270
326
expect ( resp3_2 . status ) . toEqual ( 200 ) ;
271
327
} ) ;
328
+
329
+ it ( "apply consumer with modifiedIndex" , async ( ) => {
330
+ const resp = await client . put ( ENDPOINT , consumerWithModifiedIndex ) ;
331
+ expect ( resp . status ) . toEqual ( 202 ) ;
332
+
333
+ const resp_1 = await client . get ( "/r1" , { headers : { "apikey" : "invalid-key" } } ) . catch ( ( err ) => err . response ) ;
334
+ expect ( resp_1 . status ) . toEqual ( 401 ) ;
335
+ const resp_2 = await client . get ( "/r1" , { headers : { "apikey" : "jack-key" } } ) ;
336
+ expect ( resp_2 . status ) . toEqual ( 200 ) ;
337
+
338
+ const updatedConsumer = structuredClone ( consumerWithModifiedIndex ) ;
339
+
340
+ // update key of key-auth plugin, but modifiedIndex is not changed
341
+ updatedConsumer . consumers [ 0 ] . plugins [ "key-auth" ] = { "key" : "jack-key-updated" } ;
342
+ const resp2 = await client . put ( ENDPOINT , updatedConsumer ) ;
343
+ expect ( resp2 . status ) . toEqual ( 202 ) ;
344
+
345
+ const resp2_1 = await client . get ( "/r1" , { headers : { "apikey" : "jack-key-updated" } } ) . catch ( ( err ) => err . response ) ;
346
+ expect ( resp2_1 . status ) . toEqual ( 401 ) ;
347
+ const resp2_2 = await client . get ( "/r1" , { headers : { "apikey" : "jack-key" } } ) ;
348
+ expect ( resp2_2 . status ) . toEqual ( 200 ) ;
349
+
350
+ // update key of key-auth plugin, and modifiedIndex is changed
351
+ updatedConsumer . consumers [ 0 ] . modifiedIndex ++ ;
352
+ const resp3 = await client . put ( ENDPOINT , updatedConsumer ) ;
353
+ const resp3_1 = await client . get ( "/r1" , { headers : { "apikey" : "jack-key-updated" } } ) ;
354
+ expect ( resp3_1 . status ) . toEqual ( 200 ) ;
355
+ const resp3_2 = await client . get ( "/r1" , { headers : { "apikey" : "jack-key" } } ) . catch ( ( err ) => err . response ) ;
356
+ expect ( resp3_2 . status ) . toEqual ( 401 ) ;
357
+ } ) ;
358
+
359
+ it ( "apply consumer with credentials" , async ( ) => {
360
+ const resp = await client . put ( ENDPOINT , credential1 ) ;
361
+ expect ( resp . status ) . toEqual ( 202 ) ;
362
+
363
+ const resp_1 = await client . get ( "/r1" , { headers : { "apikey" : "auth-a" } } ) ;
364
+ expect ( resp_1 . status ) . toEqual ( 200 ) ;
365
+ const resp_2 = await client . get ( "/r1" , { headers : { "apikey" : "auth-b" } } ) ;
366
+ expect ( resp_2 . status ) . toEqual ( 200 ) ;
367
+ const resp_3 = await client . get ( "/r1" , { headers : { "apikey" : "invalid-key" } } ) . catch ( ( err ) => err . response ) ;
368
+ expect ( resp_3 . status ) . toEqual ( 401 ) ;
369
+ } ) ;
272
370
} ) ;
273
371
274
372
describe ( "Exceptions" , ( ) => {
@@ -279,18 +377,19 @@ describe("Admin - Standalone", () => {
279
377
280
378
it ( "update config (lower conf_version)" , async ( ) => {
281
379
const resp = await clientException . put (
380
+ ENDPOINT ,
381
+ { routes_conf_version : 100 } ,
382
+ { headers : { "Content-Type" : "application/yaml" } }
383
+ ) ;
384
+ const resp2 = await clientException . put (
282
385
ENDPOINT ,
283
386
YAML . stringify ( invalidConfVersionConfig1 ) ,
284
- {
285
- headers : {
286
- "Content-Type" : "application/yaml" ,
287
- } ,
288
- }
387
+ { headers : { "Content-Type" : "application/yaml" } }
289
388
) ;
290
- expect ( resp . status ) . toEqual ( 400 ) ;
291
- expect ( resp . data ) . toEqual ( {
389
+ expect ( resp2 . status ) . toEqual ( 400 ) ;
390
+ expect ( resp2 . data ) . toEqual ( {
292
391
error_msg :
293
- "routes_conf_version must be greater than or equal to (20 )" ,
392
+ "routes_conf_version must be greater than or equal to (100 )" ,
294
393
} ) ;
295
394
} ) ;
296
395
0 commit comments