Newer
Older
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
from __future__ import division
__copyright__ = "Copyright (C) 2018 Dong Zhuang"
__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from django.test import TestCase
from course.content import get_repo_blob
from tests.base_test_mixins import SingleCourseQuizPageTestMixin
from tests.test_sandbox import (
SingleCoursePageSandboxTestBaseMixin
)
from tests.contants import PAGE_ERRORS
from tests.utils import mock
UPLOAD_WITH_NEGATIVE_MAXIMUM_SIZE_MARKDOWN = """
type: FileUploadQuestion
id: test
value: 5
maximum_megabytes: -0.5
prompt: |
# Upload a pdf file
mime_types:
- application/pdf
rubric: |
uploaded?
"""
UPLOAD_WITH_NEGATIVE_VALUE_MARKDOWN = """
type: FileUploadQuestion
id: test
value: -5
maximum_megabytes: 0.5
prompt: |
# Upload a pdf file
mime_types:
- application/pdf
rubric: |
uploaded?
"""
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
UPLOAD_WITHOUT_VALUE_MARKDOWN = """
type: FileUploadQuestion
id: test
maximum_megabytes: 0.5
prompt: |
# Upload a pdf file
mime_types:
- application/pdf
rubric: |
uploaded?
"""
UPLOAD_WITH_UNKNOWN_MIME_TYPES_MARKDOWN = """
type: FileUploadQuestion
id: test
value: 5
maximum_megabytes: 0.5
prompt: |
# Upload a file
mime_types:
- application/pdf
- application/unknown
rubric: |
uploaded?
"""
UPLOAD_WITH_SMALL_MAX_ALLOWED_SIZE = """
type: FileUploadQuestion
id: test
maximum_megabytes: 0.0001
value: 1
prompt: |
# Upload a pdf file
mime_types:
- application/pdf
rubric: |
uploaded?
"""
UPLOAD_WITH_TWO_ALLOWED_MIME_TYPES = """
type: FileUploadQuestion
id: proof
maximum_megabytes: 0.5
value: 5
prompt: |
# Upload a file
mime_types:
- application/pdf
- text/plain
rubric: |
uploaded?
"""
class FileUploadQuestionSandBoxTest(SingleCoursePageSandboxTestBaseMixin, TestCase):
def test_size_validation(self):
markdown = UPLOAD_WITH_NEGATIVE_MAXIMUM_SIZE_MARKDOWN
resp = self.get_page_sandbox_preview_response(markdown)
self.assertEqual(resp.status_code, 200)
self.assertSandboxNotHasValidPage(resp)
self.assertResponseContextContains(
resp, PAGE_ERRORS,
"'maximum_megabytes' expects a positive value, "
def test_negative_value(self):
markdown = UPLOAD_WITH_NEGATIVE_VALUE_MARKDOWN
resp = self.get_page_sandbox_preview_response(markdown)
self.assertEqual(resp.status_code, 200)
self.assertSandboxNotHasValidPage(resp)
self.assertResponseContextContains(
resp, PAGE_ERRORS,
"sandboxAttribute 'value' expects a non-negative value, "
"got -5 instead")
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
def test_mime_types(self):
markdown = UPLOAD_WITH_UNKNOWN_MIME_TYPES_MARKDOWN
resp = self.get_page_sandbox_preview_response(markdown)
self.assertEqual(resp.status_code, 200)
self.assertSandboxNotHasValidPage(resp)
self.assertResponseContextContains(
resp, PAGE_ERRORS,
"unrecognized mime types 'application/unknown'")
def test_no_value(self):
markdown = UPLOAD_WITHOUT_VALUE_MARKDOWN
resp = self.get_page_sandbox_preview_response(markdown)
self.assertEqual(resp.status_code, 200)
self.assertSandboxHasValidPage(resp)
self.assertSandboxWarningTextContain(
resp, "upload question does not have assigned point value")
def test_upload_file_with_size_exceed(self):
markdown = UPLOAD_WITH_SMALL_MAX_ALLOWED_SIZE
from tests.contants import TEST_PDF_FILE_PATH
with open(TEST_PDF_FILE_PATH, 'rb') as fp:
answer_data = {"uploaded_file": fp}
resp = self.get_page_sandbox_submit_answer_response(
markdown,
answer_data=answer_data)
self.assertFormErrorLoose(resp, "Please keep file size under")
self.assertFormErrorLoose(resp, "Current filesize is")
def get_repo_blob_side_effect(repo, full_name, commit_sha, allow_tree=True):
# Fake the inline multiple question yaml for specific commit
if not full_name == "questions/pdf-file-upload-example.yml":
return get_repo_blob(repo, full_name, commit_sha, allow_tree)
else:
class Blob(object):
pass
blob = Blob()
blob.data = UPLOAD_WITH_TWO_ALLOWED_MIME_TYPES.encode()
return blob
class UploadQuestionNormalizeTest(SingleCourseQuizPageTestMixin, TestCase):
def test_two_mime_types_normalize(self):
with mock.patch("course.content.get_repo_blob") as mock_get_repo_blob:
mock_get_repo_blob.side_effect = get_repo_blob_side_effect
self.c.force_login(self.student_participation.user)
self.start_flow(self.flow_id)
# self.default_submit_page_answer_by_page_id_and_test(
# page_id="proof", do_grading=True)
self.submit_page_answer_by_page_id_and_test(
page_id="proof", do_grading=True, do_human_grade=True,
ensure_download_after_grading=True, dl_file_extension=".dat")
# vim: fdm=marker