Skip to content

Commit 02a9fe2

Browse files
committed
Add more test cases for global security
1 parent 84928ab commit 02a9fe2

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

modules/swagger-codegen/src/test/java/io/swagger/codegen/DefaultGeneratorTest.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void testSecurityWithGlobal() throws Exception {
9090
gen.opts(clientOptInput);
9191
Map<String, List<CodegenOperation>> paths = gen.processPaths(swagger.getPaths());
9292

93-
CodegenSecurity apiKey, petstoreAuth;
93+
CodegenSecurity cs, apiKey, apiKey2, petstoreAuth;
9494

9595
// security of "getPetById": api_key
9696
CodegenOperation getPetById = findCodegenOperationByOperationId(paths, "getPetById");
@@ -106,16 +106,41 @@ public void testSecurityWithGlobal() throws Exception {
106106
assertEquals(petstoreAuth.name, "petstore_auth");
107107
assertEquals(petstoreAuth.type, "oauth2");
108108

109-
// security of "loginUser": api_key (from global security)
109+
// security of "loginUser": api_key, petstore_auth (from global security)
110110
CodegenOperation loginUser = findCodegenOperationByOperationId(paths, "loginUser");
111-
assertEquals(loginUser.authMethods.size(), 1);
112-
apiKey = loginUser.authMethods.iterator().next();
111+
assertEquals(loginUser.authMethods.size(), 2);
112+
cs = loginUser.authMethods.get(0);
113+
if ("api_key".equals(cs.name)) {
114+
apiKey = cs;
115+
petstoreAuth = loginUser.authMethods.get(1);
116+
} else {
117+
petstoreAuth = cs;
118+
apiKey = loginUser.authMethods.get(1);
119+
}
113120
assertEquals(apiKey.name, "api_key");
114121
assertEquals(apiKey.type, "apiKey");
122+
assertEquals(petstoreAuth.name, "petstore_auth");
123+
assertEquals(petstoreAuth.type, "oauth2");
115124

116125
// security of "logoutUser": null (override global security)
117126
CodegenOperation logoutUser = findCodegenOperationByOperationId(paths, "logoutUser");
118127
assertNull(logoutUser.authMethods);
128+
129+
// security of "getUserByName": api_key, api_key2 (override global security)
130+
CodegenOperation getUserByName = findCodegenOperationByOperationId(paths, "getUserByName");
131+
assertEquals(getUserByName.authMethods.size(), 2);
132+
cs = getUserByName.authMethods.get(0);
133+
if ("api_key".equals(cs.name)) {
134+
apiKey = cs;
135+
apiKey2 = getUserByName.authMethods.get(1);
136+
} else {
137+
apiKey2 = cs;
138+
apiKey = getUserByName.authMethods.get(1);
139+
}
140+
assertEquals(apiKey.name, "api_key");
141+
assertEquals(apiKey.type, "apiKey");
142+
assertEquals(apiKey2.name, "api_key2");
143+
assertEquals(apiKey2.type, "apiKey");
119144
}
120145

121146
@Test

modules/swagger-codegen/src/test/resources/2_0/globalSecurity.json

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,13 @@
738738
"400": {
739739
"description": "Invalid username supplied"
740740
}
741-
}
741+
},
742+
"security": [
743+
{
744+
"api_key": [],
745+
"api_key2": []
746+
}
747+
]
742748
},
743749
"put": {
744750
"tags": [
@@ -815,6 +821,11 @@
815821
"name": "api_key",
816822
"in": "header"
817823
},
824+
"api_key2": {
825+
"type": "apiKey",
826+
"name": "api_key2",
827+
"in": "query"
828+
},
818829
"petstore_auth": {
819830
"type": "oauth2",
820831
"authorizationUrl": "http://petstore.swagger.io/api/oauth/dialog",
@@ -827,7 +838,11 @@
827838
},
828839
"security": [
829840
{
830-
"api_key": []
841+
"api_key": [],
842+
"petstore_auth": [
843+
"write:pets",
844+
"read:pets"
845+
]
831846
}
832847
],
833848
"definitions": {

0 commit comments

Comments
 (0)