Skip to content

Commit

Permalink
[fix] Separated tests bound to development environment
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pandafy authored Dec 1, 2020
1 parent e448e1b commit 4c953dc
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 10 deletions.
2 changes: 2 additions & 0 deletions openwisp_controller/config/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ def test_download_device_config_404(self):
response = self.client.get(path)
self.assertEqual(response.status_code, 404)

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

@patch('openwisp_controller.config.settings.HARDWARE_ID_ENABLED', True)
def test_hardware_id_in_change_device(self):
d = self._create_device()
t = Template.objects.first()
Expand Down
5 changes: 3 additions & 2 deletions openwisp_controller/config/tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from copy import deepcopy
from unittest.mock import patch

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

@patch.dict(app_settings.CONTEXT, {'vpnserver1': 'vpn.testdomain.com'})
def test_context_setting(self):
config = {'general': {'vpnserver1': '{{ vpnserver1 }}'}}
c = Config(
device=self._create_device(), backend='netjsonconfig.OpenWrt', config=config
)
output = c.backend_instance.render()
vpnserver1 = settings.OPENWISP_CONTROLLER_CONTEXT['vpnserver1']
vpnserver1 = app_settings.CONTEXT['vpnserver1']
self.assertIn(vpnserver1, output)

def test_mac_address_as_hostname(self):
Expand Down
1 change: 1 addition & 0 deletions openwisp_controller/config/tests/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ def test_registration_disabled(self):
self.assertEqual(response.status_code, 403)

@patch('openwisp_controller.config.settings.REGISTRATION_SELF_CREATION', False)
@patch('openwisp_controller.config.settings.HARDWARE_ID_ENABLED', True)
def test_self_creation_disabled(self):
self._create_org()
options = {
Expand Down
1 change: 1 addition & 0 deletions openwisp_controller/config/tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def test_str_name(self):
d = Device(name='test')
self.assertEqual(str(d), 'test')

@mock.patch('openwisp_controller.config.settings.HARDWARE_ID_ENABLED', True)
@mock.patch('openwisp_controller.config.settings.HARDWARE_ID_AS_NAME', True)
def test_str_hardware_id(self):
d = Device(name='test', hardware_id='123')
Expand Down
7 changes: 4 additions & 3 deletions openwisp_controller/config/tests/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from unittest import mock

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

@mock.patch.dict(app_settings.CONTEXT, {'vpnserver1': 'vpn.testdomain.com'})
def test_template_context_var(self):
org = self._get_org()
t = self._create_template(
Expand All @@ -164,13 +164,14 @@ def test_template_context_var(self):
# clear cache
del c.backend_instance
output = c.backend_instance.render()
vpnserver1 = settings.OPENWISP_CONTROLLER_CONTEXT['vpnserver1']
vpnserver1 = app_settings.CONTEXT['vpnserver1']
self.assertIn(vpnserver1, output)

@mock.patch.dict(app_settings.CONTEXT, {'vpnserver1': 'vpn.testdomain.com'})
def test_get_context(self):
t = self._create_template()
expected = {}
expected.update(settings.OPENWISP_CONTROLLER_CONTEXT)
expected.update(app_settings.CONTEXT)
self.assertEqual(t.get_context(), expected)

def test_tamplates_clone(self):
Expand Down
7 changes: 4 additions & 3 deletions openwisp_controller/config/tests/test_vpn.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from unittest import mock

from celery.exceptions import SoftTimeLimitExceeded
from django.conf import settings
from django.core.exceptions import ValidationError
from django.test import TestCase, TransactionTestCase
from swapper import load_model
Expand Down Expand Up @@ -225,6 +224,7 @@ def test_get_auto_context_keys(self):
}
self.assertEqual(keys, control)

@mock.patch.dict(app_settings.CONTEXT, {'vpnserver1': 'vpn.testdomain.com'})
def test_get_context(self):
v = self._create_vpn()
expected = {
Expand All @@ -233,7 +233,7 @@ def test_get_context(self):
'key': v.cert.private_key,
'dh': v.dh,
}
expected.update(settings.OPENWISP_CONTROLLER_CONTEXT)
expected.update(app_settings.CONTEXT)
self.assertEqual(v.get_context(), expected)
self.assertNotEqual(v.get_context(), app_settings.CONTEXT)

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

@mock.patch.dict(app_settings.CONTEXT, {'vpnserver1': 'vpn.testdomain.com'})
def test_get_context_empty_vpn(self):
v = Vpn()
self.assertEqual(v.get_context(), settings.OPENWISP_CONTROLLER_CONTEXT)
self.assertEqual(v.get_context(), app_settings.CONTEXT)

def test_key_validator(self):
v = self._create_vpn()
Expand Down
2 changes: 0 additions & 2 deletions tests/openwisp2/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,6 @@
OPENWISP_CONTROLLER_CONTEXT = {'vpnserver1': 'vpn.testdomain.com'}

TEST_RUNNER = 'openwisp_utils.tests.TimeLoggingTestRunner'
if TESTING:
OPENWISP_CONTROLLER_HARDWARE_ID_ENABLED = True

if os.environ.get('SAMPLE_APP', False):
# Replace Config
Expand Down

0 comments on commit 4c953dc

Please sign in to comment.