test_textbooks.py 14.7 KB
Newer Older
David Baumgold committed
1 2 3 4 5
import json
from unittest import TestCase
from .utils import CourseTestCase
from django.core.urlresolvers import reverse
from contentstore.utils import get_modulestore
David Baumgold committed
6
from xmodule.modulestore.inheritance import own_metadata
David Baumgold committed
7 8

from contentstore.views.course import (
David Baumgold committed
9
    validate_textbooks_json, validate_textbook_json, TextbookValidationError)
David Baumgold committed
10 11


David Baumgold committed
12
class TextbookIndexTestCase(CourseTestCase):
David Baumgold committed
13
    "Test cases for the textbook index page"
David Baumgold committed
14
    def setUp(self):
David Baumgold committed
15
        "Set the URL for tests"
David Baumgold committed
16
        super(TextbookIndexTestCase, self).setUp()
David Baumgold committed
17 18 19 20 21 22 23
        self.url = reverse('textbook_index', kwargs={
            'org': self.course.location.org,
            'course': self.course.location.course,
            'name': self.course.location.name,
        })

    def test_view_index(self):
David Baumgold committed
24
        "Basic check that the textbook index page responds correctly"
David Baumgold committed
25
        resp = self.client.get(self.url)
26
        self.assertEqual(resp.status_code, 200)
David Baumgold committed
27 28 29 30 31 32
        # we don't have resp.context right now,
        # due to bugs in our testing harness :(
        if resp.context:
            self.assertEqual(resp.context['course'], self.course)

    def test_view_index_xhr(self):
David Baumgold committed
33
        "Check that we get a JSON response when requested via AJAX"
David Baumgold committed
34 35 36 37 38
        resp = self.client.get(
            self.url,
            HTTP_ACCEPT="application/json",
            HTTP_X_REQUESTED_WITH='XMLHttpRequest'
        )
39
        self.assertEqual(resp.status_code, 200)
David Baumgold committed
40 41 42
        obj = json.loads(resp.content)
        self.assertEqual(self.course.pdf_textbooks, obj)

43
    def test_view_index_xhr_content(self):
David Baumgold committed
44
        "Check that the response maps to the content of the modulestore"
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
        content = [
            {
                "tab_title": "my textbook",
                "url": "/abc.pdf",
                "id": "992"
            }, {
                "tab_title": "pineapple",
                "id": "0pineapple",
                "chapters": [
                    {
                        "title": "The Fruit",
                        "url": "/a/b/fruit.pdf",
                    }, {
                        "title": "The Legend",
                        "url": "/b/c/legend.pdf",
                    }
                ]
            }
        ]
        self.course.pdf_textbooks = content
65 66 67
        # Save the data that we've just changed to the underlying
        # MongoKeyValueStore before we update the mongo datastore.
        self.course.save()
68 69 70 71 72 73 74 75
        store = get_modulestore(self.course.location)
        store.update_metadata(self.course.location, own_metadata(self.course))

        resp = self.client.get(
            self.url,
            HTTP_ACCEPT="application/json",
            HTTP_X_REQUESTED_WITH='XMLHttpRequest'
        )
76
        self.assertEqual(resp.status_code, 200)
77 78 79
        obj = json.loads(resp.content)
        self.assertEqual(content, obj)

David Baumgold committed
80
    def test_view_index_xhr_post(self):
David Baumgold committed
81
        "Check that you can save information to the server"
David Baumgold committed
82 83 84 85 86 87 88 89 90 91 92
        textbooks = [
            {"tab_title": "Hi, mom!"},
            {"tab_title": "Textbook 2"},
        ]
        resp = self.client.post(
            self.url,
            data=json.dumps(textbooks),
            content_type="application/json",
            HTTP_ACCEPT="application/json",
            HTTP_X_REQUESTED_WITH='XMLHttpRequest'
        )
93
        self.assertEqual(resp.status_code, 200)
David Baumgold committed
94 95 96 97

        # reload course
        store = get_modulestore(self.course.location)
        course = store.get_item(self.course.location)
David Baumgold committed
98 99 100 101 102 103
        # should be the same, except for added ID
        no_ids = []
        for textbook in course.pdf_textbooks:
            del textbook["id"]
            no_ids.append(textbook)
        self.assertEqual(no_ids, textbooks)
David Baumgold committed
104

105
    def test_view_index_xhr_post_invalid(self):
David Baumgold committed
106
        "Check that you can't save invalid JSON"
107 108 109 110 111 112 113
        resp = self.client.post(
            self.url,
            data="invalid",
            content_type="application/json",
            HTTP_ACCEPT="application/json",
            HTTP_X_REQUESTED_WITH='XMLHttpRequest'
        )
114
        self.assertEqual(resp.status_code, 400)
115 116 117
        obj = json.loads(resp.content)
        self.assertIn("error", obj)

David Baumgold committed
118

David Baumgold committed
119
class TextbookCreateTestCase(CourseTestCase):
David Baumgold committed
120 121
    "Test cases for creating a new PDF textbook"

David Baumgold committed
122
    def setUp(self):
David Baumgold committed
123
        "Set up a url and some textbook content for tests"
David Baumgold committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137
        super(TextbookCreateTestCase, self).setUp()
        self.url = reverse('create_textbook', kwargs={
            'org': self.course.location.org,
            'course': self.course.location.course,
            'name': self.course.location.name,
        })
        self.textbook = {
            "tab_title": "Economics",
            "chapters": {
                "title": "Chapter 1",
                "url": "/a/b/c/ch1.pdf",
            }
        }

David Baumgold committed
138
    def test_happy_path(self):
David Baumgold committed
139
        "Test that you can create a textbook"
David Baumgold committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
        resp = self.client.post(
            self.url,
            data=json.dumps(self.textbook),
            content_type="application/json",
            HTTP_ACCEPT="application/json",
            HTTP_X_REQUESTED_WITH="XMLHttpRequest",
        )
        self.assertEqual(resp.status_code, 201)
        self.assertIn("Location", resp)
        textbook = json.loads(resp.content)
        self.assertIn("id", textbook)
        del textbook["id"]
        self.assertEqual(self.textbook, textbook)

    def test_get(self):
David Baumgold committed
155
        "Test that GET is not allowed"
David Baumgold committed
156 157 158 159 160 161 162 163
        resp = self.client.get(
            self.url,
            HTTP_ACCEPT="application/json",
            HTTP_X_REQUESTED_WITH="XMLHttpRequest",
        )
        self.assertEqual(resp.status_code, 405)

    def test_valid_id(self):
David Baumgold committed
164
        "Textbook IDs must begin with a number; try a valid one"
David Baumgold committed
165 166 167 168 169 170 171 172 173 174 175 176 177
        self.textbook["id"] = "7x5"
        resp = self.client.post(
            self.url,
            data=json.dumps(self.textbook),
            content_type="application/json",
            HTTP_ACCEPT="application/json",
            HTTP_X_REQUESTED_WITH="XMLHttpRequest",
        )
        self.assertEqual(resp.status_code, 201)
        textbook = json.loads(resp.content)
        self.assertEqual(self.textbook, textbook)

    def test_invalid_id(self):
David Baumgold committed
178
        "Textbook IDs must begin with a number; try an invalid one"
David Baumgold committed
179 180 181 182 183 184 185 186
        self.textbook["id"] = "xxx"
        resp = self.client.post(
            self.url,
            data=json.dumps(self.textbook),
            content_type="application/json",
            HTTP_ACCEPT="application/json",
            HTTP_X_REQUESTED_WITH="XMLHttpRequest",
        )
187
        self.assertEqual(resp.status_code, 400)
David Baumgold committed
188 189 190 191
        self.assertNotIn("Location", resp)


class TextbookByIdTestCase(CourseTestCase):
David Baumgold committed
192 193
    "Test cases for the `textbook_by_id` view"

David Baumgold committed
194
    def setUp(self):
David Baumgold committed
195
        "Set some useful content and URLs for tests"
David Baumgold committed
196 197 198 199 200 201 202
        super(TextbookByIdTestCase, self).setUp()
        self.textbook1 = {
            "tab_title": "Economics",
            "id": 1,
            "chapters": {
                "title": "Chapter 1",
                "url": "/a/b/c/ch1.pdf",
David Baumgold committed
203
            }
David Baumgold committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
        }
        self.url1 = reverse('textbook_by_id', kwargs={
            'org': self.course.location.org,
            'course': self.course.location.course,
            'name': self.course.location.name,
            'tid': 1,
        })
        self.textbook2 = {
            "tab_title": "Algebra",
            "id": 2,
            "chapters": {
                "title": "Chapter 11",
                "url": "/a/b/ch11.pdf",
            }
        }
        self.url2 = reverse('textbook_by_id', kwargs={
            'org': self.course.location.org,
            'course': self.course.location.course,
            'name': self.course.location.name,
            'tid': 2,
        })
        self.course.pdf_textbooks = [self.textbook1, self.textbook2]
226 227 228
        # Save the data that we've just changed to the underlying
        # MongoKeyValueStore before we update the mongo datastore.
        self.course.save()
David Baumgold committed
229 230 231 232 233 234 235 236 237 238
        self.store = get_modulestore(self.course.location)
        self.store.update_metadata(self.course.location, own_metadata(self.course))
        self.url_nonexist = reverse('textbook_by_id', kwargs={
            'org': self.course.location.org,
            'course': self.course.location.course,
            'name': self.course.location.name,
            'tid': 20,
        })

    def test_get_1(self):
David Baumgold committed
239
        "Get the first textbook"
David Baumgold committed
240
        resp = self.client.get(self.url1)
241
        self.assertEqual(resp.status_code, 200)
David Baumgold committed
242 243 244 245
        compare = json.loads(resp.content)
        self.assertEqual(compare, self.textbook1)

    def test_get_2(self):
David Baumgold committed
246
        "Get the second textbook"
David Baumgold committed
247
        resp = self.client.get(self.url2)
248
        self.assertEqual(resp.status_code, 200)
David Baumgold committed
249 250 251 252
        compare = json.loads(resp.content)
        self.assertEqual(compare, self.textbook2)

    def test_get_nonexistant(self):
David Baumgold committed
253
        "Get a nonexistent textbook"
David Baumgold committed
254 255 256 257
        resp = self.client.get(self.url_nonexist)
        self.assertEqual(resp.status_code, 404)

    def test_delete(self):
David Baumgold committed
258
        "Delete a textbook by ID"
David Baumgold committed
259
        resp = self.client.delete(self.url1)
260
        self.assertEqual(resp.status_code, 204)
David Baumgold committed
261 262 263 264
        course = self.store.get_item(self.course.location)
        self.assertEqual(course.pdf_textbooks, [self.textbook2])

    def test_delete_nonexistant(self):
David Baumgold committed
265
        "Delete a textbook by ID, when the ID doesn't match an existing textbook"
David Baumgold committed
266 267 268 269 270 271
        resp = self.client.delete(self.url_nonexist)
        self.assertEqual(resp.status_code, 404)
        course = self.store.get_item(self.course.location)
        self.assertEqual(course.pdf_textbooks, [self.textbook1, self.textbook2])

    def test_create_new_by_id(self):
David Baumgold committed
272
        "Create a textbook by ID"
David Baumgold committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
        textbook = {
            "tab_title": "a new textbook",
            "url": "supercool.pdf",
            "id": "1supercool",
        }
        url = reverse("textbook_by_id", kwargs={
            'org': self.course.location.org,
            'course': self.course.location.course,
            'name': self.course.location.name,
            'tid': "1supercool",
        })
        resp = self.client.post(
            url,
            data=json.dumps(textbook),
            content_type="application/json",
        )
        self.assertEqual(resp.status_code, 201)
        resp2 = self.client.get(url)
291
        self.assertEqual(resp2.status_code, 200)
David Baumgold committed
292 293 294 295 296 297 298 299 300
        compare = json.loads(resp2.content)
        self.assertEqual(compare, textbook)
        course = self.store.get_item(self.course.location)
        self.assertEqual(
            course.pdf_textbooks,
            [self.textbook1, self.textbook2, textbook]
        )

    def test_replace_by_id(self):
David Baumgold committed
301
        "Create a textbook by ID, overwriting an existing textbook ID"
David Baumgold committed
302 303 304 305 306 307 308 309 310 311 312 313
        replacement = {
            "tab_title": "You've been replaced!",
            "url": "supercool.pdf",
            "id": "2",
        }
        resp = self.client.post(
            self.url2,
            data=json.dumps(replacement),
            content_type="application/json",
        )
        self.assertEqual(resp.status_code, 201)
        resp2 = self.client.get(self.url2)
314
        self.assertEqual(resp2.status_code, 200)
David Baumgold committed
315 316 317 318 319 320 321 322 323 324
        compare = json.loads(resp2.content)
        self.assertEqual(compare, replacement)
        course = self.store.get_item(self.course.location)
        self.assertEqual(
            course.pdf_textbooks,
            [self.textbook1, replacement]
        )


class TextbookValidationTestCase(TestCase):
David Baumgold committed
325 326
    "Tests for the code to validate the structure of a PDF textbook"

David Baumgold committed
327
    def setUp(self):
David Baumgold committed
328
        "Set some useful content for tests"
David Baumgold committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
        self.tb1 = {
            "tab_title": "Hi, mom!",
            "url": "/mom.pdf"
        }
        self.tb2 = {
            "tab_title": "Hi, dad!",
            "chapters": [
                {
                    "title": "Baseball",
                    "url": "baseball.pdf",
                }, {
                    "title": "Basketball",
                    "url": "crazypants.pdf",
                }
            ]
        }
        self.textbooks = [self.tb1, self.tb2]

    def test_happy_path_plural(self):
David Baumgold committed
348
        "Test that the plural validator works properly"
David Baumgold committed
349 350 351 352
        result = validate_textbooks_json(json.dumps(self.textbooks))
        self.assertEqual(self.textbooks, result)

    def test_happy_path_singular_1(self):
David Baumgold committed
353
        "Test that the singular validator works properly"
David Baumgold committed
354 355 356 357
        result = validate_textbook_json(json.dumps(self.tb1))
        self.assertEqual(self.tb1, result)

    def test_happy_path_singular_2(self):
David Baumgold committed
358
        "Test that the singular validator works properly, with different data"
David Baumgold committed
359 360
        result = validate_textbook_json(json.dumps(self.tb2))
        self.assertEqual(self.tb2, result)
David Baumgold committed
361

David Baumgold committed
362
    def test_valid_id(self):
David Baumgold committed
363
        "Test that a valid ID doesn't trip the validator, and comes out unchanged"
David Baumgold committed
364 365 366 367 368
        self.tb1["id"] = 1
        result = validate_textbook_json(json.dumps(self.tb1))
        self.assertEqual(self.tb1, result)

    def test_invalid_id(self):
David Baumgold committed
369
        "Test that an invalid ID trips the validator"
David Baumgold committed
370 371 372 373 374
        self.tb1["id"] = "abc"
        with self.assertRaises(TextbookValidationError):
            validate_textbook_json(json.dumps(self.tb1))

    def test_invalid_json_plural(self):
David Baumgold committed
375
        "Test that invalid JSON trips the plural validator"
David Baumgold committed
376 377 378 379
        with self.assertRaises(TextbookValidationError):
            validate_textbooks_json("[{'abc'}]")

    def test_invalid_json_singular(self):
David Baumgold committed
380
        "Test that invalid JSON trips the singluar validator"
David Baumgold committed
381 382 383 384
        with self.assertRaises(TextbookValidationError):
            validate_textbook_json("[{1]}")

    def test_wrong_json_plural(self):
David Baumgold committed
385
        "Test that a JSON object trips the plural validators (requires a list)"
David Baumgold committed
386 387 388 389
        with self.assertRaises(TextbookValidationError):
            validate_textbooks_json('{"tab_title": "Hi, mom!"}')

    def test_wrong_json_singular(self):
David Baumgold committed
390
        "Test that a JSON list trips the plural validators (requires an object)"
David Baumgold committed
391 392
        with self.assertRaises(TextbookValidationError):
            validate_textbook_json('[{"tab_title": "Hi, mom!"}, {"tab_title": "Hi, dad!"}]')
David Baumgold committed
393

David Baumgold committed
394
    def test_no_tab_title_plural(self):
David Baumgold committed
395
        "Test that `tab_title` is required for the plural validator"
David Baumgold committed
396
        with self.assertRaises(TextbookValidationError):
David Baumgold committed
397
            validate_textbooks_json('[{"url": "/textbook.pdf"}]')
David Baumgold committed
398

David Baumgold committed
399
    def test_no_tab_title_singular(self):
David Baumgold committed
400
        "Test that `tab_title` is required for the singular validator"
David Baumgold committed
401
        with self.assertRaises(TextbookValidationError):
David Baumgold committed
402
            validate_textbook_json('{"url": "/textbook.pdf"}')
David Baumgold committed
403

David Baumgold committed
404
    def test_duplicate_ids(self):
David Baumgold committed
405
        "Test that duplicate IDs in the plural validator trips the validator"
David Baumgold committed
406 407 408 409 410 411 412 413 414
        textbooks = [{
            "tab_title": "name one",
            "url": "one.pdf",
            "id": 1,
        }, {
            "tab_title": "name two",
            "url": "two.pdf",
            "id": 1,
        }]
David Baumgold committed
415
        with self.assertRaises(TextbookValidationError):
David Baumgold committed
416
            validate_textbooks_json(json.dumps(textbooks))