Skip to content

Commit 4c953dc

Browse files
authored
[fix] Separated tests bound to development environment
Removed settings specifically set for running tests in settings.py and mocked then on per test basis. After upgrading openwisp-controller in anisble-openwisp2 role, the build was failing due to failing test cases. After debugging, I found out that there are specific settings which are set in test project of openwisp-controller which are absent from settings.py template in ansible-openwisp2. The separation performed in this change allows to fix that problem cleanly.
1 parent e448e1b commit 4c953dc

File tree

7 files changed

+15
-10
lines changed

7 files changed

+15
-10
lines changed

openwisp_controller/config/tests/test_admin.py

+2
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ def test_download_device_config_404(self):
445445
response = self.client.get(path)
446446
self.assertEqual(response.status_code, 404)
447447

448+
@patch('openwisp_controller.config.settings.HARDWARE_ID_ENABLED', True)
448449
def test_preview_device_config(self):
449450
templates = Template.objects.all()
450451
path = reverse(f'admin:{self.app_label}_device_preview')
@@ -807,6 +808,7 @@ def test_ip_in_change_device(self):
807808
response = self.client.get(path)
808809
self.assertContains(response, 'last_ip')
809810

811+
@patch('openwisp_controller.config.settings.HARDWARE_ID_ENABLED', True)
810812
def test_hardware_id_in_change_device(self):
811813
d = self._create_device()
812814
t = Template.objects.first()

openwisp_controller/config/tests/test_config.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from copy import deepcopy
2+
from unittest.mock import patch
23

3-
from django.conf import settings
44
from django.core.exceptions import ValidationError
55
from django.db.transaction import atomic
66
from django.test import TestCase
@@ -229,13 +229,14 @@ def test_context_validation(self):
229229
'the supplied value is not a JSON object', message_dict['context']
230230
)
231231

232+
@patch.dict(app_settings.CONTEXT, {'vpnserver1': 'vpn.testdomain.com'})
232233
def test_context_setting(self):
233234
config = {'general': {'vpnserver1': '{{ vpnserver1 }}'}}
234235
c = Config(
235236
device=self._create_device(), backend='netjsonconfig.OpenWrt', config=config
236237
)
237238
output = c.backend_instance.render()
238-
vpnserver1 = settings.OPENWISP_CONTROLLER_CONTEXT['vpnserver1']
239+
vpnserver1 = app_settings.CONTEXT['vpnserver1']
239240
self.assertIn(vpnserver1, output)
240241

241242
def test_mac_address_as_hostname(self):

openwisp_controller/config/tests/test_controller.py

+1
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ def test_registration_disabled(self):
705705
self.assertEqual(response.status_code, 403)
706706

707707
@patch('openwisp_controller.config.settings.REGISTRATION_SELF_CREATION', False)
708+
@patch('openwisp_controller.config.settings.HARDWARE_ID_ENABLED', True)
708709
def test_self_creation_disabled(self):
709710
self._create_org()
710711
options = {

openwisp_controller/config/tests/test_device.py

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def test_str_name(self):
2828
d = Device(name='test')
2929
self.assertEqual(str(d), 'test')
3030

31+
@mock.patch('openwisp_controller.config.settings.HARDWARE_ID_ENABLED', True)
3132
@mock.patch('openwisp_controller.config.settings.HARDWARE_ID_AS_NAME', True)
3233
def test_str_hardware_id(self):
3334
d = Device(name='test', hardware_id='123')

openwisp_controller/config/tests/test_template.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from unittest import mock
33

44
from celery.exceptions import SoftTimeLimitExceeded
5-
from django.conf import settings
65
from django.contrib.auth import get_user_model
76
from django.core.exceptions import ValidationError
87
from django.test import TestCase, TransactionTestCase
@@ -145,6 +144,7 @@ def test_auto_client_template_auto_cert_False(self):
145144
self.assertEqual(len(t.config['files']), 1)
146145
self.assertIn('ca_path', t.config['files'][0]['path'])
147146

147+
@mock.patch.dict(app_settings.CONTEXT, {'vpnserver1': 'vpn.testdomain.com'})
148148
def test_template_context_var(self):
149149
org = self._get_org()
150150
t = self._create_template(
@@ -164,13 +164,14 @@ def test_template_context_var(self):
164164
# clear cache
165165
del c.backend_instance
166166
output = c.backend_instance.render()
167-
vpnserver1 = settings.OPENWISP_CONTROLLER_CONTEXT['vpnserver1']
167+
vpnserver1 = app_settings.CONTEXT['vpnserver1']
168168
self.assertIn(vpnserver1, output)
169169

170+
@mock.patch.dict(app_settings.CONTEXT, {'vpnserver1': 'vpn.testdomain.com'})
170171
def test_get_context(self):
171172
t = self._create_template()
172173
expected = {}
173-
expected.update(settings.OPENWISP_CONTROLLER_CONTEXT)
174+
expected.update(app_settings.CONTEXT)
174175
self.assertEqual(t.get_context(), expected)
175176

176177
def test_tamplates_clone(self):

openwisp_controller/config/tests/test_vpn.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from unittest import mock
22

33
from celery.exceptions import SoftTimeLimitExceeded
4-
from django.conf import settings
54
from django.core.exceptions import ValidationError
65
from django.test import TestCase, TransactionTestCase
76
from swapper import load_model
@@ -225,6 +224,7 @@ def test_get_auto_context_keys(self):
225224
}
226225
self.assertEqual(keys, control)
227226

227+
@mock.patch.dict(app_settings.CONTEXT, {'vpnserver1': 'vpn.testdomain.com'})
228228
def test_get_context(self):
229229
v = self._create_vpn()
230230
expected = {
@@ -233,7 +233,7 @@ def test_get_context(self):
233233
'key': v.cert.private_key,
234234
'dh': v.dh,
235235
}
236-
expected.update(settings.OPENWISP_CONTROLLER_CONTEXT)
236+
expected.update(app_settings.CONTEXT)
237237
self.assertEqual(v.get_context(), expected)
238238
self.assertNotEqual(v.get_context(), app_settings.CONTEXT)
239239

@@ -248,9 +248,10 @@ def test_dh(self, mocked_dhparam):
248248
self.assertTrue(v.dh.startswith('-----BEGIN DH PARAMETERS-----'))
249249
self.assertTrue(v.dh.endswith('-----END DH PARAMETERS-----\n'))
250250

251+
@mock.patch.dict(app_settings.CONTEXT, {'vpnserver1': 'vpn.testdomain.com'})
251252
def test_get_context_empty_vpn(self):
252253
v = Vpn()
253-
self.assertEqual(v.get_context(), settings.OPENWISP_CONTROLLER_CONTEXT)
254+
self.assertEqual(v.get_context(), app_settings.CONTEXT)
254255

255256
def test_key_validator(self):
256257
v = self._create_vpn()

tests/openwisp2/settings.py

-2
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,6 @@
180180
OPENWISP_CONTROLLER_CONTEXT = {'vpnserver1': 'vpn.testdomain.com'}
181181

182182
TEST_RUNNER = 'openwisp_utils.tests.TimeLoggingTestRunner'
183-
if TESTING:
184-
OPENWISP_CONTROLLER_HARDWARE_ID_ENABLED = True
185183

186184
if os.environ.get('SAMPLE_APP', False):
187185
# Replace Config

0 commit comments

Comments
 (0)