@@ -19,6 +19,12 @@ import axios from "axios";
1919import YAML from "yaml" ;
2020
2121const ENDPOINT = "/apisix/admin/configs" ;
22+ const clientConfig = {
23+ baseURL : "http://localhost:1984" ,
24+ headers : {
25+ "X-API-KEY" : "edd1c9f034335f136f87ad84b625c8f1" ,
26+ } ,
27+ } ;
2228const config1 = {
2329 routes : [
2430 {
@@ -65,12 +71,60 @@ const routeWithModifiedIndex = {
6571 } ,
6672 ] ,
6773} ;
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+ }
74128
75129describe ( "Admin - Standalone" , ( ) => {
76130 const client = axios . create ( clientConfig ) ;
@@ -192,14 +246,15 @@ describe("Admin - Standalone", () => {
192246 it ( 'check route "r2"' , ( ) =>
193247 expect ( client . get ( "/r2" ) ) . rejects . toThrow (
194248 "Request failed with status code 404"
195- ) ) ;
249+ ) ) ;
196250
197251 it ( "only set routes_conf_version" , async ( ) => {
198252 const resp = await client . put (
199253 ENDPOINT ,
200254 YAML . stringify ( { routes_conf_version : 15 } ) ,
201- { headers : { "Content-Type" : "application/yaml" } ,
202- } ) ;
255+ {
256+ headers : { "Content-Type" : "application/yaml" } ,
257+ } ) ;
203258 expect ( resp . status ) . toEqual ( 202 ) ;
204259
205260 const resp_1 = await client . get ( ENDPOINT ) ;
@@ -213,8 +268,9 @@ describe("Admin - Standalone", () => {
213268 const resp2 = await client . put (
214269 ENDPOINT ,
215270 YAML . stringify ( { routes_conf_version : 17 } ) ,
216- { headers : { "Content-Type" : "application/yaml" } ,
217- } ) ;
271+ {
272+ headers : { "Content-Type" : "application/yaml" } ,
273+ } ) ;
218274 expect ( resp2 . status ) . toEqual ( 202 ) ;
219275
220276 const resp2_1 = await client . get ( ENDPOINT ) ;
@@ -269,6 +325,48 @@ describe("Admin - Standalone", () => {
269325 const resp3_2 = await client . get ( "/r2" ) ;
270326 expect ( resp3_2 . status ) . toEqual ( 200 ) ;
271327 } ) ;
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+ } ) ;
272370 } ) ;
273371
274372 describe ( "Exceptions" , ( ) => {
@@ -279,18 +377,19 @@ describe("Admin - Standalone", () => {
279377
280378 it ( "update config (lower conf_version)" , async ( ) => {
281379 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 (
282385 ENDPOINT ,
283386 YAML . stringify ( invalidConfVersionConfig1 ) ,
284- {
285- headers : {
286- "Content-Type" : "application/yaml" ,
287- } ,
288- }
387+ { headers : { "Content-Type" : "application/yaml" } }
289388 ) ;
290- expect ( resp . status ) . toEqual ( 400 ) ;
291- expect ( resp . data ) . toEqual ( {
389+ expect ( resp2 . status ) . toEqual ( 400 ) ;
390+ expect ( resp2 . data ) . toEqual ( {
292391 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 )" ,
294393 } ) ;
295394 } ) ;
296395
0 commit comments