james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [james-project] 06/06: JAMES-2729 DomainMapping validating source domain existence
Date Mon, 29 Apr 2019 12:45:39 GMT
This is an automated email from the ASF dual-hosted git repository.

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

commit be5147d86fbad0980b5ca85f2b00918966cc624d
Author: Tran Tien Duc <dtran@linagora.com>
AuthorDate: Tue Apr 23 11:53:36 2019 +0700

    JAMES-2729 DomainMapping validating source domain existence
---
 .../webadmin/routes/DomainMappingsRoutes.java      | 36 ++++++++++++++--------
 .../webadmin/routes/DomainMappingsRoutesTest.java  | 30 ++++++++++++++++--
 src/site/markdown/server/manage-webadmin.md        |  1 +
 3 files changed, 51 insertions(+), 16 deletions(-)

diff --git a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainMappingsRoutes.java
b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainMappingsRoutes.java
index 225aac4..55ec507 100644
--- a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainMappingsRoutes.java
+++ b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainMappingsRoutes.java
@@ -37,6 +37,7 @@ import javax.ws.rs.Produces;
 import org.apache.james.core.Domain;
 import org.apache.james.rrt.api.RecipientRewriteTable;
 import org.apache.james.rrt.api.RecipientRewriteTableException;
+import org.apache.james.rrt.api.SourceDomainIsNotInDomainListException;
 import org.apache.james.rrt.lib.Mapping;
 import org.apache.james.rrt.lib.MappingSource;
 import org.apache.james.rrt.lib.Mappings;
@@ -45,7 +46,6 @@ import org.apache.james.webadmin.utils.ErrorResponder;
 import org.apache.james.webadmin.utils.JsonTransformer;
 import org.eclipse.jetty.http.HttpStatus;
 
-import com.github.fge.lambdas.consumers.ThrowingBiConsumer;
 import com.github.steveash.guavate.Guavate;
 import com.google.common.annotations.VisibleForTesting;
 
@@ -101,14 +101,29 @@ public class DomainMappingsRoutes implements Routes {
     @ApiResponses(value = {
         @ApiResponse(code = HttpStatus.NO_CONTENT_204, message = "Ok"),
         @ApiResponse(code = HttpStatus.BAD_REQUEST_400, message = "Domain name is invalid"),
+        @ApiResponse(code = HttpStatus.BAD_REQUEST_400, message = "Domain in the source is
not managed by the DomainList"),
         @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
             message = "Internal server error - Something went bad on the server side.")
     })
-    public HaltException addDomainMapping(Request request, Response response) {
-        doMapping(request, recipientRewriteTable::addAliasDomainMapping);
+    public HaltException addDomainMapping(Request request, Response response) throws RecipientRewriteTableException
{
+        MappingSource mappingSource = mappingSourceFrom(request);
+        Domain destinationDomain = extractDomain(request.body());
+        addAliasDomainMapping(mappingSource, destinationDomain);
         return halt(HttpStatus.NO_CONTENT_204);
     }
 
+    private void addAliasDomainMapping(MappingSource source, Domain destinationDomain) throws
RecipientRewriteTableException {
+        try {
+            recipientRewriteTable.addAliasDomainMapping(source, destinationDomain);
+        } catch (SourceDomainIsNotInDomainListException e) {
+            throw ErrorResponder.builder()
+                .statusCode(HttpStatus.BAD_REQUEST_400)
+                .type(ErrorResponder.ErrorType.INVALID_ARGUMENT)
+                .message(e.getMessage())
+                .haltError();
+        }
+    }
+
     @DELETE
     @Path(SPECIFIC_MAPPING_PATH)
     @ApiOperation(value = "Removes domain mapping between source and destination domains.")
@@ -121,8 +136,11 @@ public class DomainMappingsRoutes implements Routes {
         @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
             message = "Internal server error - Something went bad on the server side.")
     })
-    public HaltException removeDomainMapping(Request request, Response response) {
-        doMapping(request, recipientRewriteTable::removeAliasDomainMapping);
+    public HaltException removeDomainMapping(Request request, Response response) throws RecipientRewriteTableException
{
+        MappingSource mappingSource = mappingSourceFrom(request);
+        Domain destinationDomain = extractDomain(request.body());
+
+        recipientRewriteTable.removeAliasDomainMapping(mappingSource, destinationDomain);
         return halt(HttpStatus.NO_CONTENT_204);
     }
 
@@ -176,14 +194,6 @@ public class DomainMappingsRoutes implements Routes {
         return MappingSource.fromDomain(fromDomain);
     }
 
-    private void doMapping(Request request, ThrowingBiConsumer<MappingSource, Domain>
mappingOperation) {
-        MappingSource fromDomain = mappingSourceFrom(request);
-
-        Domain toDomain = extractDomain(request.body());
-
-        mappingOperation.accept(fromDomain, toDomain);
-    }
-
     private Domain extractDomain(String domainAsString) {
         try {
             return Domain.of(domainAsString.trim());
diff --git a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainMappingsRoutesTest.java
b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainMappingsRoutesTest.java
index 16f8bd7..313f9a2 100644
--- a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainMappingsRoutesTest.java
+++ b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainMappingsRoutesTest.java
@@ -31,6 +31,8 @@ import static org.assertj.core.api.Assertions.entry;
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.Matchers.isEmptyString;
 import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.when;
 
@@ -39,8 +41,8 @@ import java.util.Map;
 import java.util.function.Function;
 
 import org.apache.james.core.Domain;
+import org.apache.james.domainlist.api.DomainList;
 import org.apache.james.metrics.logger.DefaultMetricFactory;
-import org.apache.james.rrt.api.RecipientRewriteTable;
 import org.apache.james.rrt.api.RecipientRewriteTableException;
 import org.apache.james.rrt.lib.Mapping;
 import org.apache.james.rrt.lib.MappingSource;
@@ -52,11 +54,13 @@ import org.apache.james.webadmin.WebAdminUtils;
 import org.apache.james.webadmin.utils.ErrorResponder;
 import org.apache.james.webadmin.utils.JsonTransformer;
 import org.eclipse.jetty.http.HttpStatus;
+import org.hamcrest.Matchers;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.Assumptions;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Nested;
 import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
@@ -68,8 +72,9 @@ import io.restassured.response.Response;
 import io.restassured.specification.RequestSpecification;
 
 class DomainMappingsRoutesTest {
-    private RecipientRewriteTable recipientRewriteTable;
+    private MemoryRecipientRewriteTable recipientRewriteTable;
     private WebAdminServer webAdminServer;
+    private DomainList domainList;
 
     private void createServer(DomainMappingsRoutes domainMappingsRoutes) throws Exception
{
         webAdminServer = WebAdminUtils.createWebAdminServer(
@@ -87,7 +92,9 @@ class DomainMappingsRoutesTest {
     @BeforeEach
     void setUp() throws Exception {
         recipientRewriteTable = spy(new MemoryRecipientRewriteTable());
-
+        domainList = mock(DomainList.class);
+        recipientRewriteTable.setDomainList(domainList);
+        Mockito.when(domainList.containsDomain(any())).thenReturn(true);
         createServer(new DomainMappingsRoutes(recipientRewriteTable, new JsonTransformer()));
     }
 
@@ -321,6 +328,23 @@ class DomainMappingsRoutesTest {
 
     @Nested
     class IllegalInputs {
+
+        @Test
+        void addMappingContainingSourceDomainNotInDomainListShouldReturnBadRequest() throws
Exception {
+            doReturn(false)
+                .when(domainList).containsDomain(any());
+
+            given()
+                .body("destination.tld")
+            .when()
+                .put("not-managed-domain.tld")
+            .then()
+                .statusCode(HttpStatus.BAD_REQUEST_400)
+                .body("statusCode", Matchers.is(400))
+                .body("type", Matchers.is(ErrorResponder.ErrorType.INVALID_ARGUMENT.getType()))
+                .body("message", is("Source domain 'not-managed-domain.tld' is not managed
by the domainList"));
+        }
+
         @Test
         void addDomainMappingShouldRespondWithNotFound() {
             when()
diff --git a/src/site/markdown/server/manage-webadmin.md b/src/site/markdown/server/manage-webadmin.md
index f6ce1cc..05f87b5 100644
--- a/src/site/markdown/server/manage-webadmin.md
+++ b/src/site/markdown/server/manage-webadmin.md
@@ -1440,6 +1440,7 @@ Response codes:
 
  - 200: OK
  - 400: The `fromDomain` resource name is invalid
+ - 400: The `fromDomain` is not managed by the DomainList
  - 404: The `fromDomain` resource name is not found
 
 ### Adding a domain mapping


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


Mime
View raw message