implements table hs_office_relationship to HsOfficeRelationshipController
This commit is contained in:
57
src/test/java/net/hostsharing/hsadminng/MapperUnitTest.java
Normal file
57
src/test/java/net/hostsharing/hsadminng/MapperUnitTest.java
Normal file
@@ -0,0 +1,57 @@
|
||||
package net.hostsharing.hsadminng;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class MapperUnitTest {
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class SourceBean {
|
||||
private String a;
|
||||
private String b;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class TargetBean {
|
||||
private String a;
|
||||
private String b;
|
||||
private String c;
|
||||
}
|
||||
|
||||
@Test
|
||||
void mapsNullBeanToNull() {
|
||||
final SourceBean givenSource = null;
|
||||
final var result = Mapper.map(givenSource, TargetBean.class, (s, t) -> { fail("should not have been called"); });
|
||||
assertThat(result).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void mapsBean() {
|
||||
final SourceBean givenSource = new SourceBean("1234", "Text");
|
||||
final var result = Mapper.map(givenSource, TargetBean.class, null);
|
||||
assertThat(result).usingRecursiveComparison().isEqualTo(
|
||||
new TargetBean("1234", "Text", null)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void mapsBeanWithPostmapper() {
|
||||
final SourceBean givenSource = new SourceBean("1234", "Text");
|
||||
final var result = Mapper.map(givenSource, TargetBean.class, (s, t) -> { t.setC("Extra"); });
|
||||
assertThat(result).usingRecursiveComparison().isEqualTo(
|
||||
new TargetBean("1234", "Text", "Extra")
|
||||
);
|
||||
}
|
||||
}
|
@@ -0,0 +1,99 @@
|
||||
package net.hostsharing.hsadminng;
|
||||
|
||||
import lombok.*;
|
||||
import lombok.experimental.FieldNameConstants;
|
||||
import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactEntity;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import java.util.UUID;
|
||||
|
||||
import static net.hostsharing.hsadminng.Stringify.stringify;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class StringifyUnitTest {
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@FieldNameConstants
|
||||
public static class TestBean implements Stringifyable {
|
||||
|
||||
private static Stringify<TestBean> toString = stringify(TestBean.class, "bean")
|
||||
.withProp(TestBean.Fields.label, TestBean::getLabel)
|
||||
.withProp(TestBean.Fields.content, TestBean::getContent)
|
||||
.withProp(TestBean.Fields.active, TestBean::isActive);
|
||||
|
||||
private UUID uuid;
|
||||
|
||||
private String label;
|
||||
|
||||
private SubBean content;
|
||||
|
||||
private boolean active;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return toString.apply(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toShortString() {
|
||||
return label;
|
||||
}
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@FieldNameConstants
|
||||
public static class SubBean implements Stringifyable {
|
||||
|
||||
private static Stringify<SubBean> toString = stringify(SubBean.class)
|
||||
.withProp(SubBean.Fields.key, SubBean::getKey)
|
||||
.withProp(Fields.value, SubBean::getValue);
|
||||
|
||||
private String key;
|
||||
private Integer value;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return toString.apply(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toShortString() {
|
||||
return key + ":" + value;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void stringifyWhenAllPropsHaveValues() {
|
||||
final var given = new TestBean(UUID.randomUUID(), "some label",
|
||||
new SubBean("some content", 1234), false);
|
||||
final var result = given.toString();
|
||||
assertThat(result).isEqualTo("bean(label='some label', content='some content:1234', active=false)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void stringifyWhenAllNullablePropsHaveNulValues() {
|
||||
final var given = new TestBean();
|
||||
final var result = given.toString();
|
||||
assertThat(result).isEqualTo("bean(active=false)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void stringifyWithoutExplicitNameUsesSimpleClassName() {
|
||||
final var given = new SubBean("some key", 1234);
|
||||
final var result = given.toString();
|
||||
assertThat(result).isEqualTo("SubBean(key='some key', value=1234)");
|
||||
}
|
||||
}
|
@@ -74,6 +74,7 @@ class HsOfficeContactControllerAcceptanceTest {
|
||||
{ "label": "forth contact" },
|
||||
{ "label": "fifth contact" },
|
||||
{ "label": "sixth contact" },
|
||||
{ "label": "seventh contact" },
|
||||
{ "label": "eighth contact" },
|
||||
{ "label": "ninth contact" },
|
||||
{ "label": "tenth contact" },
|
||||
@@ -173,7 +174,7 @@ class HsOfficeContactControllerAcceptanceTest {
|
||||
|
||||
RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "customer-admin@firstcontact.example.com")
|
||||
.header("current-user", "contact-admin@firstcontact.example.com")
|
||||
.port(port)
|
||||
.when()
|
||||
.get("http://localhost/api/hs/office/contacts/" + givenContactUuid)
|
||||
@@ -183,7 +184,7 @@ class HsOfficeContactControllerAcceptanceTest {
|
||||
.body("", lenientlyEquals("""
|
||||
{
|
||||
"label": "first contact",
|
||||
"emailAddresses": "customer-admin@firstcontact.example.com",
|
||||
"emailAddresses": "contact-admin@firstcontact.example.com",
|
||||
"phoneNumbers": "+49 123 1234567"
|
||||
}
|
||||
""")); // @formatter:on
|
||||
|
@@ -0,0 +1,21 @@
|
||||
package net.hostsharing.hsadminng.hs.office.contact;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class HsOfficeContactEntityUnitTest {
|
||||
|
||||
@Test
|
||||
void toStringReturnsNullForNullContact() {
|
||||
final HsOfficeContactEntity givenContact = null;
|
||||
assertThat("" + givenContact).isEqualTo("null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void toStringReturnsLabel() {
|
||||
final var givenContact = HsOfficeContactEntity.builder().label("given label").build();
|
||||
assertThat("" + givenContact).isEqualTo("contact(label='given label')");
|
||||
}
|
||||
|
||||
}
|
@@ -244,7 +244,7 @@ class HsOfficePartnerControllerAcceptanceTest {
|
||||
|
||||
RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "customer-admin@firstcontact.example.com")
|
||||
.header("current-user", "contact-admin@firstcontact.example.com")
|
||||
.port(port)
|
||||
.when()
|
||||
.get("http://localhost/api/hs/office/partners/" + givenPartnerUuid)
|
||||
@@ -390,7 +390,7 @@ class HsOfficePartnerControllerAcceptanceTest {
|
||||
|
||||
RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "customer-admin@forthcontact.example.com")
|
||||
.header("current-user", "contact-admin@forthcontact.example.com")
|
||||
.port(port)
|
||||
.when()
|
||||
.delete("http://localhost/api/hs/office/partners/" + givenPartner.getUuid())
|
||||
|
@@ -56,6 +56,7 @@ class HsOfficePartnerEntityPatcherUnitTest extends PatchUnitTestBase<
|
||||
lenient().when(em.getReference(eq(HsOfficePersonEntity.class), any())).thenAnswer(invocation ->
|
||||
HsOfficePersonEntity.builder().uuid(invocation.getArgument(1)).build());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected HsOfficePartnerEntity newInitialEntity() {
|
||||
final var entity = new HsOfficePartnerEntity();
|
||||
|
@@ -393,9 +393,6 @@ class HsOfficePartnerRepositoryIntegrationTest extends ContextBasedTest {
|
||||
context("superuser-alex@hostsharing.net", null);
|
||||
tempPartners.forEach(tempPartner -> {
|
||||
System.out.println("DELETING temporary partner: " + tempPartner.getDisplayName());
|
||||
if ( tempPartner.getContact().getLabel().equals("sixth contact")) {
|
||||
toString();
|
||||
}
|
||||
partnerRepo.deleteByUuid(tempPartner.getUuid());
|
||||
});
|
||||
}
|
||||
|
@@ -70,35 +70,47 @@ class HsOfficePersonControllerAcceptanceTest {
|
||||
.body("", lenientlyEquals("""
|
||||
[
|
||||
{
|
||||
"personType": "JOINT_REPRESENTATION",
|
||||
"tradeName": "Erben Bessler",
|
||||
"givenName": "Bessler",
|
||||
"familyName": "Mel"
|
||||
},
|
||||
{
|
||||
"personType": "LEGAL",
|
||||
"tradeName": "First Impressions GmbH",
|
||||
"givenName": null,
|
||||
"familyName": null
|
||||
},
|
||||
{
|
||||
"personType": "SOLE_REPRESENTATION",
|
||||
"tradeName": "Ostfriesische Kuhhandel OHG",
|
||||
"givenName": null,
|
||||
"familyName": null
|
||||
},
|
||||
{
|
||||
"personType": "NATURAL",
|
||||
"tradeName": null,
|
||||
"givenName": "Smith",
|
||||
"familyName": "Peter"
|
||||
},
|
||||
{
|
||||
"personType": "LEGAL",
|
||||
"tradeName": "Rockshop e.K.",
|
||||
"givenName": "Miller",
|
||||
"familyName": "Sandra"
|
||||
}
|
||||
"personType": "NATURAL",
|
||||
"tradeName": null,
|
||||
"givenName": "Anita",
|
||||
"familyName": "Bessler"
|
||||
},
|
||||
{
|
||||
"personType": "JOINT_REPRESENTATION",
|
||||
"tradeName": "Erben Bessler",
|
||||
"givenName": "Bessler",
|
||||
"familyName": "Mel"
|
||||
},
|
||||
{
|
||||
"personType": "LEGAL",
|
||||
"tradeName": "First Impressions GmbH",
|
||||
"givenName": null,
|
||||
"familyName": null
|
||||
},
|
||||
{
|
||||
"personType": "SOLE_REPRESENTATION",
|
||||
"tradeName": "Ostfriesische Kuhhandel OHG",
|
||||
"givenName": null,
|
||||
"familyName": null
|
||||
},
|
||||
{
|
||||
"personType": "LEGAL",
|
||||
"tradeName": "Rockshop e.K.",
|
||||
"givenName": "Miller",
|
||||
"familyName": "Sandra"
|
||||
},
|
||||
{
|
||||
"personType": "NATURAL",
|
||||
"tradeName": null,
|
||||
"givenName": "Peter",
|
||||
"familyName": "Smith"
|
||||
},
|
||||
{
|
||||
"personType": "NATURAL",
|
||||
"tradeName": null,
|
||||
"givenName": "Paul",
|
||||
"familyName": "Winkler"
|
||||
}
|
||||
]
|
||||
"""
|
||||
));
|
||||
|
@@ -2,6 +2,8 @@ package net.hostsharing.hsadminng.hs.office.person;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class HsOfficePersonEntityUnitTest {
|
||||
@@ -29,4 +31,55 @@ class HsOfficePersonEntityUnitTest {
|
||||
assertThat(actualDisplay).isEqualTo("some family name, some given name");
|
||||
}
|
||||
|
||||
@Test
|
||||
void toShortStringWithTradeNameReturnsTradeName() {
|
||||
final var givenPersonEntity = HsOfficePersonEntity.builder()
|
||||
.tradeName("some trade name")
|
||||
.familyName("some family name")
|
||||
.givenName("some given name")
|
||||
.build();
|
||||
|
||||
final var actualDisplay = givenPersonEntity.toShortString();
|
||||
|
||||
assertThat(actualDisplay).isEqualTo("some trade name");
|
||||
}
|
||||
|
||||
@Test
|
||||
void toShortStringWithoutTradeNameReturnsFamilyAndGivenName() {
|
||||
final var givenPersonEntity = HsOfficePersonEntity.builder()
|
||||
.familyName("some family name")
|
||||
.givenName("some given name")
|
||||
.build();
|
||||
|
||||
final var actualDisplay = givenPersonEntity.toShortString();
|
||||
|
||||
assertThat(actualDisplay).isEqualTo("some family name, some given name");
|
||||
}
|
||||
|
||||
@Test
|
||||
void toStringWithAllFieldsReturnsAllButUuid() {
|
||||
final var givenPersonEntity = HsOfficePersonEntity.builder()
|
||||
.uuid(UUID.randomUUID())
|
||||
.personType(HsOfficePersonType.NATURAL)
|
||||
.tradeName("some trade name")
|
||||
.familyName("some family name")
|
||||
.givenName("some given name")
|
||||
.build();
|
||||
|
||||
final var actualDisplay = givenPersonEntity.toString();
|
||||
|
||||
assertThat(actualDisplay).isEqualTo("person(personType='NATURAL', tradeName='some trade name', familyName='some family name', givenName='some given name')");
|
||||
}
|
||||
|
||||
@Test
|
||||
void toStringSkipsNullFields() {
|
||||
final var givenPersonEntity = HsOfficePersonEntity.builder()
|
||||
.familyName("some family name")
|
||||
.givenName("some given name")
|
||||
.build();
|
||||
|
||||
final var actualDisplay = givenPersonEntity.toString();
|
||||
|
||||
assertThat(actualDisplay).isEqualTo("person(familyName='some family name', givenName='some given name')");
|
||||
}
|
||||
}
|
||||
|
@@ -143,7 +143,7 @@ class HsOfficePersonRepositoryIntegrationTest extends ContextBasedTest {
|
||||
// then
|
||||
allThesePersonsAreReturned(
|
||||
result,
|
||||
"Peter, Smith",
|
||||
"Smith, Peter",
|
||||
"Rockshop e.K.",
|
||||
"Ostfriesische Kuhhandel OHG",
|
||||
"Erben Bessler");
|
||||
|
@@ -0,0 +1,510 @@
|
||||
package net.hostsharing.hsadminng.hs.office.relationship;
|
||||
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.http.ContentType;
|
||||
import net.hostsharing.hsadminng.Accepts;
|
||||
import net.hostsharing.hsadminng.HsadminNgApplication;
|
||||
import net.hostsharing.hsadminng.context.Context;
|
||||
import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactRepository;
|
||||
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeRelationshipTypeResource;
|
||||
import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonRepository;
|
||||
import net.hostsharing.test.JpaAttempt;
|
||||
import org.json.JSONException;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import static net.hostsharing.test.IsValidUuidMatcher.isUuidValid;
|
||||
import static net.hostsharing.test.JsonMatcher.lenientlyEquals;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assumptions.assumeThat;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
|
||||
@SpringBootTest(
|
||||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
||||
classes = { HsadminNgApplication.class, JpaAttempt.class }
|
||||
)
|
||||
@Transactional
|
||||
class HsOfficeRelationshipControllerAcceptanceTest {
|
||||
|
||||
@LocalServerPort
|
||||
private Integer port;
|
||||
|
||||
@Autowired
|
||||
Context context;
|
||||
|
||||
@Autowired
|
||||
Context contextMock;
|
||||
|
||||
@Autowired
|
||||
HsOfficeRelationshipRepository relationshipRepo;
|
||||
|
||||
@Autowired
|
||||
HsOfficePersonRepository personRepo;
|
||||
|
||||
@Autowired
|
||||
HsOfficeContactRepository contactRepo;
|
||||
|
||||
@Autowired
|
||||
JpaAttempt jpaAttempt;
|
||||
|
||||
Set<UUID> tempRelationshipUuids = new HashSet<>();
|
||||
|
||||
@Nested
|
||||
@Accepts({ "Relationship:F(Find)" })
|
||||
class ListRelationships {
|
||||
|
||||
@Test
|
||||
void globalAdmin_withoutAssumedRoles_canViewAllRelationshipsOfGivenPersonAndType_ifNoCriteriaGiven() throws JSONException {
|
||||
|
||||
// given
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final var givenPerson = personRepo.findPersonByOptionalNameLike("Smith").get(0);
|
||||
|
||||
RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "superuser-alex@hostsharing.net")
|
||||
.port(port)
|
||||
.when()
|
||||
.get("http://localhost/api/hs/office/relationships?personUuid=%s&relationshipType=%s"
|
||||
.formatted(givenPerson.getUuid(), HsOfficeRelationshipTypeResource.SOLE_AGENT))
|
||||
.then().log().all().assertThat()
|
||||
.statusCode(200)
|
||||
.contentType("application/json")
|
||||
.body("", lenientlyEquals("""
|
||||
[
|
||||
{
|
||||
"relAnchor": {
|
||||
"personType": "SOLE_REPRESENTATION",
|
||||
"tradeName": "Ostfriesische Kuhhandel OHG"
|
||||
},
|
||||
"relHolder": {
|
||||
"personType": "NATURAL",
|
||||
"givenName": "Peter",
|
||||
"familyName": "Smith"
|
||||
},
|
||||
"relType": "SOLE_AGENT",
|
||||
"contact": { "label": "third contact" }
|
||||
},
|
||||
{
|
||||
"relAnchor": {
|
||||
"personType": "LEGAL",
|
||||
"tradeName": "Rockshop e.K.",
|
||||
"givenName": "Miller",
|
||||
"familyName": "Sandra"
|
||||
},
|
||||
"relHolder": {
|
||||
"personType": "NATURAL",
|
||||
"givenName": "Peter",
|
||||
"familyName": "Smith"
|
||||
},
|
||||
"relType": "SOLE_AGENT",
|
||||
"contact": { "label": "second contact" }
|
||||
},
|
||||
{
|
||||
"relAnchor": {
|
||||
"personType": "LEGAL",
|
||||
"tradeName": "First Impressions GmbH"
|
||||
},
|
||||
"relHolder": {
|
||||
"personType": "NATURAL",
|
||||
"tradeName": null,
|
||||
"givenName": "Peter",
|
||||
"familyName": "Smith"
|
||||
},
|
||||
"relType": "SOLE_AGENT",
|
||||
"contact": { "label": "first contact" }
|
||||
}
|
||||
]
|
||||
"""));
|
||||
// @formatter:on
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@Accepts({ "Relationship:C(Create)" })
|
||||
class AddRelationship {
|
||||
|
||||
@Test
|
||||
void globalAdmin_withoutAssumedRole_canAddRelationship() {
|
||||
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final var givenAnchorPerson = personRepo.findPersonByOptionalNameLike("Ostfriesische").get(0);
|
||||
final var givenHolderPerson = personRepo.findPersonByOptionalNameLike("Paul").get(0);
|
||||
final var givenContact = contactRepo.findContactByOptionalLabelLike("forth").get(0);
|
||||
|
||||
final var location = RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "superuser-alex@hostsharing.net")
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{
|
||||
"relType": "%s",
|
||||
"relAnchorUuid": "%s",
|
||||
"relHolderUuid": "%s",
|
||||
"contactUuid": "%s"
|
||||
}
|
||||
""".formatted(
|
||||
HsOfficeRelationshipTypeResource.ACCOUNTING_CONTACT,
|
||||
givenAnchorPerson.getUuid(),
|
||||
givenHolderPerson.getUuid(),
|
||||
givenContact.getUuid()))
|
||||
.port(port)
|
||||
.when()
|
||||
.post("http://localhost/api/hs/office/relationships")
|
||||
.then().log().all().assertThat()
|
||||
.statusCode(201)
|
||||
.contentType(ContentType.JSON)
|
||||
.body("uuid", isUuidValid())
|
||||
.body("relType", is("ACCOUNTING_CONTACT"))
|
||||
.body("relAnchor.tradeName", is("Ostfriesische Kuhhandel OHG"))
|
||||
.body("relHolder.givenName", is("Paul"))
|
||||
.body("contact.label", is("forth contact"))
|
||||
.header("Location", startsWith("http://localhost"))
|
||||
.extract().header("Location"); // @formatter:on
|
||||
|
||||
// finally, the new relationship can be accessed under the generated UUID
|
||||
final var newUserUuid = toCleanup(UUID.fromString(
|
||||
location.substring(location.lastIndexOf('/') + 1)));
|
||||
assertThat(newUserUuid).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void globalAdmin_canNotAddRelationship_ifAnchorPersonDoesNotExist() {
|
||||
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final var givenAnchorPersonUuid = UUID.fromString("3fa85f64-5717-4562-b3fc-2c963f66afa6");
|
||||
final var givenHolderPerson = personRepo.findPersonByOptionalNameLike("Smith").get(0);
|
||||
final var givenContact = contactRepo.findContactByOptionalLabelLike("forth").get(0);
|
||||
|
||||
final var location = RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "superuser-alex@hostsharing.net")
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{
|
||||
"relType": "%s",
|
||||
"relAnchorUuid": "%s",
|
||||
"relHolderUuid": "%s",
|
||||
"contactUuid": "%s"
|
||||
}
|
||||
""".formatted(
|
||||
HsOfficeRelationshipTypeResource.ACCOUNTING_CONTACT,
|
||||
givenAnchorPersonUuid,
|
||||
givenHolderPerson.getUuid(),
|
||||
givenContact.getUuid()))
|
||||
.port(port)
|
||||
.when()
|
||||
.post("http://localhost/api/hs/office/relationships")
|
||||
.then().log().all().assertThat()
|
||||
.statusCode(404)
|
||||
.body("message", is("cannot find relAnchorUuid 3fa85f64-5717-4562-b3fc-2c963f66afa6"));
|
||||
// @formatter:on
|
||||
}
|
||||
|
||||
@Test
|
||||
void globalAdmin_canNotAddRelationship_ifHolderPersonDoesNotExist() {
|
||||
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final var givenAnchorPerson = personRepo.findPersonByOptionalNameLike("Ostfriesische").get(0);
|
||||
final var givenHolderPersonUuid = UUID.fromString("3fa85f64-5717-4562-b3fc-2c963f66afa6");
|
||||
final var givenContact = contactRepo.findContactByOptionalLabelLike("forth").get(0);
|
||||
|
||||
final var location = RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "superuser-alex@hostsharing.net")
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{
|
||||
"relType": "%s",
|
||||
"relAnchorUuid": "%s",
|
||||
"relHolderUuid": "%s",
|
||||
"contactUuid": "%s"
|
||||
}
|
||||
""".formatted(
|
||||
HsOfficeRelationshipTypeResource.ACCOUNTING_CONTACT,
|
||||
givenAnchorPerson.getUuid(),
|
||||
givenHolderPersonUuid,
|
||||
givenContact.getUuid()))
|
||||
.port(port)
|
||||
.when()
|
||||
.post("http://localhost/api/hs/office/relationships")
|
||||
.then().log().all().assertThat()
|
||||
.statusCode(404)
|
||||
.body("message", is("cannot find relHolderUuid 3fa85f64-5717-4562-b3fc-2c963f66afa6"));
|
||||
// @formatter:on
|
||||
}
|
||||
|
||||
@Test
|
||||
void globalAdmin_canNotAddRelationship_ifContactDoesNotExist() {
|
||||
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final var givenAnchorPerson = personRepo.findPersonByOptionalNameLike("Ostfriesische").get(0);
|
||||
final var givenHolderPerson = personRepo.findPersonByOptionalNameLike("Paul").get(0);
|
||||
final var givenContactUuid = UUID.fromString("3fa85f64-5717-4562-b3fc-2c963f66afa6");
|
||||
|
||||
final var location = RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "superuser-alex@hostsharing.net")
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{
|
||||
"relType": "%s",
|
||||
"relAnchorUuid": "%s",
|
||||
"relHolderUuid": "%s",
|
||||
"contactUuid": "%s"
|
||||
}
|
||||
""".formatted(
|
||||
HsOfficeRelationshipTypeResource.ACCOUNTING_CONTACT,
|
||||
givenAnchorPerson.getUuid(),
|
||||
givenHolderPerson.getUuid(),
|
||||
givenContactUuid))
|
||||
.port(port)
|
||||
.when()
|
||||
.post("http://localhost/api/hs/office/relationships")
|
||||
.then().log().all().assertThat()
|
||||
.statusCode(404)
|
||||
.body("message", is("cannot find contactUuid 3fa85f64-5717-4562-b3fc-2c963f66afa6"));
|
||||
// @formatter:on
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@Accepts({ "Relationship:R(Read)" })
|
||||
class GetRelationship {
|
||||
|
||||
@Test
|
||||
void globalAdmin_withoutAssumedRole_canGetArbitraryRelationship() {
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final UUID givenRelationshipUuid = findRelationship("First", "Smith").getUuid();
|
||||
|
||||
RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "superuser-alex@hostsharing.net")
|
||||
.port(port)
|
||||
.when()
|
||||
.get("http://localhost/api/hs/office/relationships/" + givenRelationshipUuid)
|
||||
.then().log().body().assertThat()
|
||||
.statusCode(200)
|
||||
.contentType("application/json")
|
||||
.body("", lenientlyEquals("""
|
||||
{
|
||||
"relAnchor": { "tradeName": "First Impressions GmbH" },
|
||||
"relHolder": { "familyName": "Smith" },
|
||||
"contact": { "label": "first contact" }
|
||||
}
|
||||
""")); // @formatter:on
|
||||
}
|
||||
|
||||
@Test
|
||||
@Accepts({ "Relationship:X(Access Control)" })
|
||||
void normalUser_canNotGetUnrelatedRelationship() {
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final UUID givenRelationshipUuid = findRelationship("First", "Smith").getUuid();
|
||||
|
||||
RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "selfregistered-user-drew@hostsharing.org")
|
||||
.port(port)
|
||||
.when()
|
||||
.get("http://localhost/api/hs/office/relationships/" + givenRelationshipUuid)
|
||||
.then().log().body().assertThat()
|
||||
.statusCode(404); // @formatter:on
|
||||
}
|
||||
|
||||
@Test
|
||||
@Accepts({ "Relationship:X(Access Control)" })
|
||||
void contactAdminUser_canGetRelatedRelationship() {
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final var givenRelationship = findRelationship("First", "Smith");
|
||||
assertThat(givenRelationship.getContact().getLabel()).isEqualTo("first contact");
|
||||
|
||||
RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "contact-admin@firstcontact.example.com")
|
||||
.port(port)
|
||||
.when()
|
||||
.get("http://localhost/api/hs/office/relationships/" + givenRelationship.getUuid())
|
||||
.then().log().body().assertThat()
|
||||
.statusCode(200)
|
||||
.contentType("application/json")
|
||||
.body("", lenientlyEquals("""
|
||||
{
|
||||
"relAnchor": { "tradeName": "First Impressions GmbH" },
|
||||
"relHolder": { "familyName": "Smith" },
|
||||
"contact": { "label": "first contact" }
|
||||
}
|
||||
""")); // @formatter:on
|
||||
}
|
||||
}
|
||||
|
||||
private HsOfficeRelationshipEntity findRelationship(
|
||||
final String anchorPersonName,
|
||||
final String holderPersoneName) {
|
||||
final var anchorPersonUuid = personRepo.findPersonByOptionalNameLike(anchorPersonName).get(0).getUuid();
|
||||
final var holderPersonUuid = personRepo.findPersonByOptionalNameLike(holderPersoneName).get(0).getUuid();
|
||||
final var givenRelationship = relationshipRepo
|
||||
.findRelationshipRelatedToPersonUuid(anchorPersonUuid)
|
||||
.stream()
|
||||
.filter(r -> r.getRelHolder().getUuid().equals(holderPersonUuid))
|
||||
.findFirst().orElseThrow();
|
||||
return givenRelationship;
|
||||
}
|
||||
|
||||
@Nested
|
||||
@Accepts({ "Relationship:U(Update)" })
|
||||
class PatchRelationship {
|
||||
|
||||
@Test
|
||||
void globalAdmin_withoutAssumedRole_canPatchContactOfArbitraryRelationship() {
|
||||
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final var givenRelationship = givenSomeTemporaryRelationshipBessler();
|
||||
assertThat(givenRelationship.getContact().getLabel()).isEqualTo("seventh contact");
|
||||
final var givenContact = contactRepo.findContactByOptionalLabelLike("forth").get(0);
|
||||
|
||||
final var location = RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "superuser-alex@hostsharing.net")
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{
|
||||
"contactUuid": "%s"
|
||||
}
|
||||
""".formatted(givenContact.getUuid()))
|
||||
.port(port)
|
||||
.when()
|
||||
.patch("http://localhost/api/hs/office/relationships/" + givenRelationship.getUuid())
|
||||
.then().log().all().assertThat()
|
||||
.statusCode(200)
|
||||
.contentType(ContentType.JSON)
|
||||
.body("uuid", isUuidValid())
|
||||
.body("relType", is("JOINT_AGENT"))
|
||||
.body("relAnchor.tradeName", is("Erben Bessler"))
|
||||
.body("relHolder.familyName", is("Winkler"))
|
||||
.body("contact.label", is("forth contact"));
|
||||
// @formatter:on
|
||||
|
||||
// finally, the relationship is actually updated
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
assertThat(relationshipRepo.findByUuid(givenRelationship.getUuid())).isPresent().get()
|
||||
.matches(rel -> {
|
||||
assertThat(rel.getRelAnchor().getTradeName()).contains("Bessler");
|
||||
assertThat(rel.getRelHolder().getFamilyName()).contains("Winkler");
|
||||
assertThat(rel.getContact().getLabel()).isEqualTo("forth contact");
|
||||
assertThat(rel.getRelType()).isEqualTo(HsOfficeRelationshipType.JOINT_AGENT);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@Accepts({ "Relationship:D(Delete)" })
|
||||
class DeleteRelationship {
|
||||
|
||||
@Test
|
||||
void globalAdmin_withoutAssumedRole_canDeleteArbitraryRelationship() {
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final var givenRelationship = givenSomeTemporaryRelationshipBessler();
|
||||
|
||||
RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "superuser-alex@hostsharing.net")
|
||||
.port(port)
|
||||
.when()
|
||||
.delete("http://localhost/api/hs/office/relationships/" + givenRelationship.getUuid())
|
||||
.then().log().body().assertThat()
|
||||
.statusCode(204); // @formatter:on
|
||||
|
||||
// then the given relationship is gone
|
||||
assertThat(relationshipRepo.findByUuid(givenRelationship.getUuid())).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Accepts({ "Relationship:X(Access Control)" })
|
||||
void contactAdminUser_canNotDeleteRelatedRelationship() {
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final var givenRelationship = givenSomeTemporaryRelationshipBessler();
|
||||
assertThat(givenRelationship.getContact().getLabel()).isEqualTo("seventh contact");
|
||||
|
||||
RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "contact-admin@seventhcontact.example.com")
|
||||
.port(port)
|
||||
.when()
|
||||
.delete("http://localhost/api/hs/office/relationships/" + givenRelationship.getUuid())
|
||||
.then().log().body().assertThat()
|
||||
.statusCode(403); // @formatter:on
|
||||
|
||||
// then the given relationship is still there
|
||||
assertThat(relationshipRepo.findByUuid(givenRelationship.getUuid())).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Accepts({ "Relationship:X(Access Control)" })
|
||||
void normalUser_canNotDeleteUnrelatedRelationship() {
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final var givenRelationship = givenSomeTemporaryRelationshipBessler();
|
||||
assertThat(givenRelationship.getContact().getLabel()).isEqualTo("seventh contact");
|
||||
|
||||
RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "selfregistered-user-drew@hostsharing.org")
|
||||
.port(port)
|
||||
.when()
|
||||
.delete("http://localhost/api/hs/office/relationships/" + givenRelationship.getUuid())
|
||||
.then().log().body().assertThat()
|
||||
.statusCode(404); // @formatter:on
|
||||
|
||||
// then the given relationship is still there
|
||||
assertThat(relationshipRepo.findByUuid(givenRelationship.getUuid())).isNotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
private HsOfficeRelationshipEntity givenSomeTemporaryRelationshipBessler() {
|
||||
return jpaAttempt.transacted(() -> {
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final var givenAnchorPerson = personRepo.findPersonByOptionalNameLike("Erben Bessler").get(0);
|
||||
final var givenHolderPerson = personRepo.findPersonByOptionalNameLike("Winkler").get(0);
|
||||
final var givenContact = contactRepo.findContactByOptionalLabelLike("seventh contact").get(0);
|
||||
final var newRelationship = HsOfficeRelationshipEntity.builder()
|
||||
.uuid(UUID.randomUUID())
|
||||
.relType(HsOfficeRelationshipType.JOINT_AGENT)
|
||||
.relAnchor(givenAnchorPerson)
|
||||
.relHolder(givenHolderPerson)
|
||||
.contact(givenContact)
|
||||
.build();
|
||||
|
||||
toCleanup(newRelationship.getUuid());
|
||||
|
||||
return relationshipRepo.save(newRelationship);
|
||||
}).assertSuccessful().returnedValue();
|
||||
}
|
||||
|
||||
private UUID toCleanup(final UUID tempRelationshipUuid) {
|
||||
tempRelationshipUuids.add(tempRelationshipUuid);
|
||||
return tempRelationshipUuid;
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanup() {
|
||||
tempRelationshipUuids.forEach(uuid -> {
|
||||
jpaAttempt.transacted(() -> {
|
||||
context.define("superuser-alex@hostsharing.net", null);
|
||||
System.out.println("DELETING temporary relationship: " + uuid);
|
||||
final var count = relationshipRepo.deleteByUuid(uuid);
|
||||
System.out.println("DELETED temporary relationship: " + uuid + (count > 0 ? " successful" : " failed"));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,90 @@
|
||||
package net.hostsharing.hsadminng.hs.office.relationship;
|
||||
|
||||
import net.hostsharing.hsadminng.PatchUnitTestBase;
|
||||
import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactEntity;
|
||||
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeRelationshipPatchResource;
|
||||
import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonEntity;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.lenient;
|
||||
|
||||
@TestInstance(PER_CLASS)
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class HsOfficeRelationshipEntityPatcherUnitTest extends PatchUnitTestBase<
|
||||
HsOfficeRelationshipPatchResource,
|
||||
HsOfficeRelationshipEntity
|
||||
> {
|
||||
|
||||
static final UUID INITIAL_RELATIONSHIP_UUID = UUID.randomUUID();
|
||||
static final UUID PATCHED_CONTACT_UUID = UUID.randomUUID();
|
||||
|
||||
@Mock
|
||||
EntityManager em;
|
||||
|
||||
@BeforeEach
|
||||
void initMocks() {
|
||||
lenient().when(em.getReference(eq(HsOfficeContactEntity.class), any())).thenAnswer(invocation ->
|
||||
HsOfficeContactEntity.builder().uuid(invocation.getArgument(1)).build());
|
||||
}
|
||||
|
||||
final HsOfficePersonEntity givenInitialAnchorPerson = HsOfficePersonEntity.builder()
|
||||
.uuid(UUID.randomUUID())
|
||||
.build();
|
||||
final HsOfficePersonEntity givenInitialHolderPerson = HsOfficePersonEntity.builder()
|
||||
.uuid(UUID.randomUUID())
|
||||
.build();
|
||||
final HsOfficeContactEntity givenInitialContact = HsOfficeContactEntity.builder()
|
||||
.uuid(UUID.randomUUID())
|
||||
.build();
|
||||
|
||||
@Override
|
||||
protected HsOfficeRelationshipEntity newInitialEntity() {
|
||||
final var entity = new HsOfficeRelationshipEntity();
|
||||
entity.setUuid(INITIAL_RELATIONSHIP_UUID);
|
||||
entity.setRelType(HsOfficeRelationshipType.SOLE_AGENT);
|
||||
entity.setRelAnchor(givenInitialAnchorPerson);
|
||||
entity.setRelHolder(givenInitialHolderPerson);
|
||||
entity.setContact(givenInitialContact);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected HsOfficeRelationshipPatchResource newPatchResource() {
|
||||
return new HsOfficeRelationshipPatchResource();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected HsOfficeRelationshipEntityPatcher createPatcher(final HsOfficeRelationshipEntity relationship) {
|
||||
return new HsOfficeRelationshipEntityPatcher(em, relationship);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Stream<Property> propertyTestDescriptors() {
|
||||
return Stream.of(
|
||||
new JsonNullableProperty<>(
|
||||
"contact",
|
||||
HsOfficeRelationshipPatchResource::setContactUuid,
|
||||
PATCHED_CONTACT_UUID,
|
||||
HsOfficeRelationshipEntity::setContact,
|
||||
newContact(PATCHED_CONTACT_UUID))
|
||||
.notNullable()
|
||||
);
|
||||
}
|
||||
|
||||
static HsOfficeContactEntity newContact(final UUID uuid) {
|
||||
final var newContact = new HsOfficeContactEntity();
|
||||
newContact.setUuid(uuid);
|
||||
return newContact;
|
||||
}
|
||||
}
|
@@ -0,0 +1,421 @@
|
||||
package net.hostsharing.hsadminng.hs.office.relationship;
|
||||
|
||||
import net.hostsharing.hsadminng.context.Context;
|
||||
import net.hostsharing.hsadminng.context.ContextBasedTest;
|
||||
import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactRepository;
|
||||
import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonRepository;
|
||||
import net.hostsharing.hsadminng.rbac.rbacgrant.RawRbacGrantRepository;
|
||||
import net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleRepository;
|
||||
import net.hostsharing.test.Array;
|
||||
import net.hostsharing.test.JpaAttempt;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.orm.jpa.JpaSystemException;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import static net.hostsharing.hsadminng.rbac.rbacgrant.RawRbacGrantEntity.grantDisplaysOf;
|
||||
import static net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleEntity.roleNamesOf;
|
||||
import static net.hostsharing.test.JpaAttempt.attempt;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assumptions.assumeThat;
|
||||
|
||||
@DataJpaTest
|
||||
@ComponentScan(basePackageClasses = { HsOfficeRelationshipRepository.class, Context.class, JpaAttempt.class })
|
||||
@DirtiesContext
|
||||
class HsOfficeRelationshipRepositoryIntegrationTest extends ContextBasedTest {
|
||||
|
||||
@Autowired
|
||||
HsOfficeRelationshipRepository relationshipRepo;
|
||||
|
||||
@Autowired
|
||||
HsOfficePersonRepository personRepo;
|
||||
|
||||
@Autowired
|
||||
HsOfficeContactRepository contactRepo;
|
||||
|
||||
@Autowired
|
||||
RawRbacRoleRepository rawRoleRepo;
|
||||
|
||||
@Autowired
|
||||
RawRbacGrantRepository rawGrantRepo;
|
||||
|
||||
@Autowired
|
||||
EntityManager em;
|
||||
|
||||
@Autowired
|
||||
JpaAttempt jpaAttempt;
|
||||
|
||||
@MockBean
|
||||
HttpServletRequest request;
|
||||
|
||||
Set<HsOfficeRelationshipEntity> tempRelationships = new HashSet<>();
|
||||
|
||||
@Nested
|
||||
class CreateRelationship {
|
||||
|
||||
@Test
|
||||
public void testHostsharingAdmin_withoutAssumedRole_canCreateNewRelationship() {
|
||||
// given
|
||||
context("superuser-alex@hostsharing.net");
|
||||
final var count = relationshipRepo.count();
|
||||
final var givenAnchorPerson = personRepo.findPersonByOptionalNameLike("Bessler").get(0);
|
||||
final var givenHolderPerson = personRepo.findPersonByOptionalNameLike("Anita").get(0);
|
||||
final var givenContact = contactRepo.findContactByOptionalLabelLike("forth contact").get(0);
|
||||
|
||||
// when
|
||||
final var result = attempt(em, () -> {
|
||||
final var newRelationship = toCleanup(HsOfficeRelationshipEntity.builder()
|
||||
.uuid(UUID.randomUUID())
|
||||
.relAnchor(givenAnchorPerson)
|
||||
.relHolder(givenHolderPerson)
|
||||
.relType(HsOfficeRelationshipType.JOINT_AGENT)
|
||||
.contact(givenContact)
|
||||
.build());
|
||||
return relationshipRepo.save(newRelationship);
|
||||
});
|
||||
|
||||
// then
|
||||
result.assertSuccessful();
|
||||
assertThat(result.returnedValue()).isNotNull().extracting(HsOfficeRelationshipEntity::getUuid).isNotNull();
|
||||
assertThatRelationshipIsPersisted(result.returnedValue());
|
||||
assertThat(relationshipRepo.count()).isEqualTo(count + 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createsAndGrantsRoles() {
|
||||
// given
|
||||
context("superuser-alex@hostsharing.net");
|
||||
final var initialRoleNames = roleNamesOf(rawRoleRepo.findAll());
|
||||
final var initialGrantNames = grantDisplaysOf(rawGrantRepo.findAll());
|
||||
|
||||
// when
|
||||
attempt(em, () -> {
|
||||
final var givenAnchorPerson = personRepo.findPersonByOptionalNameLike("Bessler").get(0);
|
||||
final var givenHolderPerson = personRepo.findPersonByOptionalNameLike("Anita").get(0);
|
||||
final var givenContact = contactRepo.findContactByOptionalLabelLike("forth contact").get(0);
|
||||
final var newRelationship = toCleanup(HsOfficeRelationshipEntity.builder()
|
||||
.uuid(UUID.randomUUID())
|
||||
.relAnchor(givenAnchorPerson)
|
||||
.relHolder(givenHolderPerson)
|
||||
.relType(HsOfficeRelationshipType.JOINT_AGENT)
|
||||
.contact(givenContact)
|
||||
.build());
|
||||
return relationshipRepo.save(newRelationship);
|
||||
});
|
||||
|
||||
// then
|
||||
assertThat(roleNamesOf(rawRoleRepo.findAll())).containsExactlyInAnyOrder(Array.from(
|
||||
initialRoleNames,
|
||||
"hs_office_relationship#BesslerAnita-with-JOINT_AGENT-BesslerAnita.admin",
|
||||
"hs_office_relationship#BesslerAnita-with-JOINT_AGENT-BesslerAnita.owner",
|
||||
"hs_office_relationship#BesslerAnita-with-JOINT_AGENT-BesslerAnita.tenant"));
|
||||
assertThat(grantDisplaysOf(rawGrantRepo.findAll())).containsExactlyInAnyOrder(Array.fromSkippingNull(
|
||||
initialGrantNames,
|
||||
|
||||
"{ grant perm * on hs_office_relationship#BesslerAnita-with-JOINT_AGENT-BesslerAnita to role hs_office_relationship#BesslerAnita-with-JOINT_AGENT-BesslerAnita.owner by system and assume }",
|
||||
"{ grant role hs_office_relationship#BesslerAnita-with-JOINT_AGENT-BesslerAnita.owner to role global#global.admin by system and assume }",
|
||||
"{ grant role hs_office_relationship#BesslerAnita-with-JOINT_AGENT-BesslerAnita.owner to role hs_office_person#BesslerAnita.admin by system and assume }",
|
||||
|
||||
"{ grant perm edit on hs_office_relationship#BesslerAnita-with-JOINT_AGENT-BesslerAnita to role hs_office_relationship#BesslerAnita-with-JOINT_AGENT-BesslerAnita.admin by system and assume }",
|
||||
"{ grant role hs_office_relationship#BesslerAnita-with-JOINT_AGENT-BesslerAnita.admin to role hs_office_relationship#BesslerAnita-with-JOINT_AGENT-BesslerAnita.owner by system and assume }",
|
||||
|
||||
"{ grant perm view on hs_office_relationship#BesslerAnita-with-JOINT_AGENT-BesslerAnita to role hs_office_relationship#BesslerAnita-with-JOINT_AGENT-BesslerAnita.tenant by system and assume }",
|
||||
"{ grant role hs_office_relationship#BesslerAnita-with-JOINT_AGENT-BesslerAnita.tenant to role hs_office_contact#forthcontact.admin by system and assume }",
|
||||
"{ grant role hs_office_relationship#BesslerAnita-with-JOINT_AGENT-BesslerAnita.tenant to role hs_office_person#BesslerAnita.admin by system and assume }",
|
||||
|
||||
"{ grant role hs_office_contact#forthcontact.tenant to role hs_office_relationship#BesslerAnita-with-JOINT_AGENT-BesslerAnita.tenant by system and assume }",
|
||||
"{ grant role hs_office_person#BesslerAnita.tenant to role hs_office_relationship#BesslerAnita-with-JOINT_AGENT-BesslerAnita.tenant by system and assume }",
|
||||
null)
|
||||
);
|
||||
}
|
||||
|
||||
private void assertThatRelationshipIsPersisted(final HsOfficeRelationshipEntity saved) {
|
||||
final var found = relationshipRepo.findByUuid(saved.getUuid());
|
||||
assertThat(found).isNotEmpty().get().usingRecursiveComparison().isEqualTo(saved);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class FindAllRelationships {
|
||||
|
||||
@Test
|
||||
public void globalAdmin_withoutAssumedRole_canViewAllRelationshipsOfArbitraryPerson() {
|
||||
// given
|
||||
context("superuser-alex@hostsharing.net");
|
||||
final var person = personRepo.findPersonByOptionalNameLike("Smith").stream().findFirst().orElseThrow();
|
||||
|
||||
// when
|
||||
final var result = relationshipRepo.findRelationshipRelatedToPersonUuid(person.getUuid());
|
||||
|
||||
// then
|
||||
allTheseRelationshipsAreReturned(
|
||||
result,
|
||||
"rel(relAnchor='First Impressions GmbH', relType='SOLE_AGENT', relHolder='Smith, Peter', contact='first contact')",
|
||||
"rel(relAnchor='Ostfriesische Kuhhandel OHG', relType='SOLE_AGENT', relHolder='Smith, Peter', contact='third contact')",
|
||||
"rel(relAnchor='Rockshop e.K.', relType='SOLE_AGENT', relHolder='Smith, Peter', contact='second contact')");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void normalUser_canViewRelationshipsOfOwnedPersons() {
|
||||
// given:
|
||||
context("person-FirstImpressionsGmbH@example.com");
|
||||
final var person = personRepo.findPersonByOptionalNameLike("First").stream().findFirst().orElseThrow();
|
||||
|
||||
// when:
|
||||
final var result = relationshipRepo.findRelationshipRelatedToPersonUuid(person.getUuid());
|
||||
|
||||
// then:
|
||||
exactlyTheseRelationshipsAreReturned(
|
||||
result,
|
||||
"rel(relAnchor='First Impressions GmbH', relType='SOLE_AGENT', relHolder='Smith, Peter', contact='first contact')");
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class UpdateRelationship {
|
||||
|
||||
@Test
|
||||
public void hostsharingAdmin_withoutAssumedRole_canUpdateContactOfArbitraryRelationship() {
|
||||
// given
|
||||
context("superuser-alex@hostsharing.net");
|
||||
final var givenRelationship = givenSomeTemporaryRelationshipBessler(
|
||||
"Anita", "fifth contact");
|
||||
assertThatRelationshipIsVisibleForUserWithRole(
|
||||
givenRelationship,
|
||||
"hs_office_person#ErbenBesslerMelBessler.admin");
|
||||
assertThatRelationshipActuallyInDatabase(givenRelationship);
|
||||
context("superuser-alex@hostsharing.net");
|
||||
final var givenContact = contactRepo.findContactByOptionalLabelLike("sixth contact").get(0);
|
||||
|
||||
// when
|
||||
final var result = jpaAttempt.transacted(() -> {
|
||||
context("superuser-alex@hostsharing.net");
|
||||
givenRelationship.setContact(givenContact);
|
||||
return toCleanup(relationshipRepo.save(givenRelationship));
|
||||
});
|
||||
|
||||
// then
|
||||
result.assertSuccessful();
|
||||
assertThat(result.returnedValue().getContact().getLabel()).isEqualTo("sixth contact");
|
||||
assertThatRelationshipIsVisibleForUserWithRole(
|
||||
result.returnedValue(),
|
||||
"global#global.admin");
|
||||
assertThatRelationshipIsVisibleForUserWithRole(
|
||||
result.returnedValue(),
|
||||
"hs_office_contact#sixthcontact.admin");
|
||||
|
||||
assertThatRelationshipIsNotVisibleForUserWithRole(
|
||||
result.returnedValue(),
|
||||
"hs_office_contact#fifthcontact.admin");
|
||||
|
||||
relationshipRepo.deleteByUuid(givenRelationship.getUuid());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void relHolderAdmin_canNotUpdateRelatedRelationship() {
|
||||
// given
|
||||
context("superuser-alex@hostsharing.net");
|
||||
final var givenRelationship = givenSomeTemporaryRelationshipBessler(
|
||||
"Anita", "eighth");
|
||||
assertThatRelationshipIsVisibleForUserWithRole(
|
||||
givenRelationship,
|
||||
"hs_office_person#BesslerAnita.admin");
|
||||
assertThatRelationshipActuallyInDatabase(givenRelationship);
|
||||
|
||||
// when
|
||||
final var result = jpaAttempt.transacted(() -> {
|
||||
context("superuser-alex@hostsharing.net", "hs_office_person#BesslerAnita.admin");
|
||||
givenRelationship.setContact(null);
|
||||
return relationshipRepo.save(givenRelationship);
|
||||
});
|
||||
|
||||
// then
|
||||
result.assertExceptionWithRootCauseMessage(JpaSystemException.class,
|
||||
"[403] Subject ", " is not allowed to update hs_office_relationship uuid");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contactAdmin_canNotUpdateRelatedRelationship() {
|
||||
// given
|
||||
context("superuser-alex@hostsharing.net");
|
||||
final var givenRelationship = givenSomeTemporaryRelationshipBessler(
|
||||
"Anita", "ninth");
|
||||
assertThatRelationshipIsVisibleForUserWithRole(
|
||||
givenRelationship,
|
||||
"hs_office_contact#ninthcontact.admin");
|
||||
assertThatRelationshipActuallyInDatabase(givenRelationship);
|
||||
|
||||
// when
|
||||
final var result = jpaAttempt.transacted(() -> {
|
||||
context("superuser-alex@hostsharing.net", "hs_office_contact#ninthcontact.admin");
|
||||
givenRelationship.setContact(null); // TODO
|
||||
return relationshipRepo.save(givenRelationship);
|
||||
});
|
||||
|
||||
// then
|
||||
result.assertExceptionWithRootCauseMessage(JpaSystemException.class,
|
||||
"[403] Subject ", " is not allowed to update hs_office_relationship uuid");
|
||||
}
|
||||
|
||||
private void assertThatRelationshipActuallyInDatabase(final HsOfficeRelationshipEntity saved) {
|
||||
final var found = relationshipRepo.findByUuid(saved.getUuid());
|
||||
assertThat(found).isNotEmpty().get().isNotSameAs(saved).usingRecursiveComparison().isEqualTo(saved);
|
||||
}
|
||||
|
||||
private void assertThatRelationshipIsVisibleForUserWithRole(
|
||||
final HsOfficeRelationshipEntity entity,
|
||||
final String assumedRoles) {
|
||||
jpaAttempt.transacted(() -> {
|
||||
context("superuser-alex@hostsharing.net", assumedRoles);
|
||||
assertThatRelationshipActuallyInDatabase(entity);
|
||||
}).assertSuccessful();
|
||||
}
|
||||
|
||||
private void assertThatRelationshipIsNotVisibleForUserWithRole(
|
||||
final HsOfficeRelationshipEntity entity,
|
||||
final String assumedRoles) {
|
||||
jpaAttempt.transacted(() -> {
|
||||
context("superuser-alex@hostsharing.net", assumedRoles);
|
||||
final var found = relationshipRepo.findByUuid(entity.getUuid());
|
||||
assertThat(found).isEmpty();
|
||||
}).assertSuccessful();
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class DeleteByUuid {
|
||||
|
||||
@Test
|
||||
public void globalAdmin_withoutAssumedRole_canDeleteAnyRelationship() {
|
||||
// given
|
||||
context("superuser-alex@hostsharing.net", null);
|
||||
final var givenRelationship = givenSomeTemporaryRelationshipBessler(
|
||||
"Anita", "tenth");
|
||||
|
||||
// when
|
||||
final var result = jpaAttempt.transacted(() -> {
|
||||
context("superuser-alex@hostsharing.net");
|
||||
relationshipRepo.deleteByUuid(givenRelationship.getUuid());
|
||||
});
|
||||
|
||||
// then
|
||||
result.assertSuccessful();
|
||||
assertThat(jpaAttempt.transacted(() -> {
|
||||
context("superuser-fran@hostsharing.net", null);
|
||||
return relationshipRepo.findByUuid(givenRelationship.getUuid());
|
||||
}).assertSuccessful().returnedValue()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contactUser_canViewButNotDeleteTheirRelatedRelationship() {
|
||||
// given
|
||||
context("superuser-alex@hostsharing.net", null);
|
||||
final var givenRelationship = givenSomeTemporaryRelationshipBessler(
|
||||
"Anita", "eleventh");
|
||||
|
||||
// when
|
||||
final var result = jpaAttempt.transacted(() -> {
|
||||
context("contact-admin@eleventhcontact.example.com");
|
||||
assertThat(relationshipRepo.findByUuid(givenRelationship.getUuid())).isPresent();
|
||||
relationshipRepo.deleteByUuid(givenRelationship.getUuid());
|
||||
});
|
||||
|
||||
// then
|
||||
result.assertExceptionWithRootCauseMessage(
|
||||
JpaSystemException.class,
|
||||
"[403] Subject ", " not allowed to delete hs_office_relationship");
|
||||
assertThat(jpaAttempt.transacted(() -> {
|
||||
context("superuser-alex@hostsharing.net");
|
||||
return relationshipRepo.findByUuid(givenRelationship.getUuid());
|
||||
}).assertSuccessful().returnedValue()).isPresent(); // still there
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deletingARelationshipAlsoDeletesRelatedRolesAndGrants() {
|
||||
// given
|
||||
context("superuser-alex@hostsharing.net");
|
||||
final var initialRoleNames = Array.from(roleNamesOf(rawRoleRepo.findAll()));
|
||||
final var initialGrantNames = Array.from(grantDisplaysOf(rawGrantRepo.findAll()));
|
||||
final var givenRelationship = givenSomeTemporaryRelationshipBessler(
|
||||
"Anita", "twelfth");
|
||||
assertThat(rawRoleRepo.findAll().size()).as("unexpected number of roles created")
|
||||
.isEqualTo(initialRoleNames.length + 3);
|
||||
assertThat(rawGrantRepo.findAll().size()).as("unexpected number of grants created")
|
||||
.isEqualTo(initialGrantNames.length + 12);
|
||||
|
||||
// when
|
||||
final var result = jpaAttempt.transacted(() -> {
|
||||
context("superuser-alex@hostsharing.net");
|
||||
return relationshipRepo.deleteByUuid(givenRelationship.getUuid());
|
||||
});
|
||||
|
||||
// then
|
||||
result.assertSuccessful();
|
||||
assertThat(result.returnedValue()).isEqualTo(1);
|
||||
assertThat(roleNamesOf(rawRoleRepo.findAll())).containsExactlyInAnyOrder(initialRoleNames);
|
||||
assertThat(grantDisplaysOf(rawGrantRepo.findAll())).containsExactlyInAnyOrder(initialGrantNames);
|
||||
}
|
||||
}
|
||||
|
||||
private HsOfficeRelationshipEntity givenSomeTemporaryRelationshipBessler(final String holderPerson, final String contact) {
|
||||
return jpaAttempt.transacted(() -> {
|
||||
context("superuser-alex@hostsharing.net");
|
||||
final var givenAnchorPerson = personRepo.findPersonByOptionalNameLike("Erben Bessler").get(0);
|
||||
final var givenHolderPerson = personRepo.findPersonByOptionalNameLike(holderPerson).get(0);
|
||||
final var givenContact = contactRepo.findContactByOptionalLabelLike(contact).get(0);
|
||||
final var newRelationship = HsOfficeRelationshipEntity.builder()
|
||||
.uuid(UUID.randomUUID())
|
||||
.relType(HsOfficeRelationshipType.JOINT_AGENT)
|
||||
.relAnchor(givenAnchorPerson)
|
||||
.relHolder(givenHolderPerson)
|
||||
.contact(givenContact)
|
||||
.build();
|
||||
|
||||
toCleanup(newRelationship);
|
||||
|
||||
return relationshipRepo.save(newRelationship);
|
||||
}).assertSuccessful().returnedValue();
|
||||
}
|
||||
|
||||
private HsOfficeRelationshipEntity toCleanup(final HsOfficeRelationshipEntity tempRelationship) {
|
||||
tempRelationships.add(tempRelationship);
|
||||
return tempRelationship;
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanup() {
|
||||
context("superuser-alex@hostsharing.net", null);
|
||||
tempRelationships.forEach(tempRelationship -> {
|
||||
System.out.println("DELETING temporary relationship: " + tempRelationship);
|
||||
relationshipRepo.deleteByUuid(tempRelationship.getUuid());
|
||||
});
|
||||
}
|
||||
|
||||
void exactlyTheseRelationshipsAreReturned(
|
||||
final List<HsOfficeRelationshipEntity> actualResult,
|
||||
final String... relationshipNames) {
|
||||
assertThat(actualResult)
|
||||
.extracting(HsOfficeRelationshipEntity::toString)
|
||||
.containsExactlyInAnyOrder(relationshipNames);
|
||||
}
|
||||
|
||||
void allTheseRelationshipsAreReturned(
|
||||
final List<HsOfficeRelationshipEntity> actualResult,
|
||||
final String... relationshipNames) {
|
||||
assertThat(actualResult)
|
||||
.extracting(HsOfficeRelationshipEntity::toString)
|
||||
.contains(relationshipNames);
|
||||
}
|
||||
}
|
@@ -38,7 +38,7 @@ public class RawRbacGrantEntity {
|
||||
@Column(name = "descendantidname", updatable = false, insertable = false)
|
||||
private String descendantIdName;
|
||||
|
||||
@Column(name = "descenantuuid", updatable = false, insertable = false)
|
||||
@Column(name = "descendantuuid", updatable = false, insertable = false)
|
||||
private UUID descendantUuid;
|
||||
|
||||
@Column(name = "assumed", updatable = false, insertable = false)
|
||||
|
@@ -3,6 +3,7 @@ package net.hostsharing.test;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Java has List.of(...), Set.of(...) and Map.of(...) all with varargs parameter,
|
||||
@@ -17,13 +18,19 @@ public class Array {
|
||||
|
||||
public static String[] from(final List<String> initialList, final String... additionalStrings) {
|
||||
final var resultList = new ArrayList<>(initialList);
|
||||
resultList.addAll(Arrays.asList(additionalStrings));
|
||||
resultList.addAll(Arrays.stream(additionalStrings).toList());
|
||||
return resultList.toArray(String[]::new);
|
||||
}
|
||||
|
||||
public static String[] fromSkippingNull(final List<String> initialList, final String... additionalStrings) {
|
||||
final var resultList = new ArrayList<>(initialList);
|
||||
resultList.addAll(Arrays.stream(additionalStrings).filter(Objects::nonNull).toList());
|
||||
return resultList.toArray(String[]::new);
|
||||
}
|
||||
|
||||
public static String[] from(final String[] initialStrings, final String... additionalStrings) {
|
||||
final var resultList = Arrays.asList(initialStrings);
|
||||
resultList.addAll(Arrays.asList(additionalStrings));
|
||||
resultList.addAll(Arrays.stream(additionalStrings).toList());
|
||||
return resultList.toArray(String[]::new);
|
||||
}
|
||||
|
||||
|
@@ -124,7 +124,7 @@ public class JpaAttempt {
|
||||
public void assertExceptionWithRootCauseMessage(
|
||||
final Class<? extends RuntimeException> expectedExceptionClass,
|
||||
final String... expectedRootCauseMessages) {
|
||||
assertThat(wasSuccessful()).isFalse();
|
||||
assertThat(wasSuccessful()).as("wasSuccessful").isFalse();
|
||||
final String firstRootCauseMessageLine = firstRootCauseMessageLineOf(caughtException(expectedExceptionClass));
|
||||
for (String expectedRootCauseMessage : expectedRootCauseMessages) {
|
||||
assertThat(firstRootCauseMessageLine).contains(expectedRootCauseMessage);
|
||||
|
Reference in New Issue
Block a user