From 71fda92bb6efea1d871db1681ca68a29311e8b20 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Fri, 20 Oct 2023 12:12:52 +0900 Subject: [PATCH] test: write change-voucher-user to giftcard-controller-test --- .../contorller/GiftcardControllerTest.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/test/java/org/swmaestro/repl/gifthub/giftcard/contorller/GiftcardControllerTest.java b/src/test/java/org/swmaestro/repl/gifthub/giftcard/contorller/GiftcardControllerTest.java index 74adaa52..120b0501 100644 --- a/src/test/java/org/swmaestro/repl/gifthub/giftcard/contorller/GiftcardControllerTest.java +++ b/src/test/java/org/swmaestro/repl/gifthub/giftcard/contorller/GiftcardControllerTest.java @@ -15,7 +15,10 @@ import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.web.servlet.MockMvc; import org.swmaestro.repl.gifthub.giftcard.dto.GiftcardResponseDto; +import org.swmaestro.repl.gifthub.giftcard.entity.Giftcard; import org.swmaestro.repl.gifthub.giftcard.service.GiftcardService; +import org.swmaestro.repl.gifthub.util.JwtProvider; +import org.swmaestro.repl.gifthub.vouchers.entity.Voucher; import com.fasterxml.jackson.databind.ObjectMapper; @@ -31,6 +34,9 @@ class GiftcardControllerTest { @MockBean private GiftcardService giftcardService; + @MockBean + private JwtProvider jwtProvider; + @Test @WithMockUser(username = "test", roles = "USER") void read() throws Exception { @@ -64,4 +70,35 @@ void read() throws Exception { .andExpect(jsonPath("$.data.brand_name").value(giftcardResponseDto.getBrandName())) .andExpect(jsonPath("$.data.expires_at").value(giftcardResponseDto.getExpiresAt().toString())); } + + @Test + @WithMockUser(username = "test", roles = "USER") + void changeVoucherUser() throws Exception { + // given + String giftcardId = "id"; + String apiPath = "/giftcards/" + giftcardId + "/acquire"; + + Voucher voucher = Voucher.builder() + .id(1L) + .build(); + + Giftcard giftcard = Giftcard.builder() + .id(giftcardId) + .voucher(voucher) + .password("0000") + .message("메시지") + .expiresAt(LocalDate.now().atStartOfDay()) + .build(); + + // when + when(jwtProvider.resolveToken(any())).thenReturn("my_awesome_access_token"); + when(jwtProvider.getUsername(anyString())).thenReturn("test"); + when(giftcardService.changeVoucherUser(giftcardId, "test")).thenReturn(giftcard); + + // then + mockMvc.perform(post(apiPath) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.path").value(apiPath)); + } } \ No newline at end of file