forked from django-json-api/django-rest-framework-json-api
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_views.py
255 lines (214 loc) · 11.7 KB
/
test_views.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
importjson
fromdjango.testimportRequestFactory
fromdjango.utilsimporttimezone
fromrest_framework.reverseimportreverse
fromrest_framework.testimportAPITestCase
fromrest_framework.testimportforce_authenticate
fromrest_framework_json_api.utilsimportformat_resource_type
fromexample.modelsimportBlog, Entry, Comment, Author
from .. importviews
from . importTestBase
classTestRelationshipView(APITestCase):
defsetUp(self):
self.author=Author.objects.create(name='Super powerful superhero', email='i.am@lost.com')
self.blog=Blog.objects.create(name='Some Blog', tagline="It's a blog")
self.other_blog=Blog.objects.create(name='Other blog', tagline="It's another blog")
self.first_entry=Entry.objects.create(
blog=self.blog,
headline='headline one',
body_text='body_text two',
pub_date=timezone.now(),
mod_date=timezone.now(),
n_comments=0,
n_pingbacks=0,
rating=3
)
self.second_entry=Entry.objects.create(
blog=self.blog,
headline='headline two',
body_text='body_text one',
pub_date=timezone.now(),
mod_date=timezone.now(),
n_comments=0,
n_pingbacks=0,
rating=1
)
self.first_comment=Comment.objects.create(entry=self.first_entry, body="This entry is cool", author=None)
self.second_comment=Comment.objects.create(
entry=self.second_entry,
body="This entry is not cool",
author=self.author
)
deftest_get_entry_relationship_blog(self):
url=reverse('entry-relationships', kwargs={'pk': self.first_entry.id, 'related_field': 'blog'})
response=self.client.get(url)
expected_data= {'type': format_resource_type('Blog'), 'id': str(self.first_entry.blog.id)}
assertresponse.data==expected_data
deftest_get_entry_relationship_invalid_field(self):
response=self.client.get('/entries/{}/relationships/invalid_field'.format(self.first_entry.id))
assertresponse.status_code==404
deftest_get_blog_relationship_entry_set(self):
response=self.client.get('/blogs/{}/relationships/entry_set'.format(self.blog.id))
expected_data= [{'type': format_resource_type('Entry'), 'id': str(self.first_entry.id)},
{'type': format_resource_type('Entry'), 'id': str(self.second_entry.id)}]
assertresponse.data==expected_data
deftest_put_entry_relationship_blog_returns_405(self):
url='/entries/{}/relationships/blog'.format(self.first_entry.id)
response=self.client.put(url, data={})
assertresponse.status_code==405
deftest_patch_invalid_entry_relationship_blog_returns_400(self):
url='/entries/{}/relationships/blog'.format(self.first_entry.id)
response=self.client.patch(url,
data=json.dumps({'data': {'invalid': ''}}),
content_type='application/vnd.api+json')
assertresponse.status_code==400
deftest_relationship_view_errors_format(self):
url='/entries/{}/relationships/blog'.format(self.first_entry.id)
response=self.client.patch(url,
data=json.dumps({'data': {'invalid': ''}}),
content_type='application/vnd.api+json')
assertresponse.status_code==400
result=json.loads(response.content.decode('utf-8'))
assert'data'notinresult
assert'errors'inresult
deftest_get_empty_to_one_relationship(self):
url='/comments/{}/relationships/author'.format(self.first_entry.id)
response=self.client.get(url)
expected_data=None
assertresponse.data==expected_data
deftest_get_to_many_relationship_self_link(self):
url='/authors/{}/relationships/comment_set'.format(self.author.id)
response=self.client.get(url)
expected_data= {
'links': {'self': 'http://testserver/authors/1/relationships/comment_set'},
'data': [{'id': str(self.second_comment.id), 'type': format_resource_type('Comment')}]
}
assertjson.loads(response.content.decode('utf-8')) ==expected_data
deftest_patch_to_one_relationship(self):
url='/entries/{}/relationships/blog'.format(self.first_entry.id)
request_data= {
'data': {'type': format_resource_type('Blog'), 'id': str(self.other_blog.id)}
}
response=self.client.patch(url, data=json.dumps(request_data), content_type='application/vnd.api+json')
assertresponse.status_code==200, response.content.decode()
assertresponse.data==request_data['data']
response=self.client.get(url)
assertresponse.data==request_data['data']
deftest_patch_one_to_many_relationship(self):
url='/blogs/{}/relationships/entry_set'.format(self.first_entry.id)
request_data= {
'data': [{'type': format_resource_type('Entry'), 'id': str(self.first_entry.id)}, ]
}
response=self.client.patch(url, data=json.dumps(request_data), content_type='application/vnd.api+json')
assertresponse.status_code==200, response.content.decode()
assertresponse.data==request_data['data']
response=self.client.get(url)
assertresponse.data==request_data['data']
deftest_patch_many_to_many_relationship(self):
url='/entries/{}/relationships/authors'.format(self.first_entry.id)
request_data= {
'data': [
{
'type': format_resource_type('Author'),
'id': str(self.author.id)
},
]
}
response=self.client.patch(url,
data=json.dumps(request_data),
content_type='application/vnd.api+json')
assertresponse.status_code==200, response.content.decode()
assertresponse.data==request_data['data']
response=self.client.get(url)
assertresponse.data==request_data['data']
deftest_post_to_one_relationship_should_fail(self):
url='/entries/{}/relationships/blog'.format(self.first_entry.id)
request_data= {
'data': {'type': format_resource_type('Blog'), 'id': str(self.other_blog.id)}
}
response=self.client.post(url, data=json.dumps(request_data), content_type='application/vnd.api+json')
assertresponse.status_code==405, response.content.decode()
deftest_post_to_many_relationship_with_no_change(self):
url='/entries/{}/relationships/comment_set'.format(self.first_entry.id)
request_data= {
'data': [{'type': format_resource_type('Comment'), 'id': str(self.first_comment.id)}, ]
}
response=self.client.post(url, data=json.dumps(request_data), content_type='application/vnd.api+json')
assertresponse.status_code==204, response.content.decode()
deftest_post_to_many_relationship_with_change(self):
url='/entries/{}/relationships/comment_set'.format(self.first_entry.id)
request_data= {
'data': [{'type': format_resource_type('Comment'), 'id': str(self.second_comment.id)}, ]
}
response=self.client.post(url, data=json.dumps(request_data), content_type='application/vnd.api+json')
assertresponse.status_code==200, response.content.decode()
assertrequest_data['data'][0] inresponse.data
deftest_delete_to_one_relationship_should_fail(self):
url='/entries/{}/relationships/blog'.format(self.first_entry.id)
request_data= {
'data': {'type': format_resource_type('Blog'), 'id': str(self.other_blog.id)}
}
response=self.client.delete(url, data=json.dumps(request_data), content_type='application/vnd.api+json')
assertresponse.status_code==405, response.content.decode()
deftest_delete_relationship_overriding_with_none(self):
url='/comments/{}'.format(self.second_comment.id)
request_data= {
'data': {
'type': 'comments',
'id': self.second_comment.id,
'relationships': {
'author': {
'data': None
}
}
}
}
response=self.client.patch(url, data=json.dumps(request_data), content_type='application/vnd.api+json')
assertresponse.status_code==200, response.content.decode()
assertresponse.data['author'] ==None
deftest_delete_to_many_relationship_with_no_change(self):
url='/entries/{}/relationships/comment_set'.format(self.first_entry.id)
request_data= {
'data': [{'type': format_resource_type('Comment'), 'id': str(self.second_comment.id)}, ]
}
response=self.client.delete(url, data=json.dumps(request_data), content_type='application/vnd.api+json')
assertresponse.status_code==204, response.content.decode()
deftest_delete_one_to_many_relationship_with_not_null_constraint(self):
url='/entries/{}/relationships/comment_set'.format(self.first_entry.id)
request_data= {
'data': [{'type': format_resource_type('Comment'), 'id': str(self.first_comment.id)}, ]
}
response=self.client.delete(url, data=json.dumps(request_data), content_type='application/vnd.api+json')
assertresponse.status_code==409, response.content.decode()
deftest_delete_to_many_relationship_with_change(self):
url='/authors/{}/relationships/comment_set'.format(self.author.id)
request_data= {
'data': [{'type': format_resource_type('Comment'), 'id': str(self.second_comment.id)}, ]
}
response=self.client.delete(url, data=json.dumps(request_data), content_type='application/vnd.api+json')
assertresponse.status_code==200, response.content.decode()
classTestValidationErrorResponses(TestBase):
deftest_if_returns_error_on_empty_post(self):
view=views.BlogViewSet.as_view({'post': 'create'})
response=self._get_create_response("{}", view)
self.assertEqual(400, response.status_code)
expected= [{'detail': 'Received document does not contain primary data', 'status': '400', 'source': {'pointer': '/data'}}]
self.assertEqual(expected, response.data)
deftest_if_returns_error_on_missing_form_data_post(self):
view=views.BlogViewSet.as_view({'post': 'create'})
response=self._get_create_response('{"data":{"attributes":{},"type":"blogs"}}', view)
self.assertEqual(400, response.status_code)
expected= [{'status': '400', 'detail': 'This field is required.', 'source': {'pointer': '/data/attributes/name'}}]
self.assertEqual(expected, response.data)
deftest_if_returns_error_on_bad_endpoint_name(self):
view=views.BlogViewSet.as_view({'post': 'create'})
response=self._get_create_response('{"data":{"attributes":{},"type":"bad"}}', view)
self.assertEqual(409, response.status_code)
expected= [{'detail': "The resource object's type (bad) is not the type that constitute the collection represented by the endpoint (blogs).", 'source': {'pointer': '/data'}, 'status': '409'}]
self.assertEqual(expected, response.data)
def_get_create_response(self, data, view):
factory=RequestFactory()
request=factory.post('/', data, content_type='application/vnd.api+json')
user=self.create_user('user', 'pass')
force_authenticate(request, user)
returnview(request)