|
| 1 | +package nextstep.app.oauth2; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import nextstep.app.domain.Member; |
| 5 | +import nextstep.app.domain.MemberRepository; |
| 6 | +import org.junit.jupiter.api.BeforeEach; |
| 7 | +import org.junit.jupiter.params.ParameterizedTest; |
| 8 | +import org.junit.jupiter.params.provider.MethodSource; |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 11 | +import org.springframework.boot.test.context.SpringBootTest; |
| 12 | +import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock; |
| 13 | +import org.springframework.http.HttpHeaders; |
| 14 | +import org.springframework.mock.web.MockHttpSession; |
| 15 | +import org.springframework.test.context.ActiveProfiles; |
| 16 | +import org.springframework.test.web.servlet.MockMvc; |
| 17 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
| 18 | +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; |
| 19 | + |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.stream.Stream; |
| 23 | + |
| 24 | +import static com.github.tomakehurst.wiremock.client.WireMock.*; |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; |
| 27 | + |
| 28 | +@ActiveProfiles("test") |
| 29 | +@SpringBootTest |
| 30 | +@AutoConfigureMockMvc |
| 31 | +@AutoConfigureWireMock(port = 8089) |
| 32 | +class GithubAuthenticationFilterTest { |
| 33 | + |
| 34 | + @Autowired |
| 35 | + private MockMvc mockMvc; |
| 36 | + |
| 37 | + @Autowired |
| 38 | + private MemberRepository memberRepository; |
| 39 | + |
| 40 | + @BeforeEach |
| 41 | + void setupMockServer() throws Exception { |
| 42 | + UserStub userB = new UserStub( "b", "b_access_token", "[email protected]", "b", "b_avatar_url"); |
| 43 | + UserStub userC = new UserStub( "c", "c_access_token", "[email protected]", "c", "c_avatar_url"); |
| 44 | + |
| 45 | + setupUserStub(userB); |
| 46 | + setupUserStub(userC); |
| 47 | + } |
| 48 | + |
| 49 | + @ParameterizedTest |
| 50 | + @MethodSource("userProvider") |
| 51 | + void authenticationFilter(UserStub user) throws Exception { |
| 52 | + String requestUri = "/login/oauth2/code/github?code=" + user.code; |
| 53 | + |
| 54 | + mockMvc.perform(MockMvcRequestBuilders.get(requestUri)) |
| 55 | + .andDo(print()) |
| 56 | + .andExpect(MockMvcResultMatchers.status().is3xxRedirection()) |
| 57 | + .andExpect(MockMvcResultMatchers.redirectedUrl("/")); |
| 58 | + |
| 59 | + Member savedMember = memberRepository.findByEmail(user.email).get(); |
| 60 | + assertThat(savedMember).isNotNull(); |
| 61 | + assertThat(savedMember.getEmail()).isEqualTo(user.email); |
| 62 | + assertThat(savedMember.getName()).isEqualTo(user.name); |
| 63 | + } |
| 64 | + |
| 65 | + @ParameterizedTest |
| 66 | + @MethodSource("userProvider") |
| 67 | + void authenticationFilterWithState(UserStub user) throws Exception { |
| 68 | + MockHttpSession session = new MockHttpSession(); |
| 69 | + |
| 70 | + String state = mockMvc.perform(MockMvcRequestBuilders.get("/oauth2/authorization/github").session(session)) |
| 71 | + .andExpect(MockMvcResultMatchers.status().is3xxRedirection()) |
| 72 | + .andReturn().getResponse().getHeader(HttpHeaders.LOCATION).split("&state=")[1]; |
| 73 | + |
| 74 | + String requestUri = "/login/oauth2/code/github?code=" + user.code + "&state=" + state; |
| 75 | + |
| 76 | + mockMvc.perform(MockMvcRequestBuilders.get(requestUri).session(session)) |
| 77 | + .andDo(print()) |
| 78 | + .andExpect(MockMvcResultMatchers.status().is3xxRedirection()) |
| 79 | + .andExpect(MockMvcResultMatchers.redirectedUrl("/")); |
| 80 | + |
| 81 | + Member savedMember = memberRepository.findByEmail(user.email).get(); |
| 82 | + assertThat(savedMember).isNotNull(); |
| 83 | + assertThat(savedMember.getEmail()).isEqualTo(user.email); |
| 84 | + assertThat(savedMember.getName()).isEqualTo(user.name); |
| 85 | + } |
| 86 | + |
| 87 | + private void setupUserStub(UserStub user) throws Exception { |
| 88 | + Map<String, String> accessTokenResponseBody = new HashMap<>(); |
| 89 | + accessTokenResponseBody.put("access_token", user.accessToken); |
| 90 | + accessTokenResponseBody.put("token_type", "bearer"); |
| 91 | + String accessTokenJsonResponse = new ObjectMapper().writeValueAsString(accessTokenResponseBody); |
| 92 | + |
| 93 | + stubFor(post(urlEqualTo("/login/oauth/access_token")) |
| 94 | + .willReturn(aResponse() |
| 95 | + .withHeader(HttpHeaders.CONTENT_TYPE, "application/json") |
| 96 | + .withBody(accessTokenJsonResponse))); |
| 97 | + |
| 98 | + Map<String, String> userProfile = new HashMap<>(); |
| 99 | + userProfile.put("email", user.email); |
| 100 | + userProfile.put("name", user.name); |
| 101 | + userProfile.put("avatar_url", user.avatarUrl); |
| 102 | + String profileJsonResponse = new ObjectMapper().writeValueAsString(userProfile); |
| 103 | + |
| 104 | + stubFor(get(urlEqualTo("/user")) |
| 105 | + .willReturn(aResponse() |
| 106 | + .withHeader(HttpHeaders.CONTENT_TYPE, "application/json") |
| 107 | + .withBody(profileJsonResponse))); |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + static Stream<UserStub> userProvider() { |
| 112 | + return Stream.of( |
| 113 | + new UserStub( "b", "b_access_token", "[email protected]", "b", "b_avatar_url"), |
| 114 | + new UserStub( "c", "c_access_token", "[email protected]", "c", "c_avatar_url") |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + static class UserStub { |
| 119 | + String code; |
| 120 | + String accessToken; |
| 121 | + String email; |
| 122 | + String name; |
| 123 | + String avatarUrl; |
| 124 | + |
| 125 | + public UserStub(String code, String accessToken, String email, String name, String avatarUrl) { |
| 126 | + this.code = code; |
| 127 | + this.accessToken = accessToken; |
| 128 | + this.email = email; |
| 129 | + this.name = name; |
| 130 | + this.avatarUrl = avatarUrl; |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments