You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by rc...@apache.org on 2020/11/17 03:37:23 UTC

[james-project] 04/06: JAMES-3400 Add user create command

This is an automated email from the ASF dual-hosted git repository.

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

commit 13383db2409b8e8b4049eb815688e8f0c32b2f35
Author: quanth <hq...@linagora.com>
AuthorDate: Wed Nov 11 17:26:12 2020 +0700

    JAMES-3400 Add user create command
---
 .../org/apache/james/cli/user/UserCommand.java     |  3 +-
 .../apache/james/cli/user/UserCreateCommand.java   | 69 ++++++++++++++++++++++
 .../org/apache/james/httpclient/UserClient.java    |  8 +++
 .../{UserClient.java => model/UserPassword.java}   | 18 +++---
 .../java/org/apache/james/cli/UserManageTest.java  | 33 +++++++++++
 5 files changed, 121 insertions(+), 10 deletions(-)

diff --git a/server/protocols/webadmin-cli/src/main/java/org/apache/james/cli/user/UserCommand.java b/server/protocols/webadmin-cli/src/main/java/org/apache/james/cli/user/UserCommand.java
index 92c27ae..fdd7da1 100644
--- a/server/protocols/webadmin-cli/src/main/java/org/apache/james/cli/user/UserCommand.java
+++ b/server/protocols/webadmin-cli/src/main/java/org/apache/james/cli/user/UserCommand.java
@@ -30,7 +30,8 @@ import picocli.CommandLine;
     name = "user",
     description = "Manage Users",
     subcommands = {
-        UserListCommand.class
+        UserListCommand.class,
+        UserCreateCommand.class
     })
 public class UserCommand implements Callable<Integer> {
 
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
new file mode 100644
index 0000000..7c0cd52
--- /dev/null
+++ b/server/protocols/webadmin-cli/src/main/java/org/apache/james/cli/user/UserCreateCommand.java
@@ -0,0 +1,69 @@
+/******************************************************************
+ * 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.cli.user;
+
+import java.util.concurrent.Callable;
+
+import org.apache.james.cli.WebAdminCli;
+import org.apache.james.httpclient.UserClient;
+import org.apache.james.httpclient.model.UserPassword;
+
+import feign.Feign;
+import feign.Response;
+import feign.jackson.JacksonEncoder;
+import picocli.CommandLine;
+
+@CommandLine.Command(
+    name = "create",
+    description = "Create a new User")
+public class UserCreateCommand implements Callable<Integer> {
+
+    public static final int CREATED_CODE = 204;
+    public static final int BAD_REQUEST_CODE = 400;
+
+    @CommandLine.ParentCommand UserCommand userCommand;
+
+    @CommandLine.Parameters(description = "Username")
+    String userName;
+
+    @CommandLine.Option(names = {"-p", "--password"}, description = "Password", arity = "0..1", interactive = true, required = true)
+    char[] password;
+
+    @Override
+    public Integer call() {
+        try {
+            UserClient userClient = Feign.builder()
+                .encoder(new JacksonEncoder())
+                .target(UserClient.class, userCommand.webAdminCli.jamesUrl + "/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");
+                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 deb2b2b..90b134b 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
@@ -22,12 +22,20 @@ package org.apache.james.httpclient;
 import java.util.List;
 
 import org.apache.james.httpclient.model.UserName;
+import org.apache.james.httpclient.model.UserPassword;
 
+import feign.Headers;
+import feign.Param;
 import feign.RequestLine;
+import feign.Response;
 
 public interface UserClient {
 
     @RequestLine("GET")
     List<UserName> getUserNameList();
 
+    @RequestLine("PUT /{userName}")
+    @Headers("Content-Type: application/json")
+    Response createAUser(@Param("userName") String userName, UserPassword password);
+
 }
\ No newline at end of file
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/model/UserPassword.java
similarity index 82%
copy from server/protocols/webadmin-cli/src/main/java/org/apache/james/httpclient/UserClient.java
copy to server/protocols/webadmin-cli/src/main/java/org/apache/james/httpclient/model/UserPassword.java
index deb2b2b..a0e0a9e 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/model/UserPassword.java
@@ -17,17 +17,17 @@
  * under the License.                                             *
  ******************************************************************/
 
-package org.apache.james.httpclient;
+package org.apache.james.httpclient.model;
 
-import java.util.List;
+import com.fasterxml.jackson.annotation.JsonProperty;
 
-import org.apache.james.httpclient.model.UserName;
+public class UserPassword {
 
-import feign.RequestLine;
+    @JsonProperty("password")
+    private String password;
 
-public interface UserClient {
+    public UserPassword(String password) {
+        this.password = password;
+    }
 
-    @RequestLine("GET")
-    List<UserName> getUserNameList();
-
-}
\ No newline at end of file
+}
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 ebfe623..6804ec3 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
@@ -75,4 +75,37 @@ public class UserManageTest {
         assertThat(exitCode).isEqualTo(0);
         assertThat(outputStreamCaptor.toString().trim().toCharArray()).containsOnly("hqtran@linagora.com".concat("\n").concat("testing@linagora.com").toCharArray());
     }
+
+    @Test
+    void userCreateShouldAddValidUserSucceed(GuiceJamesServer server) throws Exception {
+        Port port = server.getProbe(WebAdminGuiceProbe.class).getWebAdminPort();
+        dataProbe = server.getProbe(DataProbeImpl.class);
+        dataProbe.fluent().addDomain("linagora.com");
+
+        int exitCode = WebAdminCli.executeFluent(new PrintStream(outputStreamCaptor), new PrintStream(errorStreamCaptor),
+            "--url", "http://127.0.0.1:" + port.getValue(), "user", "create", "hqtran@linagora.com", "--password", "123456");
+
+        assertThat(exitCode).isEqualTo(0);
+        assertThat(outputStreamCaptor.toString().trim()).isEqualTo("The user was created successfully");
+        assertThat(dataProbe.listUsers()).containsOnly("hqtran@linagora.com");
+    }
+
+    @Test
+    void userCreateShouldFailWithInvalidUsername(GuiceJamesServer server) throws Exception {
+        Port port = server.getProbe(WebAdminGuiceProbe.class).getWebAdminPort();
+        dataProbe = server.getProbe(DataProbeImpl.class);
+        dataProbe.fluent().addDomain("linagora.com");
+
+        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");
+
+        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");
+
+        assertThat(exitCode).isEqualTo(1);
+        assertThat(exitCode1).isEqualTo(1);
+        assertThat(outputStreamCaptor.toString().trim()).isEqualTo("The user name or the payload is invalid");
+        assertThat(dataProbe.listUsers()).isEmpty();
+    }
+
 }
\ 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