-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
3 changed files
with
88 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,26 @@ | ||
package org.swmaestro.repl.gifthub.auth.controller; | ||
|
||
import static org.mockito.ArgumentMatchers.*; | ||
import static org.mockito.Mockito.*; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
import java.security.PrivateKey; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.swmaestro.repl.gifthub.auth.dto.GoogleDto; | ||
import org.swmaestro.repl.gifthub.auth.dto.KakaoDto; | ||
import org.swmaestro.repl.gifthub.auth.dto.SignInDto; | ||
import org.swmaestro.repl.gifthub.auth.dto.SignUpDto; | ||
import org.swmaestro.repl.gifthub.auth.dto.TokenDto; | ||
import org.swmaestro.repl.gifthub.auth.dto.*; | ||
import org.swmaestro.repl.gifthub.auth.entity.Member; | ||
import org.swmaestro.repl.gifthub.auth.service.AppleService; | ||
import org.swmaestro.repl.gifthub.auth.service.AuthService; | ||
import org.swmaestro.repl.gifthub.auth.service.GoogleService; | ||
import org.swmaestro.repl.gifthub.auth.service.KakaoService; | ||
import org.swmaestro.repl.gifthub.auth.service.MemberService; | ||
import org.swmaestro.repl.gifthub.auth.service.NaverService; | ||
import org.swmaestro.repl.gifthub.auth.service.RefreshTokenService; | ||
import org.swmaestro.repl.gifthub.auth.service.*; | ||
import org.swmaestro.repl.gifthub.util.JwtProvider; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.security.PrivateKey; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
|
@@ -68,66 +59,64 @@ public class AuthControllerTest { | |
@Test | ||
public void signUpTest() throws Exception { | ||
SignUpDto signUpDto = SignUpDto.builder() | ||
.username("jinlee1703") | ||
.password("abc123##") | ||
.nickname("이진우") | ||
.build(); | ||
.username("jinlee1703") | ||
.password("abc123##") | ||
.nickname("이진우") | ||
.build(); | ||
|
||
TokenDto tokenDto = TokenDto.builder() | ||
.accessToken("myawesomejwt") | ||
.refreshToken("myawesomejwt") | ||
.build(); | ||
.accessToken("myawesomejwt") | ||
.refreshToken("myawesomejwt") | ||
.build(); | ||
|
||
// when | ||
when(memberService.create(signUpDto)).thenReturn(tokenDto); | ||
|
||
// then | ||
mockMvc.perform(post("/auth/sign-up") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(signUpDto))) | ||
.andExpect(status().isOk()); | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(signUpDto))) | ||
.andExpect(status().isOk()); | ||
} | ||
|
||
@Test | ||
public void signInTest() throws Exception { | ||
SignInDto loginDto = SignInDto.builder() | ||
.username("jinlee1703") | ||
.password("abc123##") | ||
.build(); | ||
.username("jinlee1703") | ||
.password("abc123##") | ||
.build(); | ||
|
||
TokenDto tokenDto = TokenDto.builder() | ||
.accessToken("myawesomejwt") | ||
.refreshToken("myawesomejwt") | ||
.build(); | ||
.accessToken("myawesomejwt") | ||
.refreshToken("myawesomejwt") | ||
.build(); | ||
|
||
when(authService.signIn(any(SignInDto.class))).thenReturn(tokenDto); | ||
|
||
mockMvc.perform(post("/auth/sign-up") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(loginDto))) | ||
.andExpect(status().isOk()); | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(objectMapper.writeValueAsString(loginDto))) | ||
.andExpect(status().isOk()); | ||
} | ||
|
||
@Test | ||
public void validateRefreshTokenTest() throws Exception { | ||
public void reissueAccessTokenTest() throws Exception { | ||
//given | ||
String refreshToken = "유효하지 않은 Refresh Token"; | ||
String refreshToken = "유효한 Refresh Token"; | ||
String newAccessToken = "sampleNewAccessToken"; | ||
String newRefreshToken = "sampleNewRefreshToken"; | ||
String username = "jinlee1703"; | ||
|
||
TokenDto tokenDto = TokenDto.builder() | ||
.accessToken(newAccessToken) | ||
.refreshToken(newRefreshToken) | ||
.build(); | ||
.accessToken(newAccessToken) | ||
.refreshToken(refreshToken) | ||
.build(); | ||
|
||
when(refreshTokenService.createNewAccessTokenByValidateRefreshToken(refreshToken)).thenReturn(null); | ||
when(refreshTokenService.createNewRefreshTokenByValidateRefreshToken(refreshToken)).thenReturn(null); | ||
when(refreshTokenService.createNewAccessTokenByValidateRefreshToken(refreshToken)).thenReturn(newAccessToken); | ||
when(jwtProvider.getUsername(refreshToken)).thenReturn(username); | ||
|
||
mockMvc.perform(post("/auth/refresh") | ||
.header("Authorization", refreshToken)) | ||
.andExpect(status().isUnauthorized()); | ||
.header("Authorization", refreshToken)) | ||
.andExpect(status().isUnauthorized()); | ||
} | ||
|
||
@Test | ||
|
@@ -137,29 +126,29 @@ public void kakaoSignInCallbackTest() throws Exception { | |
String state = "myawesome_state"; | ||
|
||
TokenDto kakaoTokenDto = TokenDto.builder() | ||
.accessToken("myawesomeKakaojwt") | ||
.refreshToken("myawesomeKakaojwt") | ||
.build(); | ||
.accessToken("myawesomeKakaojwt") | ||
.refreshToken("myawesomeKakaojwt") | ||
.build(); | ||
|
||
TokenDto tokenDto = TokenDto.builder() | ||
.accessToken("myawesomejwt") | ||
.refreshToken("myawesomejwt") | ||
.build(); | ||
.accessToken("myawesomejwt") | ||
.refreshToken("myawesomejwt") | ||
.build(); | ||
|
||
KakaoDto kakaoDto = KakaoDto.builder() | ||
.nickname("정인희") | ||
.username("[email protected]") | ||
.build(); | ||
.nickname("정인희") | ||
.username("[email protected]") | ||
.build(); | ||
|
||
when(kakaoService.getToken(code)).thenReturn(tokenDto); | ||
when(kakaoService.getUserInfo(tokenDto)).thenReturn(kakaoDto); | ||
when(kakaoService.signIn(kakaoDto)).thenReturn(tokenDto); | ||
|
||
mockMvc.perform(get("/auth/sign-in/naver/callback") | ||
.queryParam("code", code) | ||
.queryParam("state", state) | ||
.header("Authorization", "Bearer " + accesstoken)) | ||
.andExpect(status().isOk()); | ||
.queryParam("code", code) | ||
.queryParam("state", state) | ||
.header("Authorization", "Bearer " + accesstoken)) | ||
.andExpect(status().isOk()); | ||
} | ||
|
||
@Test | ||
|
@@ -169,29 +158,29 @@ public void googleSignInCallbackTest() throws Exception { | |
String state = "myawesome_state"; | ||
|
||
TokenDto googleTokenDto = TokenDto.builder() | ||
.accessToken("myawesomeKakaojwt") | ||
.refreshToken("myawesomeKakaojwt") | ||
.build(); | ||
.accessToken("myawesomeKakaojwt") | ||
.refreshToken("myawesomeKakaojwt") | ||
.build(); | ||
|
||
TokenDto tokenDto = TokenDto.builder() | ||
.accessToken("myawesomejwt") | ||
.refreshToken("myawesomejwt") | ||
.build(); | ||
.accessToken("myawesomejwt") | ||
.refreshToken("myawesomejwt") | ||
.build(); | ||
|
||
GoogleDto googleDto = GoogleDto.builder() | ||
.nickname("정인희") | ||
.username("[email protected]") | ||
.build(); | ||
.nickname("정인희") | ||
.username("[email protected]") | ||
.build(); | ||
|
||
when(googleService.getToken(code)).thenReturn(tokenDto); | ||
when(googleService.getUserInfo(tokenDto)).thenReturn(googleDto); | ||
when(googleService.signIn(googleDto)).thenReturn(tokenDto); | ||
|
||
mockMvc.perform(get("/auth/sign-in/naver/callback") | ||
.queryParam("code", code) | ||
.queryParam("state", state) | ||
.header("Authorization", "Bearer " + accesstoken)) | ||
.andExpect(status().isOk()); | ||
.queryParam("code", code) | ||
.queryParam("state", state) | ||
.header("Authorization", "Bearer " + accesstoken)) | ||
.andExpect(status().isOk()); | ||
} | ||
|
||
@Test | ||
|
@@ -200,22 +189,22 @@ public void naverSignInCallbackTest() throws Exception { | |
String code = "myawesome_code"; | ||
String state = "myawesome_state"; | ||
TokenDto token = TokenDto.builder() | ||
.accessToken(accesstoken) | ||
.refreshToken(accesstoken) | ||
.build(); | ||
.accessToken(accesstoken) | ||
.refreshToken(accesstoken) | ||
.build(); | ||
Member member = Member.builder() | ||
.username("[email protected]") | ||
.nickname("이진우") | ||
.build(); | ||
.username("[email protected]") | ||
.nickname("이진우") | ||
.build(); | ||
|
||
when(naverService.getNaverToken("token", code)).thenReturn(token); | ||
when(naverService.getNaverUserByToken(token)).thenReturn(member); | ||
|
||
mockMvc.perform(get("/auth/sign-in/naver/callback") | ||
.queryParam("code", code) | ||
.queryParam("state", state) | ||
.header("Authorization", "Bearer " + accesstoken)) | ||
.andExpect(status().isOk()); | ||
.queryParam("code", code) | ||
.queryParam("state", state) | ||
.header("Authorization", "Bearer " + accesstoken)) | ||
.andExpect(status().isOk()); | ||
} | ||
|
||
@Test | ||
|
@@ -228,9 +217,9 @@ public void appleSignInCallbackTest() throws Exception { | |
PrivateKey privateKey = mock(PrivateKey.class); | ||
|
||
TokenDto token = TokenDto.builder() | ||
.accessToken(accesstoken) | ||
.refreshToken(refreshToken) | ||
.build(); | ||
.accessToken(accesstoken) | ||
.refreshToken(refreshToken) | ||
.build(); | ||
|
||
when(appleService.readKeyPath()).thenReturn(keyPath); | ||
when(appleService.craetePrivateKey(keyPath)).thenReturn(privateKey); | ||
|
@@ -239,8 +228,8 @@ public void appleSignInCallbackTest() throws Exception { | |
when(appleService.getToken(idToken)).thenReturn(token); | ||
|
||
mockMvc.perform(post("/auth/sign-in/apple/callback") | ||
.requestAttr("code", "my_awesome_code") | ||
.header("Authorization", "Bearer " + accesstoken)) | ||
.andExpect(status().isBadRequest()); | ||
.requestAttr("code", "my_awesome_code") | ||
.header("Authorization", "Bearer " + accesstoken)) | ||
.andExpect(status().isBadRequest()); | ||
} | ||
} |