forked from itsee-birmingham/django_wce_api_tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_integration.py
430 lines (388 loc) · 20.4 KB
/
test_integration.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import json
import datetime
from unittest import skip
from django.conf import settings as django_settings
from django.utils import timezone
from django.test import TestCase, Client
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth import get_user_model
from rest_framework.test import APIRequestFactory, APIClient, APITestCase
from rest_framework.authtoken.models import Token
from api_tests import models
User = get_user_model()
class APIPostTests(APITestCase):
base_url = '/api/{}/{}/'
def add_data_manager_user(self, credentials):
try:
g2 = Group.objects.filter(name='data_managers')[0]
except IndexError:
g2 = Group(name='data_managers')
g2.save()
self.add_data_manager_permissions(g2)
user = User.objects.create_user(**credentials)
user.groups.add(g2)
user.save()
return user
def add_data_editor_user(self, credentials):
try:
g2 = Group.objects.filter(name='data_editors')[0]
except IndexError:
g2 = Group(name='data_editors')
g2.save()
self.add_data_editor_permissions(g2)
user = User.objects.create_user(**credentials)
user.groups.add(g2)
user.save()
return user
def add_data_editor_permissions(self, group):
content_type = ContentType.objects.get_for_model(models.Author)
permission = Permission.objects.get(content_type=content_type, codename='add_author')
group.permissions.add(permission)
content_type = ContentType.objects.get_for_model(models.Author)
permission = Permission.objects.get(content_type=content_type, codename='change_author')
group.permissions.add(permission)
group.save()
def add_data_manager_permissions(self, group):
content_type = ContentType.objects.get_for_model(models.Author)
permission = Permission.objects.get(content_type=content_type, codename='add_author')
group.permissions.add(permission)
content_type = ContentType.objects.get_for_model(models.Author)
permission = Permission.objects.get(content_type=content_type, codename='change_author')
group.permissions.add(permission)
content_type = ContentType.objects.get_for_model(models.Author)
permission = Permission.objects.get(content_type=content_type, codename='delete_author')
group.permissions.add(permission)
content_type = ContentType.objects.get_for_model(models.PublicationPlan)
permission = Permission.objects.get(content_type=content_type, codename='add_publicationplan')
group.permissions.add(permission)
content_type = ContentType.objects.get_for_model(models.PublicationPlan)
permission = Permission.objects.get(content_type=content_type, codename='change_publicationplan')
group.permissions.add(permission)
content_type = ContentType.objects.get_for_model(models.PublicationPlan)
permission = Permission.objects.get(content_type=content_type, codename='delete_publicationplan')
group.permissions.add(permission)
group.save()
def test_getUser(self):
user = self.add_data_editor_user({'username': '[email protected]',
'email': '[email protected]',
'password': 'xyz'})
if django_settings.USER_IDENTIFIER_FIELD:
setattr(user, django_settings.USER_IDENTIFIER_FIELD, 'Test User')
user.save()
client = APIClient()
# first if not logged in
response = client.get('/api/whoami')
self.assertEqual(response.status_code, 401)
# then if logged in
login = client.login(username='[email protected]', password='xyz')
self.assertEqual(login, True)
response = client.get('/api/whoami')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['id'], user.id)
# @skip('')
def test_POSTAuthor(self):
authors = models.Author.objects.all()
self.assertTrue(len(authors) == 0)
a1_data = {'identifier': 'JS1',
'name': 'John Smith',
'age': 28,
'active': True
}
response = self.client.post('%screate' % self.base_url.format('api_tests', 'author'), a1_data)
# we should not be able to create unless we are logged in - 403 Authentication credentials were not provided.
self.assertEqual(response.status_code, 403)
# now login
user = self.add_data_editor_user({'username': '[email protected]',
'email': '[email protected]',
'password': 'xyz'})
if django_settings.USER_IDENTIFIER_FIELD:
setattr(user, django_settings.USER_IDENTIFIER_FIELD, 'Test User')
user.save()
self.assertTrue(user.has_perm('api_tests.add_author'))
client = APIClient()
login = client.login(username='[email protected]', password='xyz')
self.assertEqual(login, True)
response = client.post('%screate' % self.base_url.format('api_tests', 'author'),
json.dumps(a1_data), content_type='application/json')
self.assertEqual(response.status_code, 201)
authors = models.Author.objects.all()
self.assertTrue(len(authors) == 1)
self.assertEqual(authors[0].identifier, 'JS1')
self.assertEqual(authors[0].created_by, getattr(user, django_settings.USER_IDENTIFIER_FIELD))
authors = models.Author.objects.all()
self.assertTrue(len(authors) == 1)
client.logout()
# now try again but as a different user with no full_name.
user2 = self.add_data_editor_user({'username': '[email protected]',
'email': '[email protected]',
'password': 'xyz'})
self.assertTrue(user2.has_perm('api_tests.add_author'))
client2 = APIClient()
login = client2.login(username='[email protected]', password='xyz')
self.assertEqual(login, True)
# use the same data as added by someone else
response = client2.post('%screate' % self.base_url.format('api_tests', 'author'),
json.dumps(a1_data), content_type='application/json')
# it does not work because identifier must be unique
self.assertEqual(response.status_code, 400)
authors = models.Author.objects.all()
self.assertTrue(len(authors) == 1)
# now try with different data
a2_data = {'identifier': 'JS2',
'name': 'Jane Smart',
'age': 34,
'active': True
}
response = client2.post('%screate' % self.base_url.format('api_tests', 'author'),
json.dumps(a2_data), content_type='application/json')
self.assertEqual(response.status_code, 201)
authors = models.Author.objects.all()
self.assertTrue(len(authors) == 2)
author = models.Author.objects.filter(identifier='JS2')[0]
self.assertEqual(author.identifier, 'JS2')
self.assertEqual(author.created_by, user2.username)
# @skip('')
def test_PATCHAuthorPartial(self):
# make an author to modify
a1_data = {'identifier': 'JS1',
'name': 'John Smith',
'age': 28,
'date_joined': datetime.datetime.strptime('2010 4 6', '%Y %m %d').date(),
'active': True
}
a1 = models.Author.objects.create(**a1_data)
response = self.client.patch('%supdate/%s' % (self.base_url.format('api_tests', 'author'), a1.id),
json.dumps({'name': 'My new name'}), content_type='application/json')
# we should not be able to modify unless we are logged in - 403 Authentication credentials were not provided.
self.assertEqual(response.status_code, 403)
# now login
user = self.add_data_editor_user({'username': '[email protected]',
'email': '[email protected]',
'password': 'xyz'})
self.assertTrue(user.has_perm('api_tests.change_author'))
client = APIClient()
login = client.login(username='[email protected]', password='xyz')
self.assertEqual(login, True)
response = client.patch('%supdate/%s' % (self.base_url.format('api_tests', 'author'), a1.id),
json.dumps({'name': 'My new name'}), content_type='application/json')
self.assertEqual(response.status_code, 200)
response_json = response.json()
# make sure we are getting the full object back not just the changed fields
self.assertTrue('age' in response_json)
self.assertEqual(response_json['age'], 28)
self.assertTrue('active' in response_json)
self.assertEqual(response_json['active'], True)
# make sure the changes were applied
authors = models.Author.objects.all()
self.assertTrue(len(authors) == 1)
self.assertEqual(authors[0].name, 'My new name')
self.assertEqual(authors[0].last_modified_by, '[email protected]')
def test_PUTAuthorNoChange(self):
# make an author to modify
a1_data = {'identifier': 'JS1',
'name': 'John Smith',
'age': 28,
'date_joined': datetime.datetime.strptime('2010 4 6', '%Y %m %d').date(),
'active': True
}
a1 = models.Author.objects.create(**a1_data)
# we need to get the stored version because otherwise the creation time strings are different!
response = self.client.get(self.base_url.format('api_tests', 'author', a1.id))
response_json = json.loads(response.content.decode('utf8'))
user = self.add_data_editor_user({'username': 'testuser',
'email': '[email protected]',
'password': 'xyz'})
self.assertTrue(user.has_perm('api_tests.change_author'))
client = APIClient()
login = client.login(username='[email protected]', password='xyz')
self.assertEqual(login, True)
a1_data['id'] = a1.id
response = client.put('%supdate/%s' % (self.base_url.format('api_tests', 'author'), a1.id),
json.dumps(response_json['results'][0]), content_type='application/json')
self.assertEqual(response.status_code, 200)
authors = models.Author.objects.all()
self.assertTrue(len(authors) == 1)
self.assertEqual(authors[0].name, 'John Smith')
self.assertEqual(authors[0].last_modified_by, '')
# @skip('')
def test_PUTAuthorChange(self):
# make an author to modify
a1_data = {'identifier': 'JS1',
'name': 'John Smith',
'age': 28,
'date_joined': datetime.datetime.strptime('2010 4 6', '%Y %m %d').date(),
'active': True
}
a1 = models.Author.objects.create(**a1_data)
# we need to get the stored version because otherwise the creation time strings are different!
response = self.client.get(self.base_url.format('api_tests', 'author', a1.id))
response_json = json.loads(response.content.decode('utf8'))
response_json['results'][0]['name'] = 'My new name'
user = self.add_data_editor_user({'username': 'testuser',
'email': '[email protected]',
'password': 'xyz'})
self.assertTrue(user.has_perm('api_tests.add_author'))
client = APIClient()
login = client.login(username='[email protected]', password='xyz')
self.assertEqual(login, True)
a1_data['id'] = a1.id
response = client.put('%supdate/%s' % (self.base_url.format('api_tests', 'author'), a1.id),
json.dumps(response_json['results'][0]), content_type='application/json')
self.assertEqual(response.status_code, 200)
authors = models.Author.objects.all()
self.assertTrue(len(authors) == 1)
self.assertEqual(authors[0].name, 'My new name')
self.assertEqual(authors[0].last_modified_by, '[email protected]')
self.assertNotEqual(authors[0].last_modified_time, None)
def test_DELETEAuthor(self):
a1_data = {'identifier': 'JS1',
'name': 'John Smith',
'age': 28,
'date_joined': datetime.datetime.strptime('2010 4 6', '%Y %m %d').date(),
'active': True
}
a1 = models.Author.objects.create(**a1_data)
a2_data = {'created_by': 'cat',
'created_time': timezone.now(),
'identifier': 'JS2',
'name': 'Jane Smart',
'age': 34,
'active': True
}
a2 = models.Author.objects.create(**a2_data)
authors = models.Author.objects.all()
self.assertTrue(len(authors) == 2)
client = APIClient()
response = client.delete('%sdelete/%s' % (self.base_url.format('api_tests', 'author'), a1.id))
self.assertEqual(response.status_code, 403)
authors = models.Author.objects.all()
self.assertTrue(len(authors) == 2)
user = self.add_data_editor_user({'username': 'testuser',
'email': '[email protected]',
'password': 'xyz'})
self.assertFalse(user.has_perm('api_tests.delete_author'))
client = APIClient()
login = client.login(username='[email protected]', password='xyz')
self.assertEqual(login, True)
response = client.delete('%sdelete/%s' % (self.base_url.format('api_tests', 'author'), a1.id))
self.assertEqual(response.status_code, 403)
authors = models.Author.objects.all()
self.assertTrue(len(authors) == 2)
user = self.add_data_manager_user({'username': 'testuser2',
'email': '[email protected]',
'password': 'xyz'})
self.assertTrue(user.has_perm('api_tests.delete_author'))
client2 = APIClient()
login = client2.login(username='[email protected]', password='xyz')
self.assertEqual(login, True)
response = client2.delete('%sdelete/%s' % (self.base_url.format('api_tests', 'author'), a1.id))
self.assertEqual(response.status_code, 204)
authors = models.Author.objects.all()
self.assertTrue(len(authors) == 1)
self.assertEqual(authors[0].identifier, a2.identifier)
def test_DELETEWithDeps(self):
a1_data = {'identifier': 'JS1',
'name': 'John Smith',
'age': 28,
'date_joined': datetime.datetime.strptime('2010 4 6', '%Y %m %d').date(),
'active': True
}
a1 = models.Author.objects.create(**a1_data)
w1_data = {'identifier': 'W1',
'title': 'My Title',
'author': a1}
w1 = models.Work.objects.create(**w1_data)
user = self.add_data_manager_user({'username': 'testuser',
'email': '[email protected]',
'password': 'xyz'})
self.assertTrue(user.has_perm('api_tests.delete_author'))
client = APIClient()
login = client.login(username='[email protected]', password='xyz')
self.assertEqual(login, True)
response = client.delete('%sdelete/%s' % (self.base_url.format('api_tests', 'author'), a1.id))
self.assertEqual(response.status_code, 500)
authors = models.Author.objects.all()
self.assertTrue(len(authors) == 1)
def test_M2MDelete(self):
# add users
self.u1 = self.add_data_editor_user({'username': 'User1',
'email': '[email protected]',
'password': 'secret'})
self.u2 = self.add_data_editor_user({'username': 'User2',
'email': '[email protected]',
'password': 'secret'})
self.u3 = self.add_data_editor_user({'username': 'User3',
'email': '[email protected]',
'password': 'secret'})
self.u4 = self.add_data_manager_user({'username': 'User4',
'email': '[email protected]',
'password': 'secret'})
e1_data = {'user': self.u1,
'active': True}
self.e1 = models.Editor.objects.create(**e1_data)
e2_data = {'user': self.u2,
'active': True}
self.e2 = models.Editor.objects.create(**e2_data)
e3_data = {'user': self.u3,
'active': True}
self.e3 = models.Editor.objects.create(**e3_data)
e4_data = {'user': self.u4,
'active': True}
self.e4 = models.Editor.objects.create(**e4_data)
a1_data = {'identifier': 'JS1',
'name': 'John Smith',
'age': 28,
'active': True
}
self.a1 = models.Author.objects.create(**a1_data)
w1_data = {'identifier': 'W1',
'title': 'My First Book',
'author': self.a1
}
self.w1 = models.Work.objects.create(**w1_data)
p1_data = {'managing_editor': self.u4,
'status': 'in press',
'genre': 'sci-fi'}
self.p1 = models.Project.objects.create(**p1_data)
self.p1.work.add(self.w1.id)
self.p1.editors.add(self.u2.id)
self.p1.editors.add(self.u3.id)
self.p1.save()
pp1_data = {'project': self.p1,
'current_stage': 'in progress'}
self.pp1 = models.PublicationPlan.objects.create(**pp1_data)
self.pp1.editors.add(self.e1.id)
self.pp1.editors.add(self.e2.id)
self.pp1.save()
# check not logged in users cannot delete
projects = models.Project.objects.all()
self.assertTrue(len(projects) == 1)
self.assertTrue(len(projects[0].editors.all()) == 2)
client = APIClient()
urlstring = '%s%s/editors/delete/editor/%s' % (self.base_url.format('api_tests', 'publicationplan'),
self.pp1.id,
self.e2.id)
response = client.patch('%s%s/editors/delete/editor/%s' % (self.base_url.format('api_tests',
'publicationplan'),
self.pp1.id,
self.e2.id))
self.assertEqual(response.status_code, 403)
# check everything is still the same
projects = models.Project.objects.all()
self.assertTrue(len(projects) == 1)
self.assertTrue(len(projects[0].editors.all()) == 2)
login = client.login(username='[email protected]', password='secret')
self.assertEqual(login, True)
response = client.patch('%s%s/editors/delete/editor/%s' % (self.base_url.format('api_tests',
'publicationplan'),
self.pp1.id,
self.e2.id))
publication_plans = models.PublicationPlan.objects.all()
self.assertTrue(len(publication_plans) == 1)
self.assertTrue(len(publication_plans[0].editors.all()) == 1)
# check the editor still exists and only the reference was deleted
response = client.get(self.base_url.format('api_tests', 'editor', self.e2.id))
self.assertEqual(response.status_code, 200)
editors = models.Editor.objects.all()
self.assertTrue(len(editors) == 4)