From 4f49793817e56f5a75a5922ab59ef1754fa75a8d Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Thu, 12 Sep 2024 07:31:31 +0200 Subject: [PATCH] test for setup of subdomain of non-existing superdomain and improved error message for this case --- .../HsDomainSetupHostingAssetValidator.java | 14 +++++++++++--- ...HsDomainSetupHostingAssetValidatorUnitTest.java | 12 ++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainSetupHostingAssetValidator.java b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainSetupHostingAssetValidator.java index 9583e870..785424ad 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainSetupHostingAssetValidator.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainSetupHostingAssetValidator.java @@ -57,9 +57,16 @@ class HsDomainSetupHostingAssetValidator extends HostingAssetEntityValidator { if (isDnsVerificationRequiredForUnregisteredDomain(assetEntity)) { final var superDomain = superDomain(domainName); final var expectedTxtRecordValue = "Hostsharing-domain-setup-verification-code=" + getCode.get(); - final var verificationFoundInSuperDomain = superDomain.flatMap(superDomainName -> findTxtRecord( - new Dns(superDomainName).fetchRecordsOfType("TXT"), - expectedTxtRecordValue)); + final var verificationFoundInSuperDomain = superDomain.map(superDomainName -> + { + final Dns.Result superDomainDnsResult = new Dns(superDomainName).fetchRecordsOfType("TXT"); + if (superDomainDnsResult.status() != Dns.Status.SUCCESS) { + violations.add("[DNS] lookup failed for domain name '" + superDomainName + "': " + dnsResult.exception()); + } + return superDomainDnsResult; + } + ) + .flatMap(records -> findTxtRecord(records, expectedTxtRecordValue)); if (verificationFoundInSuperDomain.isEmpty()) { violations.add( "[DNS] no TXT record '" + expectedTxtRecordValue + @@ -71,6 +78,7 @@ class HsDomainSetupHostingAssetValidator extends HostingAssetEntityValidator { } case Dns.Status.INVALID_NAME: + // should not happen because we validate the domain name at booking item level violations.add("[DNS] invalid domain name '" + assetEntity.getIdentifier() + "'"); break; diff --git a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainSetupHostingAssetValidatorUnitTest.java b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainSetupHostingAssetValidatorUnitTest.java index 36cb2504..8acca8d9 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainSetupHostingAssetValidatorUnitTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/validators/HsDomainSetupHostingAssetValidatorUnitTest.java @@ -358,6 +358,12 @@ class HsDomainSetupHostingAssetValidatorUnitTest { .isRejectedWithCauseMissingVerificationIn("example.org"); } + @Test + void rejectSetupOfUnregisteredSubdomainOfUnregisteredSuperDomain() { + domainSetupFor("sub.sub.example.org").notRegistered() + .isRejectedWithCauseDomainNameNotFound("sub.example.org"); + } + @Test void acceptSetupOfUnregisteredSubdomainWithParentAssetEvenWithoutDnsVerificationInSuperDomain() { domainSetupWithParentAssetFor("sub.example.org").notRegistered() @@ -488,6 +494,12 @@ class HsDomainSetupHostingAssetValidatorUnitTest { ); } + void isRejectedWithCauseDomainNameNotFound(final String domainName) { + assertThat(validate()).contains( + "[DNS] lookup failed for domain name '" + domainName + "': javax.naming.NameNotFoundException: domain not registered" + ); + } + void isAccepted() { assertThat(validate()).isEmpty(); }