Skip to content

Commit 9ca3710

Browse files
committed
[tests] Added test ensuring device's related connections
Added a test to ensure only the connections of device are available on browser api form. Migrated the validation error from serializer to model level. Signed-off-by: DragnEmperor <dragnemperor@gmail.com>
1 parent 6578b12 commit 9ca3710

3 files changed

Lines changed: 34 additions & 12 deletions

File tree

openwisp_controller/connection/api/serializers.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from django.utils.translation import gettext_lazy as _
21
from rest_framework import serializers
32
from swapper import load_model
43

@@ -64,17 +63,6 @@ class Meta:
6463
'modified',
6564
]
6665

67-
def validate(self, value):
68-
super().validate(value)
69-
if not DeviceConnection.objects.filter(
70-
device=self.context['device_id'],
71-
credentials__isnull=False,
72-
).exists():
73-
raise serializers.ValidationError(
74-
detail={'device': _('Device has no credentials assigned.')}
75-
)
76-
return value
77-
7866

7967
class CredentialSerializer(BaseSerializer):
8068
params = serializers.JSONField()

openwisp_controller/connection/base/models.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,11 +459,22 @@ def __str__(self):
459459

460460
def clean(self):
461461
self._verify_command_type_allowed()
462+
self._verify_connection()
462463
try:
463464
jsonschema.Draft4Validator(self._schema).validate(self.input)
464465
except SchemaError as e:
465466
raise ValidationError({'input': e.message})
466467

468+
def _verify_connection(self):
469+
"""Raises validation error if device has no connection and credentials."""
470+
DeviceConnection = load_model('connection', 'DeviceConnection')
471+
472+
if not DeviceConnection.objects.filter(
473+
device=self.device,
474+
credentials__isnull=False,
475+
).exists():
476+
raise ValidationError({'device': _('Device has no credentials assigned.')})
477+
467478
def _verify_command_type_allowed(self):
468479
"""Raises validation error if command type is not allowed."""
469480
# if device is not set, skip to avoid uncaught exception

openwisp_controller/connection/tests/test_api.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from rest_framework.exceptions import ErrorDetail
1111
from swapper import load_model
1212

13+
from openwisp_controller.connection.api.serializers import CommandSerializer
1314
from openwisp_controller.tests.utils import TestAdminMixin
1415
from openwisp_users.tests.test_api import AuthenticationMixin
1516

@@ -175,6 +176,28 @@ def test_command_attributes(self, payload):
175176
self.assertEqual(response.status_code, 201)
176177
test_command_attributes(self, payload)
177178

179+
# for ensuring that only related connections are shown
180+
def test_available_connections(self):
181+
url = self._get_path('device_command_list', self.device_id)
182+
response = self.client.get(url)
183+
serializer = CommandSerializer(
184+
instance=self.device_conn.device,
185+
context={'device_id': self.device_id, 'request': response.wsgi_request},
186+
)
187+
device = self._create_device(
188+
name='default.test.device2', mac_address='12:23:34:45:56:67'
189+
)
190+
self._create_config(device=device)
191+
credentials_2 = self._create_credentials(name='Test Credentials 2')
192+
device_conn2 = self._create_device_connection(
193+
device=device, credentials=credentials_2
194+
)
195+
connections = serializer.fields['connection']
196+
queryset = list(connections.get_queryset().values_list('id', flat=True))
197+
queryset = [str(i) for i in queryset]
198+
self.assertIn(str(self.device_conn.id), queryset)
199+
self.assertNotIn(device_conn2.id, queryset)
200+
178201
def test_command_details_api(self):
179202
command_obj = self._create_command(device_conn=self.device_conn)
180203
url = self._get_path('device_command_details', self.device_id, command_obj.id)

0 commit comments

Comments
 (0)