1- from copy import deepcopy
2-
31from django .contrib .auth .models import Permission
42from django .test import TestCase
53from django .test .client import BOUNDARY , MULTIPART_CONTENT , encode_multipart
3331
3432
3533class 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
9096class 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' ):
0 commit comments