property test access to partner-details

This commit is contained in:
Michael Hoennig 2025-01-15 11:22:58 +01:00
parent c774676fe6
commit c698754a8d

View File

@ -6,19 +6,19 @@ import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonRealRepository;
import net.hostsharing.hsadminng.hs.office.relation.HsOfficeRelationRealEntity; import net.hostsharing.hsadminng.hs.office.relation.HsOfficeRelationRealEntity;
import net.hostsharing.hsadminng.hs.office.relation.HsOfficeRelationRealRepository; import net.hostsharing.hsadminng.hs.office.relation.HsOfficeRelationRealRepository;
import net.hostsharing.hsadminng.hs.office.relation.HsOfficeRelationType; import net.hostsharing.hsadminng.hs.office.relation.HsOfficeRelationType;
import net.hostsharing.hsadminng.rbac.test.ContextBasedTestWithCleanup;
import net.hostsharing.hsadminng.rbac.grant.RawRbacGrantRepository; import net.hostsharing.hsadminng.rbac.grant.RawRbacGrantRepository;
import net.hostsharing.hsadminng.rbac.role.RawRbacObjectRepository; import net.hostsharing.hsadminng.rbac.role.RawRbacObjectRepository;
import net.hostsharing.hsadminng.rbac.role.RawRbacRoleRepository; import net.hostsharing.hsadminng.rbac.role.RawRbacRoleRepository;
import net.hostsharing.hsadminng.rbac.test.ContextBasedTestWithCleanup;
import net.hostsharing.hsadminng.rbac.test.JpaAttempt; import net.hostsharing.hsadminng.rbac.test.JpaAttempt;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.orm.jpa.JpaSystemException; import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContext;
@ -27,17 +27,17 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import static net.hostsharing.hsadminng.mapper.Array.from;
import static net.hostsharing.hsadminng.rbac.grant.RawRbacGrantEntity.distinctGrantDisplaysOf; import static net.hostsharing.hsadminng.rbac.grant.RawRbacGrantEntity.distinctGrantDisplaysOf;
import static net.hostsharing.hsadminng.rbac.role.RawRbacObjectEntity.objectDisplaysOf; import static net.hostsharing.hsadminng.rbac.role.RawRbacObjectEntity.objectDisplaysOf;
import static net.hostsharing.hsadminng.rbac.role.RawRbacRoleEntity.distinctRoleNamesOf; import static net.hostsharing.hsadminng.rbac.role.RawRbacRoleEntity.distinctRoleNamesOf;
import static net.hostsharing.hsadminng.mapper.Array.from;
import static net.hostsharing.hsadminng.rbac.role.RbacRoleType.ADMIN; import static net.hostsharing.hsadminng.rbac.role.RbacRoleType.ADMIN;
import static net.hostsharing.hsadminng.rbac.role.RbacRoleType.AGENT; import static net.hostsharing.hsadminng.rbac.role.RbacRoleType.AGENT;
import static net.hostsharing.hsadminng.rbac.test.JpaAttempt.attempt; import static net.hostsharing.hsadminng.rbac.test.JpaAttempt.attempt;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@DataJpaTest @DataJpaTest
@Import( { Context.class, JpaAttempt.class }) @Import({ Context.class, JpaAttempt.class })
class HsOfficePartnerRbacRepositoryIntegrationTest extends ContextBasedTestWithCleanup { class HsOfficePartnerRbacRepositoryIntegrationTest extends ContextBasedTestWithCleanup {
@Autowired @Autowired
@ -81,14 +81,15 @@ class HsOfficePartnerRbacRepositoryIntegrationTest extends ContextBasedTestWithC
final var partnerRel = givenSomeTemporaryHostsharingPartnerRel("Winkler", "first contact"); final var partnerRel = givenSomeTemporaryHostsharingPartnerRel("Winkler", "first contact");
// when // when
final var result = attempt(em, () -> { final var result = attempt(
final var newPartner = HsOfficePartnerRbacEntity.builder() em, () -> {
.partnerNumber(20031) final var newPartner = HsOfficePartnerRbacEntity.builder()
.partnerRel(partnerRel) .partnerNumber(20031)
.details(HsOfficePartnerDetailsEntity.builder().build()) .partnerRel(partnerRel)
.build(); .details(HsOfficePartnerDetailsEntity.builder().build())
return partnerRepo.save(newPartner); .build();
}); return partnerRepo.save(newPartner);
});
// then // then
result.assertSuccessful(); result.assertSuccessful();
@ -109,26 +110,27 @@ class HsOfficePartnerRbacRepositoryIntegrationTest extends ContextBasedTestWithC
.toList(); .toList();
// when // when
attempt(em, () -> { attempt(
final var givenPartnerPerson = personRepo.findPersonByOptionalNameLike("Erben Bessler").get(0); em, () -> {
final var givenContact = contactrealRepo.findContactByOptionalCaptionLike("fourth contact").get(0); final var givenPartnerPerson = personRepo.findPersonByOptionalNameLike("Erben Bessler").get(0);
final var givenMandantPerson = personRepo.findPersonByOptionalNameLike("Hostsharing eG").get(0); final var givenContact = contactrealRepo.findContactByOptionalCaptionLike("fourth contact").get(0);
final var givenMandantPerson = personRepo.findPersonByOptionalNameLike("Hostsharing eG").get(0);
final var newRelation = HsOfficeRelationRealEntity.builder() final var newRelation = HsOfficeRelationRealEntity.builder()
.holder(givenPartnerPerson) .holder(givenPartnerPerson)
.type(HsOfficeRelationType.PARTNER) .type(HsOfficeRelationType.PARTNER)
.anchor(givenMandantPerson) .anchor(givenMandantPerson)
.contact(givenContact) .contact(givenContact)
.build(); .build();
relationRepo.save(newRelation); relationRepo.save(newRelation);
final var newPartner = HsOfficePartnerRbacEntity.builder() final var newPartner = HsOfficePartnerRbacEntity.builder()
.partnerNumber(20032) .partnerNumber(20032)
.partnerRel(newRelation) .partnerRel(newRelation)
.details(HsOfficePartnerDetailsEntity.builder().build()) .details(HsOfficePartnerDetailsEntity.builder().build())
.build(); .build();
return partnerRepo.save(newPartner); return partnerRepo.save(newPartner);
}).assertSuccessful(); }).assertSuccessful();
// then // then
assertThat(distinctRoleNamesOf(rawRoleRepo.findAll())).containsExactlyInAnyOrder(from( assertThat(distinctRoleNamesOf(rawRoleRepo.findAll())).containsExactlyInAnyOrder(from(
@ -208,29 +210,32 @@ class HsOfficePartnerRbacRepositoryIntegrationTest extends ContextBasedTestWithC
} }
@Test @Test
public void partnerAgent_canViewOnlyRelatedPartnersWithoutDetails() { public void partnerAgent_canViewOnlyRelatedPartnersWithDetails() {
// given: // given:
context("person-FirstGmbH@example.com", context(
"person-FirstGmbH@example.com",
"hs_office.relation#HostsharingeG-with-PARTNER-FirstGmbH:AGENT"); "hs_office.relation#HostsharingeG-with-PARTNER-FirstGmbH:AGENT");
// when: // when:
final var result = partnerRepo.findPartnerByOptionalNameLike(null); final var result = partnerRepo.findPartnerByOptionalNameLike(null);
// then: // then:
exactlyThesePartnersAreReturned(result, "partner(P-10001: LP First GmbH, first contact)"); exactlyThesePartnersAreReturned(result,
"partner(P-10001: LP First GmbH, first contact)+(partnerDetails(Hamburg, RegNo123456789))");
} }
@Test @Test
public void partnerTenant_canViewRelatedPartnersButWithoutDetails() { public void partnerTenant_canViewRelatedPartnersButWithoutDetails() {
// given: // given:
context("person-FirstGmbH@example.com", context(
"person-FirstGmbH@example.com",
"hs_office.relation#HostsharingeG-with-PARTNER-FirstGmbH:TENANT"); "hs_office.relation#HostsharingeG-with-PARTNER-FirstGmbH:TENANT");
// when: // when:
final var result = partnerRepo.findPartnerByOptionalNameLike(null); final var result = partnerRepo.findPartnerByOptionalNameLike(null);
// then: // then:
exactlyThesePartnersAreReturned(result, "partner(P-10001: LP First GmbH, first contact)"); exactlyThesePartnersAreReturned(result, "partner(P-10001: LP First GmbH, first contact)+null");
} }
} }
@ -238,7 +243,7 @@ class HsOfficePartnerRbacRepositoryIntegrationTest extends ContextBasedTestWithC
class FindByNameLike { class FindByNameLike {
@Test @Test
public void globalAdmin_withoutAssumedRole_canViewAllPartners() { public void globalAdmin_withoutAssumedRole_canViewAllPartnersWithDetails() {
// given // given
context("superuser-alex@hostsharing.net"); context("superuser-alex@hostsharing.net");
@ -246,7 +251,8 @@ class HsOfficePartnerRbacRepositoryIntegrationTest extends ContextBasedTestWithC
final var result = partnerRepo.findPartnerByOptionalNameLike("third contact"); final var result = partnerRepo.findPartnerByOptionalNameLike("third contact");
// then // then
exactlyThesePartnersAreReturned(result, "partner(P-10003: IF Third OHG, third contact)"); exactlyThesePartnersAreReturned(result,
"partner(P-10003: IF Third OHG, third contact)+(partnerDetails(Hamburg, RegNo123456789))");
} }
} }
@ -315,7 +321,8 @@ class HsOfficePartnerRbacRepositoryIntegrationTest extends ContextBasedTestWithC
// when // when
final var result = jpaAttempt.transacted(() -> { final var result = jpaAttempt.transacted(() -> {
context("superuser-alex@hostsharing.net", context(
"superuser-alex@hostsharing.net",
givenPartner.getPartnerRel().roleId(AGENT)); givenPartner.getPartnerRel().roleId(AGENT));
givenPartner.getDetails().setBirthName("new birthname"); givenPartner.getDetails().setBirthName("new birthname");
return partnerRepo.save(givenPartner); return partnerRepo.save(givenPartner);
@ -327,7 +334,11 @@ class HsOfficePartnerRbacRepositoryIntegrationTest extends ContextBasedTestWithC
private void assertThatPartnerActuallyInDatabase(final HsOfficePartnerRbacEntity saved) { private void assertThatPartnerActuallyInDatabase(final HsOfficePartnerRbacEntity saved) {
final var found = partnerRepo.findByUuid(saved.getUuid()); final var found = partnerRepo.findByUuid(saved.getUuid());
assertThat(found).isNotEmpty().get().isNotSameAs(saved).extracting(HsOfficePartnerRbacEntity::toString).isEqualTo(saved.toString()); assertThat(found).isNotEmpty()
.get()
.isNotSameAs(saved)
.extracting(HsOfficePartnerRbacEntity::toString)
.isEqualTo(saved.toString());
} }
private void assertThatPartnerIsVisibleForUserWithRole( private void assertThatPartnerIsVisibleForUserWithRole(
@ -428,7 +439,7 @@ class HsOfficePartnerRbacRepositoryIntegrationTest extends ContextBasedTestWithC
select currentTask, targetTable, targetOp, targetdelta->>'partnernumber' select currentTask, targetTable, targetOp, targetdelta->>'partnernumber'
from base.tx_journal_v from base.tx_journal_v
where targettable = 'hs_office.partner'; where targettable = 'hs_office.partner';
"""); """);
// when // when
@SuppressWarnings("unchecked") final List<Object[]> customerLogEntries = query.getResultList(); @SuppressWarnings("unchecked") final List<Object[]> customerLogEntries = query.getResultList();
@ -454,7 +465,7 @@ class HsOfficePartnerRbacRepositoryIntegrationTest extends ContextBasedTestWithC
.details(HsOfficePartnerDetailsEntity.builder().build()) .details(HsOfficePartnerDetailsEntity.builder().build())
.build(); .build();
final var savedPartner = partnerRepo.save(newPartner); final var savedPartner = partnerRepo.save(newPartner);
em.flush(); em.flush();
final var partner = em.find(savedPartner.getClass(), savedPartner.getUuid()); final var partner = em.find(savedPartner.getClass(), savedPartner.getUuid());
return savedPartner; return savedPartner;
@ -478,7 +489,9 @@ class HsOfficePartnerRbacRepositoryIntegrationTest extends ContextBasedTestWithC
void exactlyThesePartnersAreReturned(final List<HsOfficePartnerRbacEntity> actualResult, final String... partnerNames) { void exactlyThesePartnersAreReturned(final List<HsOfficePartnerRbacEntity> actualResult, final String... partnerNames) {
assertThat(actualResult) assertThat(actualResult)
.extracting(HsOfficePartnerRbacEntity::toString) .extracting(partner ->
partner.toString() + "+" +
(partner.getDetails() != null ? ("(" + partner.getDetails() + ")") : "null"))
.containsExactlyInAnyOrder(partnerNames); .containsExactlyInAnyOrder(partnerNames);
} }