You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2019/11/04 11:23:51 UTC

[james-project] 15/30: JAMES-2943 Domain routes should fail upon auto-detected domain removal attempts

This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit b223e6d501f44ac6151e86b0cdf263b17c3721e2
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Thu Oct 31 09:31:46 2019 +0700

    JAMES-2943 Domain routes should fail upon auto-detected domain removal attempts
---
 .../james/webadmin/routes/DomainsRoutes.java       |  8 ++++
 .../james/webadmin/routes/DomainsRoutesTest.java   | 46 ++++++++++++++++++++--
 2 files changed, 51 insertions(+), 3 deletions(-)

diff --git a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainsRoutes.java b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainsRoutes.java
index c8c0a0ed..1aee7e9 100644
--- a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainsRoutes.java
+++ b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainsRoutes.java
@@ -32,6 +32,7 @@ import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
 
 import org.apache.james.core.Domain;
+import org.apache.james.domainlist.api.AutoDetectedDomainRemovalException;
 import org.apache.james.domainlist.api.DomainList;
 import org.apache.james.domainlist.api.DomainListException;
 import org.apache.james.rrt.api.RecipientRewriteTableException;
@@ -227,6 +228,13 @@ public class DomainsRoutes implements Routes {
             domainList.removeDomain(domain);
 
             domainAliasService.removeCorrespondingDomainAliases(domain);
+        } catch (AutoDetectedDomainRemovalException e) {
+            throw ErrorResponder.builder()
+                .statusCode(HttpStatus.BAD_REQUEST_400)
+                .type(ErrorType.INVALID_ARGUMENT)
+                .message("Can not remove domain")
+                .cause(e)
+                .haltError();
         } catch (DomainListException e) {
             LOGGER.info("{} did not exists", request.params(DOMAIN_NAME));
         }
diff --git a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainsRoutesTest.java b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainsRoutesTest.java
index 063bc79..f57070a 100644
--- a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainsRoutesTest.java
+++ b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainsRoutesTest.java
@@ -52,10 +52,11 @@ import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Nested;
 import org.junit.jupiter.api.Test;
 
+import com.google.common.collect.ImmutableList;
+
 import io.restassured.RestAssured;
 import io.restassured.http.ContentType;
 
-
 class DomainsRoutesTest {
     private static final String DOMAIN = "domain";
     private static final String ALIAS_DOMAIN = "alias.domain";
@@ -547,7 +548,7 @@ class DomainsRoutesTest {
 
                 when()
                     .get(DOMAIN + "/aliases")
-                    .then()
+                .then()
                     .contentType(ContentType.JSON)
                     .statusCode(HttpStatus.OK_200)
                     .body("source", containsInAnyOrder(ALIAS_DOMAIN));
@@ -564,7 +565,7 @@ class DomainsRoutesTest {
 
                 when()
                     .get(DOMAIN + "/aliases")
-                    .then()
+                .then()
                     .contentType(ContentType.JSON)
                     .statusCode(HttpStatus.OK_200)
                     .body("source", containsInAnyOrder(ALIAS_DOMAIN));
@@ -668,4 +669,43 @@ class DomainsRoutesTest {
 
     }
 
+    @Nested
+    class DetectedDomainHandling {
+        @BeforeEach
+        void setUp() throws Exception {
+            DNSService dnsService = mock(DNSService.class);
+            when(dnsService.getAllByName(any())).thenReturn(ImmutableList.of(InetAddress.getByName("172.45.62.13")));
+            when(dnsService.getHostName(any())).thenReturn("james.local");
+
+            MemoryDomainList domainList = new MemoryDomainList(dnsService);
+            domainList.setAutoDetectIP(true);
+            domainList.setAutoDetect(true);
+            createServer(domainList);
+        }
+
+        @Test
+        void deleteShouldFailWhenAutoDetectedDomain() {
+            when()
+                .delete("james.local")
+            .then()
+                .statusCode(HttpStatus.BAD_REQUEST_400)
+                .body("statusCode", is(HttpStatus.BAD_REQUEST_400))
+                .body("type", is("InvalidArgument"))
+                .body("message", is("Can not remove domain"))
+                .body("details", is("james.local is autodetected and cannot be removed"));
+        }
+
+        @Test
+        void deleteShouldFailWhenAutoDetectedIp() {
+            when()
+                .delete("172.45.62.13")
+            .then()
+                .statusCode(HttpStatus.BAD_REQUEST_400)
+                .body("statusCode", is(HttpStatus.BAD_REQUEST_400))
+                .body("type", is("InvalidArgument"))
+                .body("message", is("Can not remove domain"))
+                .body("details", is("172.45.62.13 is autodetected and cannot be removed"));
+        }
+    }
+
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org