Newer
Older
# -*- coding: utf-8 -*-
from __future__ import division
__copyright__ = "Copyright (C) 2017 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 datetime import datetime
import unittest
from django.test import SimpleTestCase, TestCase
from django.test.utils import override_settings
from django.utils import translation
from django.conf import settings
from relate.utils import localize_datetime, format_datetime_local
from course.utils import (
get_course_specific_language_choices, LanguageOverride,
get_custom_page_types_stop_support_deadline)
SingleCoursePageTestMixin, SubprocessRunpyContainerMixin,
SingleCourseTestMixin, FallBackStorageMessageTestMixin)
from tests.utils import mock
if DJANGO_VERSION < (2, 0):
REAL_TRANSLATION_FUNCTION_TO_MOCK = (
"django.utils.translation.trans_real.do_translate")
real_trans_side_effect = lambda x, y: x # noqa
else:
# "do_translate(message, translation_function)" was refactored to
# "gettext(message)" since Django >= 2.0
REAL_TRANSLATION_FUNCTION_TO_MOCK = (
"django.utils.translation._trans.gettext")
real_trans_side_effect = lambda x: x # noqa
def is_travis_py3():
import os
if "RL_TRAVIS_TEST" not in os.environ:
return False
if six.PY2:
return False
return True
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
class GetCourseSpecificLanguageChoicesTest(SimpleTestCase):
# test course.utils.get_course_specific_language_choices
LANGUAGES_CONF1 = [
('en', 'English'),
('zh-hans', 'Simplified Chinese'),
('de', 'German')]
LANGUAGES_CONF2 = [
('en', 'English'),
('zh-hans', 'Simplified Chinese'),
('zh-hans', 'my Simplified Chinese'),
('de', 'German')]
@override_settings(USE_I18N=False, LANGUAGES=LANGUAGES_CONF1,
LANGUAGE_CODE='ko')
def test_i18n_disabled(self):
choices = get_course_specific_language_choices()
self.assertTrue(choices[0][1].startswith("Default:"))
self.assertNotIn("disabled", choices[0][1])
self.assertEqual(len(choices), 4)
self.assertIn("(ko)", choices[0][1])
@override_settings(USE_I18N=False, LANGUAGES=LANGUAGES_CONF1,
LANGUAGE_CODE='en')
def test_i18n_disabled_lang_items_has_same_lang_code_with_language_code(self):
choices = get_course_specific_language_choices()
self.assertTrue(choices[0][1].startswith("Default:"))
self.assertNotIn("disabled", choices[0][1])
self.assertEqual(len(choices), 3)
@override_settings(USE_I18N=False, LANGUAGES=LANGUAGES_CONF2,
LANGUAGE_CODE='en-us')
def test_i18n_disabled_lang_items_having_duplicated_lang_code(self):
choices = get_course_specific_language_choices()
self.assertTrue(choices[0][1].startswith("Default:"))
self.assertNotIn("disabled", choices[0][1])
self.assertEqual(len(choices), 4)
@override_settings(USE_I18N=True, LANGUAGES=LANGUAGES_CONF1,
LANGUAGE_CODE='ko')
def test_i18n_enabled(self):
choices = get_course_specific_language_choices()
self.assertTrue(choices[0][1].startswith("Default: disabled"))
self.assertEqual(len(choices), 5)
self.assertIn("(ko)", choices[1][1])
@override_settings(USE_I18N=True, LANGUAGES=LANGUAGES_CONF1,
LANGUAGE_CODE='en')
def test_i18n_enabled_lang_items_has_same_lang_code_with_language_code(self):
choices = get_course_specific_language_choices()
self.assertTrue(choices[0][1].startswith("Default: disabled"))
self.assertEqual(len(choices), 4)
@override_settings(USE_I18N=True, LANGUAGES=LANGUAGES_CONF2,
LANGUAGE_CODE='en-us')
def test_i18n_enabled_lang_items_having_duplicated_lang_code(self):
choices = get_course_specific_language_choices()
self.assertEqual(len(choices), 5)
self.assertTrue(choices[0][1].startswith("Default: disabled"))
def lang_descr_get_translated(self, choice_count):
with mock.patch("course.utils._") as mock_ugettext, \
mock.patch("django.utils.translation.ugettext_lazy") \
as mock_ugettext_lazy:
mock_ugettext.side_effect = lambda x: x
mock_ugettext_lazy.side_effect = lambda x: x
choices = get_course_specific_language_choices()
self.assertEqual(len(choices), choice_count)
# "English", "Default", "my Simplified Chinese" and "German" are
# called by django.utils.translation.ugettext, for at least once.
# Another language description literals (especially "Simplified Chinese")
# are not called by it.
self.assertTrue(mock_ugettext.call_count >= 4)
simplified_chinese_as_arg_count = 0
my_simplified_chinese_as_arg_count = 0
for call in mock_ugettext.call_args_list:
arg, kwargs = call
if "my Simplified Chinese" in arg:
my_simplified_chinese_as_arg_count += 1
if "Simplified Chinese" in arg:
simplified_chinese_as_arg_count += 1
self.assertEqual(simplified_chinese_as_arg_count, 0)
self.assertTrue(my_simplified_chinese_as_arg_count > 0)
def test_lang_descr_translated(self):
with override_settings(USE_I18N=True, LANGUAGES=self.LANGUAGES_CONF2,
LANGUAGE_CODE='en-us'):
self.lang_descr_get_translated(choice_count=5)
with override_settings(USE_I18N=True, LANGUAGES=self.LANGUAGES_CONF2,
LANGUAGE_CODE='en-us'):
self.lang_descr_get_translated(choice_count=5)
def test_user_customized_lang_code_as_settings_language_code(self):
with override_settings(USE_I18N=True, LANGUAGES=self.LANGUAGES_CONF2,
LANGUAGE_CODE='user_customized_lang_code'):
with self.assertRaises(IOError):
# because there's no file named "user_customized_lang_code.mo"
get_course_specific_language_choices()
with mock.patch(REAL_TRANSLATION_FUNCTION_TO_MOCK) as mock_gettext:
mock_gettext.side_effect = real_trans_side_effect
choices = get_course_specific_language_choices()
# The language description is the language_code, because it can't
# be found in django.conf.locale.LANG_INFO
self.assertEqual(choices[1][1], "user_customized_lang_code")
with override_settings(USE_I18N=False, LANGUAGES=self.LANGUAGES_CONF2,
LANGUAGE_CODE='user_customized_lang_code'):
with mock.patch(REAL_TRANSLATION_FUNCTION_TO_MOCK) as mock_gettext:
mock_gettext.side_effect = real_trans_side_effect
choices = get_course_specific_language_choices()
# The language description is the language_code, because it can't
# be found in django.conf.locale.LANG_INFO
self.assertIn("user_customized_lang_code", choices[0][1])
class LanguageOverrideTest(SingleCoursePageTestMixin,
SubprocessRunpyContainerMixin, TestCase):
# test course.utils.LanguageOverride
@classmethod
def setUpTestData(cls): # noqa
super(LanguageOverrideTest, cls).setUpTestData()
cls.c.force_login(cls.instructor_participation.user)
cls.start_flow(cls.flow_id)
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
@override_settings(RELATE_ADMIN_EMAIL_LOCALE="de", LANGUAGE_CODE="ko")
def test_language_override_no_course_force_lang(self):
if self.course.force_lang:
self.course.force_lang = ""
self.course.save()
with LanguageOverride(course=self.course):
self.assertEqual(translation.get_language(), "de")
self.assertEqual(translation.ugettext("user"), u"Benutzer")
self.assertEqual(translation.get_language(), "ko")
self.assertEqual(translation.ugettext("user"), u"사용자")
@override_settings(RELATE_ADMIN_EMAIL_LOCALE="de", LANGUAGE_CODE="ko")
def test_language_override_course_has_force_lang(self):
self.course.force_lang = "zh-hans"
self.course.save()
with LanguageOverride(course=self.course):
self.assertEqual(translation.get_language(), "zh-hans")
self.assertEqual(translation.get_language(), "ko")
@override_settings(RELATE_ADMIN_EMAIL_LOCALE=None)
def test_language_override_no_course_force_lang_no_admin_lang(self):
if self.course.force_lang:
self.course.force_lang = ""
self.course.save()
with LanguageOverride(course=self.course):
self.assertEqual(translation.get_language(), None)
self.assertEqual(translation.ugettext("whatever"), "whatever")
self.assertEqual(translation.get_language(), "en-us")
@override_settings(RELATE_ADMIN_EMAIL_LOCALE="de")
def test_language_override_no_course_force_lang_no_langcode(self):
if self.course.force_lang:
self.course.force_lang = ""
self.course.save()
translation.deactivate_all()
with LanguageOverride(course=self.course):
self.assertEqual(translation.get_language(), "de")
self.assertEqual(translation.ugettext("user"), u"Benutzer")
self.assertEqual(translation.get_language(), None)
self.assertEqual(translation.ugettext("whatever"), "whatever")
@override_settings(RELATE_ADMIN_EMAIL_LOCALE="de")
def test_language_override_deactivate(self):
self.course.force_lang = "zh-hans"
self.course.save()
with LanguageOverride(course=self.course, deactivate=True):
self.assertEqual(translation.get_language(), "zh-hans")
self.assertEqual(translation.ugettext("user"), u"用户")
self.assertEqual(translation.get_language(), "en-us")
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
page_id_literal_dict = {
"half": {"literals": [ugettext_noop("No answer provided.")]},
"krylov": {"literals": [ugettext_noop("No answer provided."), ]},
"ice_cream_toppings": {
"literals": [ugettext_noop("No answer provided."), ]},
"inlinemulti": {
"literals":
[ugettext_noop("No answer provided."), ]},
"hgtext": {
"literals": [ugettext_noop("No answer provided.")]},
"quarter": {
"literals": [ugettext_noop("No answer provided."), ]},
"pymult": {
"answer": {"answer": "c = ..."},
"literals": [
ugettext_noop("Autograder feedback"),
ugettext_noop("Your answer is not correct.")
]},
"addition": {
"answer": {"answer": "c = a + b"},
"literals": [
ugettext_noop("Your answer is correct."),
ugettext_noop("It looks like you submitted code that is "
"identical to the reference solution. "
"This is not allowed."),
ugettext_noop("Here is some feedback on your code"),
]},
"anyup": {"literals": [ugettext_noop("No answer provided.")]},
}
def feedback_test(self, course_force_lang):
self.course.force_lang = course_force_lang
self.course.save()
for page_id, v in six.iteritems(self.page_id_literal_dict):
if "answer" not in v:
continue
self.post_answer_by_page_id(page_id, answer_data=v["answer"])
self.end_flow()
for page_id, v in six.iteritems(self.page_id_literal_dict):
with self.subTest(page_id=page_id, course_force_lang=course_force_lang):
resp = self.c.get(self.get_page_url_by_page_id(page_id))
for literal in v["literals"]:
if not course_force_lang:
self.assertContains(resp, literal)
else:
with translation.override(course_force_lang):
translated_literal = translation.ugettext(literal)
self.assertContains(resp, translated_literal)
@skipUnless(is_travis_py3(), "This is tested only on Travis with PY3.5")
@override_settings(RELATE_ADMIN_EMAIL_LOCALE="en-us")
def test_course_no_force_lang_feedback(self):
self.feedback_test(course_force_lang="")
@skipUnless(is_travis_py3(), "This is tested only on Travis with PY3.5")
@override_settings(RELATE_ADMIN_EMAIL_LOCALE="en-us")
def test_course_force_lang_zh_hans_feedback(self):
self.feedback_test(course_force_lang="zh-hans")
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
class GetCustomPageTypesStopSupportDeadlineTest(unittest.TestCase):
# test course.utils.get_custom_page_types_stop_support_deadline
force_deadline = datetime(2019, 1, 1, 0, 0, 0, 0)
def test_custom_deadline_before_force_deadline(self):
deadline = datetime(2017, 1, 1, 0, 0, 0, 0)
with override_settings(
RELATE_CUSTOM_PAGE_TYPES_REMOVED_DEADLINE=deadline):
self.assertEqual(
get_custom_page_types_stop_support_deadline(),
localize_datetime(deadline))
def test_custom_deadline_after_force_deadline(self):
deadline = datetime(2019, 1, 1, 1, 0, 0, 0)
with override_settings(
RELATE_CUSTOM_PAGE_TYPES_REMOVED_DEADLINE=deadline):
self.assertEqual(
get_custom_page_types_stop_support_deadline(),
localize_datetime(self.force_deadline))
def test_custom_deadline_not_configured(self):
with override_settings():
del settings.RELATE_CUSTOM_PAGE_TYPES_REMOVED_DEADLINE
self.assertEqual(
get_custom_page_types_stop_support_deadline(),
localize_datetime(self.force_deadline))
class CustomRepoPageStopSupportTest(SingleCourseTestMixin,
FallBackStorageMessageTestMixin, TestCase):
def setUp(self):
super(CustomRepoPageStopSupportTest, self).setUp()
self.current_commit_sha = self.get_course_commit_sha(
self.instructor_participation)
force_deadline = datetime(2019, 1, 1, 0, 0, 0, 0)
custom_page_type = "repo:simple_questions.MyTextQuestion"
commit_sha_deprecated = b"593a1cdcecc6f4759fd5cadaacec0ba9dd0715a7"
deprecate_warning_message_pattern = (
"Custom page type '%(page_type)s' specified. "
"Custom page types will stop being supported in "
"RELATE at %(date_time)s.")
expired_error_message_pattern = (
"Custom page type '%(page_type)s' specified. "
"Custom page types were no longer supported in "
"RELATE since %(date_time)s.")
def test_custom_page_types_deprecate(self):
deadline = datetime(2039, 1, 1, 0, 0, 0, 0)
with override_settings(
RELATE_CUSTOM_PAGE_TYPES_REMOVED_DEADLINE=deadline):
resp = self.post_update_course_content(
commit_sha=self.commit_sha_deprecated)
self.assertEqual(resp.status_code, 200)
if datetime.now() <= self.force_deadline:
expected_message = (
self.deprecate_warning_message_pattern
% {"page_type": self.custom_page_type,
"date_time": format_datetime_local(self.force_deadline)}
)
self.assertEqual(
self.get_course_commit_sha(self.instructor_participation),
self.commit_sha_deprecated)
else:
expected_message = (
self.expired_error_message_pattern
% {"page_type": self.custom_page_type,
"date_time": format_datetime_local(self.force_deadline)}
)
self.assertEqual(
self.get_course_commit_sha(self.instructor_participation),
self.current_commit_sha)
self.assertResponseMessagesContains(resp, expected_message, loose=True)
def test_custom_page_types_not_supported(self):
deadline = datetime(2017, 1, 1, 0, 0, 0, 0)
with override_settings(
RELATE_CUSTOM_PAGE_TYPES_REMOVED_DEADLINE=deadline):
resp = self.post_update_course_content(
commit_sha=self.commit_sha_deprecated)
self.assertEqual(resp.status_code, 200)
expected_message = (
self.expired_error_message_pattern
% {"page_type": self.custom_page_type,
"date_time": format_datetime_local(deadline)}
)
self.assertResponseMessagesContains(resp, expected_message, loose=True)
self.assertEqual(
self.get_course_commit_sha(self.instructor_participation),
self.current_commit_sha)
def test_custom_page_types_deadline_configured_none(self):
with override_settings(
RELATE_CUSTOM_PAGE_TYPES_REMOVED_DEADLINE=None):
resp = self.post_update_course_content(
commit_sha=self.commit_sha_deprecated)
self.assertEqual(resp.status_code, 200)
if datetime.now() <= self.force_deadline:
expected_message = (
self.deprecate_warning_message_pattern
% {"page_type": self.custom_page_type,
"date_time": format_datetime_local(self.force_deadline)}
)
self.assertEqual(
self.get_course_commit_sha(self.instructor_participation),
self.commit_sha_deprecated)
else:
expected_message = (
self.expired_error_message_pattern
% {"page_type": self.custom_page_type,
"date_time": format_datetime_local(self.force_deadline)}
)
self.assertEqual(
self.get_course_commit_sha(self.instructor_participation),
self.current_commit_sha)
self.assertResponseMessagesContains(resp, expected_message, loose=True)