Skip to content

Commit

Permalink
test: write change-voucher-user to giftcard-controller-test
Browse files Browse the repository at this point in the history
  • Loading branch information
jinlee1703 committed Oct 20, 2023
1 parent 7e1b285 commit 71fda92
Showing 1 changed file with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -31,6 +34,9 @@ class GiftcardControllerTest {
@MockBean
private GiftcardService giftcardService;

@MockBean
private JwtProvider jwtProvider;

@Test
@WithMockUser(username = "test", roles = "USER")
void read() throws Exception {
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 71fda92

Please sign in to comment.