tests.py 10.8 KB
Newer Older
Ned Batchelder committed
1
"""
2
Test the lms/staticbook views.
Ned Batchelder committed
3 4
"""

5 6 7 8
import textwrap

import mock
import requests
9
from django.core.urlresolvers import NoReverseMatch, reverse
10

11
from student.tests.factories import CourseEnrollmentFactory, UserFactory
Ned Batchelder committed
12
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
13
from xmodule.modulestore.tests.factories import CourseFactory
Ned Batchelder committed
14

15 16
IMAGE_BOOK = ("An Image Textbook", "http://example.com/the_book/")

Ned Batchelder committed
17 18 19 20
PDF_BOOK = {
    "tab_title": "Textbook",
    "title": "A PDF Textbook",
    "chapters": [
21 22 23 24 25 26 27 28 29 30 31
        {"title": "Chapter 1 for PDF", "url": "https://somehost.com/the_book/chap1.pdf"},
        {"title": "Chapter 2 for PDF", "url": "https://somehost.com/the_book/chap2.pdf"},
    ],
}

PORTABLE_PDF_BOOK = {
    "tab_title": "Textbook",
    "title": "A PDF Textbook",
    "chapters": [
        {"title": "Chapter 1 for PDF", "url": "/static/chap1.pdf"},
        {"title": "Chapter 2 for PDF", "url": "/static/chap2.pdf"},
Ned Batchelder committed
32 33 34 35 36 37 38
    ],
}

HTML_BOOK = {
    "tab_title": "Textbook",
    "title": "An HTML Textbook",
    "chapters": [
39 40
        {"title": "Chapter 1 for HTML", "url": "https://somehost.com/the_book/chap1.html"},
        {"title": "Chapter 2 for HTML", "url": "https://somehost.com/the_book/chap2.html"},
Ned Batchelder committed
41 42 43
    ],
}

Will Daly committed
44

Ned Batchelder committed
45 46 47 48 49
class StaticBookTest(ModuleStoreTestCase):
    """
    Helpers for the static book tests.
    """

50 51 52 53
    def __init__(self, *args, **kwargs):
        super(StaticBookTest, self).__init__(*args, **kwargs)
        self.course = None

Ned Batchelder committed
54 55 56 57
    def make_course(self, **kwargs):
        """
        Make a course with an enrolled logged-in student.
        """
58
        self.course = CourseFactory.create(**kwargs)
Ned Batchelder committed
59
        user = UserFactory.create()
60
        CourseEnrollmentFactory.create(user=user, course_id=self.course.id)
Ned Batchelder committed
61
        self.client.login(username=user.username, password='test')
62 63 64 65 66 67 68 69

    def make_url(self, url_name, **kwargs):
        """
        Make a URL for a `url_name` using keyword args for url slots.

        Automatically provides the course id.

        """
70
        kwargs['course_id'] = self.course.id.to_deprecated_string()
71 72
        url = reverse(url_name, kwargs=kwargs)
        return url
Ned Batchelder committed
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
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)

112
    def test_bad_page_id(self):
113
        # A bad page id will cause a 404.
114 115 116 117
        self.make_course(textbooks=[IMAGE_BOOK])
        with self.assertRaises(NoReverseMatch):
            self.make_url('book', book_index=0, page='xyzzy')

118

Ned Batchelder committed
119 120 121 122 123 124 125
class StaticPdfBookTest(StaticBookTest):
    """
    Test the PDF static book view.
    """

    def test_book(self):
        # We can access a book.
126 127
        self.make_course(pdf_textbooks=[PDF_BOOK])
        url = self.make_url('pdf_book', book_index=0)
Ned Batchelder committed
128 129 130
        response = self.client.get(url)
        self.assertContains(response, "Chapter 1 for PDF")
        self.assertNotContains(response, "options.chapterNum =")
Dave St.Germain committed
131
        self.assertNotContains(response, "page=")
Ned Batchelder committed
132 133 134

    def test_book_chapter(self):
        # We can access a book at a particular chapter.
135 136
        self.make_course(pdf_textbooks=[PDF_BOOK])
        url = self.make_url('pdf_book', book_index=0, chapter=2)
Ned Batchelder committed
137 138
        response = self.client.get(url)
        self.assertContains(response, "Chapter 2 for PDF")
Dave St.Germain committed
139 140
        self.assertContains(response, "file={}".format(PDF_BOOK['chapters'][1]['url']))
        self.assertNotContains(response, "page=")
Ned Batchelder committed
141 142 143

    def test_book_page(self):
        # We can access a book at a particular page.
144 145
        self.make_course(pdf_textbooks=[PDF_BOOK])
        url = self.make_url('pdf_book', book_index=0, page=17)
Ned Batchelder committed
146 147 148
        response = self.client.get(url)
        self.assertContains(response, "Chapter 1 for PDF")
        self.assertNotContains(response, "options.chapterNum =")
Dave St.Germain committed
149
        self.assertContains(response, "page=17")
Ned Batchelder committed
150 151 152

    def test_book_chapter_page(self):
        # We can access a book at a particular chapter and page.
153 154
        self.make_course(pdf_textbooks=[PDF_BOOK])
        url = self.make_url('pdf_book', book_index=0, chapter=2, page=17)
Ned Batchelder committed
155 156
        response = self.client.get(url)
        self.assertContains(response, "Chapter 2 for PDF")
Dave St.Germain committed
157 158
        self.assertContains(response, "file={}".format(PDF_BOOK['chapters'][1]['url']))
        self.assertContains(response, "page=17")
Ned Batchelder committed
159 160

    def test_bad_book_id(self):
161 162 163 164 165 166
        # 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):
Ned Batchelder committed
167
        # If we have one book, asking for the second book will fail with a 404.
168 169
        self.make_course(pdf_textbooks=[PDF_BOOK])
        url = self.make_url('pdf_book', book_index=1, chapter=1)
Ned Batchelder committed
170 171 172 173 174
        response = self.client.get(url)
        self.assertEqual(response.status_code, 404)

    def test_no_book(self):
        # If we have no books, asking for the first book will fail with a 404.
175 176
        self.make_course()
        url = self.make_url('pdf_book', book_index=0, chapter=1)
Ned Batchelder committed
177 178 179
        response = self.client.get(url)
        self.assertEqual(response.status_code, 404)

180 181
    def test_chapter_xss(self):
        # The chapter in the URL used to go right on the page.
182
        self.make_course(pdf_textbooks=[PDF_BOOK])
183 184
        # It's no longer possible to use a non-integer chapter.
        with self.assertRaises(NoReverseMatch):
185
            self.make_url('pdf_book', book_index=0, chapter='xyzzy')
186 187 188

    def test_page_xss(self):
        # The page in the URL used to go right on the page.
189
        self.make_course(pdf_textbooks=[PDF_BOOK])
190 191
        # It's no longer possible to use a non-integer page.
        with self.assertRaises(NoReverseMatch):
192
            self.make_url('pdf_book', book_index=0, page='xyzzy')
193

194 195 196 197 198 199 200
    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')

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
    def test_static_url_map_contentstore(self):
        """
        This ensure static  URL mapping is happening properly for
        a course that uses the contentstore
        """
        self.make_course(pdf_textbooks=[PORTABLE_PDF_BOOK])
        url = self.make_url('pdf_book', book_index=0, chapter=1)
        response = self.client.get(url)
        self.assertNotContains(response, 'file={}'.format(PORTABLE_PDF_BOOK['chapters'][0]['url']))
        self.assertContains(response, 'file=/c4x/{0.org}/{0.course}/asset/{1}'.format(
            self.course.location,
            PORTABLE_PDF_BOOK['chapters'][0]['url'].replace('/static/', '')))

    def test_static_url_map_static_asset_path(self):
        """
        Like above, but used when the course has set a static_asset_path
        """
        self.make_course(pdf_textbooks=[PORTABLE_PDF_BOOK], static_asset_path='awesomesauce')
        url = self.make_url('pdf_book', book_index=0, chapter=1)
        response = self.client.get(url)
        self.assertNotContains(response, 'file={}'.format(PORTABLE_PDF_BOOK['chapters'][0]['url']))
        self.assertNotContains(response, 'file=/c4x/{0.org}/{0.course}/asset/{1}'.format(
            self.course.location,
            PORTABLE_PDF_BOOK['chapters'][0]['url'].replace('/static/', '')))
        self.assertContains(response, 'file=/static/awesomesauce/{}'.format(
            PORTABLE_PDF_BOOK['chapters'][0]['url'].replace('/static/', '')))

228 229 230 231 232 233 234 235 236 237 238
    def test_invalid_chapter_id(self):
        """
        Test that 1st chapter is displayed to the user when an invalid chapter id is provided
        """
        self.make_course(pdf_textbooks=[PDF_BOOK])
        invalid_chapter = len(PDF_BOOK['chapters']) + 1
        url = self.make_url('pdf_book', book_index=0, chapter=invalid_chapter)
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Chapter 1 for PDF")

Ned Batchelder committed
239 240 241 242 243 244 245 246

class StaticHtmlBookTest(StaticBookTest):
    """
    Test the HTML static book view.
    """

    def test_book(self):
        # We can access a book.
247 248
        self.make_course(html_textbooks=[HTML_BOOK])
        url = self.make_url('html_book', book_index=0)
Ned Batchelder committed
249 250 251 252 253 254
        response = self.client.get(url)
        self.assertContains(response, "Chapter 1 for HTML")
        self.assertNotContains(response, "options.chapterNum =")

    def test_book_chapter(self):
        # We can access a book at a particular chapter.
255 256
        self.make_course(html_textbooks=[HTML_BOOK])
        url = self.make_url('html_book', book_index=0, chapter=2)
Ned Batchelder committed
257 258 259 260 261 262
        response = self.client.get(url)
        self.assertContains(response, "Chapter 2 for HTML")
        self.assertContains(response, "options.chapterNum = 2;")

    def test_bad_book_id(self):
        # If we have one book, asking for the second book will fail with a 404.
263 264
        self.make_course(html_textbooks=[HTML_BOOK])
        url = self.make_url('html_book', book_index=1, chapter=1)
Ned Batchelder committed
265 266 267 268 269
        response = self.client.get(url)
        self.assertEqual(response.status_code, 404)

    def test_no_book(self):
        # If we have no books, asking for the first book will fail with a 404.
270 271
        self.make_course()
        url = self.make_url('html_book', book_index=0, chapter=1)
Ned Batchelder committed
272 273
        response = self.client.get(url)
        self.assertEqual(response.status_code, 404)
274 275 276

    def test_chapter_xss(self):
        # The chapter in the URL used to go right on the page.
277
        self.make_course(pdf_textbooks=[HTML_BOOK])
278 279
        # It's no longer possible to use a non-integer chapter.
        with self.assertRaises(NoReverseMatch):
280
            self.make_url('html_book', book_index=0, chapter='xyzzy')