Skip to content

Commit

Permalink
patch ExceptionTranslatorIT
Browse files Browse the repository at this point in the history
  • Loading branch information
pblanchardie committed Jan 2, 2022
1 parent 1429182 commit c931df6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
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,39 @@ private Map<String, Object> oauth2ClientProperties(OAuth2Provider provider, Stri

return result;
}

// TODO move to security commons and test
// but this one adds @Import(ExceptionTranslatorTestConfiguration.class) to disable csrf,
// which will be disabled anyway when we use JWT
private void updateExceptionTranslator(Project project) {
String packageNamePath = project.getPackageNamePath().orElse(getPath(PACKAGE_PATH));

String destinationTest = getPath(TEST_JAVA, packageNamePath, "account/infrastructure/primary/rest");
projectRepository.template(project, getPath(SOURCE, "test"), "ExceptionTranslatorTestConfiguration.java", getPath(destinationTest));

String exceptionPath = getPath(TEST_JAVA, packageNamePath, "technical/infrastructure/primary/exception");

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);

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);

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();
}
}

0 comments on commit c931df6

Please sign in to comment.