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
24 def decorated(self, *args, **kwargs):
27 temp = tempfile.mkdtemp("-test", func.__name__)
28 shutil.copytree(join(REPO_TEMPLATES, name), join(temp, 'repo'), False)
29 settings.REPOSITORY_PATH = join(temp, 'repo')
30 func(self, *args, **kwargs)
33 if not clean and self.response:
34 print "RESULT", func.__name__, ">>>"
38 # shutil.rmtree(temp, True)
39 settings.REPOSITORY_PATH = ''
45 class SimpleTest(TestCase):
49 u = User.objects.create_user('admin', 'test@localhost', 'admin')
53 def test_documents_get_anonymous(self):
54 self.response = self.client.get( reverse("document_list_view") )
55 self.assert_json_response({
60 def test_documents_get_with_login(self):
61 self.assertTrue(self.client.login(username='admin', password='admin'))
63 self.response = self.client.get( reverse("document_list_view") )
64 self.assert_json_response({
69 def test_document_creation(self):
70 self.assertTrue(self.client.login(username='admin', password='admin'))
72 infile = tempfile.NamedTemporaryFile("w+")
73 infile.write('012340123456789')
77 self.response = self.client.post( reverse("document_list_view"),
79 'bookname': 'testbook',
84 r = self.assert_json_response( {
85 'url': reverse('document_view', args=['testbook']),
87 # can't test revision number, 'cause it's random
90 created_rev = r['revision']
91 self.response = self.client.get(r['url'])
93 result = self.assert_json_response({
94 u'public_revision': created_rev,
100 def test_document_meta_get_with_login(self):
101 self.assertTrue(self.client.login(username='admin', password='admin'))
103 self.response = self.client.get( reverse("document_list_view") )
104 self.assert_json_response({
105 # u'latest_rev': u'f94a263812dbe46a3a13d5209bb119988d0078d5',
107 {u'url': u'/api/documents/sample', u'name': u'sample'},
108 {u'url': u'/api/documents/sample_pl', u'name': u'sample_pl'}
112 self.response = self.client.get( \
113 reverse("document_view", args=['sample']) )
115 self.assert_json_response({
116 #u'latest_shared_rev': u'f94a263812dbe46a3a13d5209bb119988d0078d5',
117 #u'text_url': reverse("doctext_view", args=[u'sample']),
118 #u'dc_url': reverse("docdc_view", args=[u'sample']),
119 # u'parts_url': reverse("docparts_view", args=[u'sample']),
126 def test_document_text_with_login(self):
127 self.assertTrue(self.client.login(username='admin', password='admin'))
129 self.response = self.client.get( \
130 reverse("document_view", args=['sample']) )
132 resp = self.assert_json_response({
133 #u'latest_shared_rev': u'f94a263812dbe46a3a13d5209bb119988d0078d5',
134 #u'text_url': reverse("doctext_view", args=[u'sample']),
135 #u'dc_url': reverse("docdc_view", args=[u'sample']),
136 # u'parts_url': reverse("docparts_view", args=[u'sample']),
141 self.response = self.client.get(resp['text_url'])
142 self.assertEqual(self.response.status_code, 200)
143 self.assertEqual(self.response.content, "Ala ma kota\n")
146 # @temprepo('simple')
147 # def test_document_text_update(self):
148 # self.assertTrue(self.client.login(username='admin', password='admin'))
149 # TEXT = u"Ala ma kota i psa"
151 # self.response = self.client.put( \
152 # reverse("doctext_view", args=['testfile']), {'contents': TEXT })
153 # self.assertEqual(self.response.status_code, 200)
155 # self.response = self.client.get( \
156 # reverse("doctext_view", args=['testfile']) )
157 # self.assertEqual(self.response.status_code, 200)
158 # self.assertEqual(self.response.content, TEXT)
160 def assert_json_response(self, must_have={}, exclude=[], code=200):
161 self.assertEqual(self.response.status_code, code)
162 result = json.loads(self.response.content)
164 for (k,v) in must_have.items():
165 self.assertTrue(result.has_key(k), "Required field '%s' missing in response." % k)
166 self.assertEqual(result[k], v)
169 for (k,v) in result.items():
170 self.assertTrue(must_have.has_key(k))
171 self.assertEqual(must_have[k], v)
174 self.assertFalse(result.has_key(key))