Skip to content

Commit 1142646

Browse files
committed
[chores] Clean up in tests
1 parent 85f5be9 commit 1142646

File tree

3 files changed

+100
-95
lines changed

3 files changed

+100
-95
lines changed

openwisp_controller/config/api/views.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from openwisp_users.api.permissions import DjangoModelPermissions
1818

1919
from ...mixins import ProtectedAPIMixin
20-
from ...serializers import DeviceContextMixin
2120
from .filters import (
2221
DeviceGroupListFilter,
2322
DeviceListFilter,
@@ -98,9 +97,7 @@ class DeviceListCreateView(ProtectedAPIMixin, ListCreateAPIView):
9897
filterset_class = DeviceListFilter
9998

10099

101-
class DeviceDetailView(
102-
DeviceContextMixin, ProtectedAPIMixin, RetrieveUpdateDestroyAPIView
103-
):
100+
class DeviceDetailView(ProtectedAPIMixin, RetrieveUpdateDestroyAPIView):
104101
"""
105102
Templates: Templates flagged as _required_ will be added automatically
106103
to the `config` of a device and cannot be unassigned.

openwisp_controller/config/tests/test_api.py

Lines changed: 78 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from copy import deepcopy
2-
31
from django.contrib.auth.models import Permission
42
from django.test import TestCase
53
from django.test.client import BOUNDARY, MULTIPART_CONTENT, encode_multipart
@@ -33,58 +31,66 @@
3331

3432

3533
class ApiTestMixin:
36-
_get_template_data = {
37-
'name': 'test-template',
38-
'organization': None,
39-
'backend': 'netjsonconfig.OpenWrt',
40-
'config': {'interfaces': [{'name': 'eth0', 'type': 'ethernet'}]},
41-
}
42-
43-
_get_vpn_data = {
44-
'name': 'vpn-test',
45-
'host': 'vpn.testing.com',
46-
'organization': None,
47-
'ca': None,
48-
'backend': 'openwisp_controller.vpn_backends.OpenVpn',
49-
'config': {
50-
'openvpn': [
51-
{
52-
'ca': 'ca.pem',
53-
'cert': 'cert.pem',
54-
'dev': 'tap0',
55-
'dev_type': 'tap',
56-
'dh': 'dh.pem',
57-
'key': 'key.pem',
58-
'mode': 'server',
59-
'name': 'example-vpn',
60-
'proto': 'udp',
61-
'tls_server': True,
62-
}
63-
]
64-
},
65-
}
66-
67-
_get_device_data = {
68-
'name': 'change-test-device',
69-
'organization': None,
70-
'mac_address': '00:11:22:33:44:55',
71-
'config': {
34+
@property
35+
def _template_data(self):
36+
return {
37+
'name': 'test-template',
38+
'organization': None,
7239
'backend': 'netjsonconfig.OpenWrt',
73-
'status': 'modified',
74-
'templates': [],
75-
'context': {'lan_ip': '192.168.1.1'},
76-
'config': {'interfaces': [{'name': 'wlan0', 'type': 'wireless'}]},
77-
},
78-
}
40+
'config': {'interfaces': [{'name': 'eth0', 'type': 'ethernet'}]},
41+
}
7942

80-
_get_devicegroup_data = {
81-
'name': 'Access Points',
82-
'description': 'Group for APs of default organization',
83-
'organization': 'None',
84-
'meta_data': {'captive_portal_url': 'https://example.com'},
85-
'context': {'SSID': 'OpenWISP'},
86-
'templates': [],
87-
}
43+
@property
44+
def _vpn_data(self):
45+
return {
46+
'name': 'vpn-test',
47+
'host': 'vpn.testing.com',
48+
'organization': None,
49+
'ca': None,
50+
'backend': 'openwisp_controller.vpn_backends.OpenVpn',
51+
'config': {
52+
'openvpn': [
53+
{
54+
'ca': 'ca.pem',
55+
'cert': 'cert.pem',
56+
'dev': 'tap0',
57+
'dev_type': 'tap',
58+
'dh': 'dh.pem',
59+
'key': 'key.pem',
60+
'mode': 'server',
61+
'name': 'example-vpn',
62+
'proto': 'udp',
63+
'tls_server': True,
64+
}
65+
]
66+
},
67+
}
68+
69+
@property
70+
def _device_data(self):
71+
return {
72+
'name': 'change-test-device',
73+
'organization': None,
74+
'mac_address': '00:11:22:33:44:55',
75+
'config': {
76+
'backend': 'netjsonconfig.OpenWrt',
77+
'status': 'modified',
78+
'templates': [],
79+
'context': {'lan_ip': '192.168.1.1'},
80+
'config': {'interfaces': [{'name': 'wlan0', 'type': 'wireless'}]},
81+
},
82+
}
83+
84+
@property
85+
def _devicegroup_data(self):
86+
return {
87+
'name': 'Access Points',
88+
'description': 'Group for APs of default organization',
89+
'organization': 'None',
90+
'meta_data': {'captive_portal_url': 'https://example.com'},
91+
'context': {'SSID': 'OpenWISP'},
92+
'templates': [],
93+
}
8894

8995

9096
class TestConfigApi(
@@ -104,7 +110,7 @@ def setUp(self):
104110
def test_device_create_with_config_api(self):
105111
self.assertEqual(Device.objects.count(), 0)
106112
path = reverse('config_api:device_list')
107-
data = self._get_device_data.copy()
113+
data = self._device_data
108114
org = self._get_org()
109115
data['organization'] = org.pk
110116
r = self.client.post(path, data, content_type='application/json')
@@ -131,7 +137,7 @@ def test_config_serializer_validation(self):
131137
def test_device_create_no_config_api(self):
132138
self.assertEqual(Device.objects.count(), 0)
133139
path = reverse('config_api:device_list')
134-
data = self._get_device_data.copy()
140+
data = self._device_data
135141
org = self._get_org()
136142
data['organization'] = org.pk
137143
data.pop('config')
@@ -143,7 +149,7 @@ def test_device_create_no_config_api(self):
143149
def test_device_create_default_config_values(self):
144150
self.assertEqual(Device.objects.count(), 0)
145151
path = reverse('config_api:device_list')
146-
data = deepcopy(self._get_device_data)
152+
data = self._device_data
147153
org = self._get_org()
148154
data['organization'] = org.pk
149155
data['config'].update({'context': {}, 'config': {}})
@@ -158,7 +164,7 @@ def test_device_create_with_group(self):
158164
template = self._create_template()
159165
dg.templates.add(template)
160166
path = reverse('config_api:device_list')
161-
data = self._get_device_data.copy()
167+
data = self._device_data
162168
org = self._get_org()
163169
data['organization'] = org.pk
164170
data['group'] = dg.pk
@@ -177,7 +183,7 @@ def test_device_create_exceeds_org_device_limit(self):
177183
self.assertEqual(Device.objects.count(), 1)
178184

179185
path = reverse('config_api:device_list')
180-
data = self._get_device_data.copy()
186+
data = self._device_data
181187
data['organization'] = org.pk
182188
r = self.client.post(path, data, content_type='application/json')
183189
self.assertEqual(r.status_code, 400)
@@ -190,7 +196,7 @@ def test_device_create_exceeds_org_device_limit(self):
190196

191197
def test_device_create_with_invalid_name_api(self):
192198
path = reverse('config_api:device_list')
193-
data = self._get_device_data.copy()
199+
data = self._device_data
194200
org = self._get_org()
195201
data.pop('config')
196202
data['name'] = 'T E S T'
@@ -202,7 +208,7 @@ def test_device_create_with_invalid_name_api(self):
202208
# POST request should fail with validation error
203209
def test_device_post_with_templates_of_different_org(self):
204210
path = reverse('config_api:device_list')
205-
data = deepcopy(self._get_device_data)
211+
data = self._device_data
206212
org_1 = self._get_org()
207213
data['organization'] = org_1.pk
208214
org_2 = self._create_org(name='test org2', slug='test-org2')
@@ -227,7 +233,7 @@ def execute_assertions(data):
227233
def test_device_create_with_devicegroup(self):
228234
self.assertEqual(Device.objects.count(), 0)
229235
path = reverse('config_api:device_list')
230-
data = self._get_device_data.copy()
236+
data = self._device_data
231237
org = self._get_org()
232238
device_group = self._create_device_group()
233239
data['organization'] = org.pk
@@ -598,7 +604,7 @@ def test_deactivating_device_force_deletion(self):
598604
def test_template_create_no_org_api(self):
599605
self.assertEqual(Template.objects.count(), 0)
600606
path = reverse('config_api:template_list')
601-
data = self._get_template_data.copy()
607+
data = self._template_data
602608
r = self.client.post(path, data, content_type='application/json')
603609
self.assertEqual(Template.objects.count(), 1)
604610
self.assertEqual(r.status_code, 201)
@@ -609,7 +615,7 @@ def test_template_create_vpn_with_type_as_generic(self):
609615
test_user = self._create_operator(organizations=[self._get_org()])
610616
self.client.force_login(test_user)
611617
vpn1 = self._create_vpn(name='vpn1', organization=self._get_org())
612-
data = self._get_template_data.copy()
618+
data = self._template_data
613619
data['organization'] = self._get_org().pk
614620
data['type'] = 'generic'
615621
data['vpn'] = vpn1.id
@@ -622,7 +628,7 @@ def test_template_create_api(self):
622628
self.assertEqual(Template.objects.count(), 0)
623629
org = self._get_org()
624630
path = reverse('config_api:template_list')
625-
data = self._get_template_data.copy()
631+
data = self._template_data
626632
data['organization'] = org.pk
627633
data['required'] = True
628634
r = self.client.post(path, data, content_type='application/json')
@@ -634,7 +640,7 @@ def test_template_create_of_vpn_type(self):
634640
org = self._get_org()
635641
vpn1 = self._create_vpn(name='vpn1', organization=org)
636642
path = reverse('config_api:template_list')
637-
data = self._get_template_data.copy()
643+
data = self._template_data
638644
data['type'] = 'vpn'
639645
data['vpn'] = vpn1.id
640646
data['organization'] = org.pk
@@ -648,7 +654,7 @@ def test_template_create_with_shared_vpn(self):
648654
self.client.force_login(test_user)
649655
vpn1 = self._create_vpn(name='vpn1', organization=None)
650656
path = reverse('config_api:template_list')
651-
data = self._get_template_data.copy()
657+
data = self._template_data
652658
data['type'] = 'vpn'
653659
data['vpn'] = vpn1.id
654660
data['organization'] = org1.pk
@@ -659,7 +665,7 @@ def test_template_create_with_shared_vpn(self):
659665

660666
def test_template_creation_with_no_org_by_operator(self):
661667
path = reverse('config_api:template_list')
662-
data = self._get_template_data.copy()
668+
data = self._template_data
663669
test_user = self._create_operator(organizations=[self._get_org()])
664670
self.client.force_login(test_user)
665671
r = self.client.post(path, data, content_type='application/json')
@@ -668,7 +674,7 @@ def test_template_creation_with_no_org_by_operator(self):
668674

669675
def test_template_create_with_empty_config(self):
670676
path = reverse('config_api:template_list')
671-
data = self._get_template_data.copy()
677+
data = self._template_data
672678
data['config'] = {}
673679
data['organization'] = self._get_org().pk
674680
r = self.client.post(path, data, content_type='application/json')
@@ -820,7 +826,7 @@ def test_vpn_create_api(self):
820826
self.assertEqual(Vpn.objects.count(), 0)
821827
path = reverse('config_api:vpn_list')
822828
ca1 = self._create_ca()
823-
data = self._get_vpn_data.copy()
829+
data = self._vpn_data
824830
data['ca'] = ca1.pk
825831
r = self.client.post(path, data, content_type='application/json')
826832
self.assertEqual(r.status_code, 201)
@@ -831,7 +837,7 @@ def test_vpn_create_with_shared_objects(self):
831837
shared_ca = self._create_ca(name='shared_ca', organization=None)
832838
test_user = self._create_administrator(organizations=[org1])
833839
self.client.force_login(test_user)
834-
data = self._get_vpn_data.copy()
840+
data = self._vpn_data
835841
data['organization'] = org1.pk
836842
data['ca'] = shared_ca.pk
837843
path = reverse('config_api:vpn_list')
@@ -1008,7 +1014,7 @@ def test_devicegroup_create_api(self):
10081014
org = self._get_org()
10091015
template = self._create_template(name='t1', organization=org)
10101016
path = reverse('config_api:devicegroup_list')
1011-
data = self._get_devicegroup_data.copy()
1017+
data = self._devicegroup_data
10121018
data['organization'] = org.pk
10131019
data['templates'] = [str(template.pk)]
10141020
response = self.client.post(path, data, content_type='application/json')
@@ -1098,13 +1104,13 @@ def test_devicegroup_detail_api(self):
10981104
with self.subTest('Test PATCH'):
10991105
response = self.client.patch(
11001106
path,
1101-
data={'meta_data': self._get_devicegroup_data['meta_data']},
1107+
data={'meta_data': self._devicegroup_data['meta_data']},
11021108
content_type='application/json',
11031109
)
11041110
self.assertEqual(response.status_code, 200)
11051111
device_group.refresh_from_db()
11061112
self.assertDictEqual(
1107-
device_group.meta_data, self._get_devicegroup_data['meta_data']
1113+
device_group.meta_data, self._devicegroup_data['meta_data']
11081114
)
11091115

11101116
with self.subTest('Test DELETE'):

openwisp_controller/pki/tests/test_api.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,28 @@ def setUp(self):
2727
super().setUp()
2828
self._login()
2929

30-
_get_ca_data = {
31-
'name': 'Test CA',
32-
'organization': None,
33-
'key_length': '2048',
34-
'digest': 'sha256',
35-
}
36-
37-
_get_cert_data = {
38-
'name': 'Test Cert',
39-
'organization': None,
40-
'ca': None,
41-
'key_length': '2048',
42-
'digest': 'sha256',
43-
'serial_number': "",
44-
}
30+
def _ca_data(self):
31+
return {
32+
'name': 'Test CA',
33+
'organization': None,
34+
'key_length': '2048',
35+
'digest': 'sha256',
36+
}
37+
38+
def _cert_data(self):
39+
return {
40+
'name': 'Test Cert',
41+
'organization': None,
42+
'ca': None,
43+
'key_length': '2048',
44+
'digest': 'sha256',
45+
'serial_number': "",
46+
}
4547

4648
def test_ca_post_api(self):
4749
self.assertEqual(Ca.objects.count(), 0)
4850
path = reverse('pki_api:ca_list')
49-
data = self._get_ca_data.copy()
51+
data = self._ca_data
5052
with self.assertNumQueries(4):
5153
r = self.client.post(path, data, content_type='application/json')
5254
self.assertEqual(r.status_code, 201)
@@ -55,7 +57,7 @@ def test_ca_post_api(self):
5557
def test_ca_post_with_extensions_field(self):
5658
self.assertEqual(Ca.objects.count(), 0)
5759
path = reverse('pki_api:ca_list')
58-
data = self._get_ca_data.copy()
60+
data = self._ca_data
5961
data['extensions'] = []
6062
with self.assertNumQueries(4):
6163
r = self.client.post(path, data, content_type='application/json')
@@ -166,7 +168,7 @@ def test_ca_post_renew_api(self):
166168

167169
def test_cert_post_api(self):
168170
path = reverse('pki_api:cert_list')
169-
data = self._get_cert_data.copy()
171+
data = self._cert_data
170172
data['ca'] = self._create_ca().pk
171173
with self.assertNumQueries(8):
172174
r = self.client.post(path, data, content_type='application/json')
@@ -198,7 +200,7 @@ def test_import_cert_post_api(self):
198200

199201
def test_cert_post_with_extensions_field(self):
200202
path = reverse('pki_api:cert_list')
201-
data = self._get_cert_data.copy()
203+
data = self._cert_data
202204
data['ca'] = self._create_ca().pk
203205
data['extensions'] = []
204206
with self.assertNumQueries(8):

0 commit comments

Comments
 (0)