Skip to content

Commit

Permalink
[fix] Fixed bug in device location API browsable UI page
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed Jan 19, 2022
1 parent 171a9fa commit 04dd632
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion openwisp_controller/geo/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@

class DevicePermission(BasePermission):
def has_object_permission(self, request, view, obj):
return request.query_params.get('key') == obj.key
# checks for presence of key attribute first
# because in the browsable UI this method is
# getting passed also Location instances,
# which do not have the key attribute
return hasattr(obj, 'key') and request.query_params.get('key') == obj.key


class ListViewPagination(pagination.PageNumberPagination):
Expand Down
9 changes: 9 additions & 0 deletions openwisp_controller/geo/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ def test_get_existing_location(self):
)
self.assertEqual(self.location_model.objects.count(), 1)

def test_get_existing_location_html(self):
"""
Regression test for browsable web UI bug
"""
dl = self._create_object_location()
url = reverse(self.url_name, args=[dl.device.pk])
r = self.client.get(url, {'key': dl.device.key}, HTTP_ACCEPT='text/html')
self.assertEqual(r.status_code, 200)

def test_get_create_location(self):
self.assertEqual(self.location_model.objects.count(), 0)
device = self._create_object()
Expand Down

0 comments on commit 04dd632

Please sign in to comment.