[
https://issues.apache.org/jira/browse/JAMES-2149?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16515304#comment-16515304
]
ASF GitHub Bot commented on JAMES-2149:
---------------------------------------
Github user chibenwa commented on a diff in the pull request:
https://github.com/apache/james-project/pull/119#discussion_r195954667
--- Diff:
server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainMappingsRoutesTest.java
---
@@ -0,0 +1,387 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+package org.apache.james.webadmin.routes;
+
+import static com.jayway.restassured.RestAssured.delete;
+import static com.jayway.restassured.RestAssured.given;
+import static com.jayway.restassured.RestAssured.put;
+import static com.jayway.restassured.RestAssured.when;
+import static org.apache.james.webadmin.WebAdminServer.NO_CONFIGURATION;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.entry;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.Matchers.isEmptyString;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.jayway.restassured.RestAssured;
+import com.jayway.restassured.filter.log.LogDetail;
+import com.jayway.restassured.http.ContentType;
+import com.jayway.restassured.response.Response;
+import com.jayway.restassured.specification.RequestSpecification;
+import org.apache.james.core.Domain;
+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.lib.Mappings;
+import org.apache.james.rrt.lib.MappingsImpl;
+import org.apache.james.rrt.memory.MemoryRecipientRewriteTable;
+import org.apache.james.webadmin.WebAdminServer;
+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.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 java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+
+class DomainMappingsRoutesTest {
+ private RecipientRewriteTable recipientRewriteTable;
+ private WebAdminServer webAdminServer;
+
+ private void createServer(DomainMappingsRoutes domainMappingsRoutes)
throws Exception {
+ webAdminServer = WebAdminUtils.createWebAdminServer(new
DefaultMetricFactory(), domainMappingsRoutes);
+ webAdminServer.configure(NO_CONFIGURATION);
+ webAdminServer.await();
+
+ RestAssured.requestSpecification =
WebAdminUtils.buildRequestSpecification(webAdminServer)
+ .setBasePath("/domainMappings")
+ .log(LogDetail.METHOD)
+ .build();
+ }
+
+ @BeforeEach
+ void setUp() throws Exception {
+ recipientRewriteTable = spy(new MemoryRecipientRewriteTable());
+
+ createServer(new DomainMappingsRoutes(recipientRewriteTable, new
JsonTransformer()));
+ }
+
+ @AfterEach
+ void tearDown() {
+ webAdminServer.destroy();
+ }
+
+ @Nested
+ class NormalBehaviour {
+
+ @Test
+ void addDomainMappingShouldRespondWithNoContent() {
+ given()
+ .body("to.com")
+ .when()
+ .put("from.com")
+ .then()
+ .statusCode(HttpStatus.NO_CONTENT_204)
+ .body(isEmptyString());
+ }
+
+ @Test
+ void getDomainMappings() throws RecipientRewriteTableException {
+ String alias1 = "to_1.com";
+ String alias2 = "to_2.com";
+ String alias3 = "to_3.com";
+
+ Domain expectedDomain = Domain.of("abc.com");
+ MappingSource mappingSource =
MappingSource.fromDomain(expectedDomain);
+
+ recipientRewriteTable.addAliasDomainMapping(mappingSource,
Domain.of(alias1));
+ recipientRewriteTable.addAliasDomainMapping(mappingSource,
Domain.of(alias2));
+ recipientRewriteTable.addAliasDomainMapping(mappingSource,
Domain.of(alias3));
+
+ Map<String, List<String>> map =
+ when()
+ .get()
+ .then()
+ .contentType(ContentType.JSON)
+ .statusCode(HttpStatus.OK_200)
+ .extract()
+ .body()
+ .jsonPath()
+ .getMap(".");
+
+ assertThat(map)
+ .containsOnly(entry(expectedDomain.name(),
ImmutableList.of(alias1, alias2, alias3)));
+ }
+
+ @Test
+ void getDomainMappingsEmptyMappingsAreFilteredOut() throws
RecipientRewriteTableException {
+ MappingSource nonEmptyMapping =
MappingSource.fromDomain(Domain.of("abc.com"));
+ MappingSource emptyMapping =
MappingSource.fromDomain(Domain.of("def.com"));
+
+ Map<MappingSource, Mappings> mappings = ImmutableMap.of(
+ nonEmptyMapping,
MappingsImpl.fromRawString("domain:a.com"),
+ emptyMapping, MappingsImpl.empty()
+ );
+
+
when(recipientRewriteTable.getAllMappings()).thenReturn(mappings);
+
+ Map<String, List<String>> map =
+ when()
+ .get()
+ .then()
+ .contentType(ContentType.JSON)
+ .statusCode(HttpStatus.OK_200)
+ .extract()
+ .body()
+ .jsonPath()
+ .getMap(".");
+
+ assertThat(map)
+ .containsKey(nonEmptyMapping.asString())
+ .doesNotContainKey(emptyMapping.asString());
+ }
+
+ @Test
+ void getDomainMappingsShouldFilterNonDomainMappings() throws
RecipientRewriteTableException {
+ MappingSource mappingSource =
MappingSource.fromDomain(Domain.of("abc.com"));
+ String address = "[email protected]";
+
+ recipientRewriteTable.addAddressMapping(mappingSource,
address);
+ recipientRewriteTable.addForwardMapping(mappingSource,
address);
+ recipientRewriteTable.addErrorMapping(mappingSource, address);
+ recipientRewriteTable.addGroupMapping(mappingSource, address);
+ recipientRewriteTable.addRegexMapping(mappingSource, address);
+
+ when()
+ .get()
+ .then()
+ .contentType(ContentType.JSON)
+ .statusCode(HttpStatus.OK_200)
+ .body(is("{}"));
+ }
+
+ @Test
+ void getDomainMappingsShouldBeEmptyByDefault() {
+ when()
+ .get()
+ .then()
+ .contentType(ContentType.JSON)
+ .statusCode(HttpStatus.OK_200)
+ .body(is("{}"));
+ }
+
+ @Test
+ void deleteDomainMappingShouldRespondWithNoContent() {
+ given()
+ .body("to.com")
+ .when()
+ .delete("from.com")
+ .then()
+ .statusCode(HttpStatus.NO_CONTENT_204)
+ .body(isEmptyString());
+ }
+
+ @Test
+ void deleteDomainMappingShouldRemoveMapping() throws
RecipientRewriteTableException {
+ MappingSource mappingSource =
MappingSource.fromDomain(Domain.of("from.com"));
+ String alias = "to.com";
+
+ recipientRewriteTable.addAliasDomainMapping(mappingSource,
Domain.of(alias));
+
+
Assumptions.assumeTrue(recipientRewriteTable.getUserDomainMappings(mappingSource)
!= null);
+
+ given()
+ .body("to.com")
+ .when()
+ .delete("from.com")
+ .then()
+ .body(isEmptyString());
+
+ assertThat(recipientRewriteTable.getAllMappings()).isEmpty();
+ }
+
+ @Test
+ void
getSpecificDomainMappingShouldRespondWithNotFoundWhenHasNoAliases() {
+ String domain = "from.com";
+
+ when()
+ .get(domain)
+ .then()
+ .contentType(ContentType.JSON)
+ .statusCode(HttpStatus.NOT_FOUND_404)
+ .body("type",
is(ErrorResponder.ErrorType.NOT_FOUND.getType()))
+ .body("statusCode", is(HttpStatus.NOT_FOUND_404))
+ .body("message", is("Cannot find mappings for " + domain));
+ }
+
+ @Test
+ void
getSpecificDomainMappingShouldRespondWithNotFoundWhenHasEmptyAliases() throws
RecipientRewriteTableException {
+ String domain = "from.com";
+
+ Map<MappingSource, Mappings> allMappings = ImmutableMap.of(
+ MappingSource.fromDomain(Domain.of(domain)),
+ MappingsImpl.empty()
+ );
+
+
when(recipientRewriteTable.getAllMappings()).thenReturn(allMappings);
+
+ when()
+ .get(domain)
+ .then()
+ .contentType(ContentType.JSON)
+ .statusCode(HttpStatus.NOT_FOUND_404)
+ .body("type",
is(ErrorResponder.ErrorType.NOT_FOUND.getType()))
+ .body("statusCode", is(HttpStatus.NOT_FOUND_404))
+ .body("message", is("Cannot find mappings for " + domain));
+ }
+
+ @Test
+ void
getSpecificDomainMappingNotDomainsMappingsShouldBeFilteredOut() throws
RecipientRewriteTableException {
+ String domain = "from.com";
+ String aliasDomain = "to.com";
+ final MappingSource mappingSource =
MappingSource.fromDomain(Domain.of(domain));
+
+ recipientRewriteTable.addRegexMapping(mappingSource,
"(.*)@localhost");
+ recipientRewriteTable.addGroupMapping(mappingSource,
"[email protected]");
+ recipientRewriteTable.addForwardMapping(mappingSource,
"[email protected]");
+ recipientRewriteTable.addErrorMapping(mappingSource,
"disabled");
+ recipientRewriteTable.addAliasDomainMapping(mappingSource,
Domain.of(aliasDomain));
+
+ List<String> body =
+ when()
+ .get(domain)
+ .then()
+ .contentType(ContentType.JSON)
+ .statusCode(HttpStatus.OK_200)
+ .extract()
+ .jsonPath()
+ .getList(".");
+
+ assertThat(body).containsOnly(aliasDomain);
+ }
+
+ @Test
+ void getSpecificDomainMappingShouldResponseWithOK() throws
RecipientRewriteTableException {
+ String domain = "abc.com";
+ String aliasDomain = "a.com";
+ MappingSource mapping =
MappingSource.fromDomain(Domain.of(domain));
+ Map<MappingSource, Mappings> mappings =
ImmutableMap.of(mapping, MappingsImpl.fromRawString("domain:" + aliasDomain));
+
+
when(recipientRewriteTable.getAllMappings()).thenReturn(mappings);
--- End diff --
Why do we need to spy here?
Can't we just add the domainMapping using
`recipientRewriteTable.addMapping(MappingSource.fromDomain(domain),
Mapping.domain(aliasDomain))` ?
(Or even better: rely on the PUT operation of this endoint ^^)
> Create domain mappings via webadmin
> -----------------------------------
>
> Key: JAMES-2149
> URL: https://issues.apache.org/jira/browse/JAMES-2149
> Project: James Server
> Issue Type: New Feature
> Components: webadmin
> Affects Versions: master
> Reporter: Tellier Benoit
> Priority: Major
> Labels: feature, newbie
>
> Nowadays, the Rewrite Table engine supports domain redirections. That is to
> say [email protected] will be rewritten as [email protected].
> However, such a feature is not exposed via webadmin.
> You will need to :
> - Create a new **DomainMappingsRoutes** in webadmin-data
> - You will expose in this routes, using directly RecipientsRewriteTable, the
> endpoitns for adding, removing and listing domain mappings.
> {code:java}
> GET /domainMappings/
> {"fromDomain1":"toDomain1", "fromDomain2": "toDomain2"}
> PUT /domainMappings/fromDomain
> "toDomain"
> DELETE /domainMappings/fromDomain
> "toDomain"
> {code}
> You will write a test class from your endpoints. See *GroupsRoutesTest*.
> Don't hesitate to ask for help on the *Gitter* chat.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]