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__, ">>>"
35 print self.response.content
38 shutil.rmtree(temp, True)
40 settings.REPOSITORY_PATH = ''
46 class SimpleTest(TestCase):
50 u = User.objects.create_user('admin', 'test@localhost', 'admin')
54 def test_documents_get_anonymous(self):
55 self.response = self.client.get( reverse("document_list_view") )
56 self.assert_json_response({
61 def test_documents_get_with_login(self):
62 self.assertTrue(self.client.login(username='admin', password='admin'))
64 self.response = self.client.get( reverse("document_list_view") )
65 self.assert_json_response({
70 def test_document_creation(self):
71 self.assertTrue(self.client.login(username='admin', password='admin'))
73 infile = tempfile.NamedTemporaryFile("w+")
74 infile.write('012340123456789')
78 self.response = self.client.post( reverse("document_list_view"),
80 'bookname': 'testbook',
85 r = self.assert_json_response( {
86 'url': reverse('document_view', args=['testbook']),
88 # can't test revision number, 'cause it's random
91 created_rev = r['revision']
92 self.response = self.client.get(r['url'])
94 result = self.assert_json_response({
95 u'public_revision': created_rev,
101 def test_document_meta_get_with_login(self):
102 self.assertTrue(self.client.login(username='admin', password='admin'))
104 self.response = self.client.get( reverse("document_list_view") )
105 self.assert_json_response({
106 # u'latest_rev': u'f94a263812dbe46a3a13d5209bb119988d0078d5',
108 {u'url': u'/api/documents/sample', u'name': u'sample', u'parts': []},
109 {u'url': u'/api/documents/sample_pl', u'name': u'sample_pl', u'parts': []}
113 self.response = self.client.get( \
114 reverse("document_view", args=['sample']) )
116 self.assert_json_response({
117 #u'latest_shared_rev': u'f94a263812dbe46a3a13d5209bb119988d0078d5',
118 #u'text_url': reverse("doctext_view", args=[u'sample']),
119 #u'dc_url': reverse("docdc_view", args=[u'sample']),
120 # u'parts_url': reverse("docparts_view", args=[u'sample']),
127 def test_document_text_with_login(self):
128 self.assertTrue(self.client.login(username='admin', password='admin'))
130 self.response = self.client.get( \
131 reverse("document_view", args=['sample']) )
133 resp = self.assert_json_response({
134 #u'latest_shared_rev': u'f94a263812dbe46a3a13d5209bb119988d0078d5',
135 #u'text_url': reverse("doctext_view", args=[u'sample']),
136 #u'dc_url': reverse("docdc_view", args=[u'sample']),
137 # u'parts_url': reverse("docparts_view", args=[u'sample']),
142 self.response = self.client.get(resp['text_url'])
143 self.assertEqual(self.response.status_code, 200)
144 self.assertEqual(self.response.content, "Ala ma kota\n")
148 def test_document_text_save(self):
149 self.assertTrue(self.client.login(username='admin', password='admin'))
150 TEXT = u"Ala ma kota i psa"
152 self.response = self.client.get(
153 reverse("document_view", args=['sample']) )
155 resp = self.assert_json_response()
157 self.response = self.client.put(resp['text_url'], {'contents': TEXT })
158 result = self.assert_json_response(must_have={u'document': u'sample'} )
160 #self.response = self.client.get(result['url'])
161 #self.assertEqual(self.response.content, TEXT)
163 def assert_json_response(self, must_have={}, exclude=[], code=200):
164 self.assertEqual(self.response.status_code, code)
165 result = json.loads(self.response.content)
167 for (k,v) in must_have.items():
168 self.assertTrue(result.has_key(k), "Required field '%s' missing in response." % k)
169 self.assertEqual(result[k], v)
172 for (k,v) in result.items():
173 self.assertTrue(must_have.has_key(k))
174 self.assertEqual(must_have[k], v)
177 self.assertFalse(result.has_key(key))