From e19ab4d401b9ae8f8c35aafa84629404c9fd1794 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Mon, 1 Jul 2024 15:43:14 +0200 Subject: [PATCH] add tests for preparesUnixUser and revampsUnixUser --- ...HsHostingAssetEntityValidatorUnitTest.java | 45 ----------- ...UnixUserHostingAssetValidatorUnitTest.java | 74 ++++++++++++++----- 2 files changed, 57 insertions(+), 62 deletions(-) delete mode 100644 src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityValidatorUnitTest.java diff --git a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityValidatorUnitTest.java b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityValidatorUnitTest.java deleted file mode 100644 index dffea633..00000000 --- a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsHostingAssetEntityValidatorUnitTest.java +++ /dev/null @@ -1,45 +0,0 @@ -package net.hostsharing.hsadminng.hs.hosting.asset.validators; - -import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetEntity; -import org.junit.jupiter.api.Test; - -import java.util.List; -import java.util.stream.Stream; - -import static net.hostsharing.hsadminng.hs.booking.item.TestHsBookingItem.TEST_MANAGED_SERVER_BOOKING_ITEM; -import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.MANAGED_SERVER; -import static org.assertj.core.api.Assertions.assertThat; - -class HsHostingAssetEntityValidatorUnitTest { - - @Test - void validatesEntity() { - // given - final var managedServerHostingAssetEntity = HsHostingAssetEntity.builder() - .type(MANAGED_SERVER) - .identifier("vm1234") - .bookingItem(TEST_MANAGED_SERVER_BOOKING_ITEM.toBuilder().build()) - .parentAsset(HsHostingAssetEntity.builder().type(MANAGED_SERVER).build()) - .assignedToAsset(HsHostingAssetEntity.builder().type(MANAGED_SERVER).build()) - .build(); - linkBookingItem(managedServerHostingAssetEntity); - - // when - final var validator = HsHostingAssetEntityValidatorRegistry.forType(managedServerHostingAssetEntity.getType()); - final List result = Stream.concat( - validator.validateEntity(managedServerHostingAssetEntity).stream(), - validator.validateContext(managedServerHostingAssetEntity).stream() - ).toList(); - - // then - assertThat(result).containsExactlyInAnyOrder( - "'MANAGED_SERVER:vm1234.parentAsset' must be null but is set to D-1234500:test project:test project booking item", - "'MANAGED_SERVER:vm1234.assignedToAsset' must be null but is set to D-1234500:test project:test project booking item"); - } - - private static void linkBookingItem(final HsHostingAssetEntity managedServerHostingAssetEntity) { - managedServerHostingAssetEntity.getBookingItem().setRelatedHostingAsset(managedServerHostingAssetEntity); - } -} - -// FIXME: add tests for prepareProperties, validatesContext + revampProperties diff --git a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsUnixUserHostingAssetValidatorUnitTest.java b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsUnixUserHostingAssetValidatorUnitTest.java index 2d677d40..5ef61da9 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsUnixUserHostingAssetValidatorUnitTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsUnixUserHostingAssetValidatorUnitTest.java @@ -1,12 +1,14 @@ package net.hostsharing.hsadminng.hs.hosting.asset.validators; +import net.hostsharing.hsadminng.hash.LinuxEtcShadowHashGenerator; import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetEntity; import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType; import org.junit.jupiter.api.Test; -import java.util.Map; +import java.util.HashMap; import java.util.stream.Stream; +import static java.util.Map.ofEntries; import static net.hostsharing.hsadminng.hs.booking.item.TestHsBookingItem.TEST_MANAGED_SERVER_BOOKING_ITEM; import static net.hostsharing.hsadminng.hs.booking.item.TestHsBookingItem.TEST_MANAGED_WEBSPACE_BOOKING_ITEM; import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.MANAGED_WEBSPACE; @@ -22,33 +24,53 @@ class HsUnixUserHostingAssetValidatorUnitTest { .caption("some managed server") .bookingItem(TEST_MANAGED_SERVER_BOOKING_ITEM) .build(); - private HsHostingAssetEntity TEST_MANAGED_WEBSPACE_HOSTING_ASSET = HsHostingAssetEntity.builder() + private final HsHostingAssetEntity TEST_MANAGED_WEBSPACE_HOSTING_ASSET = HsHostingAssetEntity.builder() .type(MANAGED_WEBSPACE) .bookingItem(TEST_MANAGED_WEBSPACE_BOOKING_ITEM) .parentAsset(TEST_MANAGED_SERVER_HOSTING_ASSET) .identifier("abc00") - .build();; + .build(); + private final HsHostingAssetEntity GIVEN_VALID_UNIX_USER_HOSTING_ASSET = HsHostingAssetEntity.builder() + .type(UNIX_USER) + .parentAsset(TEST_MANAGED_WEBSPACE_HOSTING_ASSET) + .identifier("abc00-temp") + .caption("some valid test UnixUser") + .config(new HashMap<>(ofEntries( + entry("SSD hard quota", 50), + entry("SSD soft quota", 40), + entry("totpKey", "0x123456789abcdef01234"), + entry("password", "Hallo Computer, lass mich rein!") + ))) + .build(); + + @Test + void preparesUnixUser() { + // given + final var unixUserHostingAsset = GIVEN_VALID_UNIX_USER_HOSTING_ASSET; + final var validator = HsHostingAssetEntityValidatorRegistry.forType(unixUserHostingAsset.getType()); + + // when + LinuxEtcShadowHashGenerator.nextSalt("Ly3LbsArtL5u4EVt"); + validator.prepareProperties(unixUserHostingAsset); + + // then + assertThat(unixUserHostingAsset.getConfig()).containsExactlyInAnyOrderEntriesOf(ofEntries( + entry("SSD hard quota", 50), + entry("SSD soft quota", 40), + entry("totpKey", "0x123456789abcdef01234"), + entry("password", "$6$Ly3LbsArtL5u4EVt$i/ayIEvm0y4bjkFB6wbg8imbRIaw4mAA4gqYRVyoSkj.iIxJKS3KiRkSjP8gweNcpKL0Q0N31EadT8fCnWErL.") + )); + } @Test void validatesValidUnixUser() { // given - final var unixUserHostingAsset = HsHostingAssetEntity.builder() - .type(UNIX_USER) - .parentAsset(TEST_MANAGED_WEBSPACE_HOSTING_ASSET) - .identifier("abc00-temp") - .caption("some valid test UnixUser") - .config(Map.ofEntries( - entry("SSD hard quota", 50), - entry("SSD soft quota", 40), - entry("totpKey", "0x123456789abcdef01234"), - entry("password", "Hallo Computer, lass mich rein!") - )) - .build(); + final var unixUserHostingAsset = GIVEN_VALID_UNIX_USER_HOSTING_ASSET; final var validator = HsHostingAssetEntityValidatorRegistry.forType(unixUserHostingAsset.getType()); // when final var result = Stream.concat( - validator.validateEntity(unixUserHostingAsset).stream(), + validator.validateEntity(unixUserHostingAsset).stream(), validator.validateContext(unixUserHostingAsset).stream() ).toList(); @@ -64,7 +86,7 @@ class HsUnixUserHostingAssetValidatorUnitTest { .parentAsset(TEST_MANAGED_WEBSPACE_HOSTING_ASSET) .identifier("abc00-temp") .caption("some test UnixUser with invalid properties") - .config(Map.ofEntries( + .config(ofEntries( entry("SSD hard quota", 100), entry("SSD soft quota", 200), entry("HDD hard quota", 100), @@ -112,6 +134,24 @@ class HsUnixUserHostingAssetValidatorUnitTest { "'identifier' expected to match '^abc00$|^abc00-[a-z0-9]+$', but is 'xyz99-temp'"); } + @Test + void revampsUnixUser() { + // given + final var unixUserHostingAsset = GIVEN_VALID_UNIX_USER_HOSTING_ASSET; + final var validator = HsHostingAssetEntityValidatorRegistry.forType(unixUserHostingAsset.getType()); + + // when + LinuxEtcShadowHashGenerator.nextSalt("Ly3LbsArtL5u4EVt"); + final var result = validator.revampProperties(unixUserHostingAsset, unixUserHostingAsset.getConfig()); + + // then + assertThat(result).containsExactlyInAnyOrderEntriesOf(ofEntries( + entry("SSD hard quota", 50), + entry("SSD soft quota", 40), + entry("homedir", "/home/pacs/abc00/users/temp") + )); + } + @Test void describesItsProperties() { // given