Skip to content

Commit

Permalink
[DDCZ-2W]: Fix setting assistant admins
Browse files Browse the repository at this point in the history
  • Loading branch information
Almad committed Jul 14, 2024
1 parent 1b2bfca commit dce8cbb
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 3 deletions.
8 changes: 5 additions & 3 deletions ddcz/models/used/tavern.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def update_access_privileges(
)
processing_privileges.add(TavernAccessRights.ACCESS_BANNED)
if assistant_admins is not None:
unprocessed_access_map[TavernAccessRights.ASSISTANT_ADMIN] = (
unprocessed_access_map[TavernAccessRights.ASSISTANT_ADMIN] = set(
assistant_admins
)
processing_privileges.add(TavernAccessRights.ASSISTANT_ADMIN)
Expand Down Expand Up @@ -219,7 +219,9 @@ def get_current_privileges_map(self):

for privilege in privileges:
access_type = TavernAccessRights(privilege.access_type)
if access_type == TavernAccessRights.ASSISTANT_ADMIN:
if access_type == TavernAccessRights.ASSISTANT_ADMIN and isinstance(
privilege.user_nick_or_id, int
):
nick = UserProfile.objects.values("nick").get(
pk=privilege.user_nick_or_id
)["nick"]
Expand All @@ -242,7 +244,7 @@ def update_legacy_privileges(self, unprocessed_access_map, processing_privileges
* Add new privileges
* Drop the deleted ones
"""
# Copy as we are using it as processing queue and it needs to be reatined
# Copy as we are using it as processing queue and it needs to be retained
# for updating future privileges
access_map = deepcopy(unprocessed_access_map)
table_acls_to_delete = []
Expand Down
106 changes: 106 additions & 0 deletions ddcz/tests/test_integration/test_tavern/test_admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
from django.urls import reverse
from django.test import Client, TestCase

from ...model_generator import get_alphabetic_user_profiles

from ddcz.tavern import create_tavern_table, TavernAccessRights


class TestTableAdminAccess(TestCase):
def setUp(self):
super().setUp()

self.profiles = get_alphabetic_user_profiles(
number_of_users=3, saved=True, with_corresponding_user=True
)
self.owner = self.profiles[0]
self.banned = self.profiles[1]
self.unaffected = self.profiles[2]

self.public_table = create_tavern_table(
owner=self.owner,
public=True,
name="Public",
description="Public Tavern Table",
)

self.public_table.update_access_privileges(access_banned=[self.banned.pk])

self.admin_url = reverse("ddcz:tavern-table-admin", args=[self.public_table.pk])
self.client = Client()

def tearDown(self):
super().tearDown()
self.client.logout()

def test_nonadmin_cannot_access(self):
self.client.login(
username=self.banned.user.username, password=self.banned.user.email
)
self.assertEqual(302, self.client.get(self.admin_url).status_code)

self.client.login(
username=self.unaffected.user.username, password=self.unaffected.user.email
)
self.assertEqual(403, self.client.get(self.admin_url).status_code)

def test_only_admin_can_access_admin(self):
self.client.login(
username=self.owner.user.username, password=self.owner.user.email
)
self.assertEqual(200, self.client.get(self.admin_url).status_code)


class TestTableAdmin(TestCase):
def setUp(self):
super().setUp()

self.profiles = get_alphabetic_user_profiles(
number_of_users=2, saved=True, with_corresponding_user=True
)
self.owner = self.profiles[0]
self.helper = self.profiles[1]

self.public_table = create_tavern_table(
owner=self.owner,
public=True,
name="Public",
description="Public Tavern Table",
)

self.admin_url = reverse("ddcz:tavern-table-admin", args=[self.public_table.pk])
self.client = Client()
self.client.login(
username=self.owner.user.username, password=self.owner.user.email
)

def tearDown(self):
super().tearDown()
self.client.logout()

def test_update_privileges_can_be_done_with_nickname(self):
response = self.client.post(
self.admin_url,
{
"name": self.public_table.name,
"description": self.public_table.description,
"allow_rep": self.public_table.allow_rep,
"assistant_admins": self.helper.nick,
"write_allowed": "",
"access_allowed": "",
"access_banned": "",
},
)

self.assertEqual(302, response.status_code)

self.public_table.refresh_from_db()
self.assertEqual(
{self.helper.nick},
self.public_table.get_current_privileges_map()[
TavernAccessRights.ASSISTANT_ADMIN
],
)

# Check page renders well after (we had bugs with storing bad data well and then not rendering)
self.assertEqual(200, self.client.get(self.admin_url).status_code)

0 comments on commit dce8cbb

Please sign in to comment.