From 8f65be8b7120fb0dca518447adf4d49df7113ca8 Mon Sep 17 00:00:00 2001 From: Paul Marechal Date: Wed, 6 Mar 2024 11:37:13 -0500 Subject: [PATCH] finally fix test suite --- .../org/eclipse/openvsx/RegistryAPITest.java | 54 +++++++------- .../java/org/eclipse/openvsx/UserAPITest.java | 7 +- .../openvsx/adapter/VSCodeAPITest.java | 7 +- .../eclipse/openvsx/admin/AdminAPITest.java | 71 ++++++++++++++----- .../openvsx/cache/CacheServiceTest.java | 9 --- 5 files changed, 94 insertions(+), 54 deletions(-) diff --git a/server/src/test/java/org/eclipse/openvsx/RegistryAPITest.java b/server/src/test/java/org/eclipse/openvsx/RegistryAPITest.java index 33d2553e7..5f79fd876 100644 --- a/server/src/test/java/org/eclipse/openvsx/RegistryAPITest.java +++ b/server/src/test/java/org/eclipse/openvsx/RegistryAPITest.java @@ -27,6 +27,7 @@ import org.eclipse.openvsx.search.ExtensionSearch; import org.eclipse.openvsx.search.ISearchService; import org.eclipse.openvsx.search.SearchUtilService; +import org.eclipse.openvsx.security.AuthUserFactory; import org.eclipse.openvsx.security.OAuth2UserServices; import org.eclipse.openvsx.security.SecurityConfig; import org.eclipse.openvsx.security.TokenService; @@ -118,7 +119,7 @@ public void testPublicNamespace() throws Exception { var namespace = mockNamespace(); Mockito.when(repositories.countMemberships(namespace, NamespaceMembership.ROLE_OWNER)) .thenReturn(0L); - + mockMvc.perform(get("/api/{namespace}", "foobar")) .andExpect(status().isOk()) .andExpect(content().json(namespaceJson(n -> { @@ -1207,7 +1208,7 @@ public void testCreateNamespace() throws Exception { .andExpect(redirectedUrl("http://localhost/api/foobar")) .andExpect(content().json(successJson("Created namespace foobar"))); } - + @Test public void testCreateNamespaceNoName() throws Exception { mockAccessToken(); @@ -1217,7 +1218,7 @@ public void testCreateNamespaceNoName() throws Exception { .andExpect(status().isOk()) .andExpect(content().json(errorJson("Missing required property 'name'."))); } - + @Test public void testCreateNamespaceInvalidName() throws Exception { mockAccessToken(); @@ -1227,7 +1228,7 @@ public void testCreateNamespaceInvalidName() throws Exception { .andExpect(status().isBadRequest()) .andExpect(content().json(errorJson("Invalid namespace name: foo.bar"))); } - + @Test public void testCreateNamespaceInactiveToken() throws Exception { var token = mockAccessToken(); @@ -1238,7 +1239,7 @@ public void testCreateNamespaceInactiveToken() throws Exception { .andExpect(status().isBadRequest()) .andExpect(content().json(errorJson("Invalid access token."))); } - + @Test public void testCreateExistingNamespace() throws Exception { mockAccessToken(); @@ -1246,7 +1247,7 @@ public void testCreateExistingNamespace() throws Exception { namespace.setName("foobar"); Mockito.when(repositories.findNamespace("foobar")) .thenReturn(namespace); - + mockMvc.perform(post("/api/-/namespace/create?token={token}", "my_token") .contentType(MediaType.APPLICATION_JSON) .content(namespaceJson(n -> { n.name = "foobar"; }))) @@ -1296,7 +1297,7 @@ public void testVerifyTokenNoPermission() throws Exception { mockMvc.perform(get("/api/{namespace}/verify-pat?token={token}", "foobar", "my_token")) .andExpect(status().isBadRequest()); } - + @Test public void testPublishOrphan() throws Exception { mockForPublish("orphan"); @@ -1307,7 +1308,7 @@ public void testPublishOrphan() throws Exception { .andExpect(status().isBadRequest()) .andExpect(content().json(errorJson("Insufficient access rights for publisher: foo"))); } - + @Test public void testPublishRequireLicenseNone() throws Exception { var previousRequireLicense = extensions.requireLicense; @@ -1324,7 +1325,7 @@ public void testPublishRequireLicenseNone() throws Exception { extensions.requireLicense = previousRequireLicense; } } - + @Test public void testPublishRequireLicenseOk() throws Exception { var previousRequireLicense = extensions.requireLicense; @@ -1350,7 +1351,7 @@ public void testPublishRequireLicenseOk() throws Exception { extensions.requireLicense = previousRequireLicense; } } - + @Test public void testPublishInactiveToken() throws Exception { mockForPublish("invalid"); @@ -1361,7 +1362,7 @@ public void testPublishInactiveToken() throws Exception { .andExpect(status().isBadRequest()) .andExpect(content().json(errorJson("Invalid access token."))); } - + @Test public void testPublishUnknownNamespace() throws Exception { mockAccessToken(); @@ -1373,7 +1374,7 @@ public void testPublishUnknownNamespace() throws Exception { .andExpect(content().json(errorJson("Unknown publisher: foo" + "\nUse the 'create-namespace' command to create a namespace corresponding to your publisher name."))); } - + @Test public void testPublishVerifiedOwner() throws Exception { mockForPublish("owner"); @@ -1535,7 +1536,7 @@ public void testPublishInvalidVersion() throws Exception { .andExpect(status().isBadRequest()) .andExpect(content().json(errorJson("The version string 'latest' is reserved."))); } - + @Test public void testPostReview() throws Exception { var user = mockUserData(); @@ -1558,7 +1559,7 @@ public void testPostReview() throws Exception { .andExpect(status().isCreated()) .andExpect(content().json(successJson("Added review for foo.bar"))); } - + @Test public void testPostReviewNotLoggedIn() throws Exception { mockMvc.perform(post("/api/{namespace}/{extension}/review", "foo", "bar") @@ -1568,7 +1569,7 @@ public void testPostReviewNotLoggedIn() throws Exception { })).with(csrf().asHeader())) .andExpect(status().isForbidden()); } - + @Test public void testPostReviewInvalidRating() throws Exception { mockMvc.perform(post("/api/{namespace}/{extension}/review", "foo", "bar") @@ -1581,7 +1582,7 @@ public void testPostReviewInvalidRating() throws Exception { .andExpect(status().isBadRequest()) .andExpect(content().json(errorJson("The rating must be an integer number between 0 and 5."))); } - + @Test public void testPostReviewUnknownExtension() throws Exception { mockUserData(); @@ -1595,7 +1596,7 @@ public void testPostReviewUnknownExtension() throws Exception { .andExpect(status().isBadRequest()) .andExpect(content().json(errorJson("Extension not found: foo.bar"))); } - + @Test public void testPostExistingReview() throws Exception { var user = mockUserData(); @@ -1620,7 +1621,7 @@ public void testPostExistingReview() throws Exception { .andExpect(status().isBadRequest()) .andExpect(content().json(errorJson("You must not submit more than one review for an extension."))); } - + @Test public void testDeleteReview() throws Exception { var user = mockUserData(); @@ -1643,13 +1644,13 @@ public void testDeleteReview() throws Exception { .andExpect(status().isOk()) .andExpect(content().json(successJson("Deleted review for foo.bar"))); } - + @Test public void testDeleteReviewNotLoggedIn() throws Exception { mockMvc.perform(post("/api/{namespace}/{extension}/review/delete", "foo", "bar").with(csrf())) .andExpect(status().isForbidden()); } - + @Test public void testDeleteReviewUnknownExtension() throws Exception { mockUserData(); @@ -1659,7 +1660,7 @@ public void testDeleteReviewUnknownExtension() throws Exception { .andExpect(status().isBadRequest()) .andExpect(content().json(errorJson("Extension not found: foo.bar"))); } - + @Test public void testDeleteNonExistingReview() throws Exception { var user = mockUserData(); @@ -2088,7 +2089,7 @@ private PersonalAccessToken mockAccessToken() { .thenReturn(token); return token; } - + private void mockForPublish(String mode) { var token = mockAccessToken(); if (mode.equals("invalid")) { @@ -2262,7 +2263,7 @@ private byte[] createExtensionPackage(String name, String version, String licens archive.finish(); return bytes.toByteArray(); } - + @TestConfiguration @Import(SecurityConfig.class) static class TestConfig { @@ -2318,5 +2319,10 @@ LatestExtensionVersionCacheKeyGenerator latestExtensionVersionCacheKeyGenerator( PublishExtensionVersionHandler publishExtensionVersionHandler() { return new PublishExtensionVersionHandler(); } + + @Bean + AuthUserFactory authUserFactory() { + return new AuthUserFactory(); + } } -} \ No newline at end of file +} diff --git a/server/src/test/java/org/eclipse/openvsx/UserAPITest.java b/server/src/test/java/org/eclipse/openvsx/UserAPITest.java index cd8d85ceb..42034dd45 100644 --- a/server/src/test/java/org/eclipse/openvsx/UserAPITest.java +++ b/server/src/test/java/org/eclipse/openvsx/UserAPITest.java @@ -37,6 +37,7 @@ import org.eclipse.openvsx.json.ResultJson; import org.eclipse.openvsx.json.UserJson; import org.eclipse.openvsx.repositories.RepositoryService; +import org.eclipse.openvsx.security.AuthUserFactory; import org.eclipse.openvsx.security.OAuth2UserServices; import org.eclipse.openvsx.security.SecurityConfig; import org.eclipse.openvsx.security.TokenService; @@ -575,6 +576,10 @@ VersionService versionService() { LatestExtensionVersionCacheKeyGenerator latestExtensionVersionCacheKeyGenerator() { return new LatestExtensionVersionCacheKeyGenerator(); } + + @Bean + AuthUserFactory authUserFactory() { + return new AuthUserFactory(); + } } - } \ No newline at end of file diff --git a/server/src/test/java/org/eclipse/openvsx/adapter/VSCodeAPITest.java b/server/src/test/java/org/eclipse/openvsx/adapter/VSCodeAPITest.java index a3b2f5d17..f91b589a8 100644 --- a/server/src/test/java/org/eclipse/openvsx/adapter/VSCodeAPITest.java +++ b/server/src/test/java/org/eclipse/openvsx/adapter/VSCodeAPITest.java @@ -42,6 +42,7 @@ import org.eclipse.openvsx.search.ExtensionSearch; import org.eclipse.openvsx.search.ISearchService; import org.eclipse.openvsx.search.SearchUtilService; +import org.eclipse.openvsx.security.AuthUserFactory; import org.eclipse.openvsx.security.OAuth2UserServices; import org.eclipse.openvsx.security.SecurityConfig; import org.eclipse.openvsx.security.TokenService; @@ -955,6 +956,10 @@ VersionService getVersionService() { LatestExtensionVersionCacheKeyGenerator latestExtensionVersionCacheKeyGenerator() { return new LatestExtensionVersionCacheKeyGenerator(); } - } + @Bean + AuthUserFactory authUserFactory() { + return new AuthUserFactory(); + } + } } diff --git a/server/src/test/java/org/eclipse/openvsx/admin/AdminAPITest.java b/server/src/test/java/org/eclipse/openvsx/admin/AdminAPITest.java index d0a0c8a40..7fe00bfa1 100644 --- a/server/src/test/java/org/eclipse/openvsx/admin/AdminAPITest.java +++ b/server/src/test/java/org/eclipse/openvsx/admin/AdminAPITest.java @@ -9,20 +9,57 @@ ********************************************************************************/ package org.eclipse.openvsx.admin; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import io.micrometer.core.instrument.simple.SimpleMeterRegistry; -import org.eclipse.openvsx.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyCollection; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; +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.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.function.Consumer; +import java.util.stream.Collectors; + +import org.eclipse.openvsx.ExtensionService; +import org.eclipse.openvsx.ExtensionValidator; +import org.eclipse.openvsx.LocalRegistryService; +import org.eclipse.openvsx.MockTransactionTemplate; +import org.eclipse.openvsx.UpstreamRegistryService; +import org.eclipse.openvsx.UserService; import org.eclipse.openvsx.adapter.VSCodeIdService; import org.eclipse.openvsx.cache.CacheService; import org.eclipse.openvsx.cache.LatestExtensionVersionCacheKeyGenerator; import org.eclipse.openvsx.eclipse.EclipseService; -import org.eclipse.openvsx.entities.*; -import org.eclipse.openvsx.json.*; +import org.eclipse.openvsx.entities.AdminStatistics; +import org.eclipse.openvsx.entities.Extension; +import org.eclipse.openvsx.entities.ExtensionVersion; +import org.eclipse.openvsx.entities.Namespace; +import org.eclipse.openvsx.entities.NamespaceMembership; +import org.eclipse.openvsx.entities.PersonalAccessToken; +import org.eclipse.openvsx.entities.UserData; +import org.eclipse.openvsx.json.AdminStatisticsJson; +import org.eclipse.openvsx.json.ChangeNamespaceJson; +import org.eclipse.openvsx.json.ExtensionJson; +import org.eclipse.openvsx.json.NamespaceJson; +import org.eclipse.openvsx.json.NamespaceMembershipJson; +import org.eclipse.openvsx.json.NamespaceMembershipListJson; +import org.eclipse.openvsx.json.ResultJson; +import org.eclipse.openvsx.json.UserJson; +import org.eclipse.openvsx.json.UserPublishInfoJson; import org.eclipse.openvsx.publish.ExtensionVersionIntegrityService; import org.eclipse.openvsx.publish.PublishExtensionVersionHandler; import org.eclipse.openvsx.repositories.RepositoryService; import org.eclipse.openvsx.search.SearchUtilService; +import org.eclipse.openvsx.security.AuthUserFactory; import org.eclipse.openvsx.security.OAuth2UserServices; import org.eclipse.openvsx.security.SecurityConfig; import org.eclipse.openvsx.security.TokenService; @@ -51,20 +88,11 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.transaction.support.TransactionTemplate; -import jakarta.persistence.EntityManager; -import java.time.LocalDateTime; -import java.util.*; -import java.util.function.Consumer; -import java.util.stream.Collectors; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyCollection; -import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; -import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; -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.*; +import io.micrometer.core.instrument.simple.SimpleMeterRegistry; +import jakarta.persistence.EntityManager; @WebMvcTest(AdminAPI.class) @AutoConfigureWebClient @@ -1243,5 +1271,10 @@ VersionService versionService() { LatestExtensionVersionCacheKeyGenerator latestExtensionVersionCacheKeyGenerator() { return new LatestExtensionVersionCacheKeyGenerator(); } + + @Bean + AuthUserFactory authUserFactory() { + return new AuthUserFactory(); + } } } \ No newline at end of file diff --git a/server/src/test/java/org/eclipse/openvsx/cache/CacheServiceTest.java b/server/src/test/java/org/eclipse/openvsx/cache/CacheServiceTest.java index 4866df3f3..b51a8b9ed 100644 --- a/server/src/test/java/org/eclipse/openvsx/cache/CacheServiceTest.java +++ b/server/src/test/java/org/eclipse/openvsx/cache/CacheServiceTest.java @@ -433,13 +433,4 @@ private ExtensionVersion insertExtensionVersion(String version) { return extVersion; } - - @TestConfiguration - static class CacheServiceTestConfiguration { - - @Bean - AuthUserFactory authUserFactory() { - return new AuthUserFactory(); - } - } }