james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [james-project] 03/06: JAMES-2729 AliasRoutes validating source domain existence
Date Mon, 29 Apr 2019 12:45:36 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 c8aed2d003a63e11dd27e11c15439f0a013bc897
Author: Tran Tien Duc <dtran@linagora.com>
AuthorDate: Tue Apr 23 10:38:15 2019 +0700

    JAMES-2729 AliasRoutes validating source domain existence
---
 .../apache/james/webadmin/routes/AliasRoutes.java  |  4 ++-
 .../james/webadmin/routes/AliasRoutesTest.java     | 36 +++++++++++++++++++---
 src/site/markdown/server/manage-webadmin.md        |  1 +
 3 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/AliasRoutes.java
b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/AliasRoutes.java
index 8a7ffd9..47a9b0a 100644
--- a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/AliasRoutes.java
+++ b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/AliasRoutes.java
@@ -38,6 +38,7 @@ import org.apache.james.rrt.api.MappingAlreadyExistsException;
 import org.apache.james.rrt.api.RecipientRewriteTable;
 import org.apache.james.rrt.api.RecipientRewriteTableException;
 import org.apache.james.rrt.api.SameSourceAndDestinationException;
+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.user.api.UsersRepository;
@@ -139,6 +140,7 @@ public class AliasRoutes implements Routes {
         @ApiResponse(code = HttpStatus.BAD_REQUEST_400, message = ALIAS_DESTINATION_ADDRESS
+ " or alias structure format is not valid"),
         @ApiResponse(code = HttpStatus.BAD_REQUEST_400, message = "The alias source exists
as an user already"),
         @ApiResponse(code = HttpStatus.BAD_REQUEST_400, message = "Source and destination
can't be the same!"),
+        @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.")
     })
@@ -156,7 +158,7 @@ public class AliasRoutes implements Routes {
             recipientRewriteTable.addAliasMapping(source, aliasSourceAddress.asString());
         } catch (MappingAlreadyExistsException e) {
             // ignore
-        } catch (SameSourceAndDestinationException e) {
+        } catch (SameSourceAndDestinationException | SourceDomainIsNotInDomainListException
e) {
             throw ErrorResponder.builder()
                 .statusCode(HttpStatus.BAD_REQUEST_400)
                 .type(ErrorResponder.ErrorType.INVALID_ARGUMENT)
diff --git a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/AliasRoutesTest.java
b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/AliasRoutesTest.java
index 84ef6cc..3f590dc 100644
--- a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/AliasRoutesTest.java
+++ b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/AliasRoutesTest.java
@@ -31,6 +31,7 @@ import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
 
 import java.util.List;
 import java.util.Map;
@@ -42,7 +43,6 @@ import org.apache.james.domainlist.api.DomainList;
 import org.apache.james.domainlist.lib.DomainListConfiguration;
 import org.apache.james.domainlist.memory.MemoryDomainList;
 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.MappingSource;
 import org.apache.james.rrt.memory.MemoryRecipientRewriteTable;
@@ -66,6 +66,7 @@ import io.restassured.http.ContentType;
 class AliasRoutesTest {
 
     private static final Domain DOMAIN = Domain.of("b.com");
+    private static final Domain ALIAS_DOMAIN = Domain.of("alias");
     public static final String BOB = "bob@" + DOMAIN.name();
     public static final String BOB_WITH_SLASH = "bob/@" + DOMAIN.name();
     public static final String BOB_WITH_ENCODED_SLASH = "bob%2F@" + DOMAIN.name();
@@ -115,7 +116,9 @@ class AliasRoutesTest {
                 .autoDetect(false)
                 .autoDetectIp(false));
             domainList.addDomain(DOMAIN);
+            domainList.addDomain(ALIAS_DOMAIN);
             MappingSourceModule module = new MappingSourceModule();
+            memoryRecipientRewriteTable.setDomainList(domainList);
 
             usersRepository = MemoryUsersRepository.withVirtualHosting();
             usersRepository.setDomainList(domainList);
@@ -408,7 +411,7 @@ class AliasRoutesTest {
             super.setUp();
             memoryRecipientRewriteTable.addErrorMapping(MappingSource.fromUser("error", DOMAIN),
"disabled");
             memoryRecipientRewriteTable.addRegexMapping(MappingSource.fromUser("regex", DOMAIN),
".*@b\\.com");
-            memoryRecipientRewriteTable.addAliasDomainMapping(MappingSource.fromDomain(Domain.of("alias")),
DOMAIN);
+            memoryRecipientRewriteTable.addAliasDomainMapping(MappingSource.fromDomain(ALIAS_DOMAIN),
DOMAIN);
         }
 
     }
@@ -416,18 +419,41 @@ class AliasRoutesTest {
     @Nested
     class ExceptionHandling {
 
-        private RecipientRewriteTable memoryRecipientRewriteTable;
+        private MemoryRecipientRewriteTable memoryRecipientRewriteTable;
+        private DomainList domainList;
 
         @BeforeEach
         void setUp() throws Exception {
-            memoryRecipientRewriteTable = mock(RecipientRewriteTable.class);
+            memoryRecipientRewriteTable = spy(new MemoryRecipientRewriteTable());
             UsersRepository userRepository = mock(UsersRepository.class);
-            DomainList domainList = mock(DomainList.class);
+            domainList = mock(DomainList.class);
+            memoryRecipientRewriteTable.setDomainList(domainList);
             Mockito.when(domainList.containsDomain(any())).thenReturn(true);
             createServer(new AliasRoutes(memoryRecipientRewriteTable, userRepository, new
JsonTransformer()));
         }
 
         @Test
+        void putAliasSourceContainingNotManagedDomainShouldReturnBadRequest() throws Exception
{
+            Mockito.when(domainList.containsDomain(any()))
+                .thenReturn(false);
+
+            Map<String, Object> errors = when()
+                .put(BOB + SEPARATOR + "sources" + SEPARATOR + "bob@not-managed-domain.tld")
+            .then()
+                .statusCode(HttpStatus.BAD_REQUEST_400)
+                .contentType(ContentType.JSON)
+                .extract()
+                .body()
+                .jsonPath()
+                .getMap(".");
+
+            assertThat(errors)
+                .containsEntry("statusCode", HttpStatus.BAD_REQUEST_400)
+                .containsEntry("type", "InvalidArgument")
+                .containsEntry("message", "Source domain 'not-managed-domain.tld' is not
managed by the domainList");
+        }
+
+        @Test
         void putMalformedUserDestinationShouldReturnBadRequest() {
             Map<String, Object> errors = when()
                 .put("not-an-address" + SEPARATOR + "sources" + SEPARATOR + BOB_ALIAS)
diff --git a/src/site/markdown/server/manage-webadmin.md b/src/site/markdown/server/manage-webadmin.md
index 92f2d12..c6bfcef 100644
--- a/src/site/markdown/server/manage-webadmin.md
+++ b/src/site/markdown/server/manage-webadmin.md
@@ -1373,6 +1373,7 @@ Response codes:
  - 400: Alias structure or member is not valid
  - 400: The alias source exists as an user already
  - 400: Source and destination can't be the same!
+ - 400: Domain in the source is not managed by the DomainList
 
 ### Removing an alias of an user
 


---------------------------------------------------------------------
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