Skip to content

Commit

Permalink
[fix] Fixed DeviceMetricView post request for re-activated devices
Browse files Browse the repository at this point in the history
  • Loading branch information
pandafy committed Nov 14, 2024
1 parent c6927eb commit 9332e79
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
6 changes: 6 additions & 0 deletions openwisp_monitoring/device/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from openwisp_controller.config.signals import (
checksum_requested,
config_status_changed,
device_activated,
device_deactivated,
)
from openwisp_controller.connection import settings as connection_settings
Expand Down Expand Up @@ -160,6 +161,11 @@ def connect_device_signals(self):
sender=Device,
dispatch_uid=('device_deactivated_invalidate_view_device_cache'),
)
device_activated.connect(
DeviceMetricView.invalidate_get_device_cache,
sender=Device,
dispatch_uid=('device_activated_invalidate_view_device_cache'),
)

@classmethod
def device_post_save_receiver(cls, instance, created, **kwargs):
Expand Down
38 changes: 22 additions & 16 deletions openwisp_monitoring/device/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,6 @@ def test_200_none(self):
def test_200_create(self):
self.create_test_data(no_resources=True)

def test_deactivated_device_read_only(self):
self.create_test_data(no_resources=True)
device = self.device_model.objects.first()
device.deactivate()
response = self._post_data(device.id, device.key, {'type': 'DeviceMonitoring'})
self.assertEqual(response.status_code, 404)
# Read requests should continue to work
response = self.client.get(self._url(device.pk, device.key))
self.assertEqual(response.status_code, 200)

@patch('openwisp_monitoring.device.tasks.write_device_metrics.delay')
def test_background_write(self, mocked_task):
device = self._create_device(organization=self._create_org())
Expand Down Expand Up @@ -358,14 +348,30 @@ def test_404_disabled_organization(self):
self.assertEqual(self.metric_queryset.count(), 0)
self.assertEqual(self.chart_queryset.count(), 0)

def test_404_deactivated_device(self):
device = self._create_device()
device.deactivate()
def test_device_activate_deactivate(self):
# "self.create_test_data" creates a device and makes
# a POST request to DeviceMetricView ensuring that
# the device is cached.
self.create_test_data(no_resources=True)
device = self.device_model.objects.first()
data = {'type': 'DeviceMonitoring'}
with self.assertNumQueries(2):
response = self._post_data(device.id, device.key, self._data())
response = self._post_data(device.id, device.key, data)

# Deactivating the device will invalidate the cache.
# The view will only allow readonly requests (GET).
device.deactivate()
response = self.client.get(self._url(device.pk, device.key))
self.assertEqual(response.status_code, 200)
with self.assertNumQueries(1):
response = self._post_data(device.id, device.key, data)
self.assertEqual(response.status_code, 404)
self.assertEqual(self.metric_queryset.count(), 0)
self.assertEqual(self.chart_queryset.count(), 0)

# Re-activating the device will allow POST requests again.
device.activate()
with self.assertNumQueries(4):
response = self._post_data(device.id, device.key, data)
self.assertEqual(response.status_code, 200)

def test_garbage_wireless_clients(self):
o = self._create_org()
Expand Down

0 comments on commit 9332e79

Please sign in to comment.