|
| 1 | +"""Anonymous 'show all points' map visibility.""" |
| 2 | +import json |
| 3 | +import uuid |
| 4 | + |
| 5 | +from django.contrib.gis.geos import Point |
| 6 | +from django.contrib.auth.models import User |
| 7 | +from django.test import Client, TestCase |
| 8 | +from django.urls import reverse |
| 9 | + |
| 10 | +from datasets.models import DataEntry, DataGeometry, DataSet, VirtualContributor |
| 11 | + |
| 12 | + |
| 13 | +class AnonymousShowAllPointsTests(TestCase): |
| 14 | + def setUp(self): |
| 15 | + self.owner = User.objects.create_user(username='owner', password='pass') |
| 16 | + self.dataset = DataSet.objects.create( |
| 17 | + name='Anon Dataset', |
| 18 | + owner=self.owner, |
| 19 | + allow_anonymous_data_input=True, |
| 20 | + anonymous_show_all_points=False, |
| 21 | + ) |
| 22 | + self.dataset.ensure_anonymous_access_token() |
| 23 | + self.dataset.refresh_from_db() |
| 24 | + |
| 25 | + self.vc_self = VirtualContributor.objects.create( |
| 26 | + dataset=self.dataset, |
| 27 | + uuid=uuid.uuid4(), |
| 28 | + display_name='Self', |
| 29 | + ) |
| 30 | + self.vc_other = VirtualContributor.objects.create( |
| 31 | + dataset=self.dataset, |
| 32 | + uuid=uuid.uuid4(), |
| 33 | + display_name='Other', |
| 34 | + ) |
| 35 | + |
| 36 | + self.geom_self = DataGeometry.objects.create( |
| 37 | + dataset=self.dataset, |
| 38 | + id_kurz='A', |
| 39 | + address='A', |
| 40 | + geometry=Point(16.37, 48.21, srid=4326), |
| 41 | + virtual_contributor=self.vc_self, |
| 42 | + ) |
| 43 | + self.geom_other = DataGeometry.objects.create( |
| 44 | + dataset=self.dataset, |
| 45 | + id_kurz='B', |
| 46 | + address='B', |
| 47 | + geometry=Point(16.38, 48.22, srid=4326), |
| 48 | + virtual_contributor=self.vc_other, |
| 49 | + ) |
| 50 | + self.entry_other = DataEntry.objects.create( |
| 51 | + geometry=self.geom_other, |
| 52 | + name='Other entry', |
| 53 | + year=2020, |
| 54 | + virtual_contributor=self.vc_other, |
| 55 | + ) |
| 56 | + |
| 57 | + self.anon_client = Client() |
| 58 | + sid = self.dataset.id |
| 59 | + session = self.anon_client.session |
| 60 | + session[f'anonymous_token_{sid}'] = self.dataset.anonymous_access_token |
| 61 | + session[f'virtual_contributor_uuid_{sid}'] = str(self.vc_self.uuid) |
| 62 | + session.save() |
| 63 | + |
| 64 | + def _map_ids(self): |
| 65 | + url = reverse('dataset_map_data', kwargs={'dataset_id': self.dataset.id}) |
| 66 | + r = self.anon_client.get(url) |
| 67 | + self.assertEqual(r.status_code, 200) |
| 68 | + return {row['id'] for row in r.json().get('map_data', [])} |
| 69 | + |
| 70 | + def test_map_data_only_own_points_when_flag_off(self): |
| 71 | + self.assertEqual(self._map_ids(), {self.geom_self.id}) |
| 72 | + |
| 73 | + def test_map_data_all_points_when_flag_on(self): |
| 74 | + self.dataset.anonymous_show_all_points = True |
| 75 | + self.dataset.save(update_fields=['anonymous_show_all_points']) |
| 76 | + self.assertSetEqual(self._map_ids(), {self.geom_self.id, self.geom_other.id}) |
| 77 | + |
| 78 | + def test_geometry_details_other_denied_when_flag_off(self): |
| 79 | + url = reverse('geometry_details', kwargs={'geometry_id': self.geom_other.id}) |
| 80 | + r = self.anon_client.get(url) |
| 81 | + self.assertEqual(r.status_code, 403) |
| 82 | + |
| 83 | + def test_geometry_details_other_allowed_when_flag_on(self): |
| 84 | + self.dataset.anonymous_show_all_points = True |
| 85 | + self.dataset.save(update_fields=['anonymous_show_all_points']) |
| 86 | + url = reverse('geometry_details', kwargs={'geometry_id': self.geom_other.id}) |
| 87 | + r = self.anon_client.get(url) |
| 88 | + self.assertEqual(r.status_code, 200) |
| 89 | + self.assertTrue(r.json().get('success')) |
| 90 | + |
| 91 | + def test_save_entries_other_geometry_denied_when_flag_off(self): |
| 92 | + url = reverse('save_entries') |
| 93 | + r = self.anon_client.post( |
| 94 | + url, |
| 95 | + { |
| 96 | + 'geometry_id': str(self.geom_other.id), |
| 97 | + 'entries[0][id]': str(self.entry_other.id), |
| 98 | + }, |
| 99 | + ) |
| 100 | + self.assertEqual(r.status_code, 403) |
| 101 | + |
| 102 | + def test_save_entries_other_geometry_allowed_when_flag_on(self): |
| 103 | + self.dataset.anonymous_show_all_points = True |
| 104 | + self.dataset.save(update_fields=['anonymous_show_all_points']) |
| 105 | + url = reverse('save_entries') |
| 106 | + r = self.anon_client.post( |
| 107 | + url, |
| 108 | + { |
| 109 | + 'geometry_id': str(self.geom_other.id), |
| 110 | + 'entries[0][id]': str(self.entry_other.id), |
| 111 | + }, |
| 112 | + ) |
| 113 | + self.assertEqual(r.status_code, 200) |
| 114 | + self.assertTrue(r.json().get('success')) |
| 115 | + |
| 116 | + def test_geometry_create_denied_when_anonymous_disable_new_points(self): |
| 117 | + self.dataset.anonymous_disable_new_points = True |
| 118 | + self.dataset.save(update_fields=['anonymous_disable_new_points']) |
| 119 | + url = reverse('geometry_create', kwargs={'dataset_id': self.dataset.id}) |
| 120 | + payload = { |
| 121 | + 'id_kurz': 'NEW_PT', |
| 122 | + 'address': 'New', |
| 123 | + 'geometry': {'type': 'Point', 'coordinates': [16.4, 48.25]}, |
| 124 | + } |
| 125 | + r = self.anon_client.post( |
| 126 | + url, |
| 127 | + data=json.dumps(payload), |
| 128 | + content_type='application/json', |
| 129 | + HTTP_X_REQUESTED_WITH='XMLHttpRequest', |
| 130 | + ) |
| 131 | + self.assertEqual(r.status_code, 403) |
| 132 | + body = r.json() |
| 133 | + self.assertFalse(body.get('success', True)) |
| 134 | + self.assertIn('disabled', (body.get('error') or '').lower()) |
| 135 | + |
| 136 | + def test_geometry_create_allowed_when_anonymous_disable_new_points_off(self): |
| 137 | + url = reverse('geometry_create', kwargs={'dataset_id': self.dataset.id}) |
| 138 | + payload = { |
| 139 | + 'id_kurz': 'NEW_PT2', |
| 140 | + 'address': 'New', |
| 141 | + 'geometry': {'type': 'Point', 'coordinates': [16.41, 48.26]}, |
| 142 | + } |
| 143 | + r = self.anon_client.post( |
| 144 | + url, |
| 145 | + data=json.dumps(payload), |
| 146 | + content_type='application/json', |
| 147 | + HTTP_X_REQUESTED_WITH='XMLHttpRequest', |
| 148 | + ) |
| 149 | + self.assertEqual(r.status_code, 200) |
| 150 | + self.assertTrue(r.json().get('success')) |
0 commit comments