diff --git a/WebServiceBySpringBootAndAWS/src/main/java/com/banjjoknim/book/springboot/config/auth/SecurityConfig.java b/WebServiceBySpringBootAndAWS/src/main/java/com/banjjoknim/book/springboot/config/auth/SecurityConfig.java index db07b49..adc7031 100644 --- a/WebServiceBySpringBootAndAWS/src/main/java/com/banjjoknim/book/springboot/config/auth/SecurityConfig.java +++ b/WebServiceBySpringBootAndAWS/src/main/java/com/banjjoknim/book/springboot/config/auth/SecurityConfig.java @@ -17,7 +17,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { http.csrf().disable().headers().frameOptions().disable() .and() .authorizeRequests() - .antMatchers("/", "/css/**", "/images/**", "/js/**", "/h2-console/**").permitAll() + .antMatchers("/", "/css/**", "/images/**", "/js/**", "/h2-console/**", "/profile").permitAll() .antMatchers("/api/v1/**").hasRole(Role.USER.name()) .anyRequest().authenticated() .and() diff --git a/WebServiceBySpringBootAndAWS/src/main/java/com/banjjoknim/book/springboot/web/ProfileController.java b/WebServiceBySpringBootAndAWS/src/main/java/com/banjjoknim/book/springboot/web/ProfileController.java new file mode 100644 index 0000000..f54969d --- /dev/null +++ b/WebServiceBySpringBootAndAWS/src/main/java/com/banjjoknim/book/springboot/web/ProfileController.java @@ -0,0 +1,29 @@ +package com.banjjoknim.book.springboot.web; + +import lombok.RequiredArgsConstructor; +import org.springframework.core.env.Environment; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Arrays; +import java.util.List; + +@RequiredArgsConstructor +@RestController +public class ProfileController { + private final Environment env; + + @GetMapping("/profile") + public String profile() { + List profiles = Arrays.asList(env.getActiveProfiles()); + + List realProfiles = Arrays.asList("real", "real1", "real2"); + + String defaultProfile = profiles.isEmpty() ? "default" : profiles.get(0); + + return profiles.stream() + .filter(realProfiles::contains) + .findAny() + .orElse(defaultProfile); + } +} diff --git a/WebServiceBySpringBootAndAWS/src/test/java/com/banjjoknim/book/springboot/web/ProfileControllerTest.java b/WebServiceBySpringBootAndAWS/src/test/java/com/banjjoknim/book/springboot/web/ProfileControllerTest.java new file mode 100644 index 0000000..9a38607 --- /dev/null +++ b/WebServiceBySpringBootAndAWS/src/test/java/com/banjjoknim/book/springboot/web/ProfileControllerTest.java @@ -0,0 +1,34 @@ +package com.banjjoknim.book.springboot.web; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class ProfileControllerTest { + + @LocalServerPort + private int port; + + @Autowired + private TestRestTemplate restTemplate; + + @Test + public void profile은_인증없이_호출된다() { + String expected = "default"; + + ResponseEntity response = restTemplate.getForEntity("/profile", String.class); + + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(response.getBody()).isEqualTo(expected); + } +} \ No newline at end of file diff --git a/WebServiceBySpringBootAndAWS/src/test/java/com/banjjoknim/book/springboot/web/ProfileControllerUnitTest.java b/WebServiceBySpringBootAndAWS/src/test/java/com/banjjoknim/book/springboot/web/ProfileControllerUnitTest.java new file mode 100644 index 0000000..f31dcc5 --- /dev/null +++ b/WebServiceBySpringBootAndAWS/src/test/java/com/banjjoknim/book/springboot/web/ProfileControllerUnitTest.java @@ -0,0 +1,60 @@ +package com.banjjoknim.book.springboot.web; + +import org.junit.Test; +import org.springframework.mock.env.MockEnvironment; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ProfileControllerUnitTest { + + @Test + public void real_profile이_조회된다() { + //given + String expectedProfile = "real"; + MockEnvironment env = new MockEnvironment(); + env.addActiveProfile(expectedProfile); + env.addActiveProfile("oauth"); + env.addActiveProfile("real-db"); + + ProfileController controller = new ProfileController(env); + + //when + String profile = controller.profile(); + + //then + assertThat(profile).isEqualTo(expectedProfile); + } + + @Test + public void real_profile이_없으면_첫_번째가_조회된다() { + //given + String expectedProfile = "oauth"; + MockEnvironment env = new MockEnvironment(); + env.addActiveProfile(expectedProfile); + env.addActiveProfile("real-db"); + + ProfileController controller = new ProfileController(env); + + //when + String profile = controller.profile(); + + //then + assertThat(profile).isEqualTo(expectedProfile); + } + + @Test + public void active_profile이_없으면_default가_조회된다() { + //given + String expectedProfile = "default"; + MockEnvironment env = new MockEnvironment(); + + ProfileController controller = new ProfileController(env); + + //when + String profile = controller.profile(); + + //then + assertThat(profile).isEqualTo(expectedProfile); + } + +} \ No newline at end of file