| author | ymh <ymh.work@gmail.com> |
| Thu, 22 Jun 2017 12:09:48 +0200 | |
| changeset 74 | 043477fd5c5c |
| parent 31 | 63be3ce389f7 |
| child 119 | 8ff8e2aee0f9 |
| permissions | -rw-r--r-- |
| 31 | 1 |
""" |
2 |
Tests the core api for sessions |
|
3 |
""" |
|
4 |
import logging |
|
|
74
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
5 |
from uuid import uuid4 |
| 31 | 6 |
|
7 |
from django.contrib.auth import get_user_model |
|
8 |
from django.urls import reverse |
|
9 |
from django.utils import timezone |
|
10 |
from rest_framework import status |
|
11 |
from rest_framework.test import APITransactionTestCase |
|
12 |
||
13 |
from notes.models import Session, Note |
|
14 |
||
15 |
logger = logging.getLogger(__name__) |
|
16 |
||
17 |
class SessionApiTests(APITransactionTestCase): |
|
18 |
||
19 |
def setUp(self): |
|
20 |
User = get_user_model() |
|
21 |
user1 = User.objects.create_user( |
|
22 |
username='test_user1', |
|
23 |
email='test_user@emial.com', |
|
24 |
password='top_secret' |
|
25 |
) |
|
26 |
user2 = User.objects.create_user( |
|
27 |
username='test_user2', |
|
28 |
email='test_user@emial.com', |
|
29 |
password='top_secret' |
|
30 |
) |
|
31 |
user3 = User.objects.create_user( |
|
32 |
username='test_user3', |
|
33 |
email='test_user@emial.com', |
|
34 |
password='top_secret' |
|
35 |
) |
|
36 |
||
37 |
self.session1 = Session.objects.create( |
|
38 |
title="a new session 1", |
|
39 |
description="Description 1", |
|
40 |
protocol="[]", |
|
41 |
owner=user1 |
|
42 |
) |
|
43 |
||
44 |
self.session2 = Session.objects.create( |
|
45 |
title="a new session 2", |
|
46 |
description="Description 2", |
|
47 |
protocol="[]", |
|
48 |
owner=user2 |
|
49 |
) |
|
50 |
||
51 |
Session.objects.create( |
|
52 |
title="a new session 3", |
|
53 |
description="Description 3", |
|
54 |
protocol="[]", |
|
55 |
owner=user3 |
|
56 |
) |
|
57 |
||
58 |
Note.objects.create( |
|
59 |
tc_start=timezone.now(), |
|
60 |
tc_end=timezone.now(), |
|
61 |
session=self.session1, |
|
62 |
plain="example note 1", |
|
63 |
html="<i>example note 1</i>", |
|
64 |
raw="<i>example note 1</i>", |
|
65 |
margin_note="margin note 1", |
|
66 |
categorization="[]" |
|
67 |
) |
|
68 |
||
69 |
Note.objects.create( |
|
70 |
tc_start=timezone.now(), |
|
71 |
tc_end=timezone.now(), |
|
72 |
session=self.session2, |
|
73 |
plain="example note 2", |
|
74 |
html="<i>example note</i>", |
|
75 |
raw="<i>example note</i>", |
|
76 |
margin_note="margin note", |
|
77 |
categorization="[]" |
|
78 |
) |
|
79 |
||
80 |
||
81 |
def test_list_session_no_user(self): |
|
|
74
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
82 |
url = reverse('notes:session-list') |
| 31 | 83 |
response = self.client.post(url) |
|
74
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
84 |
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) |
| 31 | 85 |
|
86 |
||
87 |
def test_list_session(self): |
|
|
74
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
88 |
url = reverse('notes:session-list') |
| 31 | 89 |
self.client.login(username='test_user1', password='top_secret') |
90 |
response = self.client.get(url) |
|
91 |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
|
92 |
json = response.json() |
|
93 |
self.assertEqual(len(json), 1, "must have one session") |
|
94 |
for session in json: |
|
95 |
self.assertEqual(session['owner'], 'test_user1') |
|
96 |
||
97 |
||
98 |
def test_create_session_no_user(self): |
|
|
74
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
99 |
url = reverse('notes:session-list') |
| 31 | 100 |
response = self.client.post(url, { |
101 |
'title': "a new session", |
|
102 |
'description': "description of the session", |
|
103 |
'protocol': "[]" |
|
104 |
}, format='json') |
|
105 |
||
|
74
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
106 |
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) |
| 31 | 107 |
|
108 |
||
109 |
def test_create_session(self): |
|
|
74
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
110 |
url = reverse('notes:session-list') |
| 31 | 111 |
self.client.login(username='test_user1', password='top_secret') |
112 |
response = self.client.post(url, { |
|
113 |
'title': "a new session", |
|
114 |
'description': "description of the session", |
|
115 |
'protocol': "[]" |
|
116 |
}, format='json') |
|
117 |
||
118 |
||
119 |
self.assertEqual(response.status_code, status.HTTP_201_CREATED) |
|
120 |
json = response.json() |
|
121 |
self.assertIn('ext_id', json) |
|
122 |
||
|
74
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
123 |
def test_create_session_with_ext_id(self): |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
124 |
url = reverse('notes:session-list') |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
125 |
self.client.login(username='test_user1', password='top_secret') |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
126 |
ext_id = str(uuid4()) |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
127 |
response = self.client.post(url, { |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
128 |
'ext_id': ext_id, |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
129 |
'title': "a new session", |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
130 |
'description': "description of the session", |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
131 |
'protocol': "[]" |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
132 |
}, format='json') |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
133 |
|
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
134 |
self.assertEqual(response.status_code, status.HTTP_201_CREATED) |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
135 |
json = response.json() |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
136 |
self.assertIn('ext_id', json) |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
137 |
self.assertEqual(json['ext_id'], ext_id) |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
138 |
|
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
139 |
|
| 31 | 140 |
def test_detail_session(self): |
|
74
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
141 |
url = reverse('notes:session-detail', kwargs={'ext_id':str(self.session1.ext_id)}) |
| 31 | 142 |
self.client.login(username='test_user1', password='top_secret') |
143 |
response = self.client.get(url, format='json') |
|
144 |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
|
145 |
||
146 |
def test_list_notes(self): |
|
|
74
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
147 |
url = reverse('notes:notes-list', kwargs={'session_ext_id':str(self.session1.ext_id)}) |
| 31 | 148 |
self.client.login(username='test_user1', password='top_secret') |
149 |
response = self.client.get(url, format='json') |
|
150 |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
|
151 |
||
152 |
def test_detail_session_bad(self): |
|
|
74
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
153 |
url = reverse('notes:session-detail', kwargs={'ext_id':str(self.session2.ext_id)}) |
| 31 | 154 |
self.client.login(username='test_user1', password='top_secret') |
155 |
response = self.client.get(url, format='json') |
|
156 |
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) |
|
157 |
||
158 |
def test_list_notes_bad(self): |
|
|
74
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
159 |
url = reverse('notes:notes-list', kwargs={'session_ext_id':str(self.session2.ext_id)}) |
| 31 | 160 |
logger.debug("URL: %s", url) |
161 |
self.client.login(username='test_user1', password='top_secret') |
|
162 |
response = self.client.get(url, format='json') |
|
163 |
logger.debug(response.json()) |
|
164 |
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) |