|
| 1 | +# Copyright The IETF Trust 2011, All Rights Reserved |
| 2 | + |
| 3 | +import os, shutil, datetime |
| 4 | +from StringIO import StringIO |
| 5 | +from pyquery import PyQuery |
| 6 | + |
| 7 | +from django.conf import settings |
| 8 | +from django.core.urlresolvers import reverse as urlreverse |
| 9 | + |
| 10 | +from ietf.doc.models import Document, State, DocAlias |
| 11 | +from ietf.group.models import Group |
| 12 | +from ietf.utils.test_utils import TestCase, login_testing_unauthorized |
| 13 | +from ietf.utils.test_data import make_test_data |
| 14 | + |
| 15 | +class GroupMaterialTests(TestCase): |
| 16 | + def setUp(self): |
| 17 | + self.materials_dir = os.path.abspath("tmp-document-dir") |
| 18 | + os.mkdir(self.materials_dir) |
| 19 | + os.mkdir(os.path.join(self.materials_dir, "slides")) |
| 20 | + settings.DOCUMENT_PATH_PATTERN = self.materials_dir + "/{doc.type_id}/" |
| 21 | + |
| 22 | + def tearDown(self): |
| 23 | + shutil.rmtree(self.materials_dir) |
| 24 | + |
| 25 | + def create_slides(self): |
| 26 | + group = Group.objects.create(type_id="team", acronym="testteam", name="Test Team", state_id="active") |
| 27 | + |
| 28 | + doc = Document.objects.create(name="slides-testteam-test-file", rev="00", type_id="slides", group=group) |
| 29 | + doc.set_state(State.objects.get(type="slides", slug="active")) |
| 30 | + DocAlias.objects.create(name=doc.name, document=doc) |
| 31 | + |
| 32 | + return doc |
| 33 | + |
| 34 | + def test_choose_material_type(self): |
| 35 | + group = Group.objects.create(type_id="team", acronym="testteam", name="Test Team", state_id="active") |
| 36 | + |
| 37 | + url = urlreverse('ietf.doc.views_material.choose_material_type', kwargs=dict(acronym=group.acronym)) |
| 38 | + login_testing_unauthorized(self, "secretary", url) |
| 39 | + |
| 40 | + # normal get |
| 41 | + r = self.client.get(url) |
| 42 | + self.assertEqual(r.status_code, 200) |
| 43 | + self.assertTrue("Slides" in r.content) |
| 44 | + |
| 45 | + def test_upload_slides(self): |
| 46 | + group = Group.objects.create(type_id="team", acronym="testteam", name="Test Team", state_id="active") |
| 47 | + |
| 48 | + url = urlreverse('group_new_material', kwargs=dict(acronym=group.acronym, doc_type="slides")) |
| 49 | + login_testing_unauthorized(self, "secretary", url) |
| 50 | + |
| 51 | + # normal get |
| 52 | + r = self.client.get(url) |
| 53 | + self.assertEqual(r.status_code, 200) |
| 54 | + |
| 55 | + content = "%PDF-1.5\n..." |
| 56 | + test_file = StringIO(content) |
| 57 | + test_file.name = "unnamed.pdf" |
| 58 | + |
| 59 | + # faulty post |
| 60 | + r = self.client.post(url, dict(title="", state="", material=test_file)) |
| 61 | + |
| 62 | + self.assertEqual(r.status_code, 200) |
| 63 | + q = PyQuery(r.content) |
| 64 | + self.assertTrue(len(q('form ul.errorlist')) > 0) |
| 65 | + |
| 66 | + test_file.seek(0) |
| 67 | + |
| 68 | + # post |
| 69 | + r = self.client.post(url, dict(title="Test File", |
| 70 | + state=State.objects.get(type="slides", slug="active").pk, |
| 71 | + material=test_file)) |
| 72 | + self.assertEqual(r.status_code, 302) |
| 73 | + |
| 74 | + doc = Document.objects.get(name="slides-%s-test-file" % group.acronym) |
| 75 | + self.assertEqual(doc.rev, "00") |
| 76 | + self.assertEqual(doc.title, "Test File") |
| 77 | + self.assertEqual(doc.get_state_slug(), "active") |
| 78 | + |
| 79 | + with open(os.path.join(self.materials_dir, "slides", doc.name + "-" + doc.rev + ".pdf")) as f: |
| 80 | + self.assertEqual(f.read(), content) |
| 81 | + |
| 82 | + # check that posting same title is prevented |
| 83 | + test_file.seek(0) |
| 84 | + |
| 85 | + r = self.client.post(url, dict(title="Test File", |
| 86 | + state=State.objects.get(type="slides", slug="active").pk, |
| 87 | + material=test_file)) |
| 88 | + self.assertEqual(r.status_code, 200) |
| 89 | + self.assertTrue(len(q('form ul.errorlist')) > 0) |
| 90 | + |
| 91 | + def test_change_state(self): |
| 92 | + doc = self.create_slides() |
| 93 | + |
| 94 | + url = urlreverse('material_edit', kwargs=dict(name=doc.name, action="state")) |
| 95 | + login_testing_unauthorized(self, "secretary", url) |
| 96 | + |
| 97 | + # post |
| 98 | + r = self.client.post(url, dict(state=State.objects.get(type="slides", slug="deleted").pk)) |
| 99 | + self.assertEqual(r.status_code, 302) |
| 100 | + doc = Document.objects.get(name=doc.name) |
| 101 | + self.assertEqual(doc.get_state_slug(), "deleted") |
| 102 | + |
| 103 | + def test_edit_title(self): |
| 104 | + doc = self.create_slides() |
| 105 | + |
| 106 | + url = urlreverse('material_edit', kwargs=dict(name=doc.name, action="title")) |
| 107 | + login_testing_unauthorized(self, "secretary", url) |
| 108 | + |
| 109 | + # post |
| 110 | + r = self.client.post(url, dict(title="New title")) |
| 111 | + self.assertEqual(r.status_code, 302) |
| 112 | + doc = Document.objects.get(name=doc.name) |
| 113 | + self.assertEqual(doc.title, "New title") |
| 114 | + |
| 115 | + def test_revise(self): |
| 116 | + doc = self.create_slides() |
| 117 | + |
| 118 | + url = urlreverse('material_edit', kwargs=dict(name=doc.name, action="revise")) |
| 119 | + login_testing_unauthorized(self, "secretary", url) |
| 120 | + |
| 121 | + content = "some text" |
| 122 | + test_file = StringIO(content) |
| 123 | + test_file.name = "unnamed.txt" |
| 124 | + |
| 125 | + # post |
| 126 | + r = self.client.post(url, dict(title="New title", |
| 127 | + state=State.objects.get(type="slides", slug="active").pk, |
| 128 | + material=test_file)) |
| 129 | + self.assertEqual(r.status_code, 302) |
| 130 | + doc = Document.objects.get(name=doc.name) |
| 131 | + self.assertEqual(doc.rev, "01") |
| 132 | + self.assertEqual(doc.title, "New title") |
| 133 | + self.assertEqual(doc.get_state_slug(), "active") |
| 134 | + |
| 135 | + with open(os.path.join(self.materials_dir, "slides", doc.name + "-" + doc.rev + ".txt")) as f: |
| 136 | + self.assertEqual(f.read(), content) |
| 137 | + |
0 commit comments