3
3
import com .fasterxml .jackson .databind .ObjectMapper ;
4
4
import jakarta .transaction .Transactional ;
5
5
import org .benchmarker .bmcontroller .common .error .ErrorCode ;
6
- import org .benchmarker .bmcontroller .mail .controller .dto .EmailCertificationDto ;
7
- import org .benchmarker .bmcontroller .mail .controller .dto .EmailResDto ;
6
+ import org .benchmarker .bmcontroller .mail .controller .dto .*;
8
7
import org .benchmarker .bmcontroller .mail .service .impl .MailSenderImpl ;
9
8
import org .benchmarker .bmcontroller .user .service .UserContext ;
9
+ import org .junit .jupiter .api .AfterEach ;
10
10
import org .junit .jupiter .api .DisplayName ;
11
11
import org .junit .jupiter .api .Test ;
12
12
import org .springframework .beans .factory .annotation .Autowired ;
13
13
import org .springframework .boot .test .context .SpringBootTest ;
14
14
import org .springframework .boot .test .mock .mockito .MockBean ;
15
15
import org .springframework .http .MediaType ;
16
+ import org .springframework .mock .web .MockHttpSession ;
16
17
import org .springframework .test .web .servlet .MockMvc ;
17
18
import org .util .annotations .RestDocsTest ;
18
19
19
20
import java .nio .charset .StandardCharsets ;
21
+ import java .time .LocalDateTime ;
20
22
21
23
import static org .assertj .core .api .Assertions .assertThat ;
22
24
import static org .mockito .ArgumentMatchers .any ;
@@ -43,6 +45,14 @@ class MailControllerTest {
43
45
@ MockBean
44
46
UserContext userContext ;
45
47
48
+ @ MockBean
49
+ private MockHttpSession httpSession ;
50
+
51
+ @ AfterEach
52
+ public void clean (){
53
+ httpSession .clearAttributes ();
54
+ }
55
+
46
56
@ Test
47
57
@ DisplayName ("메일 Send 테스트" )
48
58
public void mailSender () throws Exception {
@@ -106,4 +116,106 @@ public void mailFormatErrorTest() throws Exception {
106
116
.andExpect (jsonPath ("$.code" ).value (ErrorCode .BAD_REQUEST .name ()));
107
117
}
108
118
119
+ @ Test
120
+ @ DisplayName ("메일 인증 코드 비교 테스트" )
121
+ public void mailCertification () throws Exception {
122
+
123
+ //given
124
+ EmailCodeDto req = EmailCodeDto .builder ()
125
+ .code ("123456" )
126
+ .build ();
127
+
128
+ UserSessionInfo userSessionInfo = UserSessionInfo .builder ()
129
+ .userMail ("test@naver.com" )
130
+ .certificationCode ("123456" )
131
+ .isVerification (false )
132
+ .verificationTime (LocalDateTime .now ())
133
+ .build ();
134
+
135
+ httpSession = new MockHttpSession ();
136
+ httpSession .setAttribute ("userSessionInfo" , userSessionInfo );
137
+
138
+ // when & then
139
+ mockMvc .perform (post ("/api/mail/certification/code" )
140
+ .session (httpSession )
141
+ .contentType (MediaType .APPLICATION_JSON )
142
+ .content (objectMapper .writeValueAsString (req )))
143
+ .andDo (print ())
144
+ .andDo (result -> {
145
+ assertThat (result .getResponse ().getStatus ()).isEqualTo (200 );
146
+
147
+ EmailCodeCertificationResultDto resEmailInfo = objectMapper .readValue (
148
+ result .getResponse ().getContentAsString (StandardCharsets .UTF_8 ),
149
+ EmailCodeCertificationResultDto .class );
150
+
151
+ assertThat (resEmailInfo .getStatus ()).isEqualTo ("success" );
152
+ assertThat (resEmailInfo .getMessage ()).isEqualTo ("Authentication successful!!" );
153
+ });
154
+ }
155
+
156
+ @ Test
157
+ @ DisplayName ("session 값이 Null 일 경우 에러 테스트" )
158
+ public void sessionInfoNullExceptionTest () throws Exception {
159
+
160
+ //given
161
+ EmailCodeDto req = EmailCodeDto .builder ()
162
+ .code ("123456" )
163
+ .build ();
164
+
165
+ when (httpSession .getAttribute ("userSessionInfo" )).thenReturn (null );
166
+
167
+ // when & then
168
+ mockMvc .perform (post ("/api/mail/certification/code" )
169
+ .contentType (MediaType .APPLICATION_JSON )
170
+ .content (objectMapper .writeValueAsString (req )))
171
+ .andDo (print ())
172
+ .andDo (result -> {
173
+ assertThat (result .getResponse ().getStatus ()).isEqualTo (200 );
174
+
175
+ EmailCodeCertificationResultDto resEmailInfo = objectMapper .readValue (
176
+ result .getResponse ().getContentAsString (StandardCharsets .UTF_8 ),
177
+ EmailCodeCertificationResultDto .class );
178
+
179
+ assertThat (resEmailInfo .getStatus ()).isEqualTo ("fail" );
180
+ assertThat (resEmailInfo .getMessage ()).isEqualTo ("Session expired or invalid. Please try again." );
181
+ });
182
+ }
183
+
184
+ @ Test
185
+ @ DisplayName ("인증 번호가 틀렸을 때 에러 테스트" )
186
+ public void certificationNumberExceptionTest () throws Exception {
187
+
188
+ //given
189
+ EmailCodeDto req = EmailCodeDto .builder ()
190
+ .code ("123456" )
191
+ .build ();
192
+
193
+ UserSessionInfo userSessionInfo = UserSessionInfo .builder ()
194
+ .userMail ("test@naver.com" )
195
+ .certificationCode ("123457" )
196
+ .isVerification (false )
197
+ .verificationTime (LocalDateTime .now ())
198
+ .build ();
199
+
200
+ httpSession = new MockHttpSession ();
201
+ httpSession .setAttribute ("userSessionInfo" , userSessionInfo );
202
+
203
+ // when & then
204
+ mockMvc .perform (post ("/api/mail/certification/code" )
205
+ .session (httpSession )
206
+ .contentType (MediaType .APPLICATION_JSON )
207
+ .content (objectMapper .writeValueAsString (req )))
208
+ .andDo (print ())
209
+ .andDo (result -> {
210
+ assertThat (result .getResponse ().getStatus ()).isEqualTo (200 );
211
+
212
+ EmailCodeCertificationResultDto resEmailInfo = objectMapper .readValue (
213
+ result .getResponse ().getContentAsString (StandardCharsets .UTF_8 ),
214
+ EmailCodeCertificationResultDto .class );
215
+
216
+ assertThat (resEmailInfo .getStatus ()).isEqualTo ("fail" );
217
+ assertThat (resEmailInfo .getMessage ()).isEqualTo ("Please enter the authentication number correctly" );
218
+ });
219
+ }
220
+
109
221
}
0 commit comments