forked from FindFirst-Development/FindFirst-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserControllerTest.java
More file actions
301 lines (249 loc) · 11.3 KB
/
UserControllerTest.java
File metadata and controls
301 lines (249 loc) · 11.3 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package dev.findfirst.users.controller;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.*;
import java.util.Optional;
import java.util.Properties;
import dev.findfirst.core.annotations.IntegrationTest;
import dev.findfirst.security.userauth.models.TokenRefreshResponse;
import dev.findfirst.security.userauth.models.payload.request.SignupRequest;
import dev.findfirst.users.model.MailHogMessage;
import dev.findfirst.users.model.user.TokenPassword;
import dev.findfirst.users.model.user.User;
import dev.findfirst.users.service.UserManagementService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.TestPropertySource;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;
@Testcontainers
@IntegrationTest
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource(locations = "classpath:application-test.yml")
class UserControllerTest {
TestRestTemplate restTemplate = new TestRestTemplate();
@Mock
private UserManagementService userManagementService;
@InjectMocks
private UserController userController;
public UserControllerTest() {
MockitoAnnotations.openMocks(this);
}
@Autowired
UserControllerTest(TestRestTemplate tRestTemplate) {
this.restTemplate = tRestTemplate;
}
@Container
@ServiceConnection
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16.2-alpine3.19");
@Container
public static GenericContainer<?> mailhog =
new GenericContainer<>(DockerImageName.parse("mailhog/mailhog:latest")).withExposedPorts(1025,
8025);
@TestConfiguration
public static class JavaMailSenderConfiguration {
@Bean
public JavaMailSender javaMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(mailhog.getHost());
mailSender.setPort(mailhog.getFirstMappedPort());
mailSender.setProtocol("smtp");
Properties properties = new Properties();
properties.setProperty("mail.smtp.auth", "false");
properties.setProperty("mail.smtp.starttls.enable", "false");
mailSender.setJavaMailProperties(properties);
return mailSender;
}
}
private String userUrl = "/user";
/**
* Tests that a user should be able to sign up. After signing up another user should not be able
* use the same username or email.
*/
@Test
void userSignup() {
var headers = new HttpHeaders();
var ent = new HttpEntity<>(
new SignupRequest("Steve-Man", "steve@test.com", "$tev3s_sup3rH@rdPassword"), headers);
var response = restTemplate.exchange(userUrl + "/signup", HttpMethod.POST, ent, String.class);
assertEquals(HttpStatus.OK, response.getStatusCode());
/** This should fail as the user should already exist. */
response = restTemplate.exchange(userUrl + "/signup", HttpMethod.POST, ent, String.class);
assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
}
/**
* Create a user, gets the registration token from the email. Uses the token to complete
* registration.
*/
@Test
@Order(2)
void completeSignupAndRegistration() {
var headers = new HttpHeaders();
var ent = new HttpEntity<>(
new SignupRequest("beardedMan", "j-dog@gmail.com", "$tev3s_sup3rH@rdPassword"), headers);
var response = restTemplate.exchange(userUrl + "/signup", HttpMethod.POST, ent, String.class);
assertEquals(HttpStatus.OK, response.getStatusCode());
try {
var token = getTokenFromEmail(0, 1);
var regResponse = restTemplate.getForEntity(userUrl + "/regitrationConfirm?token={token}",
String.class, token);
assertEquals(HttpStatus.SEE_OTHER, regResponse.getStatusCode());
} catch (Exception e) {
// fail the test should show message
assertTrue(false, e.getMessage());
}
}
@Test
@Order(1)
void resetPassword() {
String token = "";
var response = restTemplate.exchange(userUrl + "/resetPassword?email={email}", HttpMethod.POST,
new HttpEntity<>(new HttpHeaders()), String.class, "jsmith@google.com");
assertEquals(HttpStatus.OK, response.getStatusCode());
try {
token = getTokenFromEmail(0, 2);
} catch (Exception e) {
// fail the test should show message
assertTrue(false, e.getMessage());
}
response = restTemplate.exchange(userUrl + "/changePassword?token={tkn}", HttpMethod.GET,
new HttpEntity<>(new HttpHeaders()), String.class, token);
assertEquals(HttpStatus.SEE_OTHER, response.getStatusCode());
var loc = Optional.ofNullable(response.getHeaders().get("Location")).orElseThrow().get(0);
var urlStruct = loc.split("/");
// token is the last part of the string
var tknParam = urlStruct[urlStruct.length - 1];
assertNotNull(tknParam);
restTemplate.exchange(userUrl + "/changePassword?tokenPassword={tkn}", HttpMethod.POST,
new HttpEntity<>(new TokenPassword(tknParam, "jsmithsNewsPassword!"), new HttpHeaders()),
String.class, token);
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth("jsmith", "jsmithsNewsPassword!");
HttpEntity<String> entity = new HttpEntity<>(headers);
var signResp = restTemplate.postForEntity("/user/signin", entity, TokenRefreshResponse.class);
assertEquals(HttpStatus.OK, signResp.getStatusCode());
}
String getTokenFromEmail(int emailIdx, int lineWithToken) throws Exception {
String host = mailhog.getHost();
int port = mailhog.getMappedPort(8025);
String url = "http://" + host + ":" + port + "/api/v2/messages";
var messageRaw = restTemplate.getForEntity(url, String.class).getBody();
ObjectMapper mapper = new ObjectMapper();
MailHogMessage mailHogMessage = mapper.readValue(messageRaw, MailHogMessage.class);
var message = mailHogMessage.items()[emailIdx];
var body = message.Content().Body();
var secondLine = body.split("\n")[lineWithToken];
var token = secondLine.split("=")[1];
return token.strip();
}
@Test
void refreshToken() {
HttpHeaders headers = new HttpHeaders();
// test user
headers.setBasicAuth("jsmith", "test");
HttpEntity<String> entity = new HttpEntity<>(headers);
var signResp = restTemplate.postForEntity("/user/signin", entity, TokenRefreshResponse.class);
var tknRefresh = Optional.ofNullable(signResp.getBody()).orElseThrow();
var refreshTkn = tknRefresh.refreshToken();
var resp = restTemplate.exchange(userUrl + "/refreshToken?token={refreshToken}",
HttpMethod.POST, new HttpEntity<>(new HttpHeaders()), String.class, refreshTkn);
assertEquals(HttpStatus.OK, resp.getStatusCode());
}
@Test
void testUploadProfilePicture_Success() throws Exception {
MockMultipartFile file = new MockMultipartFile("file", "test.jpg", "image/jpeg", "dummy content".getBytes());
int userId = 1;
User user = new User();
user.setUserId(userId);
user.setUsername("testUser");
when(userManagementService.getUserById(userId)).thenReturn(Optional.of(user));
ResponseEntity<?> response = userController.uploadProfilePicture(file, userId);
assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals("File uploaded successfully.", response.getBody());
verify(userManagementService, times(1)).changeUserPhoto(eq(user), anyString());
}
@Test
void testRemoveUserPhoto_Success() {
User user = new User();
user.setUserId(1);
user.setUsername("testUser");
user.setUserPhoto("uploads/profile-pictures/test.jpg");
when(userManagementService.getUserById(user.getUserId())).thenReturn(Optional.of(user));
userManagementService.removeUserPhoto(user);
ArgumentCaptor<User> userCaptor = ArgumentCaptor.forClass(User.class);
verify(userManagementService, times(1)).saveUser(userCaptor.capture());
assertNull(userCaptor.getValue().getUserPhoto());
}
@Test
void testUploadProfilePicture_FileSizeExceedsLimit() throws Exception {
byte[] largeContent = new byte[3 * 1024 * 1024]; // 3 MB
MockMultipartFile file = new MockMultipartFile("file", "large.jpg", "image/jpeg", largeContent);
int userId = 1;
User user = new User();
user.setUserId(userId);
user.setUsername("testUser");
when(userManagementService.getUserById(userId)).thenReturn(Optional.of(user));
ResponseEntity<?> response = userController.uploadProfilePicture(file, userId);
assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
assertEquals("File size exceeds the maximum limit of 2 MB.", response.getBody());
}
@Test
void testUploadProfilePicture_InvalidFileType() throws Exception {
MockMultipartFile file = new MockMultipartFile("file", "test.txt", "text/plain", "dummy content".getBytes());
int userId = 1;
User user = new User();
user.setUserId(userId);
user.setUsername("testUser");
when(userManagementService.getUserById(userId)).thenReturn(Optional.of(user));
ResponseEntity<?> response = userController.uploadProfilePicture(file, userId);
assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
assertEquals("Invalid file type. Only JPG and PNG are allowed.", response.getBody());
}
@Test
void testGetUserProfilePicture_NotFound() {
int userId = 1;
User user = new User();
user.setUserId(userId);
user.setUsername("testUser");
user.setUserPhoto(null);
when(userManagementService.getUserById(userId)).thenReturn(Optional.of(user));
ResponseEntity<?> response = userController.getUserProfilePicture(userId);
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
}
@Test
void testGetUserProfilePicture_Success() {
int userId = 1;
User user = new User();
user.setUserId(userId);
user.setUsername("testUser");
user.setUserPhoto("uploads/profile-pictures/test.jpg");
when(userManagementService.getUserById(userId)).thenReturn(Optional.of(user));
ResponseEntity<?> response = userController.getUserProfilePicture(userId);
assertEquals(HttpStatus.OK, response.getStatusCode());
assertNotNull(response.getBody());
}
}