Skip to content

Commit

Permalink
[tests/fix] Test passing arbitrary org params to register view
Browse files Browse the repository at this point in the history
- test_register_with_org_id: the test was passing without any change
- test_register_with_org_param: trying to pass the org ID in the
  "organization" parameter caused an uncaught exception, the change
  in the code fixes the error
  • Loading branch information
nemesifier committed Jan 18, 2024
1 parent ed1cc56 commit 226442c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions openwisp_controller/config/controller/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ def init_object(self, **kwargs):
config_model = device_model.get_config_model()
options = {}
for attr in kwargs.keys():
if attr in ['organization', 'organization_id']:
continue
# skip attributes that are not model fields
try:
device_model._meta.get_field(attr)
Expand Down
15 changes: 15 additions & 0 deletions openwisp_controller/config/tests/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
Vpn = load_model('config', 'Vpn')
Ca = load_model('django_x509', 'Ca')
OrganizationConfigSettings = load_model('config', 'OrganizationConfigSettings')
Organization = load_model('openwisp_users', 'Organization')


class TestController(
Expand Down Expand Up @@ -430,6 +431,20 @@ def test_register(self, **kwargs):
def test_register_with_management_ip(self):
self.test_register(management_ip='10.0.0.2')

def test_register_with_org_id(self):
org1 = self._get_org()
org2 = Organization.objects.create(name='org2', slug='org2')
device = self.test_register(organization_id=str(org2.id))
self.assertEqual(device.organization, org1)
self.assertNotEqual(device.organization, org2)

def test_register_with_org_param(self):
org1 = self._get_org()
org2 = Organization.objects.create(name='org2', slug='org2')
device = self.test_register(organization=str(org2.id))
self.assertEqual(device.organization, org1)
self.assertNotEqual(device.organization, org2)

def test_register_exceeds_org_device_limit(self):
org = self._get_org()
org.config_limits.device_limit = 1
Expand Down

0 comments on commit 226442c

Please sign in to comment.