Skip to content

Commit e57c53b

Browse files
committed
feat(google): helper checks if user is in OU
1 parent 4f16c20 commit e57c53b

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

compiler_admin/services/google.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,54 @@ def user_in_group(username: str, group: str) -> bool:
153153
return False
154154

155155

156+
def user_in_ou(username: str, ou: str) -> bool:
157+
"""Checks if a user is in an OU.
158+
159+
Args:
160+
username (str): The user@compiler.la to check for membership in the group.
161+
ou (str): The name of an OU to check for username's membership.
162+
Returns:
163+
True if the user is a member of the OU. False otherwise.
164+
"""
165+
if user_exists(username):
166+
with NamedTemporaryFile("w+") as stdout:
167+
CallGAMCommand(("info", "ou", ou), stdout=stdout.name, stderr="stdout")
168+
output = "\n".join(stdout.readlines())
169+
return username in output
170+
else:
171+
print(f"User does not exist: {username}")
172+
return False
173+
174+
175+
def user_is_deactivated(username: str) -> bool:
176+
"""Checks if a user is in an OU.
177+
178+
Args:
179+
username (str): The user@compiler.la to check for membership in the group.
180+
ou (str): The name of an OU to check for username's membership.
181+
Returns:
182+
True if the user is a member of the OU. False otherwise.
183+
"""
184+
return user_in_ou(username, OU_ALUMNI)
185+
186+
156187
def user_is_partner(username: str) -> bool:
188+
"""Checks if a user is a Compiler Partner.
189+
190+
Args:
191+
username (str): The user@compiler.la to check for partner status.
192+
Returns:
193+
True if the user is a Partner. False otherwise.
194+
"""
157195
return user_in_group(username, GROUP_PARTNERS)
158196

159197

160198
def user_is_staff(username: str) -> bool:
199+
"""Checks if a user is a Compiler Staff.
200+
201+
Args:
202+
username (str): The user@compiler.la to check for staff status.
203+
Returns:
204+
True if the user is a Staff. False otherwise.
205+
"""
161206
return user_in_group(username, GROUP_STAFF)

tests/services/test_google.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
remove_user_from_group,
2121
user_exists,
2222
user_in_group,
23+
user_in_ou,
2324
user_info,
2425
user_is_partner,
2526
user_is_staff,
@@ -249,6 +250,33 @@ def test_user_in_group_user_exists_not_in_group(mock_NamedTemporaryFile_with_rea
249250
assert res is False
250251

251252

253+
@pytest.mark.usefixtures("mock_google_user_exists_no")
254+
def test_user_in_ou_user_does_not_exist(capfd):
255+
res = user_in_ou("username", "ou")
256+
captured = capfd.readouterr()
257+
258+
assert res is False
259+
assert "User does not exist" in captured.out
260+
261+
262+
@pytest.mark.usefixtures("mock_google_user_exists_yes")
263+
def test_user_in_ou_user_exists_in_ou(mock_NamedTemporaryFile_with_readlines):
264+
mock_NamedTemporaryFile_with_readlines(MODULE, ["username"])
265+
266+
res = user_in_ou("username", "ou")
267+
268+
assert res is True
269+
270+
271+
@pytest.mark.usefixtures("mock_google_user_exists_yes")
272+
def test_user_in_ou_user_exists_not_in_ou(mock_NamedTemporaryFile_with_readlines):
273+
mock_NamedTemporaryFile_with_readlines(MODULE, ["nope"])
274+
275+
res = user_in_ou("username", "ou")
276+
277+
assert res is False
278+
279+
252280
def test_user_is_partner_checks_partner_group(mock_google_user_in_group):
253281
user_is_partner("username")
254282

0 commit comments

Comments
 (0)