|
53 | 53 | import org.springframework.transaction.support.TransactionTemplate; |
54 | 54 |
|
55 | 55 | import java.time.LocalDateTime; |
56 | | -import java.util.ArrayList; |
57 | | -import java.util.Collections; |
58 | | -import java.util.List; |
59 | | -import java.util.Map; |
| 56 | +import java.util.*; |
60 | 57 | import java.util.function.Consumer; |
61 | 58 | import java.util.stream.Collectors; |
62 | 59 |
|
63 | 60 | import static org.assertj.core.api.Assertions.assertThat; |
64 | | -import static org.mockito.ArgumentMatchers.any; |
65 | | -import static org.mockito.ArgumentMatchers.anyCollection; |
| 61 | +import static org.mockito.ArgumentMatchers.*; |
66 | 62 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; |
67 | 63 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; |
68 | 64 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
@@ -583,6 +579,7 @@ void testGetUserPublishInfo() throws Exception { |
583 | 579 | Mockito.when(repositories.findUserByLoginName("github", "test")).thenReturn(user); |
584 | 580 | Mockito.when(repositories.countActiveAccessTokens(user)).thenReturn(1L); |
585 | 581 | Mockito.when(repositories.findLatestVersions(user)).thenReturn(versions); |
| 582 | + Mockito.when(repositories.findActiveReviews(user)).thenReturn(Streamable.empty()); |
586 | 583 |
|
587 | 584 | mockMvc.perform(get("/admin/publisher/{provider}/{loginName}", "github", "test") |
588 | 585 | .with(user("admin_user").authorities(new SimpleGrantedAuthority(("ROLE_ADMIN")))) |
@@ -635,11 +632,14 @@ void testRevokePublisherAgreement() throws Exception { |
635 | 632 | Mockito.when(repositories.findVersionsByUser(user, true)) |
636 | 633 | .thenReturn(Streamable.of(versions)); |
637 | 634 |
|
| 635 | + Mockito.when(repositories.findActiveReviews(user)) |
| 636 | + .thenReturn(Streamable.empty()); |
| 637 | + |
638 | 638 | mockMvc.perform(post("/admin/publisher/{provider}/{loginName}/revoke", "github", "test") |
639 | 639 | .with(user("admin_user").authorities(new SimpleGrantedAuthority(("ROLE_ADMIN")))) |
640 | 640 | .with(csrf().asHeader())) |
641 | 641 | .andExpect(status().isOk()) |
642 | | - .andExpect(content().json(successJson("Deactivated 1 tokens and deactivated 1 extensions of user github/test."))); |
| 642 | + .andExpect(content().json(successJson("Deactivated 1 tokens, deactivated 1 extensions of user github/test."))); |
643 | 643 |
|
644 | 644 | assertThat(token.isActive()).isFalse(); |
645 | 645 | assertThat(versions.get(0).isActive()).isFalse(); |
@@ -1146,6 +1146,57 @@ void testChangeNamespaceAbortOnNewNamespaceExists() throws Exception { |
1146 | 1146 | .andExpect(content().json(errorJson("New namespace already exists: bar"))); |
1147 | 1147 | } |
1148 | 1148 |
|
| 1149 | + @Test |
| 1150 | + void testDeleteReview() throws Exception { |
| 1151 | + mockAdminUser(); |
| 1152 | + mockReviews(); |
| 1153 | + |
| 1154 | + mockMvc.perform(post("/admin/extension/{namespace}/{extension}/review/{provider}/{loginName}/delete", "foobar", "baz", "github", "user1") |
| 1155 | + .with(user("admin_user").authorities(new SimpleGrantedAuthority(("ROLE_ADMIN")))) |
| 1156 | + .with(csrf().asHeader())) |
| 1157 | + .andExpect(status().isOk()) |
| 1158 | + .andExpect(content().json(successJson("Deleted review from user1 for foobar.baz"))); |
| 1159 | + } |
| 1160 | + |
| 1161 | + @Test |
| 1162 | + void testDeleteReviewNotLoggedIn() throws Exception { |
| 1163 | + mockMvc.perform(post("/admin/extension/{namespace}/{extension}/review/{provider}/{loginName}/delete", "foo", "bar", "github", "user1") |
| 1164 | + .with(csrf())) |
| 1165 | + .andExpect(status().isForbidden()); |
| 1166 | + } |
| 1167 | + |
| 1168 | + @Test |
| 1169 | + void testDeleteReviewNormalUser() throws Exception { |
| 1170 | + mockNormalUser(); |
| 1171 | + |
| 1172 | + mockMvc.perform(post("/admin/extension/{namespace}/{extension}/review/{provider}/{loginName}/delete", "foo", "bar", "github", "user1") |
| 1173 | + .with(user("test_user")) |
| 1174 | + .with(csrf().asHeader())) |
| 1175 | + .andExpect(status().isForbidden()); |
| 1176 | + } |
| 1177 | + |
| 1178 | + @Test |
| 1179 | + void testDeleteReviewUnknownExtension() throws Exception { |
| 1180 | + mockAdminUser(); |
| 1181 | + mockMvc.perform(post("/admin/extension/{namespace}/{extension}/review/{provider}/{loginName}/delete", "foo", "bar", "github", "user1") |
| 1182 | + .with(user("admin_user").authorities(new SimpleGrantedAuthority(("ROLE_ADMIN")))) |
| 1183 | + .with(csrf().asHeader())) |
| 1184 | + .andExpect(status().isNotFound()) |
| 1185 | + .andExpect(content().json(errorJson("Extension not found: foo.bar"))); |
| 1186 | + } |
| 1187 | + |
| 1188 | + @Test |
| 1189 | + void testDeleteReviewNonExistingReview() throws Exception { |
| 1190 | + mockAdminUser(); |
| 1191 | + mockReviews(); |
| 1192 | + |
| 1193 | + mockMvc.perform(post("/admin/extension/{namespace}/{extension}/review/{provider}/{loginName}/delete", "foobar", "baz", "github", "user3") |
| 1194 | + .with(user("admin_user").authorities(new SimpleGrantedAuthority(("ROLE_ADMIN")))) |
| 1195 | + .with(csrf().asHeader())) |
| 1196 | + .andExpect(status().isNotFound()) |
| 1197 | + .andExpect(content().json(errorJson("No active review for extension foobar.baz and user user3 found"))); |
| 1198 | + } |
| 1199 | + |
1149 | 1200 | //---------- UTILITY ----------// |
1150 | 1201 |
|
1151 | 1202 | private PersonalAccessToken mockAdminToken() { |
@@ -1291,6 +1342,56 @@ private List<ExtensionVersion> mockExtension(int numberOfVersions, int numberOfB |
1291 | 1342 | return versions; |
1292 | 1343 | } |
1293 | 1344 |
|
| 1345 | + private List<ExtensionReview> mockReviews() { |
| 1346 | + var extVersions = mockExtension(1, 0, 0); |
| 1347 | + var extVersion = extVersions.get(0); |
| 1348 | + var extension = extVersion.getExtension(); |
| 1349 | + |
| 1350 | + var user1 = new UserData(); |
| 1351 | + user1.setLoginName("user1"); |
| 1352 | + var review1 = new ExtensionReview(); |
| 1353 | + review1.setId(1); |
| 1354 | + review1.setExtension(extension); |
| 1355 | + review1.setUser(user1); |
| 1356 | + review1.setRating(3); |
| 1357 | + review1.setComment("Somewhat ok"); |
| 1358 | + review1.setTimestamp(LocalDateTime.parse("2000-01-01T10:00")); |
| 1359 | + review1.setActive(true); |
| 1360 | + |
| 1361 | + var user2 = new UserData(); |
| 1362 | + user2.setLoginName("user2"); |
| 1363 | + var review2 = new ExtensionReview(); |
| 1364 | + review2.setId(2); |
| 1365 | + review2.setExtension(extension); |
| 1366 | + review2.setUser(user2); |
| 1367 | + review2.setRating(4); |
| 1368 | + review2.setComment("Quite good"); |
| 1369 | + review2.setTimestamp(LocalDateTime.parse("2000-01-01T10:00")); |
| 1370 | + review2.setActive(true); |
| 1371 | + |
| 1372 | + var user3 = new UserData(); |
| 1373 | + user3.setLoginName("user3"); |
| 1374 | + |
| 1375 | + Mockito.when(repositories.findUserByLoginName(anyString(), eq("user1"))) |
| 1376 | + .thenReturn(user1); |
| 1377 | + Mockito.when(repositories.findUserByLoginName(anyString(), eq("user2"))) |
| 1378 | + .thenReturn(user2); |
| 1379 | + Mockito.when(repositories.findUserByLoginName(anyString(), eq("user3"))) |
| 1380 | + .thenReturn(user3); |
| 1381 | + |
| 1382 | + Mockito.when(repositories.findActiveReviews(any(), any())) |
| 1383 | + .thenReturn(Streamable.empty()); |
| 1384 | + Mockito.when(repositories.findActiveReviews(extension, user1)) |
| 1385 | + .thenReturn(Streamable.of(review1)); |
| 1386 | + Mockito.when(repositories.findActiveReviews(extension, user2)) |
| 1387 | + .thenReturn(Streamable.of(review2)); |
| 1388 | + |
| 1389 | + Mockito.when(repositories.findActiveReviews(extension)) |
| 1390 | + .thenReturn(Streamable.of(review1, review2)); |
| 1391 | + |
| 1392 | + return List.of(review1, review2); |
| 1393 | + } |
| 1394 | + |
1294 | 1395 | private String createVersion(int major) { |
1295 | 1396 | return major + ".0.0"; |
1296 | 1397 | } |
|
0 commit comments