Skip to content

Commit e4e77d0

Browse files
committed
Add test cases for global security
1 parent 1ef51e5 commit e4e77d0

File tree

2 files changed

+1086
-0
lines changed

2 files changed

+1086
-0
lines changed

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

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717
import java.io.Writer;
1818
import java.nio.charset.StandardCharsets;
1919
import java.util.Arrays;
20+
import java.util.List;
21+
import java.util.Map;
2022

2123
import static java.nio.charset.StandardCharsets.UTF_8;
2224
import static org.junit.Assert.fail;
2325
import static org.testng.Assert.assertEquals;
2426
import static org.testng.Assert.assertTrue;
27+
import static org.testng.Assert.assertNull;
2528

2629
/**
2730
* Tests for DefaultGenerator logic
@@ -44,6 +47,95 @@ public void tearDown() throws Exception {
4447
folder.delete();
4548
}
4649

50+
@Test
51+
public void testSecurityWithoutGlobal() throws Exception {
52+
final Swagger swagger = new SwaggerParser().read("src/test/resources/2_0/petstore.json");
53+
CodegenConfig codegenConfig = new JavaClientCodegen();
54+
55+
ClientOptInput clientOptInput = new ClientOptInput().opts(new ClientOpts()).swagger(swagger).config(codegenConfig);
56+
57+
DefaultGenerator gen = new DefaultGenerator();
58+
gen.opts(clientOptInput);
59+
Map<String, List<CodegenOperation>> paths = gen.processPaths(swagger.getPaths());
60+
61+
CodegenSecurity cs, apiKey, petstoreAuth;
62+
63+
// security of "getPetById": api_key, petstore_auth
64+
CodegenOperation getPetById = findCodegenOperationByOperationId(paths, "getPetById");
65+
assertEquals(getPetById.authMethods.size(), 2);
66+
cs = getPetById.authMethods.get(0);
67+
if ("api_key".equals(cs.name)) {
68+
apiKey = cs;
69+
petstoreAuth = getPetById.authMethods.get(1);
70+
} else {
71+
petstoreAuth = cs;
72+
apiKey = getPetById.authMethods.get(1);
73+
}
74+
assertEquals(apiKey.name, "api_key");
75+
assertEquals(apiKey.type, "apiKey");
76+
assertEquals(petstoreAuth.name, "petstore_auth");
77+
assertEquals(petstoreAuth.type, "oauth2");
78+
79+
// security of "updatePetWithForm": petstore_auth
80+
CodegenOperation updatePetWithForm = findCodegenOperationByOperationId(paths, "updatePetWithForm");
81+
assertEquals(updatePetWithForm.authMethods.size(), 1);
82+
petstoreAuth = updatePetWithForm.authMethods.iterator().next();
83+
assertEquals(petstoreAuth.name, "petstore_auth");
84+
assertEquals(petstoreAuth.type, "oauth2");
85+
86+
// security of "loginUser": null (no global security either)
87+
CodegenOperation loginUser = findCodegenOperationByOperationId(paths, "loginUser");
88+
assertNull(loginUser.authMethods);
89+
}
90+
91+
@Test
92+
public void testSecurityWithGlobal() throws Exception {
93+
final Swagger swagger = new SwaggerParser().read("src/test/resources/2_0/globalSecurity.json");
94+
CodegenConfig codegenConfig = new JavaClientCodegen();
95+
96+
ClientOptInput clientOptInput = new ClientOptInput().opts(new ClientOpts()).swagger(swagger).config(codegenConfig);
97+
98+
DefaultGenerator gen = new DefaultGenerator();
99+
gen.opts(clientOptInput);
100+
Map<String, List<CodegenOperation>> paths = gen.processPaths(swagger.getPaths());
101+
102+
CodegenSecurity cs, apiKey, petstoreAuth;
103+
104+
// security of "getPetById": api_key, petstore_auth
105+
CodegenOperation getPetById = findCodegenOperationByOperationId(paths, "getPetById");
106+
assertEquals(getPetById.authMethods.size(), 2);
107+
cs = getPetById.authMethods.get(0);
108+
if ("api_key".equals(cs.name)) {
109+
apiKey = cs;
110+
petstoreAuth = getPetById.authMethods.get(1);
111+
} else {
112+
petstoreAuth = cs;
113+
apiKey = getPetById.authMethods.get(1);
114+
}
115+
assertEquals(apiKey.name, "api_key");
116+
assertEquals(apiKey.type, "apiKey");
117+
assertEquals(petstoreAuth.name, "petstore_auth");
118+
assertEquals(petstoreAuth.type, "oauth2");
119+
120+
// security of "updatePetWithForm": petstore_auth
121+
CodegenOperation updatePetWithForm = findCodegenOperationByOperationId(paths, "updatePetWithForm");
122+
assertEquals(updatePetWithForm.authMethods.size(), 1);
123+
petstoreAuth = updatePetWithForm.authMethods.iterator().next();
124+
assertEquals(petstoreAuth.name, "petstore_auth");
125+
assertEquals(petstoreAuth.type, "oauth2");
126+
127+
// security of "loginUser": api_key (from global security)
128+
CodegenOperation loginUser = findCodegenOperationByOperationId(paths, "loginUser");
129+
assertEquals(loginUser.authMethods.size(), 1);
130+
apiKey = loginUser.authMethods.iterator().next();
131+
assertEquals(apiKey.name, "api_key");
132+
assertEquals(apiKey.type, "apiKey");
133+
134+
// security of "logoutUser": null (override global security)
135+
CodegenOperation logoutUser = findCodegenOperationByOperationId(paths, "logoutUser");
136+
assertNull(logoutUser.authMethods);
137+
}
138+
47139
@Test
48140
public void testSkipOverwrite() throws Exception {
49141
final File output = folder.getRoot();
@@ -89,4 +181,15 @@ private void changeContent(File file) throws IOException {
89181
out.close();
90182
}
91183

184+
private CodegenOperation findCodegenOperationByOperationId(Map<String, List<CodegenOperation>> paths, String operationId) {
185+
for (List<CodegenOperation> ops : paths.values()) {
186+
for (CodegenOperation co : ops) {
187+
if (operationId.equals(co.operationId)) {
188+
return co;
189+
}
190+
}
191+
}
192+
return null;
193+
}
194+
92195
}

0 commit comments

Comments
 (0)