Skip to content

Commit ed1f681

Browse files
committed
[fix] Added test to proper test class
1 parent 7e0f32f commit ed1f681

File tree

2 files changed

+75
-51
lines changed

2 files changed

+75
-51
lines changed

openwisp_controller/geo/api/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class IndoorCoodinatesViewPagination(ListViewPagination):
219219

220220

221221
class IndoorCoordinatesList(
222-
RelatedDeviceProtectedAPIMixin, FilterByParentManaged, generics.ListAPIView
222+
FilterByParentManaged, ProtectedAPIMixin, generics.ListAPIView
223223
):
224224
serializer_class = IndoorCoordinatesSerializer
225225
filter_backends = [filters.DjangoFilterBackend]

openwisp_controller/geo/tests/test_api.py

Lines changed: 74 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import uuid
44

55
from django.contrib.auth import get_user_model
6-
from django.contrib.auth.models import Permission
76
from django.contrib.gis.geos import Point
87
from django.test import TestCase
98
from django.test.client import BOUNDARY, MULTIPART_CONTENT, encode_multipart
@@ -30,11 +29,12 @@
3029
User = get_user_model()
3130

3231

33-
class TestApi(TestGeoMixin, TestCase):
32+
class TestApi(TestGeoMixin, CreateDeviceMixin, TestCase):
3433
url_name = "geo_api:device_coordinates"
3534
object_location_model = DeviceLocation
3635
location_model = Location
3736
object_model = Device
37+
floorplan_model = FloorPlan
3838

3939
def test_permission_404(self):
4040
url = reverse(self.url_name, args=[self.object_model().pk])
@@ -158,6 +158,24 @@ def test_bearer_authentication(self):
158158
)
159159
self.assertEqual(response.status_code, 200)
160160

161+
with self.subTest("Test IndoorCoordinatesList"):
162+
org = self._get_org()
163+
location = self._create_location(organization=org, type="indoor")
164+
floor = self._create_floorplan(floor=1, location=location)
165+
d = self._create_device()
166+
self._create_object_location(
167+
content_object=d,
168+
location=location,
169+
floorplan=floor,
170+
organization=org,
171+
)
172+
response = self.client.get(
173+
reverse("geo_api:indoor_coordinates_list", args=[location.id]),
174+
content_type="application/json",
175+
HTTP_AUTHORIZATION=f"Bearer {token}",
176+
)
177+
self.assertEqual(response.status_code, 200)
178+
161179
def test_deactivated_device(self):
162180
device = self._create_object_location().device
163181
url = "{0}?key={1}".format(reverse(self.url_name, args=[device.pk]), device.key)
@@ -182,6 +200,7 @@ class TestMultitenantApi(TestGeoMixin, TestCase, CreateConfigTemplateMixin):
182200
object_location_model = DeviceLocation
183201
location_model = Location
184202
object_model = Device
203+
floorplan_model = FloorPlan
185204

186205
def setUp(self):
187206
super().setUp()
@@ -285,6 +304,59 @@ def test_geojson_list(self):
285304
r = self.client.get(reverse(url))
286305
self.assertEqual(r.status_code, 401)
287306

307+
@capture_any_output()
308+
def test_indoor_coodinate_list(self):
309+
url = "geo_api:indoor_coordinates_list"
310+
org_a = self._get_org("org_a")
311+
org_b = self._get_org("org_b")
312+
device_a = self._create_device(organization=org_a)
313+
device_b = self._create_device(organization=org_b)
314+
location_a = self._create_location(type="indoor", organization=org_a)
315+
location_b = self._create_location(type="indoor", organization=org_b)
316+
floor_a = self._create_floorplan(location=location_a)
317+
floor_b = self._create_floorplan(location=location_b)
318+
self._create_object_location(
319+
content_object=device_a,
320+
location=location_a,
321+
floorplan=floor_a,
322+
organization=org_a,
323+
)
324+
self._create_object_location(
325+
content_object=device_b,
326+
location=location_b,
327+
floorplan=floor_b,
328+
organization=org_b,
329+
)
330+
331+
with self.subTest("Test indoor coordinate list for org operator"):
332+
self.client.login(username="operator", password="tester")
333+
r = self.client.get(reverse(url, args=[location_a.id]))
334+
self.assertContains(r, str(device_a.id))
335+
r = self.client.get(reverse(url, args=[location_b.id]))
336+
self.assertEqual(r.status_code, 404)
337+
338+
with self.subTest("Test indoor coordinate list for org superuser"):
339+
self.client.login(username="admin", password="tester")
340+
r = self.client.get(reverse(url, args=[location_a.id]))
341+
self.assertContains(r, str(device_a.id))
342+
r = self.client.get(reverse(url, args=[location_b.id]))
343+
self.assertContains(r, str(device_b.id))
344+
345+
with self.subTest("Test indoor coordinate list for org administrator"):
346+
administrator = self._create_administrator(organizations=[org_a, org_b])
347+
self.client.force_login(administrator)
348+
r = self.client.get(reverse(url, args=[location_a.id]))
349+
self.assertEqual(r.status_code, 200)
350+
self.assertContains(r, str(device_a.id))
351+
r = self.client.get(reverse(url, args=[location_b.id]))
352+
self.assertEqual(r.status_code, 200)
353+
self.assertContains(r, str(device_b.id))
354+
355+
with self.subTest("Test for unauthenticated user"):
356+
self.client.logout()
357+
response = self.client.get(reverse(url, args=[location_a.id]))
358+
self.assertEqual(response.status_code, 401)
359+
288360

289361
class TestGeoApi(
290362
AssertNumQueriesSubTestMixin,
@@ -1188,51 +1260,3 @@ def test_indoor_coordinates_list_api(self):
11881260
self.assertEqual(len(response.data["results"]), 1)
11891261
self.assertEqual(response.data["results"][0]["device_name"], "device-0")
11901262
self.assertEqual(response.data["results"][0]["floor"], 0)
1191-
1192-
with self.subTest("Test user without explicit view permission"):
1193-
user = self._create_user(username="org_admin", email="admin@org1.com")
1194-
self._create_org_user(organization=org, user=user, is_admin=True)
1195-
self.client.force_login(user)
1196-
response = self.client.get(path)
1197-
self.assertEqual(response.status_code, 403)
1198-
1199-
with self.subTest("Test with user of different org"):
1200-
org2 = self._create_org(name="org2")
1201-
user = self._create_user(username="org2user", email="user@org2.com")
1202-
self._create_org_user(organization=org2, user=user, is_admin=True)
1203-
self.client.force_login(user)
1204-
response = self.client.get(path)
1205-
self.assertEqual(response.status_code, 404)
1206-
1207-
with self.subTest("Test user of org2 try to access its data and of org"):
1208-
location5 = self._create_location(type="indoor", organization=org2)
1209-
f3 = self._create_floorplan(floor=1, location=location5)
1210-
d3 = self._create_device(
1211-
name="device3", mac_address="00:00:00:00:00:03", organization=org2
1212-
)
1213-
self._create_object_location(
1214-
content_object=d3,
1215-
location=location5,
1216-
floorplan=f3,
1217-
organization=org2,
1218-
)
1219-
path2 = reverse("geo_api:indoor_coordinates_list", args=[location5.id])
1220-
view_perm = Permission.objects.filter(codename="view_devicelocation")
1221-
user.user_permissions.add(*view_perm)
1222-
self.client.force_login(user)
1223-
response = self.client.get(path2)
1224-
self.assertEqual(response.status_code, 200)
1225-
self.assertEqual(len(response.data["results"]), 1)
1226-
self.assertEqual(response.data["results"][0]["device_name"], "device3")
1227-
self.assertEqual(response.data["results"][0]["floor"], 1)
1228-
1229-
with self.subTest("Test with administrator which manage the device org"):
1230-
administrator = self._create_administrator(organizations=[org, org2])
1231-
self.client.force_login(administrator)
1232-
response = self.client.get(path)
1233-
self.assertEqual(response.status_code, 200)
1234-
1235-
with self.subTest("Test for unauthenticated user"):
1236-
self.client.logout()
1237-
response = self.client.get(path)
1238-
self.assertEqual(response.status_code, 401)

0 commit comments

Comments
 (0)