Skip to content

Commit

Permalink
refactor(test): consolidate user_type conditional logic into fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
angela-tran committed Jan 30, 2025
1 parent d5fa891 commit c28fdab
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 37 deletions.
15 changes: 10 additions & 5 deletions tests/pytest/core/admin/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@ def staff_group(settings):


@pytest.fixture
def admin_request(model_AdminUser, rf, staff_group):
def _admin_request(is_superuser=False, is_staff_member=False):
def admin_user_request(model_AdminUser, rf, staff_group):
def _admin_user_request(user_type="staff"):
request = rf.get("/")
request.user = model_AdminUser
model_AdminUser.is_superuser = is_superuser

model_AdminUser.is_staff = True # a user can log in if and only if this is True
if is_staff_member:

if user_type == "staff":
model_AdminUser.is_superuser = False
staff_group.user_set.add(model_AdminUser)
elif user_type == "super":
model_AdminUser.is_superuser = True

return request

return _admin_request
return _admin_user_request
46 changes: 14 additions & 32 deletions tests/pytest/core/admin/test_enrollment.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def flow_admin_model():
@pytest.mark.django_db
class TestEnrollmentEventAdmin:

def test_get_readonly_fields(self, admin_request, event_admin_model):
request = admin_request(is_superuser=False, is_staff_member=False)
def test_get_readonly_fields(self, admin_user_request, event_admin_model):
request = admin_user_request()
assert event_admin_model.get_readonly_fields(request) == ["id"]

@pytest.mark.parametrize(
Expand All @@ -35,7 +35,7 @@ def test_get_readonly_fields(self, admin_request, event_admin_model):
)
def test_has_add_permission(
self,
admin_request,
admin_user_request,
event_admin_model,
settings,
runtime_env,
Expand All @@ -44,10 +44,7 @@ def test_has_add_permission(
):
settings.RUNTIME_ENVIRONMENT = lambda: runtime_env

if user_type == "staff":
request = admin_request(is_superuser=False, is_staff_member=True)
elif user_type == "super":
request = admin_request(is_superuser=True, is_staff_member=False)
request = admin_user_request(user_type)

assert event_admin_model.has_add_permission(request) == expected

Expand All @@ -62,7 +59,7 @@ def test_has_add_permission(
)
def test_has_change_permission(
self,
admin_request,
admin_user_request,
event_admin_model,
settings,
runtime_env,
Expand All @@ -71,10 +68,7 @@ def test_has_change_permission(
):
settings.RUNTIME_ENVIRONMENT = lambda: runtime_env

if user_type == "staff":
request = admin_request(is_superuser=False, is_staff_member=True)
elif user_type == "super":
request = admin_request(is_superuser=True, is_staff_member=False)
request = admin_user_request(user_type)

assert event_admin_model.has_change_permission(request) == expected

Expand All @@ -85,11 +79,8 @@ def test_has_change_permission(
("super", True),
],
)
def test_has_view_permission(self, admin_request, event_admin_model, user_type, expected):
if user_type == "staff":
request = admin_request(is_superuser=False, is_staff_member=True)
elif user_type == "super":
request = admin_request(is_superuser=True, is_staff_member=False)
def test_has_view_permission(self, admin_user_request, event_admin_model, user_type, expected):
request = admin_user_request(user_type)

assert event_admin_model.has_view_permission(request) == expected

Expand All @@ -114,11 +105,8 @@ class TestEnrollmentFlowAdmin:
("super", None),
],
)
def test_get_exclude(self, admin_request, flow_admin_model, user_type, expected):
if user_type == "staff":
request = admin_request(is_superuser=False, is_staff_member=True)
elif user_type == "super":
request = admin_request(is_superuser=True, is_staff_member=False)
def test_get_exclude(self, admin_user_request, flow_admin_model, user_type, expected):
request = admin_user_request(user_type)

excluded = flow_admin_model.get_exclude(request)

Expand Down Expand Up @@ -147,11 +135,8 @@ def test_get_exclude(self, admin_request, flow_admin_model, user_type, expected)
("super", ()),
],
)
def test_get_readonly_fields(self, admin_request, flow_admin_model, user_type, expected):
if user_type == "staff":
request = admin_request(is_superuser=False, is_staff_member=True)
elif user_type == "super":
request = admin_request(is_superuser=True, is_staff_member=False)
def test_get_readonly_fields(self, admin_user_request, flow_admin_model, user_type, expected):
request = admin_user_request(user_type)

readonly = flow_admin_model.get_readonly_fields(request)

Expand All @@ -168,7 +153,7 @@ def test_get_readonly_fields(self, admin_request, flow_admin_model, user_type, e
)
def test_has_add_permission(
self,
admin_request,
admin_user_request,
flow_admin_model,
settings,
runtime_env,
Expand All @@ -177,9 +162,6 @@ def test_has_add_permission(
):
settings.RUNTIME_ENVIRONMENT = lambda: runtime_env

if user_type == "staff":
request = admin_request(is_superuser=False, is_staff_member=True)
else:
request = admin_request(is_superuser=True, is_staff_member=False)
request = admin_user_request(user_type)

assert flow_admin_model.has_add_permission(request) == expected

0 comments on commit c28fdab

Please sign in to comment.