Skip to content

Commit da48171

Browse files
committed
feat(google): get a user info dict
1 parent 0c887a7 commit da48171

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

compiler_admin/services/google.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from tempfile import NamedTemporaryFile
44
from typing import Any, Sequence, IO
55

6+
from compiler_admin import RESULT_SUCCESS
7+
68
# import and alias CallGAMCommand so we can simplify usage in this app
79
from gam import CallGAMCommand as __CallGAMCommand, initializeLogging
810

@@ -96,9 +98,38 @@ def user_exists(username: str) -> bool:
9698
print(f"User not in domain: {username}")
9799
return False
98100

99-
res = CallGAMCommand(("info", "user", username, "quick"))
101+
info = user_info(username)
102+
103+
return info != {}
104+
100105

101-
return res == 0
106+
def user_info(username: str) -> dict:
107+
"""Get a dict of basic user information.
108+
109+
Args:
110+
username (str): The user@compiler.la to get.
111+
Returns:
112+
A dict of user information
113+
"""
114+
if not str(username).endswith(DOMAIN):
115+
print(f"User not in domain: {username}")
116+
return {}
117+
118+
with NamedTemporaryFile("w+") as stdout:
119+
res = CallGAMCommand(("info", "user", username, "quick"), stdout=stdout.name)
120+
if res != RESULT_SUCCESS:
121+
# user doesn't exist
122+
return {}
123+
# user exists, read data
124+
lines = stdout.readlines()
125+
# split on newline and filter out lines that aren't line "Key:Value" and empty value lines like "Key:<empty>"
126+
lines = [L.strip() for L in lines if len(L.split(":")) == 2 and L.split(":")[1].strip()]
127+
# make a map by splitting the lines, trimming key and value
128+
info = {}
129+
for line in lines:
130+
k, v = line.split(":")
131+
info[k.strip()] = v.strip()
132+
return info
102133

103134

104135
def user_in_group(username: str, group: str) -> bool:

tests/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ def mock_google_user_exists(mock_module_name):
118118
return mock_module_name("user_exists")
119119

120120

121+
@pytest.fixture
122+
def mock_google_user_info(mock_module_name):
123+
"""Fixture returns a function that patches the user_info function from a given module."""
124+
return mock_module_name("user_info")
125+
126+
121127
@pytest.fixture
122128
def mock_google_user_in_group(mock_module_name):
123129
"""Fixture returns a function that patches the user_in_group function from a given module."""

tests/services/test_google.py

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

44
import pytest
55

6+
from compiler_admin import RESULT_SUCCESS, RESULT_FAILURE
67
from compiler_admin.services.google import (
78
DOMAIN,
89
GAM,
@@ -19,6 +20,7 @@
1920
remove_user_from_group,
2021
user_exists,
2122
user_in_group,
23+
user_info,
2224
user_is_partner,
2325
user_is_staff,
2426
__name__ as MODULE,
@@ -40,6 +42,11 @@ def mock_google_user_exists(mock_google_user_exists):
4042
return mock_google_user_exists(MODULE)
4143

4244

45+
@pytest.fixture
46+
def mock_google_user_info(mock_google_user_info):
47+
return mock_google_user_info(MODULE)
48+
49+
4350
@pytest.fixture
4451
def mock_google_user_in_group(mock_google_user_in_group):
4552
return mock_google_user_in_group(MODULE)
@@ -177,22 +184,39 @@ def test_user_exists_username_not_in_domain(capfd):
177184
assert "User not in domain" in captured.out
178185

179186

180-
def test_user_exists_user_exists(mock_google_CallGAMCommand):
181-
mock_google_CallGAMCommand.return_value = 0
187+
def test_user_exists_user_exists(mock_google_user_info):
188+
mock_google_user_info.return_value = {"First Name": "Test", "Last Name": "User"}
182189

183190
res = user_exists(user_account_name("username"))
184191

185192
assert res is True
186193

187194

188-
def test_user_exists_user_does_not_exists(mock_google_CallGAMCommand):
189-
mock_google_CallGAMCommand.return_value = -1
195+
def test_user_exists_user_does_not_exist(mock_google_user_info):
196+
mock_google_user_info.return_value = {}
190197

191198
res = user_exists(user_account_name("username"))
192199

193200
assert res is False
194201

195202

203+
def test_user_info_user_exists(mock_gam_CallGAMCommand, mock_NamedTemporaryFile_with_readlines):
204+
mock_NamedTemporaryFile_with_readlines(MODULE, ["First Name:Test", "Last Name:User"])
205+
mock_gam_CallGAMCommand.return_value = RESULT_SUCCESS
206+
207+
res = user_info(user_account_name("username"))
208+
209+
assert res == {"First Name": "Test", "Last Name": "User"}
210+
211+
212+
def test_user_info_user_does_not_exists(mock_gam_CallGAMCommand):
213+
mock_gam_CallGAMCommand.return_value = RESULT_FAILURE
214+
215+
res = user_info(user_account_name("username"))
216+
217+
assert res == {}
218+
219+
196220
def test_user_in_group_user_does_not_exist(mock_google_user_exists, capfd):
197221
mock_google_user_exists.return_value = False
198222

0 commit comments

Comments
 (0)