Skip to content

Commit ee590ef

Browse files
committed
[fix] Fixed CONTEXT setting getting modified at runtime #272
Fixes #272
1 parent ea266bc commit ee590ef

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

openwisp_controller/config/base/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def get_config(self):
9595
return c
9696

9797
def get_context(self):
98-
return app_settings.CONTEXT
98+
return app_settings.CONTEXT.copy()
9999

100100
@classmethod
101101
def validate_netjsonconfig_backend(cls, backend):

openwisp_controller/config/tests/test_device.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77

88
from openwisp_users.tests.utils import TestOrganizationMixin
99

10+
from .. import settings as app_settings
1011
from ..validators import device_name_validator, mac_address_validator
1112
from .utils import CreateConfigTemplateMixin
1213

1314
TEST_ORG_SHARED_SECRET = 'functional_testing_secret'
1415

1516
Config = load_model('config', 'Config')
1617
Device = load_model('config', 'Device')
18+
_original_context = app_settings.CONTEXT.copy()
1719

1820

1921
class TestDevice(CreateConfigTemplateMixin, TestOrganizationMixin, TestCase):
@@ -154,7 +156,9 @@ def test_add_device_with_context(self):
154156
def test_get_context_with_config(self):
155157
d = self._create_device()
156158
c = self._create_config(device=d)
159+
self.assertEqual(app_settings.CONTEXT, _original_context)
157160
self.assertEqual(d.get_context(), c.get_context())
161+
self.assertEqual(app_settings.CONTEXT, _original_context)
158162

159163
def test_get_context_without_config(self):
160164
d = self._create_device()

openwisp_controller/config/tests/test_template.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from openwisp_users.tests.utils import TestOrganizationMixin
99
from openwisp_utils.tests import catch_signal
1010

11+
from .. import settings as app_settings
1112
from ..signals import config_modified, config_status_changed
1213
from .utils import CreateConfigTemplateMixin, TestVpnX509Mixin
1314

@@ -19,6 +20,8 @@
1920
Cert = load_model('django_x509', 'Cert')
2021
User = get_user_model()
2122

23+
_original_context = app_settings.CONTEXT.copy()
24+
2225

2326
class TestTemplate(
2427
TestOrganizationMixin, CreateConfigTemplateMixin, TestVpnX509Mixin, TestCase
@@ -412,3 +415,24 @@ def test_template_name_and_organization_unique(self):
412415
# two templates with the same organization can't have the same name
413416
with self.assertRaises(ValidationError):
414417
self._create_template(**kwargs)
418+
419+
def test_context_regression(self):
420+
self.test_auto_generated_certificate_for_organization()
421+
422+
with self.subTest('test Template.get_context()'):
423+
template_qs = Template.objects.filter(type='vpn')
424+
self.assertEqual(template_qs.count(), 1)
425+
t = template_qs.first()
426+
self.assertEqual(t.get_context(), _original_context)
427+
self.assertEqual(app_settings.CONTEXT, _original_context)
428+
429+
with self.subTest(
430+
'test Device.get_context() interacting with VPN client template'
431+
):
432+
device_qs = Device.objects.all()
433+
self.assertEqual(device_qs.count(), 1)
434+
d = device_qs.first()
435+
orig_context_set = set(_original_context.items())
436+
context_set = set(d.get_context().items())
437+
self.assertTrue(orig_context_set.issubset(context_set))
438+
self.assertEqual(app_settings.CONTEXT, _original_context)

openwisp_controller/config/tests/test_vpn.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from openwisp_users.tests.utils import TestOrganizationMixin
99

1010
from ...vpn_backends import OpenVpn
11+
from .. import settings as app_settings
1112
from .utils import CreateConfigTemplateMixin, TestVpnX509Mixin
1213

1314
Config = load_model('config', 'Config')
@@ -232,6 +233,7 @@ def test_get_context(self):
232233
}
233234
expected.update(settings.OPENWISP_CONTROLLER_CONTEXT)
234235
self.assertEqual(v.get_context(), expected)
236+
self.assertNotEqual(v.get_context(), app_settings.CONTEXT)
235237

236238
@mock.patch('openwisp_controller.config.base.vpn.AbstractVpn.dhparam')
237239
def test_dh(self, mocked_dhparam):

0 commit comments

Comments
 (0)