Unit tests

This commit is contained in:
zandercymatics 2024-05-01 08:40:55 -06:00
parent cb68c74b68
commit b50f14fd3b
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7

View file

@ -4,7 +4,8 @@ from django.test import TestCase, Client, RequestFactory
from registrar.models import ( from registrar.models import (
WaffleFlag, WaffleFlag,
User, User,
Contact Contact,
UserGroup,
) )
from registrar.tests.common import create_superuser, create_staffuser, create_user from registrar.tests.common import create_superuser, create_staffuser, create_user
@ -23,6 +24,24 @@ class TestFeatureFlags(TestCase):
User.objects.all().delete() User.objects.all().delete()
Contact.objects.all().delete() Contact.objects.all().delete()
def assert_flag_active(self, request_user, flag_name, location="/"):
"""
Checks if the given `request_user` has `flag_name` active
using waffles `flag_is_active` function.
"""
request = self.factory.get(location)
request.user = request_user
self.assertTrue(flag_is_active(request, flag_name))
def assert_flag_not_active(self, request_user, flag_name, location="/"):
"""
Checks if the given `request_user` has `flag_name` not active
using waffles `flag_is_active` function.
"""
request = self.factory.get(location)
request.user = request_user
self.assertFalse(flag_is_active(request, flag_name))
def test_flag_active_for_superuser(self): def test_flag_active_for_superuser(self):
""" """
Tests flag_is_active for a flag with `superuser = True` Tests flag_is_active for a flag with `superuser = True`
@ -33,57 +52,110 @@ class TestFeatureFlags(TestCase):
staff=False, staff=False,
) )
# Test if superusers can access this flag # Test if superusers can access this flag
request = self.factory.get("/") self.assert_flag_active(request_user=self.superuser, flag_name=flag.name)
request.user = self.superuser
self.assertTrue(flag_is_active(request, flag.name))
# Ensure that regular staff cannot access this flag # Ensure that regular staff cannot access this flag
request_staff = self.factory.get("/") self.assert_flag_not_active(request_user=self.staffuser, flag_name=flag.name)
request_staff.user = self.staffuser
self.assertFalse(flag_is_active(request_staff, flag.name))
# Ensure that a normal user also can't access this flag # Ensure that a normal user also can't access this flag
request_normal = self.factory.get("/") self.assert_flag_not_active(request_user=self.user, flag_name=flag.name)
request_normal.user = self.user
self.assertFalse(flag_is_active(request_normal, flag.name))
@skip("not implemented yet")
def test_flag_active_for_is_staff(self): def test_flag_active_for_is_staff(self):
""" """
Tests flag_is_active for a flag with `is_staff = True` Tests flag_is_active for a flag with `is_staff = True`
""" """
# Test if staff can access this flag # We should actually expect superusers
# Ensure that superusers cannot # to not see this feature - otherwise the two distinct booleans aren't useful.
raise # In practice, we would usually use groups for toggling features.
flag, _ = WaffleFlag.objects.get_or_create(
@skip("not implemented yet") name="test_superuser_flag",
superusers=False,
staff=True,
)
# Ensure that regular staff can access this flag
self.assert_flag_active(request_user=self.staffuser, flag_name=flag.name)
# Ensure that superusers cannot access this flag
self.assert_flag_not_active(request_user=self.superuser, flag_name=flag.name)
# Ensure that a normal user also can't access this flag
self.assert_flag_not_active(request_user=self.user, flag_name=flag.name)
def test_flag_active_for_everyone(self): def test_flag_active_for_everyone(self):
""" """
Tests flag_is_active for a flag with `everyone = True` Tests flag_is_active for a flag with `everyone = True`
""" """
# Test if superuser, analyst, and a normal user can access flag, _ = WaffleFlag.objects.get_or_create(
raise name="test_superuser_flag",
everyone=True,
@skip("not implemented yet") )
# Ensure that regular staff can access this flag
self.assert_flag_active(request_user=self.staffuser, flag_name=flag.name)
# Ensure that superusers can access this flag
self.assert_flag_active(request_user=self.superuser, flag_name=flag.name)
# Ensure that normal users can access this flag
self.assert_flag_active(request_user=self.user, flag_name=flag.name)
def test_flag_active_for_everyone_is_false(self): def test_flag_active_for_everyone_is_false(self):
""" """
Tests flag_is_active for a flag with `everyone = False` Tests flag_is_active for a flag with `everyone = False`
""" """
# Test if no user type can access flag, _ = WaffleFlag.objects.get_or_create(
raise name="test_superuser_flag",
everyone=False,
)
# Ensure that regular staff cannot access this flag
self.assert_flag_not_active(request_user=self.staffuser, flag_name=flag.name)
# Ensure that superusers cannot access this flag
self.assert_flag_not_active(request_user=self.superuser, flag_name=flag.name)
# Ensure that normal users cannot access this flag
self.assert_flag_not_active(request_user=self.user, flag_name=flag.name)
@skip("not implemented yet")
def test_admin_group(self): def test_admin_group(self):
""" """
Tests flag_is_active for the admin user group Tests flag_is_active for the admin user group
""" """
# Test if no user type can access flag, _ = WaffleFlag.objects.get_or_create(
raise name="test_superuser_flag",
)
# Add the full access group to this flag
group, _ = UserGroup.objects.get_or_create(name="full_access_group")
flag.groups.set([group])
# Ensure that regular staff cannot access this flag
self.assert_flag_not_active(request_user=self.staffuser, flag_name=flag.name)
# Ensure that superusers can access this flag
self.assert_flag_active(request_user=self.superuser, flag_name=flag.name)
# Ensure that normal users cannot access this flag
self.assert_flag_not_active(request_user=self.user, flag_name=flag.name)
@skip("not implemented yet")
def test_staff_group(self): def test_staff_group(self):
""" """
Tests flag_is_active for the staff user group Tests flag_is_active for the staff user group
""" """
# Test if no user type can access flag, _ = WaffleFlag.objects.get_or_create(
raise name="test_superuser_flag",
)
# Add the analyst group to this flag
analyst_group, _ = UserGroup.objects.get_or_create(name="cisa_analysts_group")
flag.groups.set([analyst_group])
# Ensure that regular staff can access this flag
self.assert_flag_active(request_user=self.staffuser, flag_name=flag.name)
# Ensure that superusers cannot access this flag
self.assert_flag_not_active(request_user=self.superuser, flag_name=flag.name)
# Ensure that normal users cannot access this flag
self.assert_flag_not_active(request_user=self.user, flag_name=flag.name)