Skip to content

Commit

Permalink
finally fix test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-marechal committed Mar 6, 2024
1 parent 7480927 commit 8f65be8
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 54 deletions.
54 changes: 30 additions & 24 deletions server/src/test/java/org/eclipse/openvsx/RegistryAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 -> {
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -1238,15 +1239,15 @@ public void testCreateNamespaceInactiveToken() throws Exception {
.andExpect(status().isBadRequest())
.andExpect(content().json(errorJson("Invalid access token.")));
}

@Test
public void testCreateExistingNamespace() throws Exception {
mockAccessToken();
var namespace = new Namespace();
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"; })))
Expand Down Expand Up @@ -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");
Expand All @@ -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;
Expand All @@ -1324,7 +1325,7 @@ public void testPublishRequireLicenseNone() throws Exception {
extensions.requireLicense = previousRequireLicense;
}
}

@Test
public void testPublishRequireLicenseOk() throws Exception {
var previousRequireLicense = extensions.requireLicense;
Expand All @@ -1350,7 +1351,7 @@ public void testPublishRequireLicenseOk() throws Exception {
extensions.requireLicense = previousRequireLicense;
}
}

@Test
public void testPublishInactiveToken() throws Exception {
mockForPublish("invalid");
Expand All @@ -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();
Expand All @@ -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");
Expand Down Expand Up @@ -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();
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -2088,7 +2089,7 @@ private PersonalAccessToken mockAccessToken() {
.thenReturn(token);
return token;
}

private void mockForPublish(String mode) {
var token = mockAccessToken();
if (mode.equals("invalid")) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -2318,5 +2319,10 @@ LatestExtensionVersionCacheKeyGenerator latestExtensionVersionCacheKeyGenerator(
PublishExtensionVersionHandler publishExtensionVersionHandler() {
return new PublishExtensionVersionHandler();
}

@Bean
AuthUserFactory authUserFactory() {
return new AuthUserFactory();
}
}
}
}
7 changes: 6 additions & 1 deletion server/src/test/java/org/eclipse/openvsx/UserAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -575,6 +576,10 @@ VersionService versionService() {
LatestExtensionVersionCacheKeyGenerator latestExtensionVersionCacheKeyGenerator() {
return new LatestExtensionVersionCacheKeyGenerator();
}

@Bean
AuthUserFactory authUserFactory() {
return new AuthUserFactory();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -955,6 +956,10 @@ VersionService getVersionService() {
LatestExtensionVersionCacheKeyGenerator latestExtensionVersionCacheKeyGenerator() {
return new LatestExtensionVersionCacheKeyGenerator();
}
}

@Bean
AuthUserFactory authUserFactory() {
return new AuthUserFactory();
}
}
}
71 changes: 52 additions & 19 deletions server/src/test/java/org/eclipse/openvsx/admin/AdminAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1243,5 +1271,10 @@ VersionService versionService() {
LatestExtensionVersionCacheKeyGenerator latestExtensionVersionCacheKeyGenerator() {
return new LatestExtensionVersionCacheKeyGenerator();
}

@Bean
AuthUserFactory authUserFactory() {
return new AuthUserFactory();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -433,13 +433,4 @@ private ExtensionVersion insertExtensionVersion(String version) {

return extVersion;
}

@TestConfiguration
static class CacheServiceTestConfiguration {

@Bean
AuthUserFactory authUserFactory() {
return new AuthUserFactory();
}
}
}

0 comments on commit 8f65be8

Please sign in to comment.