Skip to content

Commit 3997b12

Browse files
committed
[chores] Added allowed commands to model method
Modified `get_org_choices` method to fetch choices as list of tuples to support labels wherever possible. Refactored test cases Signed-off-by: DragnEmperor <dragnemperor@gmail.com>
1 parent a2a669f commit 3997b12

4 files changed

Lines changed: 20 additions & 21 deletions

File tree

openwisp_controller/connection/api/serializers.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,9 @@ def __init__(self, *args, **kwargs):
4141
].queryset.filter(device_id=device_id)
4242
device = Device.objects.only('organization_id', 'id').get(pk=device_id)
4343
# filter command types based on the device's organization
44-
allowed_commands = Command.get_org_choices(device.organization_id)
45-
# this translates the commands to their labels
46-
commands_map = dict(COMMAND_CHOICES)
47-
self.fields['type'].choices = [
48-
(i, commands_map[i]) for i in commands_map if i in allowed_commands
49-
]
44+
self.fields['type'].choices = Command.get_org_allowed_commands(
45+
device.organization_id
46+
)
5047

5148
def to_representation(self, instance):
5249
repr = super().to_representation(instance)

openwisp_controller/connection/base/models.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,10 +437,15 @@ class Meta:
437437
ordering = ('created',)
438438

439439
@classmethod
440-
def get_org_choices(self, organization_id=None):
441-
return ORGANIZATION_ENABLED_COMMANDS.get(
440+
def get_org_allowed_commands(self, organization_id=None):
441+
"""
442+
Returns a list of allowed commands for the given organization
443+
"""
444+
allowed_commands = ORGANIZATION_ENABLED_COMMANDS.get(
442445
str(organization_id), ORGANIZATION_ENABLED_COMMANDS.get('__all__')
443446
)
447+
commands_map = dict(COMMAND_CHOICES)
448+
return [(i, commands_map[i]) for i in commands_map if i in allowed_commands]
444449

445450
@classmethod
446451
def get_org_schema(self, organization_id=None):
@@ -476,8 +481,9 @@ def _verify_command_type_allowed(self):
476481
# (standard model validation will kick in)
477482
if not hasattr(self, 'device'):
478483
return
479-
if self.type not in self.get_org_choices(
480-
organization_id=self.device.organization_id
484+
485+
if self.type not in dict(
486+
self.get_org_allowed_commands(organization_id=self.device.organization_id)
481487
):
482488
raise ValidationError(
483489
{

openwisp_controller/connection/tests/test_api.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ def test_non_existent_command(self):
355355
self.assertEqual(response.status_code, 400)
356356
self.assertIn(
357357
'"custom" is not a valid choice.',
358-
response.json()['type'][0],
358+
response.data['type'][0],
359359
)
360360

361361
def test_create_command_without_connection(self):
@@ -373,9 +373,9 @@ def test_create_command_without_connection(self):
373373
content_type='application/json',
374374
)
375375
self.assertEqual(response.status_code, 400)
376-
self.assertDictEqual(
377-
response.json(),
378-
{'device': ['Device has no credentials assigned.']},
376+
self.assertIn(
377+
'Device has no credentials assigned.',
378+
response.data['device'][0],
379379
)
380380

381381

openwisp_controller/connection/tests/test_models.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -564,14 +564,10 @@ def test_command_validation(self):
564564
)
565565

566566
with self.subTest('Test command creation without device connection'):
567-
device = self._create_device(
568-
name='default.test.device2', mac_address='11:22:33:44:55:66'
569-
)
570-
new_command = Command(
571-
device=device, type='custom', input={'command': 'echo test'}
572-
)
567+
device = dc.device
568+
device.deviceconnection_set.all().delete()
573569
with self.assertRaises(ValidationError) as context_manager:
574-
new_command.full_clean()
570+
command.full_clean()
575571
exception = context_manager.exception
576572
self.assertIn('device', exception.message_dict)
577573
self.assertEqual(

0 commit comments

Comments
 (0)