48 self.user2.profile.save() |
50 self.user2.profile.save() |
49 |
51 |
50 self.session1 = Session.objects.create( |
52 self.session1 = Session.objects.create( |
51 title="a new session 1", |
53 title="a new session 1", |
52 description="Description 1", |
54 description="Description 1", |
53 protocol="[]", |
|
54 group=self.user1.profile.default_group, |
55 group=self.user1.profile.default_group, |
55 owner=self.user1 |
56 owner=self.user1 |
56 ) |
57 ) |
57 |
58 |
58 self.session2 = Session.objects.create( |
59 self.session2 = Session.objects.create( |
59 title="a new session 2", |
60 title="a new session 2", |
60 description="Description 2", |
61 description="Description 2", |
61 protocol="[]", |
|
62 group=self.user2.profile.default_group, |
62 group=self.user2.profile.default_group, |
63 owner=self.user2 |
63 owner=self.user2 |
64 ) |
64 ) |
65 |
65 |
66 self.session3 = Session.objects.create( |
66 self.session3 = Session.objects.create( |
67 title="a new session 3", |
67 title="a new session 3", |
68 description="Description 3", |
68 description="Description 3", |
69 protocol="[]", |
|
70 group=user3.profile.default_group, |
69 group=user3.profile.default_group, |
71 owner=user3 |
70 owner=user3 |
72 ) |
71 ) |
73 |
72 |
74 self.session4 = Session.objects.create( |
73 self.session4 = Session.objects.create( |
75 title="a new session 4", |
74 title="a new session 4", |
76 description="Description 4", |
75 description="Description 4", |
77 protocol="[]", |
|
78 group=user3.profile.default_group, |
76 group=user3.profile.default_group, |
79 owner=user3 |
77 owner=user3 |
80 ) |
78 ) |
81 |
79 |
82 Note.objects.create( |
80 Note.objects.create( |
164 url = reverse('notes:session-list') |
162 url = reverse('notes:session-list') |
165 self.client.login(username='test_user1', password='top_secret') |
163 self.client.login(username='test_user1', password='top_secret') |
166 response = self.client.post(url, { |
164 response = self.client.post(url, { |
167 'title': "a new session", |
165 'title': "a new session", |
168 'description': "description of the session", |
166 'description': "description of the session", |
169 'protocol': "[]", |
|
170 'group': 'group1' |
167 'group': 'group1' |
171 }, format='json') |
168 }, format='json') |
172 |
169 |
173 self.assertEqual(response.status_code, status.HTTP_201_CREATED) |
170 self.assertEqual(response.status_code, status.HTTP_201_CREATED, "error msg: %r" % response.content) |
174 json = response.json() |
171 json = response.json() |
175 self.assertIn('ext_id', json) |
172 self.assertIn('ext_id', json) |
176 |
173 |
177 session = Session.objects.get(ext_id=json['ext_id']) |
174 session = Session.objects.get(ext_id=json['ext_id']) |
178 self.assertIsNotNone(session) |
175 self.assertIsNotNone(session) |
179 self.assertEqual(self.group1, session.group) |
176 self.assertEqual(self.group1, session.group) |
|
177 self.assertEqual(self.group1.profile.protocol, session.protocol) |
|
178 |
|
179 |
|
180 def test_create_session_protocol(self): |
|
181 url = reverse('notes:session-list') |
|
182 self.client.login(username='test_user1', password='top_secret') |
|
183 response = self.client.post(url, { |
|
184 'title': "a new session", |
|
185 'description': "description of the session", |
|
186 'group': 'group1', |
|
187 'protocol': {} |
|
188 }, format='json') |
|
189 |
|
190 self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.json()) |
|
191 json = response.json() |
|
192 self.assertIn('ext_id', json) |
|
193 |
|
194 session = Session.objects.get(ext_id=json['ext_id']) |
|
195 self.assertIsNotNone(session) |
|
196 self.assertEqual(self.group1, session.group) |
|
197 self.assertNotEqual(self.group1.profile.protocol, session.protocol) |
|
198 |
|
199 |
|
200 def test_create_session_protocol_as_str(self): |
|
201 url = reverse('notes:session-list') |
|
202 self.client.login(username='test_user1', password='top_secret') |
|
203 group1 = Group.objects.get(id=self.group1.id) |
|
204 response = self.client.post(url, { |
|
205 'title': "a new session", |
|
206 'description': "description of the session", |
|
207 'group': 'group1', |
|
208 'protocol': group1.profile.protocol |
|
209 }, format='json') |
|
210 |
|
211 self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.json()) |
|
212 json = response.json() |
|
213 self.assertIn('ext_id', json) |
|
214 |
|
215 session = Session.objects.get(ext_id=json['ext_id']) |
|
216 self.assertIsNotNone(session) |
|
217 self.assertEqual(self.group1, session.group) |
|
218 self.assertEqual(self.group1.profile.protocol, session.protocol) |
|
219 |
|
220 |
|
221 def test_create_session_protocol_as_str_unknown(self): |
|
222 url = reverse('notes:session-list') |
|
223 self.client.login(username='test_user1', password='top_secret') |
|
224 group1 = Group.objects.get(id=self.group1.id) |
|
225 response = self.client.post(url, { |
|
226 'title': "a new session", |
|
227 'description': "description of the session", |
|
228 'group': 'group1', |
|
229 'protocol': constants.PROTOCOL_URN_PREFIX + str(uuid4()) + ".1" |
|
230 }, format='json') |
|
231 |
|
232 self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.json()) |
|
233 json = response.json() |
|
234 self.assertIn('protocol', json) |
|
235 self.assertIn('Incorrect protocol', json['protocol'][0]) |
|
236 self.assertIn('does not exists', json['protocol'][0]) |
|
237 |
|
238 |
|
239 def test_create_session_protocol_as_str_bad(self): |
|
240 url = reverse('notes:session-list') |
|
241 self.client.login(username='test_user1', password='top_secret') |
|
242 group1 = Group.objects.get(id=self.group1.id) |
|
243 response = self.client.post(url, { |
|
244 'title': "a new session", |
|
245 'description': "description of the session", |
|
246 'group': 'group1', |
|
247 'protocol': constants.PROTOCOL_URN_PREFIX + "foo" |
|
248 }, format='json') |
|
249 |
|
250 self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.json()) |
|
251 json = response.json() |
|
252 self.assertIn('protocol', json) |
|
253 self.assertEqual('Incorrect format. Expected `urn:protocol:<ext_id>.<version>`.', json['protocol'][0]) |
|
254 |
|
255 |
|
256 def test_create_session_protocol_as_other_type_bad(self): |
|
257 url = reverse('notes:session-list') |
|
258 self.client.login(username='test_user1', password='top_secret') |
|
259 group1 = Group.objects.get(id=self.group1.id) |
|
260 response = self.client.post(url, { |
|
261 'title': "a new session", |
|
262 'description': "description of the session", |
|
263 'group': 'group1', |
|
264 'protocol': True |
|
265 }, format='json') |
|
266 |
|
267 self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.json()) |
|
268 json = response.json() |
|
269 self.assertIn('protocol', json) |
|
270 self.assertEqual('Incorrect format. Expected `urn:protocol:<ext_id>.<version>` or dict.', json['protocol'][0]) |
180 |
271 |
181 |
272 |
182 def test_create_session_no_group(self): |
273 def test_create_session_no_group(self): |
183 url = reverse('notes:session-list') |
274 url = reverse('notes:session-list') |
184 self.client.login(username='test_user1', password='top_secret') |
275 self.client.login(username='test_user1', password='top_secret') |
185 response = self.client.post(url, { |
276 response = self.client.post(url, { |
186 'title': "a new session", |
277 'title': "a new session", |
187 'description': "description of the session", |
278 'description': "description of the session" |
188 'protocol': "[]", |
|
189 }, format='json') |
279 }, format='json') |
190 |
280 |
191 self.assertEqual(response.status_code, status.HTTP_201_CREATED, "Error when creating session %r" % response.json()) |
281 self.assertEqual(response.status_code, status.HTTP_201_CREATED, "Error when creating session %r" % response.json()) |
192 json = response.json() |
282 json = response.json() |
193 self.assertIn('ext_id', json) |
283 self.assertIn('ext_id', json) |
194 |
284 |
195 session = Session.objects.get(ext_id=json['ext_id']) |
285 session = Session.objects.get(ext_id=json['ext_id']) |
196 self.assertIsNotNone(session) |
286 self.assertIsNotNone(session) |
197 user1_personal_group = Group.objects.get(profile__owner_personal=self.user1) |
287 user1_personal_group = Group.objects.get(profile__owner_personal=self.user1) |
198 self.assertEqual(user1_personal_group, session.group) |
288 self.assertEqual(user1_personal_group, session.group) |
|
289 self.assertEqual(user1_personal_group.profile.protocol, session.protocol) |
199 |
290 |
200 def test_create_session_no_group_default(self): |
291 def test_create_session_no_group_default(self): |
201 url = reverse('notes:session-list') |
292 url = reverse('notes:session-list') |
202 self.client.login(username='test_user2', password='top_secret') |
293 self.client.login(username='test_user2', password='top_secret') |
203 response = self.client.post(url, { |
294 response = self.client.post(url, { |
204 'title': "a new session", |
295 'title': "a new session", |
205 'description': "description of the session", |
296 'description': "description of the session" |
206 'protocol': "[]", |
|
207 }, format='json') |
297 }, format='json') |
208 |
298 |
209 self.assertEqual(response.status_code, status.HTTP_201_CREATED, "Error when creating session %r" % response.json()) |
299 self.assertEqual(response.status_code, status.HTTP_201_CREATED, "Error when creating session %r" % response.json()) |
210 json = response.json() |
300 json = response.json() |
211 self.assertIn('ext_id', json) |
301 self.assertIn('ext_id', json) |
213 session = Session.objects.get(ext_id=json['ext_id']) |
303 session = Session.objects.get(ext_id=json['ext_id']) |
214 self.assertIsNotNone(session) |
304 self.assertIsNotNone(session) |
215 self.assertEqual(self.group1, session.group) |
305 self.assertEqual(self.group1, session.group) |
216 |
306 |
217 |
307 |
218 def test_create_session_no_group_default(self): |
|
219 url = reverse('notes:session-list') |
|
220 self.client.login(username='test_user2', password='top_secret') |
|
221 response = self.client.post(url, { |
|
222 'title': "a new session", |
|
223 'description': "description of the session", |
|
224 'protocol': "[]", |
|
225 }, format='json') |
|
226 |
|
227 self.assertEqual(response.status_code, status.HTTP_201_CREATED, "Error when creating session %r" % response.json()) |
|
228 json = response.json() |
|
229 self.assertIn('ext_id', json) |
|
230 |
|
231 session = Session.objects.get(ext_id=json['ext_id']) |
|
232 self.assertIsNotNone(session) |
|
233 self.assertEqual(self.group1, session.group) |
|
234 |
|
235 |
|
236 def test_create_session_bad_group(self): |
308 def test_create_session_bad_group(self): |
237 url = reverse('notes:session-list') |
309 url = reverse('notes:session-list') |
238 self.client.login(username='test_user3', password='top_secret') |
310 self.client.login(username='test_user3', password='top_secret') |
239 response = self.client.post(url, { |
311 response = self.client.post(url, { |
240 'title': "a new session", |
312 'title': "a new session", |
241 'description': "description of the session", |
313 'description': "description of the session", |
242 'protocol': "[]", |
314 'protocol': { 'owner': 'group1' }, |
243 'group': "group1" |
315 'group': "group1" |
244 }, format='json') |
316 }, format='json') |
245 |
317 |
246 self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) |
318 self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) |
247 json = response.json() |
319 json = response.json() |
349 self.assertEqual(json['count'], 2, "must have two sessions") |
420 self.assertEqual(json['count'], 2, "must have two sessions") |
350 self.assertEqual(len(json['results']), 2, "must have two sessions") |
421 self.assertEqual(len(json['results']), 2, "must have two sessions") |
351 for session_json in json['results']: |
422 for session_json in json['results']: |
352 self.assertIn(session_json.get('title'), ['a new session 3', 'a new session 4']) |
423 self.assertIn(session_json.get('title'), ['a new session 3', 'a new session 4']) |
353 |
424 |
|
425 def test_update_session(self): |
|
426 url = reverse('notes:session-detail', kwargs={'ext_id': self.session1.ext_id}) |
|
427 |
|
428 self.client.login(username='test_user1', password='top_secret') |
|
429 response = self.client.put(url, { |
|
430 'title': "a new session", |
|
431 'description': "description of the session", |
|
432 'group': 'group1' |
|
433 }, format='json') |
|
434 |
|
435 self.assertEqual(response.status_code, status.HTTP_200_OK, "response content: %r" % response.content) |
|
436 json = response.json() |
|
437 self.assertIn('ext_id', json) |
|
438 |
|
439 session = Session.objects.get(ext_id=json['ext_id']) |
|
440 self.assertIsNotNone(session) |
|
441 self.assertEqual(self.group1, session.group) |
|
442 self.assertEqual(self.group1.profile.protocol, session.protocol) |
|
443 |
|
444 @skip("Protocol update not yet implemented") |
|
445 def test_update_session_protocol(self): |
|
446 url = reverse('notes:session-detail', kwargs={'ext_id': self.session1.ext_id}) |
|
447 |
|
448 self.client.login(username='test_user1', password='top_secret') |
|
449 response = self.client.put(url, { |
|
450 'title': "an updated session", |
|
451 'description': "description of the session", |
|
452 'group': 'group1', |
|
453 'protocol': "urn:protocol:845ac623-b85a-4ddd-91ee-a2deb1a9b54f.3" |
|
454 }, format='json') |
|
455 |
|
456 json = response.json() |
|
457 # self.assertIn('ext_id', json) |
|
458 session = Session.objects.get(ext_id=self.session1.ext_id) |
|
459 self.assertEqual(response.status_code, status.HTTP_200_OK, "response content: %r" % response.content) |
|
460 |
|
461 |
|
462 # session = Session.objects.get(ext_id=json['ext_id']) |
|
463 # self.assertIsNotNone(session) |
|
464 # self.assertEqual(self.group1, session.group) |
|
465 # self.assertEqual(self.group1.profile.protocol, session.protocol) |