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_document_creation(self):
69 self.assertTrue(self.client.login(username='admin', password='admin'))
71 infile = tempfile.NamedTemporaryFile("w+")
72 infile.write('012340123456789')
76 self.response = self.client.post( reverse("document_list_view"),
78 'bookname': 'testbook',
83 r = self.assert_json_response( {
84 'url': reverse('document_view', args=['testbook']),
86 # can't test revision number, 'cause it's random
89 created_rev = r['revision']
90 self.response = self.client.get(r['url'])
92 result = self.assert_json_response({
93 u'public_revision': created_rev,
99 def test_document_meta_get_with_login(self):
100 self.assertTrue(self.client.login(username='admin', password='admin'))
102 self.response = self.client.get( reverse("document_list_view") )
103 self.assert_json_response({
104 # u'latest_rev': u'f94a263812dbe46a3a13d5209bb119988d0078d5',
106 {u'url': u'/api/documents/sample', u'name': u'sample'},
107 {u'url': u'/api/documents/sample_pl', u'name': u'sample_pl'}
111 self.response = self.client.get( \
112 reverse("document_view", args=['sample']) )
114 self.assert_json_response({
115 #u'latest_shared_rev': u'f94a263812dbe46a3a13d5209bb119988d0078d5',
116 #u'text_url': reverse("doctext_view", args=[u'sample']),
117 #u'dc_url': reverse("docdc_view", args=[u'sample']),
118 # u'parts_url': reverse("docparts_view", args=[u'sample']),
125 def test_document_text_with_login(self):
126 self.assertTrue(self.client.login(username='admin', password='admin'))
128 self.response = self.client.get( \
129 reverse("document_view", args=['sample']) )
131 resp = self.assert_json_response({
132 #u'latest_shared_rev': u'f94a263812dbe46a3a13d5209bb119988d0078d5',
133 #u'text_url': reverse("doctext_view", args=[u'sample']),
134 #u'dc_url': reverse("docdc_view", args=[u'sample']),
135 # u'parts_url': reverse("docparts_view", args=[u'sample']),
140 self.response = self.client.get(resp['text_url'])
141 self.assertEqual(self.response.status_code, 200)
142 self.assertEqual(self.response.content, "Ala ma kota\n")
145 # @temprepo('simple')
146 # def test_document_text_update(self):
147 # self.assertTrue(self.client.login(username='admin', password='admin'))
148 # TEXT = u"Ala ma kota i psa"
150 # self.response = self.client.put( \
151 # reverse("doctext_view", args=['testfile']), {'contents': TEXT })
152 # self.assertEqual(self.response.status_code, 200)
154 # self.response = self.client.get( \
155 # reverse("doctext_view", args=['testfile']) )
156 # self.assertEqual(self.response.status_code, 200)
157 # self.assertEqual(self.response.content, TEXT)
159 def assert_json_response(self, must_have={}, exclude=[], code=200):
160 self.assertEqual(self.response.status_code, code)
161 result = json.loads(self.response.content)
163 for (k,v) in must_have.items():
164 self.assertTrue(result.has_key(k), "Required field '%s' missing in response." % k)
165 self.assertEqual(result[k], v)
168 for (k,v) in result.items():
169 self.assertTrue(must_have.has_key(k))
170 self.assertEqual(must_have[k], v)
173 self.assertFalse(result.has_key(key))