add persistsEventEntityIfDomainDnsSetupIsSupplied test

This commit is contained in:
Michael Hoennig 2024-10-02 09:58:01 +02:00
parent 7e177adff3
commit 3928c40258
3 changed files with 73 additions and 17 deletions

View File

@ -16,6 +16,7 @@ import java.net.IDN;
import java.util.List; import java.util.List;
import java.util.function.Function; import java.util.function.Function;
import static net.hostsharing.hsadminng.hs.booking.generated.api.v1.model.HsHostingAssetTypeResource.DOMAIN_DNS_SETUP;
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.DOMAIN_HTTP_SETUP; import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.DOMAIN_HTTP_SETUP;
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.DOMAIN_MBOX_SETUP; import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.DOMAIN_MBOX_SETUP;
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.DOMAIN_SMTP_SETUP; import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.DOMAIN_SMTP_SETUP;
@ -43,6 +44,7 @@ public class DomainSetupHostingAssetFactory extends HostingAssetFactory {
final var assignedToUnixUserAsset = domainHttpSetupAsset.getAssignedToAsset(); final var assignedToUnixUserAsset = domainHttpSetupAsset.getAssignedToAsset();
// TODO.legacy: don't create DNS setup as long as we need to remain support legacy web-ui etc. // TODO.legacy: don't create DNS setup as long as we need to remain support legacy web-ui etc.
assertDomainDnsSetupAssetNotSupplied(domainSetupAsset);
createDomainSubSetupAssetEntity( createDomainSubSetupAssetEntity(
domainSetupAsset, domainSetupAsset,
@ -96,6 +98,12 @@ public class DomainSetupHostingAssetFactory extends HostingAssetFactory {
return domainHttpSetupAsset; return domainHttpSetupAsset;
} }
private void assertDomainDnsSetupAssetNotSupplied(final HsHostingAssetRealEntity domainSetupAsset) {
if (getSubHostingAssetResources().stream().anyMatch(ha -> ha.getType() == DOMAIN_DNS_SETUP)) {
throw new ValidationException("domain DNS setup not allowed for legacy compatibility");
}
}
private void createDomainSubSetupAssetEntity( private void createDomainSubSetupAssetEntity(
final HsHostingAssetRealEntity domainSetupAsset, final HsHostingAssetRealEntity domainSetupAsset,
final HsHostingAssetType subAssetType, final HsHostingAssetType subAssetType,

View File

@ -21,8 +21,8 @@ abstract class HostingAssetFactory {
protected abstract HsHostingAsset create(); protected abstract HsHostingAsset create();
public String performSaveProcess() { public String performSaveProcess() {
final var newHostingAsset = create();
try { try {
final var newHostingAsset = create();
persist(newHostingAsset); persist(newHostingAsset);
return null; return null;
} catch (final Exception e) { } catch (final Exception e) {

View File

@ -26,8 +26,9 @@ import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.UNIX
import static net.hostsharing.hsadminng.mapper.PatchableMapWrapper.entry; import static net.hostsharing.hsadminng.mapper.PatchableMapWrapper.entry;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
// Tests the DomainSetupHostingAssetFactory through a HsBookingItemCreatedListener instance.
@ExtendWith(MockitoExtension.class) @ExtendWith(MockitoExtension.class)
class HsBookingItemCreatedListenerUnitTest { class DomainSetupHostingAssetFactoryUnitTest {
private final HsHostingAssetRealEntity managedWebspaceHostingAsset = HsHostingAssetRealEntity.builder() private final HsHostingAssetRealEntity managedWebspaceHostingAsset = HsHostingAssetRealEntity.builder()
.uuid(UUID.randomUUID()) .uuid(UUID.randomUUID())
@ -63,13 +64,10 @@ class HsBookingItemCreatedListenerUnitTest {
@Test @Test
void doesNotPersistEventEntityWithoutValidationErrors() { void doesNotPersistEventEntityWithoutValidationErrors() {
// given // given
final var givenBookingItem = HsBookingItemRealEntity.builder() final var givenBookingItem = createBookingItemFromResources(
.type(HsBookingItemType.DOMAIN_SETUP)
.resources(Map.ofEntries(
entry("domainName", "example.org"), entry("domainName", "example.org"),
entry("verificationCode", "just-a-fake-verification-code") entry("verificationCode", "just-a-fake-verification-code")
)) );
.build();
final var givenAssetJson = """ final var givenAssetJson = """
{ {
"identifier": "example.org", // also as default for all subAssets "identifier": "example.org", // also as default for all subAssets
@ -96,14 +94,11 @@ class HsBookingItemCreatedListenerUnitTest {
} }
@Test @Test
void persistsEventEntityIfHostingAssetVerificationFails() { void persistsEventEntityIfDomainSetupVerificationFails() {
// given // given
final var givenBookingItem = HsBookingItemRealEntity.builder() final var givenBookingItem = createBookingItemFromResources(
.type(HsBookingItemType.DOMAIN_SETUP)
.resources(Map.ofEntries(
entry("domainName", "example.org") entry("domainName", "example.org")
)) );
.build();
final var givenAssetJson = """ final var givenAssetJson = """
{ {
"identifier": "example.org", // also as default for all subAssets "identifier": "example.org", // also as default for all subAssets
@ -130,8 +125,61 @@ class HsBookingItemCreatedListenerUnitTest {
assertThat(eventEntity.getBookingItem()).isSameAs(givenBookingItem); assertThat(eventEntity.getBookingItem()).isSameAs(givenBookingItem);
assertThat(eventEntity.getAssetJson()).isEqualTo(givenAssetJson); assertThat(eventEntity.getAssetJson()).isEqualTo(givenAssetJson);
assertThat(eventEntity.getStatusMessage()).isEqualTo( assertThat(eventEntity.getStatusMessage()).isEqualTo(
"[[DNS] no TXT record 'Hostsharing-domain-setup-verification-code=null' found for domain name 'example.org' (nor in its super-domain)]"); "[[DNS] no TXT record 'Hostsharing-domain-setup-verification-code=null' found for domain name 'example.org' (nor in its super-domain)]"
);
return true; return true;
}); });
} }
@Test
void persistsEventEntityIfDomainDnsSetupIsSupplied() {
// given
final var givenBookingItem = createBookingItemFromResources(
entry("domainName", "example.org"),
entry("verificationCode", "just-a-fake-verification-code")
);
final var givenAssetJson = """
{
"identifier": "example.org", // also as default for all subAssets
"subHostingAssets": [
{
"type": "DOMAIN_HTTP_SETUP",
"assignedToAssetUuid": "{unixUserHostingAssetUuid}"
},
{
"type": "DOMAIN_DNS_SETUP",
"assignedToAssetUuid": "{managedWebspaceHostingAssetUuid}"
}
]
}
"""
.replace("{unixUserHostingAssetUuid}", unixUserHostingAsset.getUuid().toString())
.replace("{managedWebspaceHostingAssetUuid}", managedWebspaceHostingAsset.getUuid().toString());
Dns.fakeResultForDomain("example.org",
Dns.Result.fromRecords("Hostsharing-domain-setup-verification-code=just-a-fake-verification-code"));
// when
listener.onApplicationEvent(
new BookingItemCreatedAppEvent(this, givenBookingItem, givenAssetJson)
);
// then
emwFake.stream(BookingItemCreatedEventEntity.class)
.reduce(EntityManagerWrapperFake::toSingleElement)
.map(eventEntity -> {
assertThat(eventEntity.getBookingItem()).isSameAs(givenBookingItem);
assertThat(eventEntity.getAssetJson()).isEqualTo(givenAssetJson);
assertThat(eventEntity.getStatusMessage()).isEqualTo(
"domain DNS setup not allowed for legacy compatibility");
return true;
});
}
@SafeVarargs
private static HsBookingItemRealEntity createBookingItemFromResources(final Map.Entry<String, String>... givenResources) {
return HsBookingItemRealEntity.builder()
.type(HsBookingItemType.DOMAIN_SETUP)
.resources(Map.ofEntries(givenResources))
.build();
}
} }