-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_course_content_view.py
More file actions
346 lines (310 loc) · 10.7 KB
/
test_course_content_view.py
File metadata and controls
346 lines (310 loc) · 10.7 KB
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
import pytest
from django.urls import reverse
import json
LESSON_TITLE = "Introduction to Python"
LESSON_CONTENT = "Welcome to the Python course!"
def get_url() -> str:
return reverse(
"django_email_learning:api:course_content_view",
kwargs={"organization_id": 1, "course_id": 1},
)
def valid_create_course_payload(
title: str = "Python Course",
slug: str = "python",
description: str = "A beginner's course on Python programming.",
) -> dict:
return {
"title": title,
"slug": slug,
"description": description,
"imap_connection_id": None,
}
@pytest.fixture()
def create_course(superadmin_client):
url = reverse(
"django_email_learning:api:course_view",
kwargs={"organization_id": 1},
)
payload = valid_create_course_payload()
superadmin_client.post(url, json.dumps(payload), content_type="application/json")
def test_create_course_lesson_content(superadmin_client, create_course):
url = get_url()
payload = {
"content": {
"title": LESSON_TITLE,
"content": LESSON_CONTENT,
"type": "lesson",
},
"priority": 1,
"waiting_period": {"period": 2, "type": "days"},
}
response = superadmin_client.post(
url, json.dumps(payload), content_type="application/json"
)
assert response.status_code == 201
data = response.json()
assert "lesson" in data
assert data["id"] is not None
assert data["lesson"]["id"] is not None
assert data["lesson"]["title"] == LESSON_TITLE
assert data["lesson"]["content"] == LESSON_CONTENT
assert data["lesson"]["is_published"] is False
assert data["type"] == "lesson"
assert data["priority"] == 1
assert data["waiting_period"] == {"period": 2, "type": "days"}
@pytest.mark.parametrize(
"title,content", [(None, LESSON_CONTENT), (LESSON_TITLE, None)]
)
def test_validate_lesson_content(superadmin_client, create_course, title, content):
url = get_url()
payload = {
"content": {
"title": title,
"content": content,
"type": "lesson",
},
"priority": 1,
"waiting_period": {"period": 2, "type": "days"},
}
response = superadmin_client.post(
url, json.dumps(payload), content_type="application/json"
)
assert response.status_code == 400
assert "error" in response.json()
def test_viewer_can_not_create_course_content(viewer_client, create_course):
url = get_url()
payload = {
"content": {
"title": LESSON_TITLE,
"content": LESSON_CONTENT,
"type": "lesson",
},
"priority": 1,
"waiting_period": {"period": 2, "type": "days"},
}
response = viewer_client.post(
url, json.dumps(payload), content_type="application/json"
)
assert response.status_code == 403
def test_anonymous_user_cannot_create_course_content(anonymous_client, create_course):
url = get_url()
payload = {
"content": {
"title": LESSON_TITLE,
"content": LESSON_CONTENT,
"type": "lesson",
},
"priority": 1,
"waiting_period": {"period": 2, "type": "days"},
}
response = anonymous_client.post(
url, json.dumps(payload), content_type="application/json"
)
assert response.status_code == 401
def test_content_with_same_priority_cannot_be_created(superadmin_client, create_course):
url = get_url()
payload = {
"content": {
"title": LESSON_TITLE,
"content": LESSON_CONTENT,
"type": "lesson",
},
"priority": 1,
"waiting_period": {"period": 2, "type": "days"},
}
response1 = superadmin_client.post(
url, json.dumps(payload), content_type="application/json"
)
assert response1.status_code == 201
response2 = superadmin_client.post(
url, json.dumps(payload), content_type="application/json"
)
assert response2.status_code == 400
assert "error" in response2.json()
def test_create_quiz_content(superadmin_client, create_course):
url = get_url()
payload = {
"content": {
"type": "quiz",
"title": "Quiz 1",
"required_score": 70,
"questions": [
{
"text": "What is Python?",
"priority": 1,
"answers": [
{"text": "A programming language", "is_correct": True},
{"text": "A snake", "is_correct": False},
],
},
{
"text": "Which of these is a Python data type?",
"priority": 2,
"answers": [
{"text": "List", "is_correct": True},
{"text": "Car", "is_correct": False},
],
},
],
},
"priority": 2,
"waiting_period": {"period": 1, "type": "hours"},
}
response = superadmin_client.post(
url, json.dumps(payload), content_type="application/json"
)
assert response.status_code == 201
data = response.json()
assert "quiz" in data
assert data["id"] is not None
assert data["quiz"]["id"] is not None
assert data["type"] == "quiz"
assert data["quiz"]["is_published"] is False
assert data["priority"] == 2
assert data["waiting_period"] == {"period": 1, "type": "hours"}
assert data["quiz"]["title"] == "Quiz 1"
assert data["quiz"]["required_score"] == 70
assert len(data["quiz"]["questions"]) == 2
assert len(data["quiz"]["questions"][0]["answers"]) == 2
assert len(data["quiz"]["questions"][1]["answers"]) == 2
def test_invalid_quiz_content_missing_questions(superadmin_client, create_course):
url = get_url()
payload = {
"content": {
"type": "quiz",
"title": "Quiz without questions",
"required_score": 70,
"questions": [],
},
"priority": 2,
"waiting_period": {"period": 1, "type": "hours"},
}
response = superadmin_client.post(
url, json.dumps(payload), content_type="application/json"
)
assert response.status_code == 400
assert "error" in response.json()
def test_invalid_quiz_content_insufficient_answers(superadmin_client, create_course):
url = get_url()
payload = {
"content": {
"type": "quiz",
"title": "Quiz with insufficient answers",
"required_score": 70,
"questions": [
{
"text": "What is Python?",
"priority": 1,
"answers": [
{"text": "A programming language", "is_correct": True},
],
}
],
},
"priority": 2,
"waiting_period": {"period": 1, "type": "hours"},
}
response = superadmin_client.post(
url, json.dumps(payload), content_type="application/json"
)
assert response.status_code == 400
assert "error" in response.json()
def test_invalid_quiz_content_no_correct_answer(superadmin_client, create_course):
url = get_url()
payload = {
"content": {
"type": "quiz",
"title": "Quiz with no correct answer",
"required_score": 70,
"questions": [
{
"text": "What is Python?",
"priority": 1,
"answers": [
{"text": "A programming language", "is_correct": False},
{"text": "A snake", "is_correct": False},
],
}
],
},
"priority": 2,
"waiting_period": {"period": 1, "type": "hours"},
}
response = superadmin_client.post(
url, json.dumps(payload), content_type="application/json"
)
assert response.status_code == 400
assert "error" in response.json()
@pytest.mark.parametrize(
"client", ["superadmin", "editor", "viewer"], indirect=["client"]
)
def test_list_course_content_access(client, create_course):
url = get_url()
response = client.get(url)
assert response.status_code == 200
data = response.json()
assert isinstance(data["course_contents"], list)
assert len(data["course_contents"]) == 0
def test_anonymous_user_cannot_list_course_content(anonymous_client, create_course):
url = get_url()
response = anonymous_client.get(url)
assert response.status_code == 401
def test_list_course_content_with_existing_contents(superadmin_client, create_course):
url = get_url()
# Create a lesson content
lesson_payload = {
"content": {
"title": LESSON_TITLE,
"content": LESSON_CONTENT,
"type": "lesson",
},
"priority": 1,
"waiting_period": {"period": 2, "type": "days"},
}
superadmin_client.post(
url, json.dumps(lesson_payload), content_type="application/json"
)
# Create a quiz content
quiz_payload = {
"content": {
"type": "quiz",
"title": "Quiz 1",
"required_score": 70,
"questions": [
{
"text": "What is Python?",
"priority": 1,
"answers": [
{"text": "A programming language", "is_correct": True},
{"text": "A snake", "is_correct": False},
],
}
],
},
"priority": 2,
"waiting_period": {"period": 1, "type": "hours"},
}
superadmin_client.post(
url, json.dumps(quiz_payload), content_type="application/json"
)
# Now list the course contents
response = superadmin_client.get(url)
assert response.status_code == 200
data = response.json()
assert isinstance(data["course_contents"], list)
assert len(data["course_contents"]) == 2
assert data["course_contents"][0]["type"] == "lesson"
assert data["course_contents"][1]["type"] == "quiz"
assert data["course_contents"][0]["priority"] == 1
assert data["course_contents"][1]["priority"] == 2
assert data["course_contents"][0]["id"] is not None
assert data["course_contents"][1]["id"] is not None
assert data["course_contents"][0]["title"] == LESSON_TITLE
assert data["course_contents"][1]["title"] == "Quiz 1"
assert data["course_contents"][0]["waiting_period"] == {"period": 2, "type": "days"}
assert data["course_contents"][1]["waiting_period"] == {
"period": 1,
"type": "hours",
}
assert "lesson" not in data["course_contents"][0]
assert "quiz" not in data["course_contents"][1]