Repository: fractal-analytics-platform/fractal-client (language: python)

Issue:
In `fractal user` commands, use `--batch` to only return the ID in `register` and `whoami` commands



Discussion / hints:

Fix patch:
diff --git a/fractal/cmd/__init__.py b/fractal/cmd/__init__.py
index 13050d422..ea89bbe38 100644
--- a/fractal/cmd/__init__.py
+++ b/fractal/cmd/__init__.py
@@ -186,9 +186,11 @@ async def version(client: AsyncClient, **kwargs) -> PrintInterface:
     )
 
 
-async def user(client: AuthClient, subcmd: str, **kwargs) -> BaseInterface:
+async def user(
+    client: AuthClient, subcmd: str, batch: bool = False, **kwargs
+) -> BaseInterface:
     if subcmd == "register":
-        iface = await user_register(client, **kwargs)
+        iface = await user_register(client, batch=batch, **kwargs)
     elif subcmd == "list":
         iface = await user_list(client, **kwargs)
     elif subcmd == "show":
@@ -198,7 +200,7 @@ async def user(client: AuthClient, subcmd: str, **kwargs) -> BaseInterface:
     elif subcmd == "delete":
         iface = await user_delete(client, **kwargs)
     elif subcmd == "whoami":
-        iface = await user_whoami(client, **kwargs)
+        iface = await user_whoami(client, batch=batch, **kwargs)
     else:
         raise NoCommandError(f"Command user {subcmd} not found")
 
diff --git a/fractal/cmd/_user.py b/fractal/cmd/_user.py
index c8164969f..cbd5aec90 100644
--- a/fractal/cmd/_user.py
+++ b/fractal/cmd/_user.py
@@ -17,6 +17,7 @@ async def user_register(
     new_password: Optional[str] = None,
     slurm_user: Optional[str] = None,
     superuser: bool = False,
+    batch: bool = False,
     **kwargs,
 ) -> Union[RichJsonInterface, PrintInterface]:
 
@@ -32,8 +33,7 @@ async def user_register(
         if new_password == confirm_new_password:
             new_user.password = new_password
         else:
-            iface = PrintInterface(retcode=1, data="Passwords do not match.")
-            return iface
+            return PrintInterface(retcode=1, data="Passwords do not match.")
 
     res = await client.post(
         f"{settings.FRACTAL_SERVER}/auth/register",
@@ -49,9 +49,10 @@ async def user_register(
         )
         data = check_response(res, expected_status_code=200, coerce=UserRead)
 
-    iface = RichJsonInterface(retcode=0, data=data.dict())
-
-    return iface
+    if batch:
+        return PrintInterface(retcode=0, data=data.id)
+    else:
+        return RichJsonInterface(retcode=0, data=data.dict())
 
 
 async def user_list(client: AuthClient, **kwargs) -> RichJsonInterface:
@@ -123,8 +124,13 @@ async def user_delete(
     return PrintInterface(retcode=0, data="")
 
 
-async def user_whoami(client: AuthClient, **kwargs) -> RichJsonInterface:
+async def user_whoami(
+    client: AuthClient, batch: bool, **kwargs
+) -> Union[RichJsonInterface, PrintInterface]:
     res = await client.get(f"{settings.FRACTAL_SERVER}/auth/whoami")
     user = check_response(res, expected_status_code=200, coerce=UserRead)
 
-    return RichJsonInterface(retcode=0, data=user.dict())
+    if batch:
+        return PrintInterface(retcode=0, data=user.id)
+    else:
+        return RichJsonInterface(retcode=0, data=user.dict())


Test patch:
diff --git a/tests/test_user.py b/tests/test_user.py
index d61e3d718..edb96fbbc 100644
--- a/tests/test_user.py
+++ b/tests/test_user.py
@@ -35,6 +35,21 @@ async def test_register_as_superuser(invoke_as_superuser, is_superuser: bool):
     assert res.data["email"] == EMAIL_USER
 
 
+async def test_register_as_superuser_with_batch(invoke_as_superuser):
+    # Register a user with the --batch flag
+    res = await invoke_as_superuser(
+        f"--batch user register {EMAIL_USER} {PWD_USER}"
+    )
+    user_id = res.data
+    debug(user_id)
+    assert res.retcode == 0
+    # Check that the user exists
+    res = await invoke_as_superuser(f"user show {user_id}")
+    debug(res.data)
+    assert res.data["email"] == EMAIL_USER
+    assert res.retcode == 0
+
+
 async def test_list_as_user(invoke, register_user, caplog):
     with pytest.raises(SystemExit):
         await invoke("user list")
@@ -170,6 +185,13 @@ async def test_whoami_as_user(invoke, register_user):
     debug(res.data)
     assert res.data["email"] == environ["FRACTAL_USER"]
     assert not res.data["is_superuser"]
+    user_id = res.data["id"]
+
+    # Test user whoami with --batch flag
+    res = await invoke("--batch user whoami")
+    debug(res.data)
+    assert res.data == user_id
+    assert res.retcode == 0
 
 
 async def test_whoami_as_superuser(invoke_as_superuser):