You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by "vttranlina (via GitHub)" <gi...@apache.org> on 2023/02/28 11:49:17 UTC

[GitHub] [james-project] vttranlina commented on a diff in pull request #1470: JAMES-3893 Add a WebAdmin API allowing listing user default identity

vttranlina commented on code in PR #1470:
URL: https://github.com/apache/james-project/pull/1470#discussion_r1119945957


##########
server/protocols/webadmin/webadmin-jmap/src/test/java/org/apache/james/webadmin/data/jmap/UserIdentitiesRoutesTest.java:
##########
@@ -0,0 +1,293 @@
+/****************************************************************
+ * 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.data.jmap;
+
+import static io.restassured.RestAssured.given;
+import static io.restassured.RestAssured.when;
+import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+
+import java.util.List;
+import java.util.Optional;
+
+import org.apache.james.core.MailAddress;
+import org.apache.james.core.Username;
+import org.apache.james.jmap.api.identity.DefaultIdentitySupplier;
+import org.apache.james.jmap.api.identity.IdentityCreationRequest;
+import org.apache.james.jmap.api.identity.IdentityRepository;
+import org.apache.james.jmap.api.model.EmailAddress;
+import org.apache.james.jmap.api.model.Identity;
+import org.apache.james.jmap.memory.identity.MemoryCustomIdentityDAO;
+import org.apache.james.json.DTOConverter;
+import org.apache.james.mime4j.dom.address.Mailbox;
+import org.apache.james.mime4j.dom.address.MailboxList;
+import org.apache.james.task.Hostname;
+import org.apache.james.task.MemoryTaskManager;
+import org.apache.james.webadmin.WebAdminServer;
+import org.apache.james.webadmin.WebAdminUtils;
+import org.apache.james.webadmin.routes.TasksRoutes;
+import org.apache.james.webadmin.utils.JsonTransformer;
+import org.eclipse.jetty.http.HttpStatus;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import io.restassured.RestAssured;
+import net.javacrumbs.jsonunit.core.Option;
+import reactor.core.publisher.Mono;
+import reactor.core.scala.publisher.SMono;
+import scala.jdk.javaapi.CollectionConverters;
+
+class UserIdentitiesRoutesTest {
+
+    private static final Username BOB = Username.of("bob@domain.tld");
+    private static final String BASE_PATH = "/users";
+    private static final String GET_IDENTITIES_USERS_PATH = "/%s/identities";
+    private WebAdminServer webAdminServer;
+    private IdentityRepository identityRepository;
+    private DefaultIdentitySupplier identityFactory;
+
+    @BeforeEach
+    void setUp() {
+        MemoryTaskManager taskManager = new MemoryTaskManager(new Hostname("foo"));
+        identityFactory = mock(DefaultIdentitySupplier.class);
+        Mockito.when(identityFactory.userCanSendFrom(any(), any())).thenReturn(SMono.just(true).hasElement());
+
+        identityRepository = new IdentityRepository(new MemoryCustomIdentityDAO(), identityFactory);
+
+        JsonTransformer jsonTransformer = new JsonTransformer();
+        TasksRoutes tasksRoutes = new TasksRoutes(taskManager, jsonTransformer, DTOConverter.of(UploadCleanupTaskAdditionalInformationDTO.SERIALIZATION_MODULE));
+        UserIdentityRoutes userIdentityRoutes = new UserIdentityRoutes(identityRepository, new JsonTransformer());
+
+        webAdminServer = WebAdminUtils.createWebAdminServer(userIdentityRoutes, tasksRoutes).start();
+        RestAssured.requestSpecification = WebAdminUtils.buildRequestSpecification(webAdminServer)
+            .setBasePath(BASE_PATH)
+            .build();
+    }
+
+    @AfterEach
+    void stop() {
+        webAdminServer.destroy();
+    }
+
+    @Test
+    void listIdentitiesShouldReturnBothCustomAndServerSetIdentities() throws Exception {
+        // identity: server set
+        Mockito.when(identityFactory.listIdentities(BOB))
+            .thenReturn(CollectionConverters.asScala(List.of(UserIdentitiesHelper.IDENTITY1())).toList());
+
+        IdentityCreationRequest creationRequest = IdentityCreationRequest.fromJava(
+            BOB.asMailAddress(),
+            Optional.of("identity name 1"),
+            Optional.of(EmailAddress.from(new MailboxList(
+                new Mailbox("replyTo1", "james.org"),
+                new Mailbox("replyTo2", "james.org")))),
+            Optional.of(EmailAddress.from(new MailboxList(
+                new Mailbox("bcc1", "james.org"),
+                new Mailbox("bcc2", "james.org")))),
+            Optional.of(1),
+            Optional.of("textSignature 1"),
+            Optional.of("htmlSignature 1"));
+
+        // identity: custom
+        Mono.from(identityRepository.save(BOB, creationRequest)).block();
+
+        String response = when()
+            .get(String.format(GET_IDENTITIES_USERS_PATH, BOB.asString()))
+        .then()
+            .statusCode(HttpStatus.OK_200)
+            .contentType(io.restassured.http.ContentType.JSON)
+            .extract()
+            .body()
+            .asString();
+
+        String expectedResponse = "[" +
+            "    {" +
+            "        \"name\": \"identity name 1\"," +
+            "        \"email\": \"bob@domain.tld\"," +
+            "        \"id\": \"${json-unit.ignore}\"," +
+            "        \"mayDelete\": true," +
+            "        \"textSignature\": \"textSignature 1\"," +
+            "        \"htmlSignature\": \"htmlSignature 1\"," +
+            "        \"sortOrder\": 1," +
+            "        \"bcc\": [" +
+            "            {" +
+            "                \"name\": null," +
+            "                \"email\": \"bcc1@james.org\"" +
+            "            }," +
+            "            {" +
+            "                \"name\": null," +
+            "                \"email\": \"bcc2@james.org\"" +
+            "            }" +
+            "        ]," +
+            "        \"replyTo\": [" +
+            "            {" +
+            "                \"name\": null," +
+            "                \"email\": \"replyTo1@james.org\"" +
+            "            }," +
+            "            {" +
+            "                \"name\": null," +
+            "                \"email\": \"replyTo2@james.org\"" +
+            "            }" +
+            "        ]" +
+            "    }," +
+            "    {" +
+            "        \"name\": \"base name\"," +
+            "        \"email\": \"bob@domain.tld\"," +
+            "        \"id\": \"${json-unit.ignore}\"," +
+            "        \"mayDelete\": false," +
+            "        \"textSignature\": \"text signature base\"," +
+            "        \"htmlSignature\": \"html signature base\"," +
+            "        \"sortOrder\": 100," +
+            "        \"bcc\": [" +
+            "            {" +
+            "                \"name\": \"My Boss bcc 1\"," +
+            "                \"email\": \"boss_bcc_1@domain.tld\"" +
+            "            }" +
+            "        ]," +
+            "        \"replyTo\": [" +
+            "            {" +
+            "                \"name\": \"My Boss 1\"," +
+            "                \"email\": \"boss1@domain.tld\"" +
+            "            }" +
+            "        ]" +
+            "    }" +
+            "]";
+        assertThatJson(response)
+            .when(Option.IGNORING_ARRAY_ORDER)
+            .isEqualTo(expectedResponse);
+    }
+
+    @Test
+    void listIdentitiesShouldSupportDefaultParam() throws Exception {

Review Comment:
   > highest priority
   
   the ticket writes down: `smallest integer`, not `highest`
   
   > How about a test to ensure that GIVEN some user identities with different sortOrder
   
   in this test, I use 2 creation requests: one sortOrder =1, and one sortOrder =2 ,
   then the default identity return entry with sortOrder=1,
   IMO it is enough 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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