Skip to content

Commit

Permalink
Add fix to unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aviupadhyayula committed Oct 4, 2024
1 parent 7c65598 commit b3c0fbc
Showing 1 changed file with 58 additions and 49 deletions.
107 changes: 58 additions & 49 deletions backend/tests/clubs/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,9 @@ def setUp(self):
self.club1 = Club.objects.create(
code="test-club",
name="Test Club",
approved=True,
email="[email protected]",
approved=True,
approved_on=timezone.now(),
)

self.event1 = Event.objects.create(
Expand Down Expand Up @@ -1141,55 +1142,63 @@ def test_club_approve(self):

def test_club_display_after_deactivation_for_permissioned_vs_non_permissioned(self):
"""
Test club retrieval after deactivation script runs. Non-permissioned users
should see the last approved version of the club. Permissioned users (e.g.
admins, club members) should see the most up-to-date version.
Test club retrieval during renewal period after deactivation.
Non-permissioned users should see last approved version.
Privileged users (e.g. admins, club members) should see most up-to-date.
"""
# club is approved before deactivation
self.assertTrue(self.club1.approved)

call_command("deactivate", "all", "--force")

club = self.club1
club.refresh_from_db()

# after deactivation, club should not be approved and should not have approver
self.assertIsNone(club.approved)
self.assertIsNone(club.approved_by)

# non-permissioned users should see the last approved version
non_admin_resp = self.client.get(reverse("clubs-detail", args=(club.code,)))
self.assertEqual(non_admin_resp.status_code, 200)
non_admin_data = non_admin_resp.json()
self.assertTrue(non_admin_data["approved"])

# permissioned users should see the club as it is in the DB
self.client.login(username=self.user5.username, password="test")
admin_resp = self.client.get(reverse("clubs-detail", args=(club.code,)))
self.assertEqual(admin_resp.status_code, 200)
admin_data = admin_resp.json()
self.assertIsNone(admin_data["approved"])
self.client.logout()

cache.clear()

# reversing the order of operations shouldn't change anything
self.client.login(username=self.user5.username, password="test")
admin_resp = self.client.get(reverse("clubs-detail", args=(club.code,)))
self.assertEqual(admin_resp.status_code, 200)
admin_data = admin_resp.json()
self.assertIsNone(admin_data["approved"])
self.client.logout()

non_admin_resp = self.client.get(reverse("clubs-detail", args=(club.code,)))
self.assertEqual(non_admin_resp.status_code, 200)
non_admin_data = non_admin_resp.json()
self.assertTrue(non_admin_data["approved"])

# club object itself shouldn't have changed
club.refresh_from_db()
self.assertFalse(club.active)
self.assertIsNone(club.approved)
# Mock that we're in renewal period
with patch(
"django.conf.settings.RENEWAL_PERIOD",
(
(timezone.now() - datetime.timedelta(days=5)),
(timezone.now() + datetime.timedelta(days=5)),
),
):
# club is approved before deactivation
self.assertTrue(self.club1.approved)

call_command("deactivate", "all", "--force")

club = self.club1
club.refresh_from_db()

# club is not approved and does not have approver after deactivation
self.assertIsNone(club.approved)
self.assertIsNone(club.approved_by)

# non-permissioned users should see the last approved version
non_admin_resp = self.client.get(reverse("clubs-detail", args=(club.code,)))
self.assertEqual(non_admin_resp.status_code, 200)
non_admin_data = non_admin_resp.json()
self.assertTrue(non_admin_data["approved"])

# permissioned users should see the club as it is in the DB
self.client.login(username=self.user5.username, password="test")
admin_resp = self.client.get(reverse("clubs-detail", args=(club.code,)))
self.assertEqual(admin_resp.status_code, 200)
admin_data = admin_resp.json()
self.assertIsNone(admin_data["approved"])
self.client.logout()

cache.clear()

# reversing the order of operations shouldn't change anything
self.client.login(username=self.user5.username, password="test")
admin_resp = self.client.get(reverse("clubs-detail", args=(club.code,)))
self.assertEqual(admin_resp.status_code, 200)
admin_data = admin_resp.json()
self.assertIsNone(admin_data["approved"])
self.client.logout()

non_admin_resp = self.client.get(reverse("clubs-detail", args=(club.code,)))
self.assertEqual(non_admin_resp.status_code, 200)
non_admin_data = non_admin_resp.json()
self.assertTrue(non_admin_data["approved"])

# club object itself shouldn't have changed
club.refresh_from_db()
self.assertFalse(club.active)
self.assertIsNone(club.approved)

def test_club_create_url_sanitize(self):
"""
Expand Down

0 comments on commit b3c0fbc

Please sign in to comment.