diff --git a/openwisp_controller/config/tests/test_config.py b/openwisp_controller/config/tests/test_config.py index 99ca5fc67..3d25f1c6e 100644 --- a/openwisp_controller/config/tests/test_config.py +++ b/openwisp_controller/config/tests/test_config.py @@ -652,18 +652,36 @@ def test_remove_duplicate_files(self): ] }, ) - config = self._create_config(organization=self._get_org()) - config.templates.add(template1) - config.templates.add(template2) - config.refresh_from_db() - try: - result = config.get_backend_instance( - template_instances=[template1, template2] - ).render() - except ValidationError: - self.fail('ValidationError raised!') - else: - self.assertIn('# path: /etc/vpnserver1', result) + org = self._get_org() + with self.subTest('Test template applied on creating config'): + try: + config = self._create_config( + organization=org, + templates=[template1, template2], + ) + result = config.get_backend_instance( + template_instances=[template1, template2] + ).render() + except ValidationError: + self.fail('ValidationError raised!') + else: + self.assertIn('# path: /etc/vpnserver1', result) + + config.device.delete() + config.delete() + with self.subTest('Test template applied after creating config object'): + config = self._create_config(organization=org) + config.templates.add(template1) + config.templates.add(template2) + config.refresh_from_db() + try: + result = config.get_backend_instance( + template_instances=[template1, template2] + ).render() + except ValidationError: + self.fail('ValidationError raised!') + else: + self.assertIn('# path: /etc/vpnserver1', result) def test_duplicated_files_in_config(self): try: diff --git a/openwisp_controller/config/tests/utils.py b/openwisp_controller/config/tests/utils.py index d5de34cab..90147e3e5 100644 --- a/openwisp_controller/config/tests/utils.py +++ b/openwisp_controller/config/tests/utils.py @@ -59,7 +59,10 @@ def _create_config(self, **kwargs): options['device'] = self._create_device( organization=self._get_org(), name='test-device' ) + templates = options.pop('templates', []) c = Config(**options) + if templates: + c.templates.set(templates) c.full_clean() c.save() return c