Skip to content

Commit

Permalink
Merge pull request #103 from kakao-tech-campus-2nd-step3/test/101/auth
Browse files Browse the repository at this point in the history
test: ์ธ์ฆ์ธ๊ฐ€ ํ…Œ์ŠคํŠธ์ฝ”๋“œ ์ž‘์„ฑ
  • Loading branch information
westzeroright authored Nov 13, 2024
2 parents 14d9260 + a57b2d1 commit 3ea80ff
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public class Member {

protected Member() {}

public Member(String email) {
this.email = email;
}

public Member(
Long id, String email, int point, String phoneNumber, String deliveryName, String platform) {
this.id = id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public ResponseEntity<GetCreatorOrderInfoRes> getCreatorOrderInfo(
// ๊ฐ€๊ฒฉ ์ˆ˜์ •
@PutMapping("/price")
public ResponseEntity<Void> updateOrderPrice(
@LoginMember Member member, @RequestBody @Valid UpdateOrderPriceReq dto) {
@LoginMember Member member, @RequestBody @Valid UpdateOrderPriceReq dto) {
orderDetailService.updateOrderPrice(member, dto);
return ResponseEntity.ok().build();
}
Expand Down
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) {}
19 changes: 2 additions & 17 deletions src/main/java/com/ordertogether/team14_be/spot/enums/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,18 @@ public enum Category implements CodedEnum<String> {

private final String code;
private final String stringCategory;

// ํ•œ๊ธ€ ์„ค๋ช…(String)์„ ENUM์œผ๋กœ ๋ณ€ํ™˜
public static Optional<Category> fromStringToEnum(String category) {
return Arrays.stream(Category.values())
.filter(c -> c.getStringCategory().equals(category))
.findFirst();
}

// ENUM์„ ์ฝ”๋“œ(String)์œผ๋กœ ๋ณ€ํ™˜
public static Optional<String> fromEnumToString(Category category) {
return Optional.of(category.getCode())
.orElseThrow(() -> new IllegalArgumentException("์กด์žฌํ•˜์ง€ ์•Š๋Š” ์นดํ…Œ๊ณ ๋ฆฌ์ž…๋‹ˆ๋‹ค."))
.describeConstable(); // ์ƒ์ˆ˜ ํ’€์— ์ €์žฅ๋˜๋Š” ๊ฐ’์„ ์•ˆ์ „ํ•˜๊ฒŒ ์ฐธ์กฐ
}

// ํ•œ๊ธ€ ์„ค๋ช…(String)์„ ENUM์œผ๋กœ ๋ณ€ํ™˜
public static Optional<Category> fromStringToEnum(String category) {
return Arrays.stream(Category.values())
.filter(c -> c.getCategory().equals(category))
.filter(c -> c.getStringCategory().equals(category))
.findFirst();
}

// ENUM์„ ์ฝ”๋“œ(String)์œผ๋กœ ๋ณ€ํ™˜
public static Optional<String> fromEnumToString(Category category) {
return Optional.of(category.getCode())
.orElseThrow(() -> new IllegalArgumentException("์กด์žฌํ•˜์ง€ ์•Š๋Š” ์นดํ…Œ๊ณ ๋ฆฌ์ž…๋‹ˆ๋‹ค."))
.describeConstable(); // ์ƒ์ˆ˜ ํ’€์— ์ €์žฅ๋˜๋Š” ๊ฐ’์„ ์•ˆ์ „ํ•˜๊ฒŒ ์ฐธ์กฐ
.describeConstable();
}

@jakarta.persistence.Converter(autoApply = true)
Expand Down
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("์ด๋ฏธ ํšŒ์›์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.");
}
}
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]");
}
}
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());
}
}
Loading

0 comments on commit 3ea80ff

Please sign in to comment.