1 from django.test import TestCase
2 from django.test.client import Client
3 from django.core.urlresolvers import reverse
5 from django.utils import simplejson as json
7 from django.contrib.auth.models import User
10 from os.path import join, dirname
11 from StringIO import StringIO
15 REPO_TEMPLATES = join(dirname(__file__), 'data')
18 from functools import wraps
23 def decorated(self, *args, **kwargs):
26 temp = tempfile.mkdtemp("-test", func.__name__)
27 shutil.copytree(join(REPO_TEMPLATES, name), join(temp, 'repo'), False)
28 settings.REPOSITORY_PATH = join(temp, 'repo')
29 func(self, *args, **kwargs)
32 if not clean and self.response:
33 print "RESULT", func.__name__, ">>>"
37 # shutil.rmtree(temp, True)
38 settings.REPOSITORY_PATH = ''
44 class SimpleTest(TestCase):
48 u = User.objects.create_user('admin', 'test@localhost', 'admin')
52 def test_documents_get_anonymous(self):
53 self.response = self.client.get( reverse("document_list_view") )
54 self.assert_json_response({
59 def test_documents_get_with_login(self):
60 self.assertTrue(self.client.login(username='admin', password='admin'))
62 self.response = self.client.get( reverse("document_list_view") )
63 self.assert_json_response({
68 def test_documents_post(self):
69 self.assertTrue(self.client.login(username='admin', password='admin'))
71 infile = tempfile.NamedTemporaryFile("w+")
72 infile.write('0123456789')
76 self.response = self.client.post( reverse("document_list_view"),
78 'bookname': 'testbook',
85 self.assert_json_response({
86 'url': reverse('document_view', args=['testbook']),
89 # can't test revision number, 'cause it's random
93 def test_document_creation(self):
94 self.assertTrue(self.client.login(username='admin', password='admin'))
96 infile = tempfile.NamedTemporaryFile("w+")
97 infile.write('012340123456789')
101 self.response = self.client.post( reverse("document_list_view"),
103 'bookname': 'testbook',
105 'generate_dc': False,
108 r = self.assert_json_response({
109 'url': reverse('document_view', args=['testbook']),
111 # can't test revision number, 'cause it's random
114 created_rev = r['revision']
116 self.response = self.client.get( \
117 reverse("document_view", args=["testbook"]) )
119 result = self.assert_json_response({
120 u'latest_shared_rev': created_rev,
126 def test_document_meta_get_with_login(self):
127 self.assertTrue(self.client.login(username='admin', password='admin'))
129 self.response = self.client.get( reverse("document_list_view") )
130 self.assert_json_response({
131 # u'latest_rev': u'f94a263812dbe46a3a13d5209bb119988d0078d5',
132 u'documents': [{u'url': u'/api/documents/sample', u'name': u'sample'},
133 {u'url': u'/api/documents/sample_pl', u'name': u'sample_pl'}],
136 self.response = self.client.get( \
137 reverse("document_view", args=['sample']) )
139 self.assert_json_response({
140 #u'latest_shared_rev': u'f94a263812dbe46a3a13d5209bb119988d0078d5',
141 u'text_url': reverse("doctext_view", args=[u'sample']),
142 u'dc_url': reverse("docdc_view", args=[u'sample']),
143 # u'parts_url': reverse("docparts_view", args=[u'sample']),
150 def test_document_text_with_login(self):
151 self.assertTrue(self.client.login(username='admin', password='admin'))
153 self.response = self.client.get( \
154 reverse("document_view", args=['sample']) )
156 resp = self.assert_json_response({
157 #u'latest_shared_rev': u'f94a263812dbe46a3a13d5209bb119988d0078d5',
158 u'text_url': reverse("doctext_view", args=[u'sample']),
159 u'dc_url': reverse("docdc_view", args=[u'sample']),
160 # u'parts_url': reverse("docparts_view", args=[u'sample']),
165 self.response = self.client.get(resp['text_url'])
166 self.assertEqual(self.response.status_code, 200)
167 self.assertEqual(self.response.content, "Ala ma kota\n")
170 # @temprepo('simple')
171 # def test_document_text_update(self):
172 # self.assertTrue(self.client.login(username='admin', password='admin'))
173 # TEXT = u"Ala ma kota i psa"
175 # self.response = self.client.put( \
176 # reverse("doctext_view", args=['testfile']), {'contents': TEXT })
177 # self.assertEqual(self.response.status_code, 200)
179 # self.response = self.client.get( \
180 # reverse("doctext_view", args=['testfile']) )
181 # self.assertEqual(self.response.status_code, 200)
182 # self.assertEqual(self.response.content, TEXT)
184 def assert_json_response(self, must_have={}, exclude=[]):
185 self.assertEqual(self.response.status_code, 200)
186 result = json.loads(self.response.content)
188 for (k,v) in must_have.items():
189 self.assertTrue(result.has_key(k), "Required field '%s' missing in response." % k)
190 self.assertEqual(result[k], v)
193 for (k,v) in result.items():
194 self.assertTrue(must_have.has_key(k))
195 self.assertEqual(must_have[k], v)
198 self.assertFalse(result.has_key(key))