Skip to content

Commit 64d8247

Browse files
committed
feat(commands/create): send optional notification
1 parent da8cbcc commit 64d8247

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

compiler_admin/commands/create.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@ def create(args: Namespace, *extra: Sequence[str]) -> int:
3030

3131
print(f"User does not exist, continuing: {account}")
3232

33-
res = CallGAMCommand(("create", "user", account, "password", "random", "changepassword", *extra))
33+
command = ("create", "user", account, "password", "random", "changepassword")
34+
35+
notify = getattr(args, "notify", None)
36+
if notify:
37+
command += ("notify", notify, "from", "[email protected]")
38+
39+
command += (*extra,)
40+
41+
res = CallGAMCommand(command)
3442

3543
res += add_user_to_group(account, GROUP_TEAM)
3644

compiler_admin/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ def _subcmd(name, help, add_username_arg=True) -> argparse.ArgumentParser:
4343
init_parser.add_argument("--gam", action="store_true", help="If provided, initialize a new GAM project.")
4444
init_parser.add_argument("--gyb", action="store_true", help="If provided, initialize a new GYB project.")
4545

46-
_subcmd("create", help="Create a new user in the Compiler domain.")
46+
create_parser = _subcmd("create", help="Create a new user in the Compiler domain.")
47+
create_parser.add_argument("--notify", help="An email address to send the newly created account info.")
4748

4849
convert_parser = _subcmd("convert", help="Convert a user account to a new type.")
4950
convert_parser.add_argument(

tests/commands/test_create.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from compiler_admin.commands import RESULT_FAILURE, RESULT_SUCCESS
55
from compiler_admin.commands.create import create, __name__ as MODULE
6+
from compiler_admin.services.google import USER_HELLO
67

78

89
@pytest.fixture
@@ -50,3 +51,20 @@ def test_create_user_does_not_exists(mock_google_user_exists, mock_google_CallGA
5051
assert "password random changepassword" in call_args
5152

5253
mock_google_add_user_to_group.assert_called_once()
54+
55+
56+
def test_create_user_notify(mock_google_user_exists, mock_google_CallGAMCommand, mock_google_add_user_to_group):
57+
mock_google_user_exists.return_value = False
58+
59+
args = Namespace(username="username", notify="[email protected]")
60+
res = create(args)
61+
62+
assert res == RESULT_SUCCESS
63+
64+
mock_google_CallGAMCommand.assert_called_once()
65+
call_args = " ".join(mock_google_CallGAMCommand.call_args[0][0])
66+
assert "create user" in call_args
67+
assert "password random changepassword" in call_args
68+
assert f"notify [email protected] from {USER_HELLO}" in call_args
69+
70+
mock_google_add_user_to_group.assert_called_once()

tests/test_main.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,15 @@ def test_main_create(mock_commands_create):
5959

6060
mock_commands_create.assert_called_once()
6161
call_args = mock_commands_create.call_args.args
62-
assert Namespace(command="create", username="username") in call_args
62+
assert Namespace(command="create", username="username", notify=None) in call_args
63+
64+
65+
def test_main_create_notify(mock_commands_create):
66+
main(argv=["create", "username", "--notify", "notification"])
67+
68+
mock_commands_create.assert_called_once()
69+
call_args = mock_commands_create.call_args.args
70+
assert Namespace(command="create", username="username", notify="notification") in call_args
6371

6472

6573
def test_main_create_no_username(mock_commands_create):

0 commit comments

Comments
 (0)