-
Notifications
You must be signed in to change notification settings - Fork 0
/
VerifyMailControllerTest.java
97 lines (84 loc) · 3.77 KB
/
VerifyMailControllerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package org.cardanofoundation.authentication.controller;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import com.google.gson.Gson;
import jakarta.servlet.http.HttpServletRequest;
import org.cardanofoundation.authentication.constant.CommonConstant;
import org.cardanofoundation.authentication.model.request.auth.ResetPasswordRequest;
import org.cardanofoundation.authentication.model.response.MessageResponse;
import org.cardanofoundation.authentication.provider.JwtProvider;
import org.cardanofoundation.authentication.provider.RedisProvider;
import org.cardanofoundation.authentication.service.UserService;
import org.cardanofoundation.authentication.service.VerifyService;
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.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.jpa.mapping.JpaMetamodelMappingContext;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
@WebMvcTest(VerifyMailController.class)
@AutoConfigureMockMvc(addFilters = false)
@MockBean(JpaMetamodelMappingContext.class)
class VerifyMailControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private VerifyService verifyService;
@MockBean
private UserService userService;
@MockBean
private JwtProvider jwtProvider;
@MockBean
private RedisProvider redisProvider;
private final String CODE = "CodeVerifyMail123456";
@Test
void whenCallActive() throws Exception {
MessageResponse res = MessageResponse.builder().code(CommonConstant.CODE_SUCCESS)
.message(CommonConstant.RESPONSE_SUCCESS).build();
given(verifyService.checkVerifySignUpByEmail(CODE)).willReturn(res);
mockMvc.perform(get("/api/v1/verify/active")
.param("code", CODE)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(print());
}
@Test
void whenCallForgotPassword() throws Exception {
MessageResponse res = MessageResponse.builder().code(CommonConstant.CODE_SUCCESS)
.message(CommonConstant.RESPONSE_SUCCESS).build();
HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
given(verifyService.forgotPassword("Test@gmail.com", httpServletRequest)).willReturn(res);
mockMvc.perform(get("/api/v1/verify/forgot-password")
.param("email", "Test@gmail.com")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(print());
}
@Test
void whenCallResetPassword() throws Exception {
ResetPasswordRequest request = new ResetPasswordRequest();
request.setPassword("@nhPhuc96");
request.setCode(CODE);
MessageResponse res = MessageResponse.builder().code(CommonConstant.CODE_SUCCESS)
.message(CommonConstant.RESPONSE_SUCCESS).build();
given(verifyService.resetPassword(request)).willReturn(res);
mockMvc.perform(put("/api/v1/verify/reset-password")
.content(asJsonString(request))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(print());
}
private String asJsonString(final Object obj) {
try {
return new Gson().toJson(obj);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}