-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from kakao-tech-campus-2nd-step3/test/101/auth
test: ์ธ์ฆ์ธ๊ฐ ํ ์คํธ์ฝ๋ ์์ฑ
- Loading branch information
Showing
8 changed files
with
305 additions
and
111 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
4 changes: 1 addition & 3 deletions
4
src/main/java/com/ordertogether/team14_be/order/details/dto/update/UpdateOrderPriceReq.java
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,5 +1,3 @@ | ||
package com.ordertogether.team14_be.order.details.dto.update; | ||
|
||
public record UpdateOrderPriceReq(Long orderId, int price) { | ||
|
||
} | ||
public record UpdateOrderPriceReq(Long orderId, int price) {} |
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
42 changes: 42 additions & 0 deletions
42
src/test/java/com/ordertogether/team14_be/auth/application/service/AuthServiceTest.java
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.ordertogether.team14_be.auth.application.service; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import com.ordertogether.team14_be.auth.persistence.exception.AlreadyExistMember; | ||
import com.ordertogether.team14_be.member.persistence.MemberRepository; | ||
import com.ordertogether.team14_be.member.persistence.entity.Member; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
@SpringBootTest | ||
@ActiveProfiles("test") | ||
@ExtendWith(SpringExtension.class) | ||
class AuthServiceTest { | ||
|
||
private final AuthService authService; | ||
private final MemberRepository memberRepository; | ||
|
||
@Autowired | ||
public AuthServiceTest(AuthService authService, MemberRepository memberRepository) { | ||
this.authService = authService; | ||
this.memberRepository = memberRepository; | ||
} | ||
|
||
@Test | ||
void validMemberTest() { | ||
// given | ||
String email = "[email protected]"; | ||
String platform = "KAKAO"; | ||
Member member = new Member(email, 0, "010-7731-3160", "์์์ฐ", platform); | ||
memberRepository.saveAndFlush(member); | ||
|
||
// when | ||
assertThatThrownBy(() -> authService.validMember(email, platform)) | ||
.isInstanceOf(AlreadyExistMember.class) | ||
.hasMessageContaining("์ด๋ฏธ ํ์์ด ์กด์ฌํฉ๋๋ค."); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/test/java/com/ordertogether/team14_be/auth/application/service/KakaoAuthServiceTest.java
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.ordertogether.team14_be.auth.application.service; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.ordertogether.team14_be.auth.presentation.KakaoClient; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
@ActiveProfiles("test") | ||
@ExtendWith(SpringExtension.class) | ||
@SpringBootTest | ||
class KakaoAuthServiceTest { | ||
@Autowired private KakaoAuthService kakaoAuthService; | ||
|
||
@Autowired private KakaoClient kakaoClient; | ||
|
||
private static final String authorizationCode = | ||
"iMBWo8P8X48A6i4xSdxZyGh_FbmNY2KrdXxa3-6Pkk3lfJ15z4wOqgAAAAQKKwymAAABkyZ7AD7UNEQ5evY1pg"; | ||
|
||
@Test | ||
public void ์ธ๊ฐ์ฝ๋๋ก_์ฌ์ฉ์์ด๋ฉ์ผ_์ป์ด์ค๊ธฐ() throws Exception { | ||
|
||
String userKakaoEmail = kakaoAuthService.getKakaoUserEmail(authorizationCode); | ||
|
||
assertThat(userKakaoEmail).isEqualTo("[email protected]"); | ||
} | ||
} |
134 changes: 134 additions & 0 deletions
134
src/test/java/com/ordertogether/team14_be/auth/presentation/AuthControllerTest.java
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 |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package com.ordertogether.team14_be.auth.presentation; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
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.header; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import com.ordertogether.team14_be.auth.application.service.AuthService; | ||
import com.ordertogether.team14_be.auth.application.service.KakaoAuthService; | ||
import com.ordertogether.team14_be.member.application.dto.MemberInfoRequest; | ||
import com.ordertogether.team14_be.member.application.service.MemberService; | ||
import com.ordertogether.team14_be.member.persistence.entity.Member; | ||
import java.util.Optional; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.ResponseCookie; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
|
||
class AuthControllerTest { | ||
private MockMvc mockMvc; | ||
|
||
@Mock private AuthService authService; | ||
|
||
@Mock private KakaoAuthService kakaoAuthService; | ||
|
||
@Mock private MemberService memberService; | ||
|
||
@InjectMocks private AuthController authController; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
mockMvc = MockMvcBuilders.standaloneSetup(authController).build(); | ||
} | ||
|
||
@Test | ||
void ๋ก๊ทธ์ธ์ผ๋ก_์๋น์คํ ํฐ๋ฐ๊ธ() throws Exception { | ||
String authorizationCode = "testAuthorizationCode"; | ||
String kakaoEmail = "[email protected]"; | ||
String serviceToken = "mockedServiceToken"; | ||
Member member = new Member(kakaoEmail); | ||
|
||
when(kakaoAuthService.getKakaoUserEmail(authorizationCode)).thenReturn(kakaoEmail); | ||
when(memberService.findMemberByEmail(kakaoEmail)).thenReturn(Optional.of(member)); | ||
when(authService.getServiceToken(kakaoEmail)).thenReturn(serviceToken); | ||
|
||
mockMvc | ||
.perform(get("/api/v1/auth/login").header("Authorization", "Bearer " + authorizationCode)) | ||
.andExpect(status().isOk()) | ||
.andExpect(header().exists(HttpHeaders.SET_COOKIE)) | ||
.andExpect(jsonPath("$.message").value("๋ก๊ทธ์ธ ์ฑ๊ณต")) | ||
.andExpect(jsonPath("$.data").value(serviceToken)); | ||
} | ||
|
||
@Value("${FRONT_PAGE_SIGNUP}") | ||
private String redirectPage; | ||
|
||
@Test | ||
void ์ ๊ทํ์์ผ์_๋ฆฌ๋ค์ด๋ ํธ() throws Exception { | ||
String authorizationCode = "testAuthorizationCode"; | ||
String kakaoEmail = "[email protected]"; | ||
String redirectUrl = redirectPage + kakaoEmail; | ||
|
||
when(kakaoAuthService.getKakaoUserEmail(authorizationCode)).thenReturn(kakaoEmail); | ||
when(memberService.findMemberByEmail(kakaoEmail)).thenReturn(Optional.empty()); | ||
|
||
mockMvc | ||
.perform(get("/api/v1/auth/login").header("Authorization", "Bearer " + authorizationCode)) | ||
.andExpect(status().isFound()) | ||
.andExpect(header().string(HttpHeaders.LOCATION, redirectUrl)); | ||
; | ||
|
||
verify(kakaoAuthService).getKakaoUserEmail(authorizationCode); | ||
verify(memberService).findMemberByEmail(kakaoEmail); | ||
} | ||
|
||
@Test | ||
void ํ์๊ฐ์ () throws Exception { | ||
String email = "[email protected]"; | ||
MemberInfoRequest memberInfoRequest = new MemberInfoRequest("testName", "1234567890"); | ||
String serviceToken = "newServiceToken"; | ||
|
||
when(kakaoAuthService.register( | ||
email, memberInfoRequest.deliveryName(), memberInfoRequest.phoneNumber())) | ||
.thenReturn(serviceToken); | ||
|
||
mockMvc | ||
.perform( | ||
post("/api/v1/auth/signup") | ||
.param("email", email) | ||
.contentType("application/json") | ||
.content("{\"deliveryName\":\"testName\", \"phoneNumber\":\"1234567890\"}")) | ||
.andExpect(status().isOk()) | ||
.andExpect(header().exists(HttpHeaders.SET_COOKIE)) | ||
.andExpect(jsonPath("$.message").value("ํ์๊ฐ์ ์ฑ๊ณต")) | ||
.andExpect(jsonPath("$.data").value(serviceToken)); | ||
|
||
verify(kakaoAuthService) | ||
.register(email, memberInfoRequest.deliveryName(), memberInfoRequest.phoneNumber()); | ||
} | ||
|
||
@Test | ||
void ๋ก๊ทธ์์() throws Exception { | ||
ResponseCookie deleteCookie = | ||
ResponseCookie.from("serviceToken", "") | ||
.maxAge(0) | ||
.httpOnly(true) | ||
.secure(true) | ||
.path("/") | ||
.sameSite("Strict") | ||
.build(); | ||
|
||
mockMvc | ||
.perform(post("/api/v1/auth/logout")) | ||
.andExpect(status().isOk()) | ||
.andExpect(header().exists(HttpHeaders.SET_COOKIE)) | ||
.andExpect(jsonPath("$.message").value("๋ก๊ทธ์์ ์ฑ๊ณต")) | ||
.andExpect(jsonPath("$.data").value("")); | ||
|
||
verify(authService, times(0)).getServiceToken(anyString()); | ||
} | ||
} |
Oops, something went wrong.