Repository navigation
Expand file tree
/
Copy pathAuthControllerTest.java
More file actions
173 lines (145 loc) ยท 7.41 KB
/
Copy pathAuthControllerTest.java
File metadata and controls
173 lines (145 loc) ยท 7.41 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package org.runnect.server.auth.controller;
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.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.BDDMockito;
import org.runnect.server.auth.dto.response.GetNewTokenResponseDto;
import org.runnect.server.auth.dto.response.SignInResponseDto;
import org.runnect.server.auth.dto.response.SignUpResponseDto;
import org.runnect.server.auth.service.AuthService;
import org.runnect.server.common.constant.ErrorStatus;
import org.runnect.server.config.jwt.JwtService;
import org.runnect.server.config.slack.SlackApi;
import org.runnect.server.user.exception.authException.InvalidRefreshTokenException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
@WebMvcTest(AuthController.class)
class AuthControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private AuthService authService;
@MockBean
private JwtService jwtService;
@MockBean
private SlackApi slackApi;
@Nested
@DisplayName("POST /api/auth")
class SignIn {
@Test
@DisplayName("๊ธฐ์กด ์ ์ ๋ฉด 200๊ณผ LOGIN ์๋ต์ ๋ฐํํ๋ค")
void ๊ธฐ์กด_์ ์ _๋ก๊ทธ์ธ() throws Exception {
when(authService.signIn(BDDMockito.any())).thenReturn(
SignInResponseDto.of("KAKAO", "user@runnect.io", "access-token", "refresh-token"));
mockMvc.perform(post("/api/auth")
.contentType("application/json")
.content("{\"token\":\"kakao-token\",\"provider\":\"kakao\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.email").value("user@runnect.io"));
}
@Test
@DisplayName("์ ๊ท ์ ์ ๋ฉด 200๊ณผ SIGNUP ์๋ต์ ๋ฐํํ๋ค")
void ์ ๊ท_์ ์ _ํ์๊ฐ์
() throws Exception {
when(authService.signIn(BDDMockito.any())).thenReturn(
SignUpResponseDto.of("KAKAO", "new@runnect.io", "๋ฌ๋1", "access-token", "refresh-token"));
mockMvc.perform(post("/api/auth")
.contentType("application/json")
.content("{\"token\":\"kakao-token\",\"provider\":\"kakao\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.nickname").value("๋ฌ๋1"));
}
@Test
@DisplayName("token์ด ์์ผ๋ฉด 400์ ๋ฐํํ๋ค")
void ํ ํฐ_์์() throws Exception {
mockMvc.perform(post("/api/auth")
.contentType("application/json")
.content("{\"provider\":\"kakao\"}"))
.andExpect(status().isBadRequest());
BDDMockito.verifyNoInteractions(authService);
}
@Test
@DisplayName("provider๊ฐ ์์ผ๋ฉด 400์ ๋ฐํํ๋ค")
void ํ๋ก๋ฐ์ด๋_์์() throws Exception {
mockMvc.perform(post("/api/auth")
.contentType("application/json")
.content("{\"token\":\"kakao-token\"}"))
.andExpect(status().isBadRequest());
BDDMockito.verifyNoInteractions(authService);
}
}
@Nested
@DisplayName("GET /api/auth/getNewToken")
class GetNewToken {
@Test
@DisplayName("์ ์ ์์ฒญ์ด๋ฉด 200๊ณผ ์ฌ๋ฐ๊ธ๋ ํ ํฐ์ ๋ฐํํ๋ค")
void ์ ์_์ฌ๋ฐ๊ธ() throws Exception {
when(authService.getNewToken("old-access", "valid-refresh"))
.thenReturn(GetNewTokenResponseDto.of("new-access", "new-refresh"));
mockMvc.perform(get("/api/auth/getNewToken")
.header("accessToken", "old-access")
.header("refreshToken", "valid-refresh"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.accessToken").value("new-access"));
}
@Test
@DisplayName("refreshToken ํค๋๊ฐ ์์ผ๋ฉด 400์ ๋ฐํํ๋ค")
void ๋ฆฌํ๋ ์ํ ํฐ_ํค๋_์์() throws Exception {
mockMvc.perform(get("/api/auth/getNewToken").header("accessToken", "old-access"))
.andExpect(status().isBadRequest());
BDDMockito.verifyNoInteractions(authService);
}
@Test
@DisplayName("refreshToken์ด ๋น ๋ฌธ์์ด์ด๋ฉด 400์ ๋ฐํํ๋ค")
void ๋ฆฌํ๋ ์ํ ํฐ_๋น๋ฌธ์์ด() throws Exception {
mockMvc.perform(get("/api/auth/getNewToken")
.header("accessToken", "old-access")
.header("refreshToken", ""))
.andExpect(status().isBadRequest());
BDDMockito.verifyNoInteractions(authService);
}
@Test
@DisplayName("์ ์ฅ๋ ๋ฆฌํ๋ ์ ํ ํฐ๊ณผ ์ผ์นํ์ง ์์ผ๋ฉด 401์ ๋ฐํํ๋ค")
void ๋ฆฌํ๋ ์ํ ํฐ_๋ถ์ผ์น() throws Exception {
when(authService.getNewToken("old-access", "wrong-refresh"))
.thenThrow(new InvalidRefreshTokenException(
ErrorStatus.INVALID_REFRESH_TOKEN_EXCEPTION,
ErrorStatus.INVALID_REFRESH_TOKEN_EXCEPTION.getMessage()));
mockMvc.perform(get("/api/auth/getNewToken")
.header("accessToken", "old-access")
.header("refreshToken", "wrong-refresh"))
.andExpect(status().isUnauthorized());
}
}
// prod Grafana ๋ก๊ทธ ํ์ธ ๊ฒฐ๊ณผ, ์ต๊ทผ 7์ผ๊ฐ 5xx 40๊ฑด์ด ์ ๋ถ ์ ์ ํธ๋ํฝ์ด ์๋๋ผ
// Postman ๋ฑ์ผ๋ก ์๋ชป๋ ๋ฉ์๋/ํ์์ ์ง์ ์ฐ๋ฅธ ์์ฒญ(GET /api/auth, ์๋ชป๋
// Content-Type์ POST /api/auth)์ด์๋ค. ์๋๋ 405/415๋ก ์๋ตํด์ผ ํ ์์ฒญ์ธ๋ฐ
// ControllerExceptionAdvice์ ๋ฒ์ฉ Exception ํธ๋ค๋ฌ๊ฐ ์ด๊ฑธ 500์ผ๋ก ๋ญ๊ฐ๊ณ
// Slack/Sentry ์๋ฆผ๊น์ง ๋งค๋ฒ ๋ณด๋ด๊ณ ์์๋ค โ ์ง์ง ์ฅ์ ๊ฐ ์๋๋ฐ ๋
ธ์ด์ฆ๋ง ์์ด๋ ๊ตฌ์กฐ.
@Nested
@DisplayName("์๋ชป๋ ํํ์ ์์ฒญ (ํด๋ผ์ด์ธํธ ์ค๋ฅ, ์๋ฒ ์ฅ์ ์๋)")
class InvalidRequestShape {
@Test
@DisplayName("POST๋ง ์ง์ํ๋ /api/auth์ GET์ผ๋ก ์์ฒญํ๋ฉด 405๋ฅผ ๋ฐํํ๊ณ Slack ์๋ฆผ์ ๋ณด๋ด์ง ์๋๋ค")
void ์ง์ํ์ง_์๋_๋ฉ์๋() throws Exception {
mockMvc.perform(get("/api/auth"))
.andExpect(status().isMethodNotAllowed());
BDDMockito.verifyNoInteractions(slackApi);
}
@Test
@DisplayName("JSON์ ๊ธฐ๋ํ๋ /api/auth์ multipart๋ก ์์ฒญํ๋ฉด 415๋ฅผ ๋ฐํํ๊ณ Slack ์๋ฆผ์ ๋ณด๋ด์ง ์๋๋ค")
void ์ง์ํ์ง_์๋_์ปจํ
์ธ ํ์
() throws Exception {
mockMvc.perform(org.springframework.test.web.servlet.request.MockMvcRequestBuilders
.multipart("/api/auth")
.file("token", "kakao-token".getBytes()))
.andExpect(status().isUnsupportedMediaType());
BDDMockito.verifyNoInteractions(slackApi);
}
}
}