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


The following commit(s) were added to refs/heads/master by this push:
     new 4f064d3ee3 JAMES-3885 Webadmin route to change of username - support 
`force` parameter  - do not require old user to exist (#2054)
4f064d3ee3 is described below

commit 4f064d3ee3b22a81e09c262df1524afc03eb6508
Author: vttran <vtt...@linagora.com>
AuthorDate: Wed Feb 28 02:51:11 2024 +0700

    JAMES-3885 Webadmin route to change of username - support `force` parameter 
 - do not require old user to exist (#2054)
    
    - do not require old user to exist
---
 .../docs/modules/ROOT/pages/operate/webadmin.adoc  |  2 +-
 .../webadmin/routes/UsernameChangeRoutes.java      |  3 +-
 .../webadmin/routes/UsernameChangeRoutesTest.java  | 39 ++++++++++++++++++++++
 src/site/markdown/server/manage-webadmin.md        |  2 +-
 4 files changed, 43 insertions(+), 3 deletions(-)

diff --git 
a/server/apps/distributed-app/docs/modules/ROOT/pages/operate/webadmin.adoc 
b/server/apps/distributed-app/docs/modules/ROOT/pages/operate/webadmin.adoc
index 0ff242d344..81ab4f0c00 100644
--- a/server/apps/distributed-app/docs/modules/ROOT/pages/operate/webadmin.adoc
+++ b/server/apps/distributed-app/docs/modules/ROOT/pages/operate/webadmin.adoc
@@ -683,7 +683,7 @@ Implemented migration steps are:
 Response codes:
 
 * 201: Success. Corresponding task id is returned.
-* 400: Error in the request. Details can be found in the reported error.
+* 400: Error in the request. Details can be found in the reported error. If 
you encounter the error "'oldUser' parameter should be an existing user," 
please note that this validation can be bypassed by specifying the `force` 
query parameter.
 
 The `fromStep` query parameter allows skipping previous steps, allowing to 
resume the username change from a failed step.
 
diff --git 
a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/UsernameChangeRoutes.java
 
b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/UsernameChangeRoutes.java
index f87dfecb51..b74fc450ea 100644
--- 
a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/UsernameChangeRoutes.java
+++ 
b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/UsernameChangeRoutes.java
@@ -42,6 +42,7 @@ import spark.Service;
 public class UsernameChangeRoutes implements Routes {
     private static final String OLD_USER_PARAM = "oldUser";
     private static final String NEW_USER_PARAM = "newUser";
+    private static final String FORCE_PARAM = "force";
     private static final String ROOT_PATH = "/users/:" + OLD_USER_PARAM + 
"/rename/:" + NEW_USER_PARAM;
     private static final TaskRegistrationKey RENAME = 
TaskRegistrationKey.of("rename");
 
@@ -73,7 +74,7 @@ public class UsernameChangeRoutes implements Routes {
             Username oldUser = Username.of(request.params(OLD_USER_PARAM));
             Username newUser = Username.of(request.params(NEW_USER_PARAM));
 
-            Preconditions.checkArgument(usersRepository.contains(oldUser), 
"'oldUser' parameter should be an existing user");
+            Preconditions.checkArgument(request.queryParams(FORCE_PARAM) != 
null || usersRepository.contains(oldUser), "'oldUser' parameter should be an 
existing user");
             Preconditions.checkArgument(usersRepository.contains(newUser), 
"'newUser' parameter should be an existing user");
 
             Optional<StepName> fromStep = 
Optional.ofNullable(request.queryParams("fromStep")).map(StepName::new);
diff --git 
a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/UsernameChangeRoutesTest.java
 
b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/UsernameChangeRoutesTest.java
index 26ec2cf710..34d95957ac 100644
--- 
a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/UsernameChangeRoutesTest.java
+++ 
b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/UsernameChangeRoutesTest.java
@@ -22,6 +22,7 @@ package org.apache.james.webadmin.routes;
 import static io.restassured.RestAssured.given;
 import static io.restassured.RestAssured.with;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.hamcrest.Matchers.notNullValue;
 import static org.hamcrest.core.Is.is;
 import static org.mockito.Mockito.mock;
 
@@ -191,6 +192,44 @@ class UsernameChangeRoutesTest {
                 .body("message", Matchers.is("Invalid arguments supplied in 
the user request"))
                 .body("details", Matchers.is("'oldUser' parameter should be an 
existing user"));
         }
+
+        @Test
+        void shouldAcceptUnknownSourceUserWhenForce() {
+            given()
+                .queryParam("action", "rename")
+                .queryParam("force")
+            .when()
+                .post("/users/unkn...@domain.tld/rename/" + 
NEW_USER.asString())
+            .then()
+                .statusCode(HttpStatus.CREATED_201)
+                .body("taskId", Matchers.is(notNullValue()));
+        }
+
+        @Test
+        void shouldPerformMigrationWhenUnknownSourceUserAndForce() {
+            String taskId = with()
+                .queryParam("action", "rename")
+                .queryParam("force")
+                .post("/users/unkn...@domain.tld/rename/" + 
NEW_USER.asString())
+                .jsonPath()
+                .get("taskId");
+
+            given()
+                .basePath(TasksRoutes.BASE)
+            .when()
+                .get(taskId + "/await")
+            .then()
+                .body("type", is("UsernameChangeTask"))
+                .body("status", is("completed"))
+                .body("additionalInformation.type", is("UsernameChangeTask"))
+                .body("additionalInformation.oldUser", 
is("unkn...@domain.tld"))
+                .body("additionalInformation.newUser", 
is("jessy.sm...@domain.tld"))
+                .body("additionalInformation.status.A", is("DONE"))
+                .body("additionalInformation.status.B", is("DONE"));
+
+            assertThat(behaviour1.get()).isTrue();
+            assertThat(behaviour2.get()).isTrue();
+        }
     }
 
     @Nested
diff --git a/src/site/markdown/server/manage-webadmin.md 
b/src/site/markdown/server/manage-webadmin.md
index f1bb6a26c2..ceb6e0e815 100644
--- a/src/site/markdown/server/manage-webadmin.md
+++ b/src/site/markdown/server/manage-webadmin.md
@@ -527,7 +527,7 @@ Implemented migration steps are:
 Response codes:
 
 * 201: Success. Corresponding task id is returned.
-* 400: Error in the request. Details can be found in the reported error.
+* 400: Error in the request. Details can be found in the reported error. If 
you encounter the error "'oldUser' parameter should be an existing user," 
please note that this validation can be bypassed by specifying the `force` 
query parameter.
 
 The `fromStep` query parameter allows skipping previous steps, allowing to 
resume the username change from a failed step.
 


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

Reply via email to