From 32cf1f004410eedb3a4bb50960e77a4e586aa6f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Pfeuffer?= Date: Tue, 11 Jan 2022 17:37:26 +0100 Subject: [PATCH] Fix unit tests for #1916 Fixup for commit e9f22e89eca11f87166033c7545187ca5a2def8c --- .../v2/resources/IndexDtoGeneratorTest.java | 35 +------------------ .../api/v2/resources/IndexResourceTest.java | 6 ++-- 2 files changed, 4 insertions(+), 37 deletions(-) diff --git a/scm-webapp/src/test/java/sonia/scm/api/v2/resources/IndexDtoGeneratorTest.java b/scm-webapp/src/test/java/sonia/scm/api/v2/resources/IndexDtoGeneratorTest.java index 35c4167f2f..926d429d2d 100644 --- a/scm-webapp/src/test/java/sonia/scm/api/v2/resources/IndexDtoGeneratorTest.java +++ b/scm-webapp/src/test/java/sonia/scm/api/v2/resources/IndexDtoGeneratorTest.java @@ -61,10 +61,7 @@ import static sonia.scm.SCMContext.USER_ANONYMOUS; @ExtendWith(MockitoExtension.class) class IndexDtoGeneratorTest { - private static final ScmPathInfo scmPathInfo = () -> URI.create("/api/v2"); - - @Mock - private ResourceLinks resourceLinks; + private final ResourceLinks resourceLinks = ResourceLinksMock.createMock(URI.create("/api/v2")); @Mock private BasicContextProvider contextProvider; @Mock @@ -99,7 +96,6 @@ class IndexDtoGeneratorTest { @Test void shouldAppendMeIfAuthenticated() { - mockSubjectRelatedResourceLinks(); when(subject.isAuthenticated()).thenReturn(true); when(contextProvider.getVersion()).thenReturn("2.x"); @@ -111,7 +107,6 @@ class IndexDtoGeneratorTest { @Test void shouldNotAppendMeIfUserIsAuthenticatedButAnonymous() { - mockResourceLinks(); when(subject.getPrincipal()).thenReturn(USER_ANONYMOUS); when(subject.isAuthenticated()).thenReturn(true); @@ -122,7 +117,6 @@ class IndexDtoGeneratorTest { @Test void shouldAppendMeIfUserIsAnonymousAndAnonymousModeIsFullEnabled() { - mockSubjectRelatedResourceLinks(); when(subject.getPrincipal()).thenReturn(USER_ANONYMOUS); when(subject.isAuthenticated()).thenReturn(true); when(configuration.getAnonymousMode()).thenReturn(AnonymousMode.FULL); @@ -134,7 +128,6 @@ class IndexDtoGeneratorTest { @Test void shouldNotAppendMeIfUserIsAnonymousAndAnonymousModeIsProtocolOnly() { - mockResourceLinks(); when(subject.getPrincipal()).thenReturn(USER_ANONYMOUS); when(subject.isAuthenticated()).thenReturn(true); when(configuration.getAnonymousMode()).thenReturn(AnonymousMode.PROTOCOL_ONLY); @@ -152,8 +145,6 @@ class IndexDtoGeneratorTest { searchableType("group") ); when(searchEngine.getSearchableTypes()).thenReturn(types); - mockSubjectRelatedResourceLinks(); - when(resourceLinks.search()).thenReturn(new ResourceLinks.SearchLinks(scmPathInfo)); when(subject.isAuthenticated()).thenReturn(true); @@ -182,7 +173,6 @@ class IndexDtoGeneratorTest { @Test void shouldCreateInitializationLink() { - mockBaseLink(); when(initializationFinisher.isFullyInitialized()).thenReturn(false); when(initializationFinisher.missingInitialization()).thenReturn(initializationStep); when(initializationStep.name()).thenReturn("probability"); @@ -207,27 +197,4 @@ class IndexDtoGeneratorTest { ); } } - - private void mockResourceLinks() { - mockBaseLink(); - when(resourceLinks.authentication()).thenReturn(new ResourceLinks.AuthenticationLinks(scmPathInfo)); - } - - private void mockBaseLink() { - when(resourceLinks.index()).thenReturn(new ResourceLinks.IndexLinks(scmPathInfo)); - when(resourceLinks.uiPluginCollection()).thenReturn(new ResourceLinks.UIPluginCollectionLinks(scmPathInfo)); - } - - private void mockSubjectRelatedResourceLinks() { - mockResourceLinks(); - when(resourceLinks.repositoryCollection()).thenReturn(new ResourceLinks.RepositoryCollectionLinks(scmPathInfo)); - when(resourceLinks.repositoryVerbs()).thenReturn(new ResourceLinks.RepositoryVerbLinks(scmPathInfo)); - when(resourceLinks.repositoryTypeCollection()).thenReturn(new ResourceLinks.RepositoryTypeCollectionLinks(scmPathInfo)); - when(resourceLinks.repositoryRoleCollection()).thenReturn(new ResourceLinks.RepositoryRoleCollectionLinks(scmPathInfo)); - when(resourceLinks.namespaceStrategies()).thenReturn(new ResourceLinks.NamespaceStrategiesLinks(scmPathInfo)); - when(resourceLinks.namespaceCollection()).thenReturn(new ResourceLinks.NamespaceCollectionLinks(scmPathInfo)); - when(resourceLinks.me()).thenReturn(new ResourceLinks.MeLinks(scmPathInfo, new ResourceLinks.UserLinks(scmPathInfo))); - when(resourceLinks.repository()).thenReturn(new ResourceLinks.RepositoryLinks(scmPathInfo)); - when(resourceLinks.search()).thenReturn(new ResourceLinks.SearchLinks(scmPathInfo)); - } } diff --git a/scm-webapp/src/test/java/sonia/scm/api/v2/resources/IndexResourceTest.java b/scm-webapp/src/test/java/sonia/scm/api/v2/resources/IndexResourceTest.java index cec1955b16..0ef6046564 100644 --- a/scm-webapp/src/test/java/sonia/scm/api/v2/resources/IndexResourceTest.java +++ b/scm-webapp/src/test/java/sonia/scm/api/v2/resources/IndexResourceTest.java @@ -179,17 +179,17 @@ public class IndexResourceTest { Assertions.assertThat(index.getLinks().getLinksBy("autocomplete")) .extracting("name") - .containsExactlyInAnyOrder("users", "groups"); + .containsExactlyInAnyOrder("users", "groups", "namespaces"); } @Test @SubjectAware(username = "user_without_autocomplete_permission", password = "secret") - public void userWithoutAutocompletePermissionShouldNotSeeAutoCompleteLinks() { + public void userWithoutAutocompletePermissionShouldSeeAutoCompleteLinksOnlyForNamespaces() { IndexDto index = indexResource.getIndex(); Assertions.assertThat(index.getLinks().getLinksBy("autocomplete")) .extracting("name") - .isEmpty(); + .containsExactly("namespaces"); } @Test