Skip to content

Commit

Permalink
mock tests for authorization allow host issue
Browse files Browse the repository at this point in the history
  • Loading branch information
gracekarina committed Aug 21, 2021
1 parent 12b8212 commit 263528f
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 3 deletions.
6 changes: 6 additions & 0 deletions modules/swagger-codegen/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,12 @@
<version>${swagger-codegen-generators-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>2.25.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,6 @@ public ClientOptInput toClientOptInput() {
}
return true;
};

}

final List<AuthorizationValue> authorizationValues = AuthParser.parse(auth);
Expand All @@ -543,7 +542,7 @@ public ClientOptInput toClientOptInput() {
}
}
if (authorizationValue != null) {
if (authorizationValue.getUrlMatcher() == null && urlMatcher != null) {

This comment has been minimized.

Copy link
@gracekarina

gracekarina Aug 21, 2021

Author Contributor

@frantuma urlMatcher is set true by default in AuthorizationValue's constructor so it's never null

if (urlMatcher != null) {
authorizationValue.setUrlMatcher(urlMatcher);
}
authorizationValues.add(authorizationValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

public class GeneratorServiceTest {


@Test(description = "test generator service with html2")
public void testGeneratorService_HTML2_Bearer() throws IOException {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package io.swagger.codegen.v3.utils;


import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
import io.swagger.codegen.v3.service.GenerationRequest;
import io.swagger.codegen.v3.service.GeneratorService;
import io.swagger.codegen.v3.service.HostAccessControl;
import io.swagger.codegen.v3.service.Options;
import io.swagger.v3.parser.core.models.AuthorizationValue;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.File;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;

public class AllowDeniedHostTest {

private static final int WIRE_MOCK_PORT = 9999;
private static final String EXPECTED_ACCEPTS_HEADER = "application/json, application/yaml, */*";
private static final String LOCALHOST = "localhost";
private WireMockServer wireMockServer;


@AfterMethod
public void tearDown() throws Exception {
wireMockServer.stop();
}

@BeforeMethod
public void setUp() throws Exception {
wireMockServer = new WireMockServer(WIRE_MOCK_PORT);
wireMockServer.start();
WireMock.configureFor(WIRE_MOCK_PORT);
}

@Test
public void testAuthorizationHeaderAllowedHost() throws Exception {

HostAccessControl allowedHostAccessControl = new HostAccessControl();
allowedHostAccessControl.setHost("localhost");

setupStub();

final String headerValue = "foobar";
final String headerName = "Authorization";
final AuthorizationValue authorizationValue = new AuthorizationValue(headerName, headerValue, "header",
url -> url.toString().startsWith("http://localhost"));

GenerationRequest request = new GenerationRequest();
request
.codegenVersion(GenerationRequest.CodegenVersion.V3)
.type(GenerationRequest.Type.SERVER)
.lang("java")
.specURL(getUrl())
.options(
new Options()
.outputDir(getTmpFolder().getAbsolutePath())
.authorizationValue(authorizationValue)
.allowedAuthHosts(Arrays.asList(allowedHostAccessControl))
);

new GeneratorService().generationRequest(request).generate();

verify(getRequestedFor(urlEqualTo("/v2/pet/1"))
.withHeader("Accept", equalTo(EXPECTED_ACCEPTS_HEADER))
.withHeader(headerName, equalTo(headerValue))
);
}

@Test
public void testAuthorizationHeaderWithNonAllowedHost() throws Exception {

HostAccessControl deniedHostAccessControl = new HostAccessControl();
deniedHostAccessControl.setHost("localhost");

setupStub();

final String headerValue = "foobar";
String authorization = "Authorization";
final AuthorizationValue authorizationValue = new AuthorizationValue(authorization,
headerValue, "header", u -> false);

GenerationRequest request = new GenerationRequest();
request
.codegenVersion(GenerationRequest.CodegenVersion.V3)
.type(GenerationRequest.Type.SERVER)
.lang("java")
.specURL(getUrl())
.options(
new Options()
.outputDir(getTmpFolder().getAbsolutePath())
.authorizationValue(authorizationValue)
.deniedAuthHosts(Arrays.asList(deniedHostAccessControl))
);

new GeneratorService().generationRequest(request).generate();

List<LoggedRequest> requests = WireMock.findAll(getRequestedFor(urlEqualTo("/v2/pet/1")));
assertFalse(requests.get(0).containsHeader(authorization));
assertEquals(requests.size(),2);

}

private String getUrl() {
return String.format("http://%s:%d/v2/pet/1", LOCALHOST, WIRE_MOCK_PORT);
}

private String setupStub() {
final String expectedBody = "openapi: 3.0.0\n" +
"info:\n" +
" title: test\n" +
" version: \"0.0.1\"\n" +
"\n" +
"paths:\n" +
" '/contents/{id}':\n" +
" parameters:\n" +
" - name: id\n" +
" in: path\n" +
" description: test\n" +
" required: true\n" +
" schema:\n" +
" type: integer\n" +
" get:\n" +
" description: test\n" +
" responses:\n" +
" '200':\n" +
" description: OK\n" +
" schema: null\n" +
" $ref: '#/components/schemas/Content'\n" +
"components:\n" +
" schemas:\n" +
" Content:\n" +
" type: object\n" +
" title: \t\ttest";

stubFor(get(urlEqualTo("/v2/pet/1"))
.willReturn(aResponse()
.withBody(expectedBody)
.withHeader("Content-Type", "application/json")
));
return expectedBody;
}

protected static File getTmpFolder() {
try {
File outputFolder = Files.createTempFile("codegentest-", "-tmp").toFile();
outputFolder.delete();
outputFolder.mkdir();
outputFolder.deleteOnExit();
return outputFolder;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,7 @@
<swagger-codegen-generators-version>1.0.28-SNAPSHOT</swagger-codegen-generators-version>
<swagger-core-version>2.1.10</swagger-core-version>
<swagger-core-version-v1>1.6.3-SNAPSHOT</swagger-core-version-v1>
<swagger-parser-version>2.0.27</swagger-parser-version>
<swagger-parser-version>2.0.28-SNAPSHOT</swagger-parser-version>
<swagger-parser-version-v1>1.0.56-SNAPSHOT</swagger-parser-version-v1>
<jackson-version>2.12.1</jackson-version>
<scala-version>2.11.1</scala-version>
Expand Down

0 comments on commit 263528f

Please sign in to comment.