Skip to content

Commit f343bcc

Browse files
committed
test: cover the new google service helpers
1 parent a11a1c0 commit f343bcc

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

tests/services/test_google.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,20 @@
1111
GROUP_STAFF,
1212
GROUP_TEAM,
1313
GYB,
14+
OU_ALUMNI,
1415
USER_ARCHIVE,
1516
user_account_name,
1617
CallGAMCommand,
1718
CallGYBCommand,
1819
add_user_to_group,
20+
get_backup_codes,
1921
move_user_ou,
2022
remove_user_from_group,
2123
user_exists,
2224
user_in_group,
2325
user_in_ou,
2426
user_info,
27+
user_is_deactivated,
2528
user_is_partner,
2629
user_is_staff,
2730
__name__ as MODULE,
@@ -58,6 +61,11 @@ def mock_google_user_in_group(mock_google_user_in_group):
5861
return mock_google_user_in_group(MODULE)
5962

6063

64+
@pytest.fixture
65+
def mock_google_user_in_ou(mocker):
66+
return mocker.patch(f"{MODULE}.user_in_ou")
67+
68+
6169
@pytest.fixture
6270
def mock_subprocess_call(mocker):
6371
return mocker.patch(f"{MODULE}.subprocess.call")
@@ -170,6 +178,53 @@ def test_add_user_to_group(mock_google_CallGAMCommand):
170178
mock_google_CallGAMCommand.assert_called_once()
171179

172180

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+
173228
def test_move_user_ou(mock_google_CallGAMCommand):
174229
move_user_ou("username", "theou")
175230

@@ -277,6 +332,13 @@ def test_user_in_ou_user_exists_not_in_ou(mock_NamedTemporaryFile_with_readlines
277332
assert res is False
278333

279334

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+
280342
def test_user_is_partner_checks_partner_group(mock_google_user_in_group):
281343
user_is_partner("username")
282344

0 commit comments

Comments
 (0)