Skip to content

Commit e62e971

Browse files
authoredMar 19, 2024··
Feat: option to notify on account creation (#16)
2 parents decef45 + eba15d5 commit e62e971

File tree

5 files changed

+69
-6
lines changed

5 files changed

+69
-6
lines changed
 

‎README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,14 @@ Additionally, GYB is used for Gmail backup/restore. See the [GYB Wiki](https://g
6464

6565
```bash
6666
$ compiler-admin create -h
67-
usage: compiler-admin create [-h] username [OPTIONS]
67+
usage: compiler-admin create [-h] [--notify NOTIFY] username
6868

6969
positional arguments:
70-
username The user's account name, sans domain.
70+
username A Compiler user account name, sans domain.
7171

7272
options:
73-
-h, --help show this help message and exit
73+
-h, --help show this help message and exit
74+
--notify NOTIFY An email address to send the newly created account info.
7475
```
7576

7677
Additional options are passed through to GAM, see more about [GAM user create](https://github.com/taers232c/GAMADV-XTD3/wiki/Users#create-a-user)

‎compiler_admin/commands/create.py

+9-1
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", "hello@compiler.la")
38+
39+
command += (*extra,)
40+
41+
res = CallGAMCommand(command)
3442

3543
res += add_user_to_group(account, GROUP_TEAM)
3644

‎compiler_admin/main.py

+2-1
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

+35
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,37 @@ 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="notification@example.com")
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 notification@example.com from {USER_HELLO}" in call_args
69+
70+
mock_google_add_user_to_group.assert_called_once()
71+
72+
73+
def test_create_user_extras(mock_google_user_exists, mock_google_CallGAMCommand, mock_google_add_user_to_group):
74+
mock_google_user_exists.return_value = False
75+
76+
args = Namespace(username="username")
77+
res = create(args, "extra1", "extra2")
78+
79+
assert res == RESULT_SUCCESS
80+
81+
mock_google_CallGAMCommand.assert_called_once()
82+
call_args = " ".join(mock_google_CallGAMCommand.call_args[0][0])
83+
assert "create user" in call_args
84+
assert "password random changepassword" in call_args
85+
assert "extra1 extra2" in call_args
86+
87+
mock_google_add_user_to_group.assert_called_once()

‎tests/test_main.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,25 @@ 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
71+
72+
73+
def test_main_create_extras(mock_commands_create):
74+
main(argv=["create", "username", "extra1", "extra2"])
75+
76+
mock_commands_create.assert_called_once()
77+
call_args = mock_commands_create.call_args.args
78+
assert Namespace(command="create", username="username", notify=None) in call_args
79+
assert "extra1" in call_args
80+
assert "extra2" in call_args
6381

6482

6583
def test_main_create_no_username(mock_commands_create):

0 commit comments

Comments
 (0)
Please sign in to comment.