Skip to content

Commit f68f2f7

Browse files
committed
Add unit tests to cover PR bugfix
1 parent daa7a96 commit f68f2f7

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

kpi/tests/api/v2/test_api_asset_permission_assignment.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,3 +589,73 @@ def test_partial_permission_grants_implied_view_asset(self):
589589
# `someuser` should have received the implied `view_asset`
590590
# permission
591591
assert self.someuser.has_perm(PERM_VIEW_ASSET, self.asset)
592+
593+
def test_no_assignments_saved_on_error(self):
594+
595+
# Call `get_anonymous_user()` to create AnonymousUser if it does not exist
596+
get_anonymous_user()
597+
598+
# Ensure someuser and anotheruser do not have 'view_submissions' on `self.asset`
599+
self.assertFalse(self.asset.has_perm(self.someuser, PERM_VIEW_SUBMISSIONS))
600+
self.assertFalse(self.asset.has_perm(self.anotheruser, PERM_VIEW_SUBMISSIONS))
601+
602+
# Allow someuser and anotheruser to view submissions
603+
good_assignments = [
604+
{
605+
'user': 'someuser',
606+
'permission': PERM_VIEW_SUBMISSIONS,
607+
},
608+
{
609+
'user': 'anotheruser',
610+
'permission': PERM_VIEW_SUBMISSIONS,
611+
}
612+
]
613+
614+
assignments = self.translate_usernames_and_codenames_to_urls(
615+
good_assignments
616+
)
617+
bulk_endpoint = reverse(
618+
self._get_endpoint('asset-permission-assignment-bulk-assignments'),
619+
kwargs={'parent_lookup_asset': self.asset.uid}
620+
)
621+
response = self.client.post(bulk_endpoint, assignments, format='json')
622+
623+
# Everything worked as expected, someuser and anotheruser got 'view_submissions'
624+
self.assertEqual(response.status_code, status.HTTP_200_OK)
625+
self.assertTrue(self.asset.has_perm(self.someuser, PERM_VIEW_SUBMISSIONS))
626+
self.assertTrue(self.asset.has_perm(self.anotheruser, PERM_VIEW_SUBMISSIONS))
627+
628+
# but do not have respectively 'delete_submissions' and 'change_submissions'
629+
self.assertFalse(self.asset.has_perm(self.someuser, PERM_DELETE_SUBMISSIONS))
630+
self.assertFalse(self.asset.has_perm(self.anotheruser, PERM_CHANGE_SUBMISSIONS))
631+
632+
bad_assignments = [
633+
{
634+
'user': 'AnonymousUser',
635+
'permission': PERM_ADD_SUBMISSIONS, # should return a 400
636+
},
637+
{
638+
'user': 'someuser',
639+
'permission': PERM_DELETE_SUBMISSIONS,
640+
},
641+
{
642+
'user': 'anotheruser',
643+
'permission': PERM_CHANGE_SUBMISSIONS,
644+
}
645+
]
646+
assignments = self.translate_usernames_and_codenames_to_urls(
647+
bad_assignments
648+
)
649+
650+
bulk_endpoint = reverse(
651+
self._get_endpoint('asset-permission-assignment-bulk-assignments'),
652+
kwargs={'parent_lookup_asset': self.asset.uid}
653+
)
654+
response = self.client.post(bulk_endpoint, assignments, format='json')
655+
# Could not assign 'add_submissions' to anonymous user.
656+
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
657+
658+
# Ensure that someuser and anotheruser did not get any other permissions
659+
# than the one they already had, i.e.: 'view_submissions'.
660+
self.assertFalse(self.asset.has_perm(self.someuser, PERM_DELETE_SUBMISSIONS))
661+
self.assertFalse(self.asset.has_perm(self.anotheruser, PERM_CHANGE_SUBMISSIONS))

kpi/tests/test_sorting.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# coding: utf-8
2+
from django.test import TestCase
3+
from django.contrib.auth.models import User
4+
5+
from kpi.utils.object_permission import get_anonymous_user
6+
7+
8+
class SortingTestCase(TestCase):
9+
10+
def test_different_sort_between_python_and_db(self):
11+
12+
# Ensure that `AnonymousUser` is created to include it in the list below
13+
get_anonymous_user()
14+
15+
User.objects.bulk_create([
16+
User(first_name='A', last_name='User', username='a_user'),
17+
User(first_name='Alexander', last_name='Mtembenuzeni', username='alex_Mtemb'),
18+
User(first_name='Another', last_name='User', username='anotheruser'),
19+
])
20+
21+
users = list(
22+
User.objects.filter(username__istartswith='a')
23+
.values_list('username', flat=True)
24+
.order_by('username')
25+
)
26+
27+
# The database (PostgreSQL, as of Jun, 14, 2022) seems to be case
28+
# insensitive and treats `_` after any letters.
29+
# Python is case sensitive and treats `_` before any letters.
30+
expected_database = [
31+
'alex_Mtemb',
32+
'AnonymousUser',
33+
'anotheruser',
34+
'a_user',
35+
]
36+
37+
expected_python = [
38+
'AnonymousUser',
39+
'a_user',
40+
'alex_Mtemb',
41+
'anotheruser',
42+
]
43+
44+
self.assertEqual(users, expected_database)
45+
self.assertEqual(sorted(users), expected_python)
46+
# Obviously if the first two assertions are True, the one below should
47+
# be false. No matter what, let's be paranoid and test it anyway.
48+
self.assertNotEqual(users, sorted(users))

0 commit comments

Comments
 (0)