From 1f05be063b6fa6bb2e8905f0c54c3c2bde8c4336 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Mon, 13 Jan 2025 17:57:32 +0100 Subject: [PATCH] refactoring, e.g. align CoopAssets+CoopShares and add HsBookingDebitorEntity.WithRoleId --- .../debitor/HsBookingDebitorEntity.java | 3 ++- .../HsOfficeCoopAssetsTransactionEntity.java | 7 +++--- .../debitor/HsOfficeDebitorController.java | 2 +- ...esTransactionControllerAcceptanceTest.java | 24 +++++++++++++++---- ...OfficeDebitorControllerAcceptanceTest.java | 3 ++- 5 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/hostsharing/hsadminng/hs/booking/debitor/HsBookingDebitorEntity.java b/src/main/java/net/hostsharing/hsadminng/hs/booking/debitor/HsBookingDebitorEntity.java index a09a33bc..f982a443 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/booking/debitor/HsBookingDebitorEntity.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/booking/debitor/HsBookingDebitorEntity.java @@ -5,6 +5,7 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; import net.hostsharing.hsadminng.errors.DisplayAs; +import net.hostsharing.hsadminng.rbac.role.WithRoleId; import net.hostsharing.hsadminng.repr.Stringify; import net.hostsharing.hsadminng.repr.Stringifyable; @@ -24,7 +25,7 @@ import static net.hostsharing.hsadminng.repr.Stringify.stringify; @NoArgsConstructor @AllArgsConstructor @DisplayAs("BookingDebitor") -public class HsBookingDebitorEntity implements Stringifyable { +public class HsBookingDebitorEntity implements Stringifyable, WithRoleId { public static final String DEBITOR_NUMBER_TAG = "D-"; diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntity.java b/src/main/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntity.java index c0c908df..4e4b1c41 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntity.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/coopassets/HsOfficeCoopAssetsTransactionEntity.java @@ -10,9 +10,9 @@ import net.hostsharing.hsadminng.errors.DisplayAs; import net.hostsharing.hsadminng.hs.office.membership.HsOfficeMembershipEntity; import net.hostsharing.hsadminng.persistence.BaseEntity; import net.hostsharing.hsadminng.rbac.generator.RbacSpec; +import net.hostsharing.hsadminng.rbac.generator.RbacSpec.SQL; import net.hostsharing.hsadminng.repr.Stringify; import net.hostsharing.hsadminng.repr.Stringifyable; -import org.hibernate.annotations.GenericGenerator; import jakarta.persistence.*; import java.io.IOException; @@ -57,8 +57,7 @@ public class HsOfficeCoopAssetsTransactionEntity implements Stringifyable, BaseE .quotedValues(false); @Id - @GeneratedValue(generator = "UUID") - @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator") + @GeneratedValue private UUID uuid; @Version @@ -141,7 +140,7 @@ public class HsOfficeCoopAssetsTransactionEntity implements Stringifyable, BaseE public static RbacSpec rbac() { return rbacViewFor("coopAssetsTransaction", HsOfficeCoopAssetsTransactionEntity.class) - .withIdentityView(RbacSpec.SQL.projection("reference")) + .withIdentityView(SQL.projection("reference")) .withUpdatableColumns("comment") .importEntityAlias("membership", HsOfficeMembershipEntity.class, usingDefaultCase(), dependsOnColumn("membershipUuid"), diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorController.java b/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorController.java index a3591237..73ede563 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorController.java @@ -197,7 +197,7 @@ public class HsOfficeDebitorController implements HsOfficeDebitorsApi { context.define(currentSubject, assumedRoles); - final var current = debitorRepo.findByUuid(debitorUuid).orElseThrow(); + final var current = debitorRepo.findByUuid(debitorUuid).orElseThrow().load(); new HsOfficeDebitorEntityPatcher(em, current).apply(body); diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionControllerAcceptanceTest.java index 55411a10..810f0a60 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/coopshares/HsOfficeCoopSharesTransactionControllerAcceptanceTest.java @@ -170,7 +170,9 @@ class HsOfficeCoopSharesTransactionControllerAcceptanceTest extends ContextBased final var givenMembership = membershipRepo.findMembershipByMemberNumber(1000101).orElseThrow(); final var location = RestAssured // @formatter:off - .given().header("current-subject", "superuser-alex@hostsharing.net").contentType(ContentType.JSON).body(""" + .given() + .header("current-subject", "superuser-alex@hostsharing.net") + .contentType(ContentType.JSON).body(""" { "membership.uuid": "%s", "transactionType": "SUBSCRIPTION", @@ -179,15 +181,29 @@ class HsOfficeCoopSharesTransactionControllerAcceptanceTest extends ContextBased "reference": "temp ref A", "comment": "just some test coop shares transaction" } - """.formatted(givenMembership.getUuid())).port(port).when().post("http://localhost/api/hs/office/coopsharestransactions").then().log().all().assertThat().statusCode(201).contentType(ContentType.JSON).body("uuid", isUuidValid()).body("", lenientlyEquals(""" + """.formatted(givenMembership.getUuid())) + .port(port) + .when() + .post("http://localhost/api/hs/office/coopsharestransactions") + .then() + .log().all() + .assertThat() + .statusCode(201) + .contentType(ContentType.JSON) + .body("uuid", isUuidValid()) + .body("", lenientlyEquals(""" { + "membership.uuid": "%s", "transactionType": "SUBSCRIPTION", "shareCount": 8, "valueDate": "2022-10-13", "reference": "temp ref A", "comment": "just some test coop shares transaction" } - """)).header("Location", startsWith("http://localhost")).extract().header("Location"); // @formatter:on + """.formatted(givenMembership.getUuid()))) + .header("Location", startsWith("http://localhost")) + .extract() + .header("Location"); // @formatter:on // finally, the new coopSharesTransaction can be accessed under the generated UUID final var newShareTxUuid = UUID.fromString(location.substring(location.lastIndexOf('/') + 1)); @@ -197,7 +213,7 @@ class HsOfficeCoopSharesTransactionControllerAcceptanceTest extends ContextBased @Test void globalAdmin_canAddCoopSharesReversalTransaction() { - context.define("superuser-alex@hostsharing.net", "global#global:ADMIN"); + context.define("superuser-alex@hostsharing.net", "rbac.global#global:ADMIN"); final var givenMembership = membershipRepo.findMembershipByMemberNumber(1000101).orElseThrow(); final var givenTransaction = jpaAttempt.transacted(() -> { // TODO.impl: introduce something like transactedAsSuperuser / transactedAs("...", ...) diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorControllerAcceptanceTest.java index c2009ce4..cd0f4105 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorControllerAcceptanceTest.java @@ -10,6 +10,7 @@ import net.hostsharing.hsadminng.hs.office.partner.HsOfficePartnerRepository; import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonRealRepository; import net.hostsharing.hsadminng.hs.office.relation.HsOfficeRelationRealEntity; import net.hostsharing.hsadminng.hs.office.relation.HsOfficeRelationRealRepository; +import net.hostsharing.hsadminng.rbac.role.RbacRoleType; import net.hostsharing.hsadminng.rbac.test.ContextBasedTestWithCleanup; import net.hostsharing.hsadminng.rbac.test.JpaAttempt; import net.hostsharing.hsadminng.config.DisableSecurityConfig; @@ -704,7 +705,7 @@ class HsOfficeDebitorControllerAcceptanceTest extends ContextBasedTestWithCleanu RestAssured // @formatter:off .given() .header("current-subject", "superuser-alex@hostsharing.net") - .header("assumed-roles", "hs_office.contact#tenthcontact:ADMIN") + .header("assumed-roles", givenDebitor.getDebitorRel().roleId(RbacRoleType.ADMIN) ) .contentType(ContentType.JSON) .body(""" {