|
11 | 11 | GROUP_STAFF, |
12 | 12 | GROUP_TEAM, |
13 | 13 | GYB, |
| 14 | + OU_ALUMNI, |
14 | 15 | USER_ARCHIVE, |
15 | 16 | user_account_name, |
16 | 17 | CallGAMCommand, |
17 | 18 | CallGYBCommand, |
18 | 19 | add_user_to_group, |
| 20 | + get_backup_codes, |
19 | 21 | move_user_ou, |
20 | 22 | remove_user_from_group, |
21 | 23 | user_exists, |
22 | 24 | user_in_group, |
23 | 25 | user_in_ou, |
24 | 26 | user_info, |
| 27 | + user_is_deactivated, |
25 | 28 | user_is_partner, |
26 | 29 | user_is_staff, |
27 | 30 | __name__ as MODULE, |
@@ -58,6 +61,11 @@ def mock_google_user_in_group(mock_google_user_in_group): |
58 | 61 | return mock_google_user_in_group(MODULE) |
59 | 62 |
|
60 | 63 |
|
| 64 | +@pytest.fixture |
| 65 | +def mock_google_user_in_ou(mocker): |
| 66 | + return mocker.patch(f"{MODULE}.user_in_ou") |
| 67 | + |
| 68 | + |
61 | 69 | @pytest.fixture |
62 | 70 | def mock_subprocess_call(mocker): |
63 | 71 | return mocker.patch(f"{MODULE}.subprocess.call") |
@@ -170,6 +178,53 @@ def test_add_user_to_group(mock_google_CallGAMCommand): |
170 | 178 | mock_google_CallGAMCommand.assert_called_once() |
171 | 179 |
|
172 | 180 |
|
| 181 | +def test_get_backup_codes_user_does_not_exist(mock_google_user_exists_no, capfd): |
| 182 | + username = "nonexistent" |
| 183 | + res = get_backup_codes(username) |
| 184 | + captured = capfd.readouterr() |
| 185 | + |
| 186 | + mock_google_user_exists_no.assert_called_once_with(username) |
| 187 | + assert res == "" |
| 188 | + assert f"User does not exist: {username}" in captured.out |
| 189 | + |
| 190 | + |
| 191 | +@pytest.mark.usefixtures("mock_google_user_exists_yes") |
| 192 | +def test_get_backup_codes_user_exists_has_codes(mock_gam_CallGAMCommand, mock_NamedTemporaryFile_with_readlines): |
| 193 | + username = "existent" |
| 194 | + codes = "12345678" |
| 195 | + mock_NamedTemporaryFile_with_readlines(MODULE, [codes]) |
| 196 | + |
| 197 | + res = get_backup_codes(username) |
| 198 | + |
| 199 | + assert mock_gam_CallGAMCommand.call_count == 1 |
| 200 | + assert "show" in mock_gam_CallGAMCommand.call_args[0][0] |
| 201 | + assert res == codes |
| 202 | + |
| 203 | + |
| 204 | +@pytest.mark.usefixtures("mock_google_user_exists_yes") |
| 205 | +def test_get_backup_codes_user_exists_no_codes(mocker, mock_gam_CallGAMCommand): |
| 206 | + username = "existent" |
| 207 | + no_codes_output = "Show 0 Backup Verification Codes" |
| 208 | + new_codes = "87654321" |
| 209 | + |
| 210 | + mock_file_handler = mocker.MagicMock() |
| 211 | + mock_file_handler.readlines.side_effect = [[no_codes_output], [new_codes]] |
| 212 | + mock_file_handler.name = "tempfile" |
| 213 | + |
| 214 | + mock_temp_file_context = mocker.MagicMock() |
| 215 | + mock_temp_file_context.__enter__.return_value = mock_file_handler |
| 216 | + mock_temp_file_context.__exit__.return_value = None |
| 217 | + |
| 218 | + mocker.patch(f"{MODULE}.NamedTemporaryFile", return_value=mock_temp_file_context) |
| 219 | + |
| 220 | + res = get_backup_codes(username) |
| 221 | + |
| 222 | + assert mock_gam_CallGAMCommand.call_count == 2 |
| 223 | + assert "show" in mock_gam_CallGAMCommand.call_args_list[0].args[0] |
| 224 | + assert "update" in mock_gam_CallGAMCommand.call_args_list[1].args[0] |
| 225 | + assert res == new_codes |
| 226 | + |
| 227 | + |
173 | 228 | def test_move_user_ou(mock_google_CallGAMCommand): |
174 | 229 | move_user_ou("username", "theou") |
175 | 230 |
|
@@ -277,6 +332,13 @@ def test_user_in_ou_user_exists_not_in_ou(mock_NamedTemporaryFile_with_readlines |
277 | 332 | assert res is False |
278 | 333 |
|
279 | 334 |
|
| 335 | +def test_user_is_deactivated_checks_alumni_ou(mock_google_user_in_ou): |
| 336 | + user_is_deactivated("username") |
| 337 | + |
| 338 | + mock_google_user_in_ou.assert_called_once() |
| 339 | + assert mock_google_user_in_ou.call_args.args == ("username", OU_ALUMNI) |
| 340 | + |
| 341 | + |
280 | 342 | def test_user_is_partner_checks_partner_group(mock_google_user_in_group): |
281 | 343 | user_is_partner("username") |
282 | 344 |
|
|
0 commit comments