Skip to content

Commit

Permalink
patch ExceptionTranslatorIT and clear TODO
Browse files Browse the repository at this point in the history
  • Loading branch information
pblanchardie committed Jan 3, 2022
1 parent 1429182 commit 029e17e
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ public static String getDockerKeycloakVersion() {
return DOCKER_KEYCLOAK_VERSION;
}

// TODO security commons
public static Dependency springBootStarterSecurityDependency() {
return Dependency.builder().groupId(SPRINGBOOT_PACKAGE).artifactId(STARTER_SECURITY).build();
}

// TODO security commons
public static Dependency springSecurityTestDependency() {
return Dependency.builder().groupId("org.springframework.security").artifactId("spring-security-test").scope("test").build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tech.jhipster.lite.generator.server.springboot.mvc.security.oauth2.domain;

import static tech.jhipster.lite.common.domain.FileUtils.getPath;
import static tech.jhipster.lite.generator.project.domain.Constants.TEST_JAVA;
import static tech.jhipster.lite.generator.project.domain.DefaultConfig.PACKAGE_PATH;
import static tech.jhipster.lite.generator.server.springboot.mvc.security.oauth2.domain.OAuth2Security.*;

import java.util.LinkedHashMap;
Expand Down Expand Up @@ -65,6 +67,7 @@ private void addCommons(Project project, OAuth2Provider provider, String issuerU
addOAuth2ClientDependencies(project);
OAuth2Provider providerFallback = fallbackToDefault(provider);
addOAuth2ClientProperties(project, providerFallback, issuerUri);
updateExceptionTranslator(project);
if (providerFallback == OAuth2Provider.KEYCLOAK) {
addKeycloakDocker(project);
}
Expand Down Expand Up @@ -107,4 +110,38 @@ private Map<String, Object> oauth2ClientProperties(OAuth2Provider provider, Stri

return result;
}

private void updateExceptionTranslator(Project project) {
String packageNamePath = project.getPackageNamePath().orElse(getPath(PACKAGE_PATH));
String exceptionPath = getPath(TEST_JAVA, packageNamePath, "technical/infrastructure/primary/exception");

// create ExceptionTranslatorTestConfiguration to disable csrf
projectRepository.template(project, getPath(SOURCE, "test"), "ExceptionTranslatorTestConfiguration.java", exceptionPath);

// import @Import
String oldImport1 = "import org.springframework.context.ApplicationContext;";
String newImport1 =
"""
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Import;""";
projectRepository.replaceText(project, exceptionPath, "ExceptionTranslatorIT.java", oldImport1, newImport1);

// import @WithMockUser
String oldImport2 = "import org.springframework.test.util.ReflectionTestUtils;";
String newImport2 =
"""
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.util.ReflectionTestUtils;""";
projectRepository.replaceText(project, exceptionPath, "ExceptionTranslatorIT.java", oldImport2, newImport2);

// add annotations
String oldAnnotation = "@AutoConfigureMockMvc";
String newAnnotation = """
@AutoConfigureMockMvc
@Import(ExceptionTranslatorTestConfiguration.class)
@WithMockUser""";
projectRepository.replaceText(project, exceptionPath, "ExceptionTranslatorIT.java", oldAnnotation, newAnnotation);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package {{packageName}}.technical.infrastructure.primary.exception;

import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@TestConfiguration
public class ExceptionTranslatorTestConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests(requests -> requests.anyRequest().authenticated()).csrf().disable();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ void shouldAddDefault() throws Exception {
assertSecurityDependencies(project);
assertOAuth2ClientDependencies(project);
assertOAuth2ClientProperties(project, null, null);
assertUpdateExceptionTranslatorIT(project);
// TODO assert default security configuration
}

Expand All @@ -110,6 +111,7 @@ private void shouldAddClient(OAuth2Provider provider, String issuerUri) throws E
assertSecurityDependencies(project);
assertOAuth2ClientDependencies(project);
assertOAuth2ClientProperties(project, provider, issuerUri);
assertUpdateExceptionTranslatorIT(project);
if (provider == null || provider == OAuth2Provider.KEYCLOAK) {
assertDockerKeycloak(project);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@
import static tech.jhipster.lite.TestUtils.assertFileExist;
import static tech.jhipster.lite.common.domain.FileUtils.getPath;
import static tech.jhipster.lite.generator.buildtool.maven.domain.MavenDomainService.POM_XML;
import static tech.jhipster.lite.generator.project.domain.Constants.MAIN_RESOURCES;
import static tech.jhipster.lite.generator.project.domain.Constants.TEST_RESOURCES;
import static tech.jhipster.lite.generator.project.domain.Constants.*;
import static tech.jhipster.lite.generator.server.springboot.mvc.security.oauth2.domain.OAuth2Security.DEFAULT_PROVIDER;
import static tech.jhipster.lite.generator.server.springboot.mvc.security.oauth2.domain.OAuth2Security.getDockerKeycloakImage;

import java.util.List;
import java.util.Optional;
import tech.jhipster.lite.generator.project.domain.DefaultConfig;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.server.springboot.mvc.security.oauth2.domain.OAuth2Provider;

public class OAuth2SecurityAssert {

private OAuth2SecurityAssert() {}

// TODO security commons
public static void assertSecurityDependencies(Project project) {
assertFileContent(project, POM_XML, securityDependency());
assertFileContent(project, POM_XML, securityTestDependency());
Expand Down Expand Up @@ -74,7 +73,25 @@ public static void assertOAuth2ClientProperties(Project project, OAuth2Provider
assertFileContent(project, getPath(TEST_RESOURCES, "config/application.properties"), properties);
}

// TODO security commons
public static void assertUpdateExceptionTranslatorIT(Project project) {
String basePackage = project.getPackageName().orElse("com.mycompany.myapp");
String exceptionPackage = basePackage + ".technical.infrastructure.primary.exception";

String basePath = project.getPackageNamePath().orElse(getPath(DefaultConfig.PACKAGE_PATH));
String exceptionTestPath = getPath(TEST_JAVA, basePath, "technical/infrastructure/primary/exception");

assertFileExist(project, getPath(exceptionTestPath, "ExceptionTranslatorTestConfiguration.java"));

assertFileContent(project, getPath(exceptionTestPath, "ExceptionTranslatorTestConfiguration.java"), "package " + exceptionPackage);

assertFileContent(
project,
getPath(exceptionTestPath, "ExceptionTranslatorIT.java"),
"@Import(ExceptionTranslatorTestConfiguration.class)"
);
assertFileContent(project, getPath(exceptionTestPath, "ExceptionTranslatorIT.java"), "@WithMockUser");
}

public static List<String> securityDependency() {
return List.of(
"<dependency>",
Expand All @@ -84,7 +101,6 @@ public static List<String> securityDependency() {
);
}

// TODO security commons
public static List<String> securityTestDependency() {
return List.of(
"<dependency>",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ void shouldAddClient() throws Exception {
assertSecurityDependencies(project);
assertOAuth2ClientDependencies(project);
assertOAuth2ClientProperties(project, oAuth2ClientDTO.getProvider(), oAuth2ClientDTO.getIssuerUri());
assertUpdateExceptionTranslatorIT(project);
}

@Test
Expand Down Expand Up @@ -107,6 +108,7 @@ void shouldAddDefault() throws Exception {
assertSecurityDependencies(project);
assertOAuth2ClientDependencies(project);
assertOAuth2ClientProperties(project, oAuth2ClientDTO.getProvider(), oAuth2ClientDTO.getIssuerUri());
assertUpdateExceptionTranslatorIT(project);
// TODO assert default security configuration
}
}

0 comments on commit 029e17e

Please sign in to comment.