Add tests for image books, and more tests for the other kinds too.
This commit is contained in:
@@ -61,6 +61,7 @@ class XModuleCourseFactory(Factory):
|
||||
|
||||
# Update the data in the mongo datastore
|
||||
store.update_metadata(new_course.location.url(), own_metadata(new_course))
|
||||
store.update_item(new_course.location.url(), new_course._model_data._kvs._data)
|
||||
|
||||
# update_item updates the the course as it exists in the modulestore, but doesn't
|
||||
# update the instance we are working with, so have to refetch the course after updating it.
|
||||
|
||||
@@ -2,6 +2,11 @@
|
||||
Test the lms/staticbook views.
|
||||
"""
|
||||
|
||||
import textwrap
|
||||
|
||||
import mock
|
||||
import requests
|
||||
|
||||
from django.test.utils import override_settings
|
||||
from django.core.urlresolvers import reverse, NoReverseMatch
|
||||
|
||||
@@ -11,6 +16,8 @@ from xmodule.modulestore.tests.factories import CourseFactory
|
||||
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
|
||||
|
||||
|
||||
IMAGE_BOOK = ("An Image Textbook", "http://example.com/the_book/")
|
||||
|
||||
PDF_BOOK = {
|
||||
"tab_title": "Textbook",
|
||||
"title": "A PDF Textbook",
|
||||
@@ -60,6 +67,44 @@ class StaticBookTest(ModuleStoreTestCase):
|
||||
return url
|
||||
|
||||
|
||||
class StaticImageBookTest(StaticBookTest):
|
||||
"""
|
||||
Test the image-based static book view.
|
||||
"""
|
||||
|
||||
def test_book(self):
|
||||
# We can access a book.
|
||||
with mock.patch.object(requests, 'get') as mock_get:
|
||||
mock_get.return_value.text = textwrap.dedent('''\
|
||||
<?xml version="1.0"?>
|
||||
<table_of_contents>
|
||||
<entry page="9" page_label="ix" name="Contents!?"/>
|
||||
<entry page="1" page_label="i" name="Preamble">
|
||||
<entry page="4" page_label="iv" name="About the Elephants"/>
|
||||
</entry>
|
||||
</table_of_contents>
|
||||
''')
|
||||
|
||||
self.make_course(textbooks=[IMAGE_BOOK])
|
||||
url = self.make_url('book', book_index=0)
|
||||
response = self.client.get(url)
|
||||
|
||||
self.assertContains(response, "Contents!?")
|
||||
self.assertContains(response, "About the Elephants")
|
||||
|
||||
def test_bad_book_id(self):
|
||||
# A bad book id will be a 404.
|
||||
self.make_course(textbooks=[IMAGE_BOOK])
|
||||
with self.assertRaises(NoReverseMatch):
|
||||
self.make_url('book', book_index='fooey')
|
||||
|
||||
def test_out_of_range_book_id(self):
|
||||
self.make_course()
|
||||
url = self.make_url('book', book_index=0)
|
||||
response = self.client.get(url)
|
||||
self.assertEqual(response.status_code, 404)
|
||||
|
||||
|
||||
class StaticPdfBookTest(StaticBookTest):
|
||||
"""
|
||||
Test the PDF static book view.
|
||||
@@ -102,6 +147,12 @@ class StaticPdfBookTest(StaticBookTest):
|
||||
self.assertContains(response, "options.pageNum = 17;")
|
||||
|
||||
def test_bad_book_id(self):
|
||||
# If the book id isn't an int, we'll get a 404.
|
||||
self.make_course(pdf_textbooks=[PDF_BOOK])
|
||||
with self.assertRaises(NoReverseMatch):
|
||||
self.make_url('pdf_book', book_index='fooey', chapter=1)
|
||||
|
||||
def test_out_of_range_book_id(self):
|
||||
# If we have one book, asking for the second book will fail with a 404.
|
||||
self.make_course(pdf_textbooks=[PDF_BOOK])
|
||||
url = self.make_url('pdf_book', book_index=1, chapter=1)
|
||||
@@ -117,18 +168,25 @@ class StaticPdfBookTest(StaticBookTest):
|
||||
|
||||
def test_chapter_xss(self):
|
||||
# The chapter in the URL used to go right on the page.
|
||||
course = self.make_course(pdf_textbooks=[PDF_BOOK])
|
||||
self.make_course(pdf_textbooks=[PDF_BOOK])
|
||||
# It's no longer possible to use a non-integer chapter.
|
||||
with self.assertRaises(NoReverseMatch):
|
||||
self.make_url('pdf_book', book_index=0, chapter='xyzzy')
|
||||
|
||||
def test_page_xss(self):
|
||||
# The page in the URL used to go right on the page.
|
||||
course = self.make_course(pdf_textbooks=[PDF_BOOK])
|
||||
self.make_course(pdf_textbooks=[PDF_BOOK])
|
||||
# It's no longer possible to use a non-integer page.
|
||||
with self.assertRaises(NoReverseMatch):
|
||||
self.make_url('pdf_book', book_index=0, page='xyzzy')
|
||||
|
||||
def test_chapter_page_xss(self):
|
||||
# The page in the URL used to go right on the page.
|
||||
self.make_course(pdf_textbooks=[PDF_BOOK])
|
||||
# It's no longer possible to use a non-integer page and a non-integer chapter.
|
||||
with self.assertRaises(NoReverseMatch):
|
||||
self.make_url('pdf_book', book_index=0, chapter='fooey', page='xyzzy')
|
||||
|
||||
|
||||
class StaticHtmlBookTest(StaticBookTest):
|
||||
"""
|
||||
@@ -167,7 +225,7 @@ class StaticHtmlBookTest(StaticBookTest):
|
||||
|
||||
def test_chapter_xss(self):
|
||||
# The chapter in the URL used to go right on the page.
|
||||
course = self.make_course(pdf_textbooks=[HTML_BOOK])
|
||||
self.make_course(pdf_textbooks=[HTML_BOOK])
|
||||
# It's no longer possible to use a non-integer chapter.
|
||||
with self.assertRaises(NoReverseMatch):
|
||||
self.make_url('html_book', book_index=0, chapter='xyzzy')
|
||||
|
||||
@@ -226,7 +226,7 @@ if settings.COURSEWARE_ENABLED:
|
||||
url(r'^courses/(?P<course_id>[^/]+/[^/]+/[^/]+)/book/(?P<book_index>\d+)/$',
|
||||
'staticbook.views.index', name="book"),
|
||||
url(r'^courses/(?P<course_id>[^/]+/[^/]+/[^/]+)/book/(?P<book_index>\d+)/(?P<page>\d+)$',
|
||||
'staticbook.views.index'),
|
||||
'staticbook.views.index', name="book"),
|
||||
|
||||
url(r'^courses/(?P<course_id>[^/]+/[^/]+/[^/]+)/pdfbook/(?P<book_index>\d+)/$',
|
||||
'staticbook.views.pdf_index', name="pdf_book"),
|
||||
|
||||
Reference in New Issue
Block a user