Skip to content

Commit 05c6bfb

Browse files
committed
Add tests for columns scoping
ref. T33048
1 parent 3cdba94 commit 05c6bfb

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

tests/test_permission_view.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,3 +692,53 @@ def test_multiput_with_deletions_no_perm(self):
692692
self.assertEquals(403, res.status_code)
693693

694694
country.refresh_from_db()
695+
696+
697+
class TestColumnScoping(TestCase):
698+
def setUp(self):
699+
super().setUp()
700+
701+
u = User(username='testuser_for_not_all_fields', is_active=True, is_superuser=False)
702+
u.set_password('test')
703+
u.save()
704+
705+
self.client = Client()
706+
r = self.client.login(username='testuser_for_not_all_fields', password='test')
707+
self.assertTrue(r)
708+
709+
self.zoo = Zoo(name='Artis')
710+
self.zoo.save()
711+
712+
713+
def test_column_scoping_excludes_columns(self):
714+
res = self.client.get('/zoo/{}/'.format(self.zoo.id))
715+
self.assertEqual(res.status_code, 200)
716+
717+
columns = jsonloads(res.content)['data'].keys()
718+
719+
for field in ['name', 'founding_date', 'django_picture']:
720+
self.zoo._meta.get_field(field) # check if those fields exist, otherwise throw error
721+
self.assertTrue(field not in columns)
722+
723+
for annotation in ['zoo_name']:
724+
self.assertTrue(annotation not in columns)
725+
726+
for property in ['animal_count']:
727+
self.assertTrue(property not in columns)
728+
729+
730+
def test_column_scoping_includes_columns(self):
731+
res = self.client.get('/zoo/{}/'.format(self.zoo.id))
732+
self.assertEqual(res.status_code, 200)
733+
734+
columns = jsonloads(res.content)['data'].keys()
735+
736+
for field in ['id', 'floor_plan']:
737+
self.zoo._meta.get_field(field) # check if those fields exist, otherwise throw error
738+
self.assertTrue(field in columns)
739+
740+
for annotation in ['another_zoo_name']:
741+
self.assertTrue(annotation in columns)
742+
743+
for property in ['another_animal_count']:
744+
self.assertTrue(property in columns)

tests/testapp/models/zoo.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,23 @@ class Zoo(BinderModel):
3535

3636
binder_picture_custom_extensions = BinderImageField(allowed_extensions=['png'], blank=True, null=True)
3737

38+
39+
class Annotations:
40+
zoo_name = models.F('name') # simple alias for testing scoping on annotations
41+
another_zoo_name = models.F('name') # simple alias for testing scoping on annotations
42+
43+
3844
def __str__(self):
3945
return 'zoo %d: %s' % (self.pk, self.name)
4046

4147
@property
4248
def animal_count(self):
4349
return self.animals.count()
4450

51+
@property
52+
def another_animal_count(self):
53+
return self.animals.count()
54+
4555

4656
def clean(self):
4757
if self.name == 'very_special_forbidden_zoo_name':

tests/testapp/views/zoo.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ def _require_model_perm(self, perm_type, request, pk=None):
2828
return ['all']
2929
elif perm_type == 'view' and request.user.username == 'testuser_for_bad_q_filter':
3030
return ['bad_q_filter']
31+
elif perm_type == 'view' and request.user.username == 'testuser_for_not_all_fields':
32+
return ['not_all_fields']
3133
else:
3234
model = self.perms_via if hasattr(self, 'perms_via') else self.model
3335
perm = '{}.{}_{}'.format(model._meta.app_label, perm_type, model.__name__.lower())
@@ -44,3 +46,12 @@ def _scope_view_bad_q_filter(self, request):
4446
return Q(animals__id__in=Animal.objects.all())
4547
# Correct version of filter:
4648
# return Zoo.objects.filter(animals__id__in=Animal.objects.all())
49+
50+
def _scope_view_not_all_fields(self, request):
51+
# expose only certain columns
52+
columns = {
53+
'fields': ['id', 'floor_plan'],
54+
'properties': ['another_animal_count'],
55+
'annotations': ['another_zoo_name'],
56+
}
57+
return Zoo.objects.all(), columns

0 commit comments

Comments
 (0)