-
Notifications
You must be signed in to change notification settings - Fork 167
/
tests.py
354 lines (294 loc) · 14.6 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
from django.contrib.auth import get_user_model
from django.core.cache import cache
from django.core.exceptions import ValidationError
from django.test import TestCase
from django.urls import reverse
from django_freeradius.migrations import (DEFAULT_SESSION_TIME_LIMIT, DEFAULT_SESSION_TRAFFIC_LIMIT,
SESSION_TIME_ATTRIBUTE, SESSION_TRAFFIC_ATTRIBUTE)
from django_freeradius.tests import FileMixin
from django_freeradius.tests import PostParamsMixin as BasePostParamsMixin
from django_freeradius.tests.base.test_admin import BaseTestAdmin
from django_freeradius.tests.base.test_api import BaseTestApi, BaseTestApiReject
from django_freeradius.tests.base.test_batch_add_users import BaseTestCSVUpload
from django_freeradius.tests.base.test_commands import BaseTestCommands
from django_freeradius.tests.base.test_models import (BaseTestNas, BaseTestRadiusAccounting,
BaseTestRadiusBatch, BaseTestRadiusCheck,
BaseTestRadiusGroup, BaseTestRadiusPostAuth,
BaseTestRadiusReply)
from django_freeradius.tests.base.test_social import BaseTestSocial
from django_freeradius.tests.base.test_utils import BaseTestUtils
from openwisp_users.models import Organization, OrganizationUser
from .mixins import CallCommandMixin, CreateRadiusObjectsMixin, PostParamsMixin
from .models import (Nas, OrganizationRadiusSettings, RadiusAccounting, RadiusBatch, RadiusCheck, RadiusGroup,
RadiusGroupCheck, RadiusGroupReply, RadiusPostAuth, RadiusReply, RadiusUserGroup)
_SUPERUSER = {'username': 'gino', 'password': 'cic', 'email': 'giggi_vv@gmail.it'}
_RADCHECK_ENTRY = {'username': 'Monica', 'value': 'Cam0_liX',
'attribute': 'NT-Password'}
_RADCHECK_ENTRY_PW_UPDATE = {'username': 'Monica', 'new_value': 'Cam0_liX',
'attribute': 'NT-Password'}
User = get_user_model()
class BaseTestCase(CreateRadiusObjectsMixin, TestCase):
pass
class TestNas(BaseTestNas, BaseTestCase):
nas_model = Nas
class TestRadiusAccounting(BaseTestRadiusAccounting, BaseTestCase):
radius_accounting_model = RadiusAccounting
class TestRadiusCheck(BaseTestRadiusCheck, BaseTestCase):
radius_check_model = RadiusCheck
test_create_radius_check_model = None
class TestRadiusReply(BaseTestRadiusReply, BaseTestCase):
radius_reply_model = RadiusReply
class TestRadiusGroup(BaseTestRadiusGroup, BaseTestCase):
radius_group_model = RadiusGroup
radius_groupreply_model = RadiusGroupReply
radius_groupcheck_model = RadiusGroupCheck
radius_usergroup_model = RadiusUserGroup
def test_default_groups(self):
default_org = Organization.objects.first()
queryset = self.radius_group_model.objects.filter(organization=default_org)
self.assertEqual(queryset.count(), 2)
self.assertEqual(queryset.filter(name='default-users').count(), 1)
self.assertEqual(queryset.filter(name='default-power-users').count(), 1)
self.assertEqual(queryset.filter(default=True).count(), 1)
users = queryset.get(name='default-users')
self.assertTrue(users.default)
self.assertEqual(users.radiusgroupcheck_set.count(), 2)
check = users.radiusgroupcheck_set.get(attribute=SESSION_TIME_ATTRIBUTE)
self.assertEqual(check.value, DEFAULT_SESSION_TIME_LIMIT)
check = users.radiusgroupcheck_set.get(attribute=SESSION_TRAFFIC_ATTRIBUTE)
self.assertEqual(check.value, DEFAULT_SESSION_TRAFFIC_LIMIT)
power_users = queryset.get(name='default-power-users')
self.assertEqual(power_users.radiusgroupcheck_set.count(), 0)
def test_create_organization_default_group(self):
new_org = self._create_org(name='new org', slug='new-org')
queryset = self.radius_group_model.objects.filter(organization=new_org)
self.assertEqual(queryset.count(), 2)
self.assertEqual(queryset.filter(name='new-org-users').count(), 1)
self.assertEqual(queryset.filter(name='new-org-power-users').count(), 1)
self.assertEqual(queryset.filter(default=True).count(), 1)
group = queryset.filter(default=True).first()
self.assertEqual(group.radiusgroupcheck_set.count(), 2)
self.assertEqual(group.radiusgroupreply_set.count(), 0)
def test_change_default_group(self):
org1 = self._create_org(name='org1', slug='org1')
org2 = self._create_org(name='org2', slug='org2')
new_default_org1 = self.radius_group_model(name='org1-new',
organization=org1,
description='test',
default=True)
new_default_org1.full_clean()
new_default_org1.save()
new_default_org2 = self.radius_group_model(name='org2-new',
organization=org2,
description='test',
default=True)
new_default_org2.full_clean()
new_default_org2.save()
queryset = self.radius_group_model.objects.filter(default=True,
organization=org1)
self.assertEqual(queryset.count(), 1)
self.assertEqual(queryset.filter(name='org1-new').count(), 1)
# org2
queryset = self.radius_group_model.objects.filter(default=True,
organization=org2)
self.assertEqual(queryset.count(), 1)
self.assertEqual(queryset.filter(name='org2-new').count(), 1)
def test_new_user_default_group(self):
org = Organization.objects.get(slug='default')
u = get_user_model()(username='test',
email='test@test.org',
password='test')
u.full_clean()
u.save()
org.add_user(u)
u.refresh_from_db()
usergroup_set = u.radiususergroup_set.all()
self.assertEqual(usergroup_set.count(), 1)
ug = usergroup_set.first()
self.assertTrue(ug.group.default)
def test_auto_prefix(self):
org = self._create_org(name='Cool WiFi', slug='cool-wifi')
rg = self.radius_group_model(name='guests',
organization=org)
rg.full_clean()
self.assertEqual(rg.name, '{}-guests'.format(org.slug))
class TestRadiusPostAuth(BaseTestRadiusPostAuth, BaseTestCase):
radius_postauth_model = RadiusPostAuth
class TestRadiusBatch(BaseTestRadiusBatch, BaseTestCase):
radius_batch_model = RadiusBatch
class TestAdmin(FileMixin, CallCommandMixin, PostParamsMixin,
BaseTestAdmin, BaseTestCase):
app_name = 'openwisp_radius'
nas_model = Nas
radius_accounting_model = RadiusAccounting
radius_batch_model = RadiusBatch
radius_check_model = RadiusCheck
radius_groupcheck_model = RadiusGroupCheck
radius_groupreply_model = RadiusGroupReply
radius_postauth_model = RadiusPostAuth
radius_reply_model = RadiusReply
radius_usergroup_model = RadiusUserGroup
radius_group_model = RadiusGroup
def setUp(self):
self.default_org = Organization.objects.get(slug='default')
super(TestAdmin, self).setUp()
@property
def _RADCHECK_ENTRY_PW_UPDATE(self): # noqa
data = BaseTestAdmin._RADCHECK_ENTRY_PW_UPDATE
data['organization'] = str(self.default_org.pk)
return data
def _get_csv_post_data(self):
data = super(TestAdmin, self)._get_csv_post_data()
data['organization'] = self.default_org.pk
return data
def _get_prefix_post_data(self):
data = super(TestAdmin, self)._get_prefix_post_data()
data['organization'] = self.default_org.pk
return data
def test_radiusbatch_org_user(self):
self.assertEqual(self.radius_batch_model.objects.count(), 0)
add_url = reverse('admin:{0}_radiusbatch_add'.format(self.app_name))
data = self._get_csv_post_data()
self.client.post(add_url, data, follow=True)
self.assertEqual(OrganizationUser.objects.all().count(), 3)
for u in OrganizationUser.objects.all():
self.assertEqual(u.organization,
self.radius_batch_model.objects.first().organization)
class ApiTokenMixin(BasePostParamsMixin):
"""
we don't automatically set the organization
in this mixin, because it must be inferred
from the token authentication
"""
def setUp(self):
org = self._create_org()
rad = OrganizationRadiusSettings.objects.create(token='asdfghjklqwerty',
organization=org)
self.auth_header = 'Bearer {0} {1}'.format(org.pk, rad.token)
self.token_querystring = '?token={0}&uuid={1}'.format(rad.token, str(org.pk))
self.default_org = org
class TestApi(ApiTokenMixin, BaseTestApi, BaseTestCase):
radius_postauth_model = RadiusPostAuth
radius_accounting_model = RadiusAccounting
radius_batch_model = RadiusBatch
user_model = User
def assertAcctData(self, ra, data):
# we don't expect the organization field
# because it will be inferred from the auth token
self.assertNotIn('organization', data)
# but we still want to ensure the
# organization is filled correctly
data['organization'] = self.default_org
return super().assertAcctData(ra, data)
def test_accounting_start_201(self):
data = self.acct_post_data
data['status_type'] = 'Start'
data['organization'] = str(self.default_org.pk)
data = self._get_accounting_params(**data)
response = self.post_json(data)
self.assertEqual(response.status_code, 403)
self.assertIn('setting the organization', response.data['detail'])
def test_register_201(self):
self.assertEqual(User.objects.count(), 0)
url = reverse('freeradius:rest_register', args=[self.default_org.slug])
r = self.client.post(url, {
'username': 'test@test.org',
'email': 'test@test.org',
'password1': 'password',
'password2': 'password'
})
self.assertEqual(r.status_code, 201)
self.assertIn('key', r.data)
self.assertEqual(User.objects.count(), 1)
user = User.objects.first()
self.assertIn((self.default_org.pk,), user.organizations_pk)
def test_register_404(self):
url = reverse('freeradius:rest_register', args=['madeup'])
r = self.client.post(url, {
'username': 'test@test.org',
'email': 'test@test.org',
'password1': 'password',
'password2': 'password'
})
self.assertEqual(r.status_code, 404)
def test_api_show_only_token_org(self):
org = Organization.objects.create(name='org1')
self.assertEqual(self.radius_accounting_model.objects.count(), 0)
nas_ip = '127.0.0.1'
test1 = self.radius_accounting_model(session_id='asd1',
organization=org,
nas_ip_address=nas_ip,
unique_id='123')
test1.full_clean()
test1.save()
test2 = self.radius_accounting_model(session_id='asd2',
organization=self.default_org,
nas_ip_address=nas_ip,
unique_id='1234')
test2.full_clean()
test2.save()
data = self.client.get(self._acct_url,
HTTP_AUTHORIZATION=self.auth_header)
self.assertEqual(len(data.json()), 1)
self.assertEqual(data.json()[0]['organization'], str(self.default_org.pk))
class TestApiReject(ApiTokenMixin, BaseTestApiReject, BaseTestCase):
pass
class TestCommands(FileMixin, CallCommandMixin,
BaseTestCommands, BaseTestCase):
radius_accounting_model = RadiusAccounting
radius_batch_model = RadiusBatch
radius_postauth_model = RadiusPostAuth
class TestCSVUpload(FileMixin, BaseTestCSVUpload, BaseTestCase):
radius_batch_model = RadiusBatch
class TestUtils(FileMixin, BaseTestUtils, BaseTestCase):
pass
class TestSocial(ApiTokenMixin, BaseTestSocial, BaseTestCase):
def get_url(self):
return reverse(self.view_name, args=[self.default_org.slug])
def test_redirect_cp_404(self):
u = self._create_social_user()
self.client.force_login(u)
r = self.client.get(reverse(self.view_name, args=['wrong']), {'cp': 'test'})
self.assertEqual(r.status_code, 404)
def test_redirect_cp_suspicious_400(self):
u = self._create_social_user()
u.is_staff = True
u.save()
self.client.force_login(u)
r = self.client.get(self.get_url(), {'cp': 'test'})
self.assertEqual(r.status_code, 400)
def test_redirect_cp_301(self):
super().test_redirect_cp_301()
u = User.objects.filter(username='socialuser').first()
self.assertIn((self.default_org.pk, ), u.organizations_pk)
class TestOgranizationRadiusSettings(BaseTestCase):
user_model = User
def setUp(self):
self.org = self._create_org()
def test_default_token(self):
rad = OrganizationRadiusSettings.objects.create(organization=self.org)
self.assertEqual(32, len(rad.token))
def test_bad_token(self):
try:
rad = OrganizationRadiusSettings(token='bad.t.o.k.e.n', organization=self.org)
rad.full_clean()
except ValidationError as e:
self.assertEqual(e.message_dict['token'][0],
'Key must not contain spaces, dots or slashes.')
def test_cache(self):
# clear cache set from previous tests
cache.clear()
rad = OrganizationRadiusSettings.objects.create(token='12345', organization=self.org)
options = dict(username='molly', password='barbar')
self._create_user(**options)
token_querystring = '?token={0}&uuid={1}'.format(rad.token, str(self.org.pk))
post_url = '{}{}'.format(reverse('freeradius:authorize'), token_querystring)
self.client.post(post_url, {'username': 'molly', 'password': 'barbar'})
self.assertEqual(rad.token, cache.get(rad.pk))
# test update
rad.token = '1234567'
rad.save()
self.assertEqual(rad.token, cache.get(rad.pk))
# test delete
rad.delete()
self.assertEqual(None, cache.get(rad.pk))