Skip to content

Commit

Permalink
- more tests
Browse files Browse the repository at this point in the history
- writePropertyBack("useSpringSecurity", ..)
  • Loading branch information
nhomble committed Mar 18, 2020
1 parent a2cde1d commit 183374b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import java.nio.file.Path;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.stream.Collectors;

import static org.openapitools.codegen.utils.OnceLogger.once;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ public void processOpts() {
if (additionalProperties.containsKey(USE_SPRING_SECURITY)) {
this.setUseSpringSecurity(Boolean.valueOf(additionalProperties.get(USE_SPRING_SECURITY).toString()));
}
writePropertyBack(USE_SPRING_SECURITY, this.useSpringSecurity);

typeMapping.put("file", "Resource");
importMapping.put("Resource", "org.springframework.core.io.Resource");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,32 @@ public void testPreAuthorizeOnOauth() throws IOException {
"@PreAuthorize(\"(hasAuthority('write_pets') and hasAuthority('rw_pets') or (hasAuthority('read_pets')\")");
}

@Test
public void testPreAuthorizeOnGlobalOauth() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
String outputPath = output.getAbsolutePath().replace('\\', '/');

OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_0/security-oauth.yaml", null, new ParseOptions()).getOpenAPI();

SpringCodegen codegen = new SpringCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true");
codegen.additionalProperties().put(SpringCodegen.USE_SPRING_SECURITY, "true");

ClientOptInput input = new ClientOptInput();
input.setOpenAPI(openAPI);
input.setConfig(codegen);

MockDefaultGenerator generator = new MockDefaultGenerator();
generator.opts(input).generate();

String path = outputPath + "/src/main/java/org/openapitools/api/PetsApi.java";
checkFileContains(generator, path,
"@PreAuthorize(\"(hasAuthority('read_pets')\")");
}

@Test
public void testDisableSpringSecurityForOauth() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
Expand Down Expand Up @@ -626,4 +652,30 @@ public void testDisableSpringSecurityForJwtAuth() throws IOException {
checkFileNotContains(generator, path,
"@PreAuthorize(\"hasAuthority('scope:another') or hasAuthority('scope:specific')\")");
}

@Test
public void testPreAuthorizeOnGlobalJwtAuth() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
String outputPath = output.getAbsolutePath().replace('\\', '/');

OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_0/security-oauth-bearer.yaml", null, new ParseOptions()).getOpenAPI();

SpringCodegen codegen = new SpringCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true");
codegen.additionalProperties().put(SpringCodegen.USE_SPRING_SECURITY, "true");

ClientOptInput input = new ClientOptInput();
input.setOpenAPI(openAPI);
input.setConfig(codegen);

MockDefaultGenerator generator = new MockDefaultGenerator();
generator.opts(input).generate();

String path = outputPath + "/src/main/java/org/openapitools/api/PetsApi.java";
checkFileContains(generator, path,
"@PreAuthorize(\"hasAuthority('scope:global')\")");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ paths:
- "scope:specific"
- anotherAuth:
- "scope:another"
post:
post:
summary: make the pet
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ security:

paths:
/pets:
get:
summary: Get the pet
patch:
summary: Add a new pet
security:
Expand Down

0 comments on commit 183374b

Please sign in to comment.