|
1
|
|
|
from django.contrib.messages.storage.fallback import FallbackStorage |
|
2
|
|
|
from django.test import LiveServerTestCase, TestCase |
|
3
|
|
|
from django.test.utils import override_settings |
|
4
|
|
|
from django.test import client |
|
5
|
|
|
from django.apps import apps |
|
6
|
|
|
from django import db, http |
|
7
|
|
|
|
|
8
|
|
|
from .helpers.user import * |
|
9
|
|
|
from .helpers.assignment import * |
|
10
|
|
|
from .helpers.course import create_course |
|
11
|
|
|
from .helpers.submission import create_submission |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
class MockRequest(http.HttpRequest): |
|
15
|
|
|
def __init__(self, user): |
|
16
|
|
|
self.user = user |
|
17
|
|
|
# Needed for mocking a functioning messaging middleware |
|
18
|
|
|
# see https://code.djangoproject.com/ticket/17971 |
|
19
|
|
|
self.session = 'session' |
|
20
|
|
|
self._messages = FallbackStorage(self) |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
@override_settings( |
|
24
|
|
|
PASSWORD_HASHERS=['django.contrib.auth.hashers.MD5PasswordHasher', ]) |
|
25
|
|
|
@override_settings(MEDIA_ROOT='/tmp/') |
|
26
|
|
|
class SubmitTestCase(LiveServerTestCase): |
|
27
|
|
|
def setUp(self): |
|
28
|
|
|
self.c = client.Client() |
|
29
|
|
|
|
|
30
|
|
|
def login_user(self, user_struct): |
|
31
|
|
|
self.user_struct = user_struct |
|
32
|
|
|
self.c.login(username=user_struct['username'], |
|
33
|
|
|
password=user_struct['password']) |
|
34
|
|
|
if not self.user: |
|
35
|
|
|
uid = self.c.session['_auth_user_id'] |
|
36
|
|
|
self.user = User.objects.get(pk=uid) |
|
37
|
|
|
|
|
38
|
|
|
def create_and_login_user(self, user_struct): |
|
39
|
|
|
self.user_struct = user_struct |
|
40
|
|
|
self.user = create_user(user_struct) |
|
41
|
|
|
self.login_user(user_struct) |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
class SubmitAdminTestCase(SubmitTestCase): |
|
45
|
|
|
''' |
|
46
|
|
|
Test case with an admin logged-in. |
|
47
|
|
|
''' |
|
48
|
|
|
|
|
49
|
|
|
def setUp(self): |
|
50
|
|
|
super(SubmitAdminTestCase, self).setUp() |
|
51
|
|
|
self.create_and_login_user(admin_dict) |
|
52
|
|
|
self.request = MockRequest(self.user) |
|
53
|
|
|
# Test for amok-running post_save handlers (we had such a case) |
|
54
|
|
|
assert(self.user.is_active) |
|
55
|
|
|
assert(self.user.is_superuser) |
|
56
|
|
|
assert(self.user.is_staff) |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
class SubmitTeacherTestCase(SubmitTestCase): |
|
60
|
|
|
''' |
|
61
|
|
|
Test case with an teacher (course owner) logged-in. |
|
62
|
|
|
''' |
|
63
|
|
|
|
|
64
|
|
|
def setUp(self): |
|
65
|
|
|
super(SubmitTeacherTestCase, self).setUp() |
|
66
|
|
|
self.create_and_login_user(teacher_dict) |
|
67
|
|
|
self.request = MockRequest(self.user) |
|
68
|
|
|
# Test for amok-running post_save handlers (we had such a case) |
|
69
|
|
|
assert(self.user.is_active) |
|
70
|
|
|
assert(self.user.is_staff) |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
class SubmitTutorTestCase(SubmitTestCase): |
|
74
|
|
|
''' |
|
75
|
|
|
Test case with a tutor logged-in. |
|
76
|
|
|
''' |
|
77
|
|
|
|
|
78
|
|
|
def setUp(self): |
|
79
|
|
|
super(SubmitTutorTestCase, self).setUp() |
|
80
|
|
|
self.create_and_login_user(tutor_dict) |
|
81
|
|
|
self.request = MockRequest(self.user) |
|
82
|
|
|
# Test for amok-running post_save handlers (we had such a case) |
|
83
|
|
|
assert(self.user.is_active) |
|
84
|
|
|
assert(not self.user.is_superuser) |
|
85
|
|
|
assert(self.user.is_staff) |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
class SubmitStudentTestCase(SubmitTestCase): |
|
89
|
|
|
''' |
|
90
|
|
|
Test case with a student logged-in. |
|
91
|
|
|
''' |
|
92
|
|
|
|
|
93
|
|
|
def setUp(self): |
|
94
|
|
|
super(SubmitStudentTestCase, self).setUp() |
|
95
|
|
|
self.create_and_login_user(get_student_dict(0)) |
|
96
|
|
|
self.request = MockRequest(self.user) |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
class SubmitStudentScenarioTestCase(SubmitStudentTestCase): |
|
100
|
|
|
''' |
|
101
|
|
|
As above, but with a common set of prepared resources |
|
102
|
|
|
from a default usage scenario. |
|
103
|
|
|
|
|
104
|
|
|
To speed up test runs, it is preferrable to create |
|
105
|
|
|
the needed resources explicitely, instead of inherting |
|
106
|
|
|
from this class. |
|
107
|
|
|
|
|
108
|
|
|
Some cases create the submissions at specific points, |
|
109
|
|
|
so this is not part of the constructor. |
|
110
|
|
|
''' |
|
111
|
|
|
def setUp(self): |
|
112
|
|
|
super(SubmitStudentScenarioTestCase, self).setUp() |
|
113
|
|
|
self.admin = create_user(admin_dict) |
|
114
|
|
|
self.teacher = create_user(teacher_dict) |
|
115
|
|
|
self.tutor = create_user(tutor_dict) |
|
116
|
|
|
self.course = create_course(self.teacher, self.tutor) |
|
117
|
|
|
self.course.participants.add(self.user.profile) |
|
118
|
|
|
grading = create_pass_fail_grading() |
|
119
|
|
|
|
|
120
|
|
|
self.open_assignment = create_open_assignment( |
|
121
|
|
|
self.course, grading) |
|
122
|
|
|
self.open_file_assignment = create_open_file_assignment( |
|
123
|
|
|
self.course, grading) |
|
124
|
|
|
self.soft_deadline_passed_assignment = create_soft_passed_assignment( |
|
125
|
|
|
self.course, grading) |
|
126
|
|
|
self.hard_deadline_passed_assignment = create_hard_passed_assignment( |
|
127
|
|
|
self.course, grading) |
|
128
|
|
|
self.no_hard_assignment = create_no_hard_soft_passed_assignment( |
|
129
|
|
|
self.course, grading) |
|
130
|
|
|
self.no_grading_assignment = create_no_grading_assignment( |
|
131
|
|
|
self.course) |
|
132
|
|
|
self.unpublished_assignment = create_unpublished_assignment( |
|
133
|
|
|
self.course, grading) |
|
134
|
|
|
self.uploaded_desc_assignment = create_uploaded_desc_assignment( |
|
135
|
|
|
self.course, grading) |
|
136
|
|
|
self.validated_assignment = create_validated_assignment_with_archive( |
|
137
|
|
|
self.course, grading) |
|
138
|
|
|
|
|
139
|
|
|
self.all_assignments = ( |
|
140
|
|
|
self.open_assignment, |
|
141
|
|
|
self.open_file_assignment, |
|
142
|
|
|
self.soft_deadline_passed_assignment, |
|
143
|
|
|
self.hard_deadline_passed_assignment, |
|
144
|
|
|
self.no_hard_assignment, |
|
145
|
|
|
self.no_grading_assignment, |
|
146
|
|
|
self.unpublished_assignment, |
|
147
|
|
|
self.uploaded_desc_assignment, |
|
148
|
|
|
self.validated_assignment |
|
149
|
|
|
) |
|
150
|
|
|
|
|
151
|
|
|
self.all_student_visible_assignments = ( |
|
152
|
|
|
self.open_assignment, |
|
153
|
|
|
self.open_file_assignment, |
|
154
|
|
|
self.soft_deadline_passed_assignment, |
|
155
|
|
|
self.no_hard_assignment, |
|
156
|
|
|
self.no_grading_assignment, |
|
157
|
|
|
self.uploaded_desc_assignment, |
|
158
|
|
|
self.validated_assignment |
|
159
|
|
|
) |
|
160
|
|
|
|
|
161
|
|
|
self.another_course = create_course(self.admin) |
|
162
|
|
|
|
|
163
|
|
|
self.another_open_assignment = create_open_assignment( |
|
164
|
|
|
self.another_course, grading) |
|
165
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
def create_submissions(self): |
|
168
|
|
|
self.open_assignment_sub = create_submission( |
|
169
|
|
|
self.user, |
|
170
|
|
|
self.open_assignment) |
|
171
|
|
|
self.soft_deadline_passed_assignment_sub = create_submission( |
|
172
|
|
|
self.user, |
|
173
|
|
|
self.soft_deadline_passed_assignment) |
|
174
|
|
|
self.hard_deadline_passed_assignment_sub = create_submission( |
|
175
|
|
|
self.user, |
|
176
|
|
|
self.hard_deadline_passed_assignment) |
|
177
|
|
|
self.no_hard_assignment_sub = create_submission( |
|
178
|
|
|
self.user, |
|
179
|
|
|
self.no_hard_assignment) |
|
180
|
|
|
self.no_grading_assignment_sub = create_submission( |
|
181
|
|
|
self.user, |
|
182
|
|
|
self.no_grading_assignment) |
|
183
|
|
|
self.unpublished_assignment_sub = create_submission( |
|
184
|
|
|
self.user, |
|
185
|
|
|
self.unpublished_assignment) |
|
186
|
|
|
self.uploaded_desc_assignment_sub = create_submission( |
|
187
|
|
|
self.user, |
|
188
|
|
|
self.uploaded_desc_assignment) |
|
189
|
|
|
|
|
190
|
|
|
self.submissions = ( |
|
191
|
|
|
self.open_assignment_sub, |
|
192
|
|
|
self.soft_deadline_passed_assignment_sub, |
|
193
|
|
|
self.hard_deadline_passed_assignment_sub, |
|
194
|
|
|
self.no_hard_assignment_sub, |
|
195
|
|
|
self.no_grading_assignment_sub, |
|
196
|
|
|
self.unpublished_assignment_sub, |
|
197
|
|
|
self.uploaded_desc_assignment_sub |
|
198
|
|
|
) |
|
199
|
|
|
|
|
200
|
|
|
|
|
201
|
|
|
class TestMigrations(TestCase): |
|
202
|
|
|
''' |
|
203
|
|
|
Taken from https://www.caktusgroup.com/blog/2016/02/02/ |
|
204
|
|
|
''' |
|
205
|
|
|
|
|
206
|
|
|
@property |
|
207
|
|
|
def app(self): |
|
208
|
|
|
return apps.get_containing_app_config(type(self).__module__).name |
|
209
|
|
|
|
|
210
|
|
|
migrate_from = None |
|
211
|
|
|
migrate_to = None |
|
212
|
|
|
|
|
213
|
|
|
def setUp(self): |
|
214
|
|
|
assert self.migrate_from and self.migrate_to |
|
215
|
|
|
self.migrate_from = [(self.app, self.migrate_from)] |
|
216
|
|
|
self.migrate_to = [(self.app, self.migrate_to)] |
|
217
|
|
|
executor = db.migrations.executor.MigrationExecutor(db.connection) |
|
218
|
|
|
old_apps = executor.loader.project_state(self.migrate_from).apps |
|
219
|
|
|
|
|
220
|
|
|
# Reverse to the original migration |
|
221
|
|
|
executor.migrate(self.migrate_from) |
|
222
|
|
|
|
|
223
|
|
|
self.setUpBeforeMigration(old_apps) |
|
224
|
|
|
|
|
225
|
|
|
# Run the migration to test |
|
226
|
|
|
executor = db.migrations.executor.MigrationExecutor(db.connection) |
|
227
|
|
|
executor.loader.build_graph() # reload. |
|
228
|
|
|
executor.migrate(self.migrate_to) |
|
229
|
|
|
|
|
230
|
|
|
self.apps = executor.loader.project_state(self.migrate_to).apps |
|
231
|
|
|
|
|
232
|
|
|
def setUpBeforeMigration(self, apps): |
|
233
|
|
|
pass |
|
234
|
|
|
|