You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by bt...@apache.org on 2020/12/15 01:53:01 UTC

[james-project] 07/07: JAMES-3468 Update Webadmin-cli user commands following Webadmin API change

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 1c80dccfa91a954652f1991ddee5862ceb84a1dc
Author: quanth <hq...@linagora.com>
AuthorDate: Mon Dec 14 10:47:57 2020 +0700

    JAMES-3468 Update Webadmin-cli user commands following Webadmin API change
---
 .../apache/james/cli/user/UserCreateCommand.java   | 37 +++++++++-
 .../org/apache/james/httpclient/UserClient.java    |  4 ++
 .../java/org/apache/james/cli/UserManageTest.java  | 82 +++++++++++++---------
 3 files changed, 88 insertions(+), 35 deletions(-)

diff --git a/server/protocols/webadmin-cli/src/main/java/org/apache/james/cli/user/UserCreateCommand.java b/server/protocols/webadmin-cli/src/main/java/org/apache/james/cli/user/UserCreateCommand.java
index f694ed3..47328a3 100644
--- a/server/protocols/webadmin-cli/src/main/java/org/apache/james/cli/user/UserCreateCommand.java
+++ b/server/protocols/webadmin-cli/src/main/java/org/apache/james/cli/user/UserCreateCommand.java
@@ -35,9 +35,13 @@ public class UserCreateCommand implements Callable<Integer> {
 
     public static final int CREATED_CODE = 204;
     public static final int BAD_REQUEST_CODE = 400;
+    public static final int CONFLICT_CODE = 409;
 
     @CommandLine.ParentCommand UserCommand userCommand;
 
+    @CommandLine.Option(names = "--force", description = "Update a user's password")
+    boolean force;
+
     @CommandLine.Parameters(description = "Username")
     String userName;
 
@@ -46,14 +50,25 @@ public class UserCreateCommand implements Callable<Integer> {
 
     @Override
     public Integer call() {
+        UserClient userClient = userCommand.fullyQualifiedURL("/users");
+        if (force) {
+            return updateAUserPassword(userClient);
+        } else {
+            return createAUser(userClient);
+        }
+    }
+
+    private Integer createAUser(UserClient userClient) {
         try {
-            UserClient userClient = userCommand.fullyQualifiedURL("/users");
             Response rs = userClient.createAUser(userName, new UserPassword(new String(password)));
             if (rs.status() == CREATED_CODE) {
                 userCommand.out.println("The user was created successfully");
                 return WebAdminCli.CLI_FINISHED_SUCCEED;
             } else if (rs.status() == BAD_REQUEST_CODE) {
-                userCommand.out.println("The user name or the payload is invalid");
+                userCommand.err.println("The user name or the payload is invalid");
+                return WebAdminCli.CLI_FINISHED_FAILED;
+            } else if (rs.status() == CONFLICT_CODE) {
+                userCommand.err.println("The user already exists");
                 return WebAdminCli.CLI_FINISHED_FAILED;
             }
             return WebAdminCli.CLI_FINISHED_FAILED;
@@ -62,4 +77,22 @@ public class UserCreateCommand implements Callable<Integer> {
             return WebAdminCli.CLI_FINISHED_FAILED;
         }
     }
+
+    private Integer updateAUserPassword(UserClient userClient) {
+        try {
+            Response rs = userClient.updateAUserPassword(userName, new UserPassword(new String(password)));
+            if (rs.status() == CREATED_CODE) {
+                userCommand.out.println("The user's password was successfully updated");
+                return WebAdminCli.CLI_FINISHED_SUCCEED;
+            } else if (rs.status() == BAD_REQUEST_CODE) {
+                userCommand.err.println("The user name or the payload is invalid");
+                return WebAdminCli.CLI_FINISHED_FAILED;
+            }
+            return WebAdminCli.CLI_FINISHED_FAILED;
+        } catch (Exception e) {
+            e.printStackTrace(userCommand.err);
+            return WebAdminCli.CLI_FINISHED_FAILED;
+        }
+    }
+
 }
diff --git a/server/protocols/webadmin-cli/src/main/java/org/apache/james/httpclient/UserClient.java b/server/protocols/webadmin-cli/src/main/java/org/apache/james/httpclient/UserClient.java
index 9e3a290..96b6924 100644
--- a/server/protocols/webadmin-cli/src/main/java/org/apache/james/httpclient/UserClient.java
+++ b/server/protocols/webadmin-cli/src/main/java/org/apache/james/httpclient/UserClient.java
@@ -38,6 +38,10 @@ public interface UserClient {
     @Headers("Content-Type: application/json")
     Response createAUser(@Param("userName") String userName, UserPassword password);
 
+    @RequestLine("PUT /{userName}?force")
+    @Headers("Content-Type: application/json")
+    Response updateAUserPassword(@Param("userName") String userName, UserPassword password);
+
     @RequestLine("DELETE /{userToBeDeleted}")
     Response deleteAUser(@Param("userToBeDeleted") String userName);
 
diff --git a/server/protocols/webadmin-cli/src/test/java/org/apache/james/cli/UserManageTest.java b/server/protocols/webadmin-cli/src/test/java/org/apache/james/cli/UserManageTest.java
index 6308bab..7fe002a 100644
--- a/server/protocols/webadmin-cli/src/test/java/org/apache/james/cli/UserManageTest.java
+++ b/server/protocols/webadmin-cli/src/test/java/org/apache/james/cli/UserManageTest.java
@@ -33,6 +33,7 @@ import org.apache.james.util.Port;
 import org.apache.james.utils.DataProbeImpl;
 import org.apache.james.utils.WebAdminGuiceProbe;
 import org.apache.james.webadmin.integration.WebadminIntegrationTestModule;
+import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.RegisterExtension;
 
@@ -49,11 +50,16 @@ public class UserManageTest {
     private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
     private final ByteArrayOutputStream errorStreamCaptor = new ByteArrayOutputStream();
     private DataProbeImpl dataProbe;
+    private Port port;
 
-    @Test
-    void userListShouldBeEmptyWhenNoUsers(GuiceJamesServer server) {
-        Port port = server.getProbe(WebAdminGuiceProbe.class).getWebAdminPort();
+    @BeforeEach
+    void setUp(GuiceJamesServer server) {
+        port = server.getProbe(WebAdminGuiceProbe.class).getWebAdminPort();
+        dataProbe = server.getProbe(DataProbeImpl.class);
+    }
 
+    @Test
+    void userListShouldBeEmptyWhenNoUsers() {
         int exitCode = WebAdminCli.executeFluent(new PrintStream(outputStreamCaptor), new PrintStream(errorStreamCaptor),
             "--url", "http://127.0.0.1:" + port.getValue(), "user", "list");
 
@@ -62,9 +68,7 @@ public class UserManageTest {
     }
 
     @Test
-    void userListShouldShowTwoAddedUser(GuiceJamesServer server) throws Exception {
-        Port port = server.getProbe(WebAdminGuiceProbe.class).getWebAdminPort();
-        dataProbe = server.getProbe(DataProbeImpl.class);
+    void userListShouldShowTwoAddedUser() throws Exception {
         dataProbe.fluent().addDomain("linagora.com")
             .addUser("hqtran@linagora.com", "123456")
             .addUser("testing@linagora.com", "123456");
@@ -77,9 +81,7 @@ public class UserManageTest {
     }
 
     @Test
-    void userCreateShouldAddValidUserSucceed(GuiceJamesServer server) throws Exception {
-        Port port = server.getProbe(WebAdminGuiceProbe.class).getWebAdminPort();
-        dataProbe = server.getProbe(DataProbeImpl.class);
+    void userCreateWithoutForceShouldAddValidUserSucceed() throws Exception {
         dataProbe.fluent().addDomain("linagora.com");
 
         int exitCode = WebAdminCli.executeFluent(new PrintStream(outputStreamCaptor), new PrintStream(errorStreamCaptor),
@@ -91,27 +93,49 @@ public class UserManageTest {
     }
 
     @Test
-    void userCreateShouldFailWithInvalidUsername(GuiceJamesServer server) throws Exception {
-        Port port = server.getProbe(WebAdminGuiceProbe.class).getWebAdminPort();
-        dataProbe = server.getProbe(DataProbeImpl.class);
-        dataProbe.fluent().addDomain("linagora.com");
-
+    void userCreateShouldFailWithInvalidUsername() throws Exception {
         int exitCode = WebAdminCli.executeFluent(new PrintStream(outputStreamCaptor), new PrintStream(errorStreamCaptor),
-            "--url", "http://127.0.0.1:" + port.getValue(), "user", "create", "hq/tran@linagora.com", "--password", "123456");
+            "--url", "http://127.0.0.1:" + port.getValue(), "user", "create", "hqtran@linagora.com", "--password", "123456");
 
         int exitCode1 = WebAdminCli.executeFluent(new PrintStream(outputStreamCaptor), new PrintStream(errorStreamCaptor),
-            "--url", "http://127.0.0.1:" + port.getValue(), "user", "create", "hqtran@google.com", "--password", "123456");
+            "--url", "http://127.0.0.1:" + port.getValue(), "user", "create", "--force", "hqtran@linagora.com", "--password", "123456");
 
         assertThat(exitCode).isEqualTo(1);
         assertThat(exitCode1).isEqualTo(1);
-        assertThat(outputStreamCaptor.toString().trim()).isEqualTo("The user name or the payload is invalid");
+        assertThat(errorStreamCaptor.toString().trim()).isEqualTo("The user name or the payload is invalid\nThe user name or the payload is invalid");
         assertThat(dataProbe.listUsers()).isEmpty();
     }
 
     @Test
-    void userDeleteWithAddedUserShouldSucceed(GuiceJamesServer server) throws Exception {
-        Port port = server.getProbe(WebAdminGuiceProbe.class).getWebAdminPort();
-        dataProbe = server.getProbe(DataProbeImpl.class);
+    void userCreateWithoutForceShouldNotAllowUpdateAUserPassword() throws Exception {
+        dataProbe.fluent().addDomain("linagora.com");
+
+        WebAdminCli.executeFluent(new PrintStream(new ByteArrayOutputStream()), new PrintStream(new ByteArrayOutputStream()),
+            "--url", "http://127.0.0.1:" + port.getValue(), "user", "create", "hqtran@linagora.com", "--password", "123456");
+
+        int exitCode = WebAdminCli.executeFluent(new PrintStream(outputStreamCaptor), new PrintStream(errorStreamCaptor),
+            "--url", "http://127.0.0.1:" + port.getValue(), "user", "create", "hqtran@linagora.com", "--password", "123457");
+
+        assertThat(exitCode).isEqualTo(1);
+        assertThat(errorStreamCaptor.toString().trim()).isEqualTo("The user already exists");
+    }
+
+    @Test
+    void userCreateWithForceShouldAllowUpdateAUserPassword() throws Exception {
+        dataProbe.fluent().addDomain("linagora.com");
+
+        WebAdminCli.executeFluent(new PrintStream(new ByteArrayOutputStream()), new PrintStream(new ByteArrayOutputStream()),
+            "--url", "http://127.0.0.1:" + port.getValue(), "user", "create", "hqtran@linagora.com", "--password", "123456");
+
+        int exitCode = WebAdminCli.executeFluent(new PrintStream(outputStreamCaptor), new PrintStream(errorStreamCaptor),
+            "--url", "http://127.0.0.1:" + port.getValue(), "user", "create", "--force", "hqtran@linagora.com", "--password", "123457");
+
+        assertThat(exitCode).isEqualTo(0);
+        assertThat(outputStreamCaptor.toString().trim()).isEqualTo("The user's password was successfully updated");
+    }
+
+    @Test
+    void userDeleteWithAddedUserShouldSucceed() throws Exception {
         dataProbe.fluent().addDomain("linagora.com")
             .addUser("hqtran@linagora.com", "123456");
 
@@ -123,10 +147,7 @@ public class UserManageTest {
     }
 
     @Test
-    void userDeleteWithNonExistingUserShouldSucceed(GuiceJamesServer server) throws Exception {
-        Port port = server.getProbe(WebAdminGuiceProbe.class).getWebAdminPort();
-        dataProbe = server.getProbe(DataProbeImpl.class);
-
+    void userDeleteWithNonExistingUserShouldSucceed() throws Exception {
         int exitCode = WebAdminCli.executeFluent(new PrintStream(outputStreamCaptor), new PrintStream(errorStreamCaptor),
             "--url", "http://127.0.0.1:" + port.getValue(), "user", "delete", "hqtran@linagora.com");
 
@@ -135,9 +156,7 @@ public class UserManageTest {
     }
 
     @Test
-    void userExistCommandWithNonExistingUserShouldFail(GuiceJamesServer server) {
-        Port port = server.getProbe(WebAdminGuiceProbe.class).getWebAdminPort();
-
+    void userExistCommandWithNonExistingUserShouldFail() {
         int exitCode = WebAdminCli.executeFluent(new PrintStream(outputStreamCaptor), new PrintStream(errorStreamCaptor),
             "--url", "http://127.0.0.1:" + port.getValue(), "user", "exist", "hqtran@linagora.com");
 
@@ -146,9 +165,7 @@ public class UserManageTest {
     }
 
     @Test
-    void userExistCommandWithInvalidUserNameShouldFail(GuiceJamesServer server) {
-        Port port = server.getProbe(WebAdminGuiceProbe.class).getWebAdminPort();
-
+    void userExistCommandWithInvalidUserNameShouldFail() {
         int exitCode = WebAdminCli.executeFluent(new PrintStream(outputStreamCaptor), new PrintStream(errorStreamCaptor),
             "--url", "http://127.0.0.1:" + port.getValue(), "user", "exist", "hqtran@@linagora.com");
 
@@ -165,9 +182,7 @@ public class UserManageTest {
     }
 
     @Test
-    void userExistCommandWithAddedUserShouldSucceed(GuiceJamesServer server) throws Exception {
-        Port port = server.getProbe(WebAdminGuiceProbe.class).getWebAdminPort();
-        dataProbe = server.getProbe(DataProbeImpl.class);
+    void userExistCommandWithAddedUserShouldSucceed() throws Exception {
         dataProbe.fluent().addDomain("linagora.com")
             .addUser("hqtran@linagora.com", "123456");
 
@@ -177,4 +192,5 @@ public class UserManageTest {
         assertThat(exitCode).isEqualTo(0);
         assertThat(outputStreamCaptor.toString().trim()).isEqualTo("hqtran@linagora.com exists");
     }
+
 }
\ No newline at end of file


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