Skip to content

Commit 8343313

Browse files
authored
Merge pull request #108 from opengisch/create_user
Add `create-user` CLI command, a test for it, and fix docs for `create_user`
2 parents 4a217c0 + dc5d95e commit 8343313

3 files changed

Lines changed: 63 additions & 7 deletions

File tree

qfieldcloud_sdk/cli.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,32 @@ def logout(ctx):
177177
log(payload["detail"])
178178

179179

180+
@cli.command()
181+
@click.argument("username")
182+
@click.argument("password")
183+
@click.argument("email")
184+
@click.option(
185+
"--exist-ok/--no-exist-ok",
186+
default=False,
187+
help="Do not fail when the user already exists. Default: False",
188+
)
189+
@click.pass_context
190+
def create_user(
191+
ctx: Context, username: str, password: str, email: str, exist_ok: bool
192+
) -> None:
193+
"""Create a new QFieldCloud user account."""
194+
195+
user = ctx.obj["client"].create_user(username, password, email, exist_ok=exist_ok)
196+
197+
if ctx.obj["format_json"]:
198+
print_json(user)
199+
else:
200+
if user is None:
201+
log(f'User "{username}" already exists.')
202+
else:
203+
log(f'Created user "{user["username"]}".')
204+
205+
180206
@cli.command()
181207
@click.pass_context
182208
def status(ctx: Context):

qfieldcloud_sdk/sdk.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ def create_user(
334334
self,
335335
username: str,
336336
password: str,
337-
email: str = "",
337+
email: str,
338338
exist_ok: bool = False,
339339
) -> Optional[Dict[str, Any]]:
340340
"""Create a new QFieldCloud user account (staff only).
@@ -343,20 +343,19 @@ def create_user(
343343
344344
Args:
345345
username: The username for the new account. Must be 3-150 characters
346-
and contain only letters, numbers, ``-`` and ``_``.
346+
and contain only letters, numbers, `-` and `_`.
347347
password: The password for the new account.
348-
email: Optional email address. When omitted, QFieldCloud defaults to
349-
``<username>@noreply.local``.
350-
exist_ok: When *True*, return ``None`` silently if the username is
348+
email: Email address.
349+
exist_ok: When *True*, return `None` silently if the username is
351350
already taken (HTTP 409) instead of raising. Defaults to False.
352351
353352
Returns:
354353
A dictionary with the public user info of the newly created account,
355-
or ``None`` when *exist_ok* is True and the user already exists.
354+
or `None` when `exist_ok` is `True` and the user already exists.
356355
357356
Raises:
358357
QfcRequestException: On any HTTP error other than 409, or on 409
359-
when *exist_ok* is False.
358+
when `exist_ok` is `False`.
360359
361360
Example:
362361
```python

tests/test_cli_client.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
from unittest import mock
23

34
from click.testing import CliRunner
45

@@ -60,3 +61,33 @@ def test_list_jobs(self):
6061
catch_exceptions=False,
6162
)
6263
self.assertEqual(result.exit_code, 0)
64+
65+
def test_create_user(self):
66+
client = mock.Mock()
67+
client.create_user.return_value = {
68+
"username": "field_mapper_42",
69+
"email": "field_mapper_42@example.com",
70+
}
71+
72+
with mock.patch("qfieldcloud_sdk.cli.sdk.Client", return_value=client):
73+
result = self.runner.invoke(
74+
cli,
75+
[
76+
"--json",
77+
"create-user",
78+
"field_mapper_42",
79+
"s3cr3t",
80+
"field_mapper_42@example.com",
81+
"--exist-ok",
82+
],
83+
catch_exceptions=False,
84+
)
85+
86+
self.assertEqual(result.exit_code, 0)
87+
client.create_user.assert_called_once_with(
88+
"field_mapper_42",
89+
"s3cr3t",
90+
"field_mapper_42@example.com",
91+
exist_ok=True,
92+
)
93+
self.assertIn('"username": "field_mapper_42"', result.output)

0 commit comments

Comments
 (0)