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. """ import json from django.test import TestCase from course.models import FlowSession from course import content from tests.base_test_mixins import ( SingleCourseTestMixin, improperly_configured_cache_patch, SingleCoursePageTestMixin) from tests.test_sandbox import SingleCoursePageSandboxTestBaseMixin from tests.utils import mock from tests import factories # noqa class SingleCoursePageCacheTest(SingleCoursePageTestMixin, TestCase): @classmethod def setUpTestData(cls): # noqa super(SingleCoursePageCacheTest, cls).setUpTestData() cls.start_flow(cls.flow_id) @improperly_configured_cache_patch() def test_disable_cache(self, mock_cache): from django.core.exceptions import ImproperlyConfigured with self.assertRaises(ImproperlyConfigured): from django.core.cache import cache # noqa def test_view_flow_with_cache(self): resp = self.c.get(self.get_page_url_by_ordinal(0)) self.assertEqual(resp.status_code, 200) self.c.get(self.get_page_url_by_ordinal(1)) with mock.patch("course.content.get_repo_blob") as mock_get_repo_blob: resp = self.c.get(self.get_page_url_by_ordinal(0)) self.assertEqual(resp.status_code, 200) self.assertEqual(mock_get_repo_blob.call_count, 0) def test_view_flow_with_cache_improperly_configured(self): resp = self.c.get(self.get_page_url_by_ordinal(0)) self.assertEqual(resp.status_code, 200) self.c.get(self.get_page_url_by_ordinal(1)) with improperly_configured_cache_patch(): resp = self.c.get(self.get_page_url_by_ordinal(0)) self.assertEqual(resp.status_code, 200) # {{{ Test Nbconvert for rendering ipynb notebook QUESTION_MARKUP_FULL = """ type: Page id: ipynb content: | # Ipython notebook Examples {{ render_notebook_cells("test.ipynb") }} """ QUESTION_MARKUP_SLICED1 = """ type: Page id: ipynb content: | # Ipython notebook Examples {{ render_notebook_cells("test.ipynb", indices=[0, 1, 2]) }} """ QUESTION_MARKUP_SLICED2 = """ type: Page id: ipynb content: | # Ipython notebook Examples {{ render_notebook_cells("test.ipynb", indices=[1, 2]) }} """ QUESTION_MARKUP_CLEAR_MARKDOWN = """ type: Page id: ipynb content: | # Ipython notebook Examples {{ render_notebook_cells("test.ipynb", clear_markdown=True) }} """ QUESTION_MARKUP_CLEAR_OUTPUT = """ type: Page id: ipynb content: | # Ipython notebook Examples {{ render_notebook_cells("test.ipynb", clear_output=True) }} """ QUESTION_MARKUP_CLEAR_ALL = """ type: Page id: ipynb content: | # Ipython notebook Examples {{ render_notebook_cells("test.ipynb", clear_markdown=True, clear_output=True) }} """ MARKDOWN_PLACEHOLDER = "wzxhzdk" TEST_IPYNB_BYTES = json.dumps({ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# First Title of Test NoteBook" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "scrolled": True }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is function1\n" ] } ], "source": [ "def function1():\n", " print(\"This is function1\")\n", "\n", "function1()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Second Title of Test NoteBook" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": True }, "outputs": [], "source": [ "def function2():\n", " print(\"This is function2\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is function2\n" ] } ], "source": [ "function2()" ] }, { "cell_type": "code", "execution_count": None, "metadata": { "collapsed": True }, "outputs": [], "source": [ "print(`5**18`)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.0" } }, "nbformat": 4, "nbformat_minor": 2 }).encode() FIRST_TITLE_TEXT = "First Title of Test NoteBook" SECOND_TITLE_TEXT = "Second Title of Test NoteBook" TEXT_CELL_HTML_CLASS = "text_cell_render" CODE_CELL_HTML_CLASS = "code_cell" CODE_CELL_IN_STR_PATTERN = '
\newcommand{\superscript}[1] {\ensuremath{^{\textrm{#1}}}}' '\n' 'example1
\n' 'value=${#1}
\n' '') resp = self.get_page_sandbox_preview_response(markdown) self.assertSandboxHasValidPage(resp) self.assertResponseContextContains(resp, "body", expected_literal) markdown = TEST_SANDBOX_MARK_DOWN_PATTERN % "{%endraw%}" resp = self.get_page_sandbox_preview_response(markdown) self.assertSandboxHasValidPage(resp) self.assertResponseContextContains(resp, "body", expected_literal) markdown = TEST_SANDBOX_MARK_DOWN_PATTERN % "{% endraw %}" resp = self.get_page_sandbox_preview_response(markdown) self.assertSandboxHasValidPage(resp) self.assertResponseContextContains(resp, "body", expected_literal) def test_embedded_raw_block2(self): markdown = TEST_SANDBOX_MARK_DOWN_PATTERN % "{%- endraw %}" expected_literal = ( r'\newcommand{\superscript}[1] {\ensuremath{^{\textrm{#1}}}}' '\n' 'example1
\n' 'value=${#1}\n' 'example2
') resp = self.get_page_sandbox_preview_response(markdown) self.assertSandboxHasValidPage(resp) self.assertResponseContextContains(resp, "body", expected_literal) markdown = TEST_SANDBOX_MARK_DOWN_PATTERN % "{%-endraw%}" resp = self.get_page_sandbox_preview_response(markdown) self.assertSandboxHasValidPage(resp) self.assertResponseContextContains(resp, "body", expected_literal) def test_embedded_raw_block3(self): markdown = TEST_SANDBOX_MARK_DOWN_PATTERN % "{%- endraw -%}" expected_literal = ( r'\newcommand{\superscript}[1] {\ensuremath{^{\textrm{#1}}}}' '\n' 'example1
\n' 'value=${#1}example2
') resp = self.get_page_sandbox_preview_response(markdown) self.assertSandboxHasValidPage(resp) self.assertResponseContextContains(resp, "body", expected_literal) markdown = TEST_SANDBOX_MARK_DOWN_PATTERN % "{%-endraw-%}" resp = self.get_page_sandbox_preview_response(markdown) self.assertSandboxHasValidPage(resp) self.assertResponseContextContains(resp, "body", expected_literal) def test_embedded_raw_block4(self): markdown = TEST_SANDBOX_MARK_DOWN_PATTERN % "{% endraw -%}" expected_literal = ( r'\newcommand{\superscript}[1] {\ensuremath{^{\textrm{#1}}}}' '\n' 'example1
\n' 'value=${#1}\n' 'example2
') resp = self.get_page_sandbox_preview_response(markdown) self.assertSandboxHasValidPage(resp) self.assertResponseContextContains(resp, "body", expected_literal) # }}} class GetCourseCommitShaTest(SingleCourseTestMixin, TestCase): # test content.get_course_commit_sha def setUp(self): super(GetCourseCommitShaTest, self).setUp() self.valid_sha = self.course.active_git_commit_sha self.new_sha = "some_sha" self.course.active_git_commit_sha = self.new_sha self.course.save() def test_no_participation(self): self.assertEqual( content.get_course_commit_sha( course=self.course, participation=None).decode(), self.new_sha) def test_invalid_preview_sha(self): invalid_sha = "invalid_sha" self.ta_participation.preview_git_commit_sha = invalid_sha self.ta_participation.save() self.assertEqual( content.get_course_commit_sha( course=self.course, participation=self.ta_participation).decode(), self.new_sha) def test_invalid_preview_sha_error_raised(self): invalid_sha = "invalid_sha" self.ta_participation.preview_git_commit_sha = invalid_sha self.ta_participation.save() with self.assertRaises(content.CourseCommitSHADoesNotExist) as cm: content.get_course_commit_sha( course=self.course, participation=self.ta_participation, raise_on_nonexistent_preview_commit=True) expected_error_msg = ("Preview revision '%s' does not exist--" "showing active course content instead." % invalid_sha) self.assertIn(expected_error_msg, str(cm.exception)) def test_passed_repo_not_none(self): with self.get_course_page_context(self.ta_participation.user) as pctx: self.assertEqual( content.get_course_commit_sha( self.course, self.ta_participation, repo=pctx.repo).decode(), self.course.active_git_commit_sha) def test_preview_passed_repo_not_none(self): self.ta_participation.preview_git_commit_sha = self.valid_sha self.ta_participation.save() with self.get_course_page_context(self.ta_participation.user) as pctx: self.assertEqual( content.get_course_commit_sha( self.course, self.ta_participation, repo=pctx.repo).decode(), self.valid_sha) def test_repo_is_subdir_repo(self): self.course.course_root_path = "/my_subdir" self.course.save() self.ta_participation.preview_git_commit_sha = self.valid_sha self.ta_participation.save() with self.get_course_page_context(self.ta_participation.user) as pctx: self.assertEqual( content.get_course_commit_sha( self.course, self.ta_participation, repo=pctx.repo).decode(), self.valid_sha) # vim: fdm=marker