Skip to content

Commit 263528f

Browse files
committed
mock tests for authorization allow host issue
1 parent 12b8212 commit 263528f

File tree

5 files changed

+181
-3
lines changed

5 files changed

+181
-3
lines changed

modules/swagger-codegen/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,12 @@
314314
<version>${swagger-codegen-generators-version}</version>
315315
<scope>test</scope>
316316
</dependency>
317+
<dependency>
318+
<groupId>com.github.tomakehurst</groupId>
319+
<artifactId>wiremock</artifactId>
320+
<version>2.25.0</version>
321+
<scope>test</scope>
322+
</dependency>
317323
</dependencies>
318324
<repositories>
319325
<repository>

modules/swagger-codegen/src/main/java/io/swagger/codegen/v3/config/CodegenConfigurator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,6 @@ public ClientOptInput toClientOptInput() {
531531
}
532532
return true;
533533
};
534-
535534
}
536535

537536
final List<AuthorizationValue> authorizationValues = AuthParser.parse(auth);
@@ -543,7 +542,7 @@ public ClientOptInput toClientOptInput() {
543542
}
544543
}
545544
if (authorizationValue != null) {
546-
if (authorizationValue.getUrlMatcher() == null && urlMatcher != null) {
545+
if (urlMatcher != null) {
547546
authorizationValue.setUrlMatcher(urlMatcher);
548547
}
549548
authorizationValues.add(authorizationValue);

modules/swagger-codegen/src/test/java/io/swagger/codegen/v3/service/GeneratorServiceTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
public class GeneratorServiceTest {
2121

22+
2223
@Test(description = "test generator service with html2")
2324
public void testGeneratorService_HTML2_Bearer() throws IOException {
2425

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package io.swagger.codegen.v3.utils;
2+
3+
4+
import com.github.tomakehurst.wiremock.WireMockServer;
5+
import com.github.tomakehurst.wiremock.client.WireMock;
6+
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
7+
import io.swagger.codegen.v3.service.GenerationRequest;
8+
import io.swagger.codegen.v3.service.GeneratorService;
9+
import io.swagger.codegen.v3.service.HostAccessControl;
10+
import io.swagger.codegen.v3.service.Options;
11+
import io.swagger.v3.parser.core.models.AuthorizationValue;
12+
import org.testng.annotations.AfterMethod;
13+
import org.testng.annotations.BeforeMethod;
14+
import org.testng.annotations.Test;
15+
16+
import java.io.File;
17+
import java.nio.file.Files;
18+
import java.util.Arrays;
19+
import java.util.List;
20+
21+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
22+
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
23+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
24+
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
25+
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
26+
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
27+
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
28+
import static org.testng.Assert.assertEquals;
29+
import static org.testng.Assert.assertFalse;
30+
31+
public class AllowDeniedHostTest {
32+
33+
private static final int WIRE_MOCK_PORT = 9999;
34+
private static final String EXPECTED_ACCEPTS_HEADER = "application/json, application/yaml, */*";
35+
private static final String LOCALHOST = "localhost";
36+
private WireMockServer wireMockServer;
37+
38+
39+
@AfterMethod
40+
public void tearDown() throws Exception {
41+
wireMockServer.stop();
42+
}
43+
44+
@BeforeMethod
45+
public void setUp() throws Exception {
46+
wireMockServer = new WireMockServer(WIRE_MOCK_PORT);
47+
wireMockServer.start();
48+
WireMock.configureFor(WIRE_MOCK_PORT);
49+
}
50+
51+
@Test
52+
public void testAuthorizationHeaderAllowedHost() throws Exception {
53+
54+
HostAccessControl allowedHostAccessControl = new HostAccessControl();
55+
allowedHostAccessControl.setHost("localhost");
56+
57+
setupStub();
58+
59+
final String headerValue = "foobar";
60+
final String headerName = "Authorization";
61+
final AuthorizationValue authorizationValue = new AuthorizationValue(headerName, headerValue, "header",
62+
url -> url.toString().startsWith("http://localhost"));
63+
64+
GenerationRequest request = new GenerationRequest();
65+
request
66+
.codegenVersion(GenerationRequest.CodegenVersion.V3)
67+
.type(GenerationRequest.Type.SERVER)
68+
.lang("java")
69+
.specURL(getUrl())
70+
.options(
71+
new Options()
72+
.outputDir(getTmpFolder().getAbsolutePath())
73+
.authorizationValue(authorizationValue)
74+
.allowedAuthHosts(Arrays.asList(allowedHostAccessControl))
75+
);
76+
77+
new GeneratorService().generationRequest(request).generate();
78+
79+
verify(getRequestedFor(urlEqualTo("/v2/pet/1"))
80+
.withHeader("Accept", equalTo(EXPECTED_ACCEPTS_HEADER))
81+
.withHeader(headerName, equalTo(headerValue))
82+
);
83+
}
84+
85+
@Test
86+
public void testAuthorizationHeaderWithNonAllowedHost() throws Exception {
87+
88+
HostAccessControl deniedHostAccessControl = new HostAccessControl();
89+
deniedHostAccessControl.setHost("localhost");
90+
91+
setupStub();
92+
93+
final String headerValue = "foobar";
94+
String authorization = "Authorization";
95+
final AuthorizationValue authorizationValue = new AuthorizationValue(authorization,
96+
headerValue, "header", u -> false);
97+
98+
GenerationRequest request = new GenerationRequest();
99+
request
100+
.codegenVersion(GenerationRequest.CodegenVersion.V3)
101+
.type(GenerationRequest.Type.SERVER)
102+
.lang("java")
103+
.specURL(getUrl())
104+
.options(
105+
new Options()
106+
.outputDir(getTmpFolder().getAbsolutePath())
107+
.authorizationValue(authorizationValue)
108+
.deniedAuthHosts(Arrays.asList(deniedHostAccessControl))
109+
);
110+
111+
new GeneratorService().generationRequest(request).generate();
112+
113+
List<LoggedRequest> requests = WireMock.findAll(getRequestedFor(urlEqualTo("/v2/pet/1")));
114+
assertFalse(requests.get(0).containsHeader(authorization));
115+
assertEquals(requests.size(),2);
116+
117+
}
118+
119+
private String getUrl() {
120+
return String.format("http://%s:%d/v2/pet/1", LOCALHOST, WIRE_MOCK_PORT);
121+
}
122+
123+
private String setupStub() {
124+
final String expectedBody = "openapi: 3.0.0\n" +
125+
"info:\n" +
126+
" title: test\n" +
127+
" version: \"0.0.1\"\n" +
128+
"\n" +
129+
"paths:\n" +
130+
" '/contents/{id}':\n" +
131+
" parameters:\n" +
132+
" - name: id\n" +
133+
" in: path\n" +
134+
" description: test\n" +
135+
" required: true\n" +
136+
" schema:\n" +
137+
" type: integer\n" +
138+
" get:\n" +
139+
" description: test\n" +
140+
" responses:\n" +
141+
" '200':\n" +
142+
" description: OK\n" +
143+
" schema: null\n" +
144+
" $ref: '#/components/schemas/Content'\n" +
145+
"components:\n" +
146+
" schemas:\n" +
147+
" Content:\n" +
148+
" type: object\n" +
149+
" title: \t\ttest";
150+
151+
stubFor(get(urlEqualTo("/v2/pet/1"))
152+
.willReturn(aResponse()
153+
.withBody(expectedBody)
154+
.withHeader("Content-Type", "application/json")
155+
));
156+
return expectedBody;
157+
}
158+
159+
protected static File getTmpFolder() {
160+
try {
161+
File outputFolder = Files.createTempFile("codegentest-", "-tmp").toFile();
162+
outputFolder.delete();
163+
outputFolder.mkdir();
164+
outputFolder.deleteOnExit();
165+
return outputFolder;
166+
} catch (Exception e) {
167+
e.printStackTrace();
168+
return null;
169+
}
170+
}
171+
}
172+

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1152,7 +1152,7 @@
11521152
<swagger-codegen-generators-version>1.0.28-SNAPSHOT</swagger-codegen-generators-version>
11531153
<swagger-core-version>2.1.10</swagger-core-version>
11541154
<swagger-core-version-v1>1.6.3-SNAPSHOT</swagger-core-version-v1>
1155-
<swagger-parser-version>2.0.27</swagger-parser-version>
1155+
<swagger-parser-version>2.0.28-SNAPSHOT</swagger-parser-version>
11561156
<swagger-parser-version-v1>1.0.56-SNAPSHOT</swagger-parser-version-v1>
11571157
<jackson-version>2.12.1</jackson-version>
11581158
<scala-version>2.11.1</scala-version>

0 commit comments

Comments
 (0)