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
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
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 tests.base_test_mixins import (
SubprocessRunpyContainerMixin,
)
from tests.test_sandbox import (
SingleCoursePageSandboxTestBaseMixin, PAGE_ERRORS
)
from tests.utils import LocmemBackendTestsMixin, mock, mail
from . import markdowns
NO_CORRECTNESS_INFO_MSG = "No information on correctness of answer."
NOT_ALLOW_MULTIPLE_SUBMISSION_WARNING = (
"code question does not explicitly "
"allow multiple submission. Either add "
"access_rules/add_permssions/change_answer "
"or add 'single_submission: True' to confirm that you intend "
"for only a single submission to be allowed. "
"While you're at it, consider adding "
"access_rules/add_permssions/see_correctness."
)
MAX_AUTO_FEEDBACK_POINTS_VALICATION_ERROR_MSG_PATTERN = ( # noqa
"'max_auto_feedback_points' is invalid: expecting "
"a value within [0, %(max_extra_credit_factor)s], "
"got %(invalid_value)s."
)
GRADE_CODE_FAILING_MSG = (
"The grading code failed. Sorry about that."
)
RUNPY_WITH_RETRIES_PATH = "course.page.code.request_python_run_with_retries"
class CodeQuestionTest(SingleCoursePageSandboxTestBaseMixin,
SubprocessRunpyContainerMixin, LocmemBackendTestsMixin,
TestCase):
def test_data_files_missing_random_question_data_file(self):
file_name = "foo"
markdown = (
markdowns.CODE_MARKDWON_PATTERN_WITH_DATAFILES
% {"extra_data_file": "- %s" % file_name}
)
resp = self.get_page_sandbox_preview_response(markdown)
self.assertEqual(resp.status_code, 200)
self.assertSandboxNotHaveValidPage(resp)
self.assertResponseContextContains(
resp, PAGE_ERRORS, "data file '%s' not found" % file_name)
def test_not_multiple_submit_warning(self):
markdown = (
markdowns.CODE_MARKDWON_PATTERN_WITH_DATAFILES
% {"extra_data_file": ""}
)
resp = self.get_page_sandbox_preview_response(markdown)
self.assertEqual(resp.status_code, 200)
self.assertSandboxHaveValidPage(resp)
self.assertSandboxWarningTextContain(
resp,
NOT_ALLOW_MULTIPLE_SUBMISSION_WARNING
)
def test_explicity_not_allow_multiple_submit(self):
markdown = (
markdowns.CODE_MARKDWON_PATTERN_EXPLICITLY_NOT_ALLOW_MULTI_SUBMIT
% {"extra_data_file": ""}
)
resp = self.get_page_sandbox_preview_response(markdown)
self.assertEqual(resp.status_code, 200)
self.assertSandboxHaveValidPage(resp)
self.assertSandboxWarningTextContain(resp, None)
def test_question_without_test_code(self):
markdown = markdowns.CODE_MARKDWON_PATTERN_WITHOUT_TEST_CODE
resp = self.get_page_sandbox_preview_response(markdown)
self.assertEqual(resp.status_code, 200)
self.assertSandboxHaveValidPage(resp)
self.assertSandboxWarningTextContain(resp, None)
resp = self.get_page_sandbox_submit_answer_response(
markdown,
answer_data={"answer": ['c = b + a\r']})
self.assertEqual(resp.status_code, 200)
self.assertResponseContextAnswerFeedbackCorrectnessEquals(resp, None)
self.assertResponseContextAnswerFeedbackContainsFeedback(
resp, NO_CORRECTNESS_INFO_MSG)
def test_question_without_correct_code(self):
markdown = markdowns.CODE_MARKDWON_PATTERN_WITHOUT_CORRECT_CODE
resp = self.get_page_sandbox_preview_response(markdown)
self.assertEqual(resp.status_code, 200)
self.assertSandboxHaveValidPage(resp)
self.assertSandboxWarningTextContain(resp, None)
resp = self.get_page_sandbox_submit_answer_response(
markdown,
answer_data={"answer": ['c = b + a\r']})
self.assertEqual(resp.status_code, 200)
self.assertResponseContextAnswerFeedbackCorrectnessEquals(resp, 1)
def test_request_python_run_with_retries_raise_uncaught_error(self):
with mock.patch(
RUNPY_WITH_RETRIES_PATH,
autospec=True
) as mock_runpy:
expected_error_str = ("This is an error raised with "
"request_python_run_with_retries")
mock_runpy.side_effect = RuntimeError(expected_error_str)
with mock.patch("course.page.PageContext") as mock_page_context:
mock_page_context.return_value.in_sandbox = False
# This remove the warning caused by mocked commit_sha value
# "CacheKeyWarning: Cache key contains characters that
# will cause errors ..."
mock_page_context.return_value.commit_sha = b"1234"
resp = self.get_page_sandbox_submit_answer_response(
markdowns.CODE_MARKDWON,
answer_data={"answer": ['c = b + a\r']})
self.assertEqual(resp.status_code, 200)
self.assertResponseContextAnswerFeedbackCorrectnessEquals(resp,
None)
self.assertEqual(len(mail.outbox), 1)
self.assertIn(expected_error_str, mail.outbox[0].body)
def test_send_email_failure_when_request_python_run_with_retries_raise_uncaught_error(self): # noqa
with mock.patch(
RUNPY_WITH_RETRIES_PATH,
autospec=True
) as mock_runpy:
expected_error_str = ("This is an error raised with "
"request_python_run_with_retries")
mock_runpy.side_effect = RuntimeError(expected_error_str)
with mock.patch("course.page.PageContext") as mock_page_context:
mock_page_context.return_value.in_sandbox = False
# This remove the warning caused by mocked commit_sha value
# "CacheKeyWarning: Cache key contains characters that
# will cause errors ..."
mock_page_context.return_value.commit_sha = b"1234"
with mock.patch("django.core.mail.message.EmailMessage.send") as mock_send: # noqa
mock_send.side_effect = RuntimeError("some email send error")
resp = self.get_page_sandbox_submit_answer_response(
markdowns.CODE_MARKDWON,
answer_data={"answer": ['c = b + a\r']})
self.assertContains(resp, expected_error_str)
self.assertEqual(resp.status_code, 200)
self.assertResponseContextAnswerFeedbackCorrectnessEquals(resp,
None)
self.assertEqual(len(mail.outbox), 0)
def assert_runpy_result_and_response(self, result_type, expected_msg,
correctness=0, mail_count=0,
**extra_result):
with mock.patch(RUNPY_WITH_RETRIES_PATH, autospec=True) as mock_runpy:
result = {"result": result_type}
result.update(extra_result)
mock_runpy.return_value = result
resp = self.get_page_sandbox_submit_answer_response(
markdowns.CODE_MARKDWON,
answer_data={"answer": ['c = b + a\r']})
self.assertResponseContextAnswerFeedbackContainsFeedback(resp,
expected_msg)
self.assertEqual(resp.status_code, 200)
self.assertResponseContextAnswerFeedbackCorrectnessEquals(resp,
correctness)
self.assertEqual(len(mail.outbox), mail_count)
def test_request_python_run_with_retries_timed_out(self):
self.assert_runpy_result_and_response(
"timeout",
"Your code took too long to execute.")
def test_user_compile_error(self):
self.assert_runpy_result_and_response(
"user_compile_error",
"Your code failed to compile."
)
def test_user_error(self):
self.assert_runpy_result_and_response(
"user_error",
"Your code failed with an exception.")
def test_unknown_error(self):
with self.assertRaises(RuntimeError) as e:
self.assert_runpy_result_and_response(
"unknown_error", None)
self.assertIn("invalid runpy result: unknown_error", str(e.exception))
def test_html_bleached_in_feedback(self):
self.assert_runpy_result_and_response(
"user_error",
"",
html="<p>some html</p>"
)
def test_traceback_in_feedback(self):
self.assert_runpy_result_and_response(
"user_error",
"some traceback",
traceback="some traceback"
)
def test_stdout_in_feedback(self):
self.assert_runpy_result_and_response(
"user_error",
"some stdout",
stdout="some stdout"
)
def test_stderr_in_feedback(self):
self.assert_runpy_result_and_response(
"user_error",
"some stderr",
stderr="some stderr"
)
# vim: fdm=marker