test_assets.py 2.94 KB
Newer Older
David Baumgold committed
1 2 3 4
"""
Unit tests for the asset upload endpoint.
"""

David Baumgold committed
5 6 7 8
import json
from datetime import datetime
from io import BytesIO
from pytz import UTC
David Baumgold committed
9
from unittest import TestCase, skip
David Baumgold committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
from .utils import CourseTestCase
from django.core.urlresolvers import reverse
from contentstore.views import assets


class AssetsTestCase(CourseTestCase):
    def setUp(self):
        super(AssetsTestCase, self).setUp()
        self.url = reverse("asset_index", kwargs={
            'org': self.course.location.org,
            'course': self.course.location.course,
            'name': self.course.location.name,
        })

    def test_basic(self):
        resp = self.client.get(self.url)
        self.assertEquals(resp.status_code, 200)

    def test_json(self):
        resp = self.client.get(
            self.url,
            HTTP_ACCEPT="application/json",
            HTTP_X_REQUESTED_WITH="XMLHttpRequest",
        )
        self.assertEquals(resp.status_code, 200)
        content = json.loads(resp.content)
        self.assertIsInstance(content, list)


class UploadTestCase(CourseTestCase):
David Baumgold committed
40 41 42
    """
    Unit tests for uploading a file
    """
David Baumgold committed
43 44 45 46 47 48 49 50
    def setUp(self):
        super(UploadTestCase, self).setUp()
        self.url = reverse("upload_asset", kwargs={
            'org': self.course.location.org,
            'course': self.course.location.course,
            'coursename': self.course.location.name,
        })

David Baumgold committed
51
    @skip("CorruptGridFile error on continuous integration server")
David Baumgold committed
52
    def test_happy_path(self):
David Baumgold committed
53 54 55
        f = BytesIO("sample content")
        f.name = "sample.txt"
        resp = self.client.post(self.url, {"name": "my-name", "file": f})
David Baumgold committed
56 57 58 59 60 61 62 63 64 65 66 67
        self.assert2XX(resp.status_code)

    def test_no_file(self):
        resp = self.client.post(self.url, {"name": "file.txt"})
        self.assert4XX(resp.status_code)

    def test_get(self):
        resp = self.client.get(self.url)
        self.assertEquals(resp.status_code, 405)


class AssetsToJsonTestCase(TestCase):
David Baumgold committed
68 69 70 71
    """
    Unit tests for transforming the results of a database call into something
    we can send out to the client via JSON.
    """
David Baumgold committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    def test_basic(self):
        upload_date = datetime(2013, 6, 1, 10, 30, tzinfo=UTC)
        asset = {
            "displayname": "foo",
            "chunkSize": 512,
            "filename": "foo.png",
            "length": 100,
            "uploadDate": upload_date,
            "_id": {
                "course": "course",
                "org": "org",
                "revision": 12,
                "category": "category",
                "name": "name",
                "tag": "tag",
            }
        }
        output = assets.assets_to_json_dict([asset])
        self.assertEquals(len(output), 1)
        compare = output[0]
        self.assertEquals(compare["name"], "foo")
        self.assertEquals(compare["path"], "foo.png")
        self.assertEquals(compare["uploaded"], upload_date.isoformat())
        self.assertEquals(compare["id"], "/tag/org/course/12/category/name")