diff --git a/src/main/java/de/ozgcloud/admin/organisationseinheit/OrganisationsEinheitRootProcessor.java b/src/main/java/de/ozgcloud/admin/organisationseinheit/OrganisationsEinheitRootProcessor.java index 22aaead768ad7085653873844cea2e0da73be070..c505fcdafd8d3b147b69d63cf529ac83dae33240 100644 --- a/src/main/java/de/ozgcloud/admin/organisationseinheit/OrganisationsEinheitRootProcessor.java +++ b/src/main/java/de/ozgcloud/admin/organisationseinheit/OrganisationsEinheitRootProcessor.java @@ -35,6 +35,8 @@ import org.springframework.stereotype.Component; import de.ozgcloud.admin.Root; import de.ozgcloud.admin.common.FeatureToggleProperties; +import de.ozgcloud.admin.common.user.CurrentUserService; +import de.ozgcloud.admin.common.user.UserRole; import lombok.RequiredArgsConstructor; @Component @@ -45,12 +47,12 @@ class OrganisationsEinheitRootProcessor implements RepresentationModelProcessor< static final String REL_ORGANISATIONS_EINHEITEN = "organisationsEinheiten"; static final String REL_SEARCH_ORGANISATIONS_EINHEIT = "searchOrganisationsEinheit"; - private final FeatureToggleProperties featureToggleProperties; private final OrganisationsEinheitProperties organisationsEinheitProperties; + private final CurrentUserService currentUserService; @Override public EntityModel<Root> process(EntityModel<Root> model) { - return model.addAllIf(featureToggleProperties.isOrganisationsEinheiten(), + return model.addAllIf(currentUserService.hasRole(UserRole.ADMIN_ADMIN), () -> List.of(buildGetAllOrganisationsEinheitenLink(), buildSearchOrganisationsEinheitLink())); } diff --git a/src/test/java/de/ozgcloud/admin/organisationseinheit/OrganisationsEinheitRootProcessorITCase.java b/src/test/java/de/ozgcloud/admin/organisationseinheit/OrganisationsEinheitRootProcessorITCase.java new file mode 100644 index 0000000000000000000000000000000000000000..e668f1d8c1ac56aeb8ce0e3ff6dd28aa8c7e4d0b --- /dev/null +++ b/src/test/java/de/ozgcloud/admin/organisationseinheit/OrganisationsEinheitRootProcessorITCase.java @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2024 Das Land Schleswig-Holstein vertreten durch den + * Ministerpräsidenten des Landes Schleswig-Holstein + * Staatskanzlei + * Abteilung Digitalisierung und zentrales IT-Management der Landesregierung + * + * Lizenziert unter der EUPL, Version 1.2 oder - sobald + * diese von der Europäischen Kommission genehmigt wurden - + * Folgeversionen der EUPL ("Lizenz"); + * Sie dürfen dieses Werk ausschließlich gemäß + * dieser Lizenz nutzen. + * Eine Kopie der Lizenz finden Sie hier: + * + * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12 + * + * Sofern nicht durch anwendbare Rechtsvorschriften + * gefordert oder in schriftlicher Form vereinbart, wird + * die unter der Lizenz verbreitete Software "so wie sie + * ist", OHNE JEGLICHE GEWÄHRLEISTUNG ODER BEDINGUNGEN - + * ausdrücklich oder stillschweigend - verbreitet. + * Die sprachspezifischen Genehmigungen und Beschränkungen + * unter der Lizenz sind dem Lizenztext zu entnehmen. + */ +package de.ozgcloud.admin.organisationseinheit; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ApplicationContext; + +import de.ozgcloud.common.test.ITCase; + +class OrganisationsEinheitRootProcessorITCase { + + @Nested + @SpringBootTest(properties = { + "ozgcloud.feature.organisationsEinheiten=true", + "ozgcloud.organisations-einheit.zufi-suche-url=foo" + }) + @ITCase + class TestFeatureEnabled { + + @Autowired + private ApplicationContext applicationContext; + + @Test + void shouldHaveOrganisationsEinheitRootProcessorBean() { + assertDoesNotThrow(() -> applicationContext.getBean(OrganisationsEinheitRootProcessor.class)); + } + } + + @Nested + @SpringBootTest(properties = { + "ozgcloud.feature.organisationsEinheiten=false", + "ozgcloud.organisations-einheit.zufi-suche-url=foo" + }) + @ITCase + class TestFeatureDisabled { + + @Autowired + private ApplicationContext applicationContext; + + @Test + void shouldHaveOrganisationsEinheitRootProcessorBean() { + assertThrows(NoSuchBeanDefinitionException.class, () -> applicationContext.getBean(OrganisationsEinheitRootProcessor.class)); + } + } +} \ No newline at end of file diff --git a/src/test/java/de/ozgcloud/admin/organisationseinheit/OrganisationsEinheitRootProcessorTest.java b/src/test/java/de/ozgcloud/admin/organisationseinheit/OrganisationsEinheitRootProcessorTest.java index 1d3bfcc90dd770964b2801057228df3d6bb262a2..3e1df1759dccac5b16e618d14bed5abc6a6d7e52 100644 --- a/src/test/java/de/ozgcloud/admin/organisationseinheit/OrganisationsEinheitRootProcessorTest.java +++ b/src/test/java/de/ozgcloud/admin/organisationseinheit/OrganisationsEinheitRootProcessorTest.java @@ -24,8 +24,10 @@ package de.ozgcloud.admin.organisationseinheit; import static org.assertj.core.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; @@ -35,122 +37,120 @@ import org.springframework.hateoas.Link; import com.thedeanda.lorem.LoremIpsum; +import de.ozgcloud.admin.Root; import de.ozgcloud.admin.RootTestFactory; -import de.ozgcloud.admin.common.FeatureToggleProperties; +import de.ozgcloud.admin.common.user.CurrentUserService; +import de.ozgcloud.admin.common.user.UserRole; class OrganisationsEinheitRootProcessorTest { @InjectMocks private OrganisationsEinheitRootProcessor organisationsEinheitRootProcessor; @Mock - private FeatureToggleProperties featureToggleProperties; - @Mock private OrganisationsEinheitProperties organisationsEinheitProperties; + @Mock + private CurrentUserService currentUserService; @Nested class TestProcess { private final String zufiSucheUri = LoremIpsum.getInstance().getUrl() + "?searchBy={searchBy}"; - @Nested - class OrganisationsEinheitenLinkRelation { + @Test + void shouldCheckUserRole() { + processModel(); - @Test - void shouldExistsIfFeatureEnabled() { - givenFeatureIsEnabled(); - - var model = organisationsEinheitRootProcessor.process(EntityModel.of(RootTestFactory.create())); - - assertThat(model.getLink(OrganisationsEinheitRootProcessor.REL_ORGANISATIONS_EINHEITEN)).isNotEmpty(); - } - - @Test - void shouldNotExistIfFeatureDisabled() { - givenFeatureIsDisabled(); + verify(currentUserService).hasRole(UserRole.ADMIN_ADMIN); + } - var model = organisationsEinheitRootProcessor.process(EntityModel.of(RootTestFactory.create())); + @Nested + class TestOnWrongUserRole { - assertThat(model.getLink(OrganisationsEinheitRootProcessor.REL_ORGANISATIONS_EINHEITEN)).isEmpty(); + @BeforeEach + void givenHasWrongRole() { + when(currentUserService.hasRole(anyString())).thenReturn(false); } @Test - void shouldHaveHref() { - givenFeatureIsEnabled(); - - var model = organisationsEinheitRootProcessor.process(EntityModel.of(RootTestFactory.create())); + void shouldNotAddAnyLinks() { + var model = processModel(); - assertThat(model.getLink(OrganisationsEinheitRootProcessor.REL_ORGANISATIONS_EINHEITEN)) - .get() - .extracting(Link::getHref) - .isEqualTo(OrganisationsEinheitController.PATH); + assertThat(model.getLinks()).isEmpty(); } } @Nested - class SearchOrganisationsEinheitenLink { + class TestOnAdminRole { - @Test - void shouldExistsIfFeatureEnabled() { - givenFeatureIsEnabled(); + @BeforeEach + void givenHasAdminRole() { + when(currentUserService.hasRole(anyString())).thenReturn(true); + when(organisationsEinheitProperties.getZufiSucheUri()).thenReturn(zufiSucheUri); + } - var model = organisationsEinheitRootProcessor.process(EntityModel.of(RootTestFactory.create())); + @Nested + class OrganisationsEinheitenLinkRelation { - assertThat(model.getLink(OrganisationsEinheitRootProcessor.REL_SEARCH_ORGANISATIONS_EINHEIT)).isNotEmpty(); - } + @Test + void shouldExist() { + var model = processModel(); - @Test - void shouldNotExistIfFeatureDisabled() { - givenFeatureIsDisabled(); + assertThat(model.getLink(OrganisationsEinheitRootProcessor.REL_ORGANISATIONS_EINHEITEN)).isNotEmpty(); + } - var model = organisationsEinheitRootProcessor.process(EntityModel.of(RootTestFactory.create())); + @Test + void shouldHaveHref() { + var model = processModel(); - assertThat(model.getLink(OrganisationsEinheitRootProcessor.REL_SEARCH_ORGANISATIONS_EINHEIT)).isEmpty(); + assertThat(model.getLink(OrganisationsEinheitRootProcessor.REL_ORGANISATIONS_EINHEITEN)) + .get() + .extracting(Link::getHref) + .isEqualTo(OrganisationsEinheitController.PATH); + } } - @Test - void shouldGetZufiSucheUri() { - givenFeatureIsEnabled(); + @Nested + class SearchOrganisationsEinheitenLink { - organisationsEinheitRootProcessor.process(EntityModel.of(RootTestFactory.create())); + @Test + void shouldExistsIfFeatureEnabled() { + var model = processModel(); - verify(organisationsEinheitProperties).getZufiSucheUri(); + assertThat(model.getLink(OrganisationsEinheitRootProcessor.REL_SEARCH_ORGANISATIONS_EINHEIT)).isNotEmpty(); + } - } + @Test + void shouldGetZufiSucheUri() { + processModel(); - @Test - void shouldHaveHref() { - givenFeatureIsEnabled(); + verify(organisationsEinheitProperties).getZufiSucheUri(); - var model = organisationsEinheitRootProcessor.process(EntityModel.of(RootTestFactory.create())); + } - assertThat(model.getLink(OrganisationsEinheitRootProcessor.REL_SEARCH_ORGANISATIONS_EINHEIT)) - .get() - .extracting(Link::getHref) - .isEqualTo(zufiSucheUri); - } + @Test + void shouldHaveHref() { + var model = processModel(); - @Test - void shouldBeTemplated() { - givenFeatureIsEnabled(); + assertThat(model.getLink(OrganisationsEinheitRootProcessor.REL_SEARCH_ORGANISATIONS_EINHEIT)) + .get() + .extracting(Link::getHref) + .isEqualTo(zufiSucheUri); + } - var model = organisationsEinheitRootProcessor.process(EntityModel.of(RootTestFactory.create())); + @Test + void shouldBeTemplated() { + var model = processModel(); - assertThat(model.getLink(OrganisationsEinheitRootProcessor.REL_SEARCH_ORGANISATIONS_EINHEIT)) - .get() - .extracting(Link::isTemplated) - .isEqualTo(true); + assertThat(model.getLink(OrganisationsEinheitRootProcessor.REL_SEARCH_ORGANISATIONS_EINHEIT)) + .get() + .extracting(Link::isTemplated) + .isEqualTo(true); + } } } - private void givenFeatureIsEnabled() { - when(featureToggleProperties.isOrganisationsEinheiten()).thenReturn(true); - when(organisationsEinheitProperties.getZufiSucheUri()).thenReturn(zufiSucheUri); - } - - private void givenFeatureIsDisabled() { - when(featureToggleProperties.isOrganisationsEinheiten()).thenReturn(false); + private EntityModel<Root> processModel() { + return organisationsEinheitRootProcessor.process(EntityModel.of(RootTestFactory.create())); } - } - } \ No newline at end of file