Dołączenie skryptu /api/toolbar/scriptlets do editor.html.
[redakcja.git] / apps / api / tests / __init__.py
1 from django.test import TestCase
2 from django.test.client import Client
3 from django.core.urlresolvers import reverse
4
5 from django.utils import simplejson as json
6
7 from django.contrib.auth.models import User
8
9 import settings
10 from os.path import join, dirname
11 from StringIO import StringIO
12 import shutil
13 import tempfile
14
15 REPO_TEMPLATES = join(dirname(__file__), 'data')
16
17 def temprepo(name):
18     from functools import wraps
19
20     def decorator(func):
21
22
23         @wraps(func)
24         def decorated(self, *args, **kwargs):
25             clean = False
26             try:
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)
31                 clean = True
32             finally:
33                 if not clean and self.response:
34                     print "RESULT", func.__name__, ">>>"
35                     print self.response
36                     print "<<<"
37
38                 # shutil.rmtree(temp, True)
39                 settings.REPOSITORY_PATH = ''
40            
41         return decorated
42     return decorator   
43     
44
45 class SimpleTest(TestCase):
46
47     def setUp(self):
48         self.response = None
49         u = User.objects.create_user('admin', 'test@localhost', 'admin')
50         u.save()        
51
52     @temprepo('clean')
53     def test_documents_get_anonymous(self):
54         self.response = self.client.get( reverse("document_list_view") )
55         self.assert_json_response({            
56             u'documents': [],            
57         })
58
59     @temprepo('clean')
60     def test_documents_get_with_login(self):
61         self.assertTrue(self.client.login(username='admin', password='admin'))
62         
63         self.response = self.client.get( reverse("document_list_view") )
64         self.assert_json_response({                        
65             u'documents': [],            
66         })    
67     
68     @temprepo('clean')
69     def test_document_creation(self):
70         self.assertTrue(self.client.login(username='admin', password='admin'))
71
72         infile = tempfile.NamedTemporaryFile("w+")
73         infile.write('012340123456789')
74         infile.flush()
75         infile.seek(0)
76
77         self.response = self.client.post( reverse("document_list_view"),
78         data = {
79              'bookname': 'testbook',
80              'ocr_file': infile,
81              'generate_dc': False,
82         })
83
84         r = self.assert_json_response( {
85             'url': reverse('document_view', args=['testbook']),
86             'name': 'testbook',            
87             # can't test revision number, 'cause it's random
88         }, code=201)
89
90         created_rev = r['revision']
91         self.response = self.client.get(r['url'])
92         
93         result = self.assert_json_response({
94             u'public_revision': created_rev,
95             # u'size': 15,
96         })
97
98
99     @temprepo('simple')
100     def test_document_meta_get_with_login(self):
101         self.assertTrue(self.client.login(username='admin', password='admin'))
102
103         self.response = self.client.get( reverse("document_list_view") )
104         self.assert_json_response({
105             # u'latest_rev': u'f94a263812dbe46a3a13d5209bb119988d0078d5',
106             u'documents': [
107                 {u'url': u'/api/documents/sample', u'name': u'sample', u'parts': []},
108                 {u'url': u'/api/documents/sample_pl', u'name': u'sample_pl', u'parts': []}
109             ],
110         })
111
112         self.response = self.client.get( \
113             reverse("document_view", args=['sample']) )
114
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']),
120             u'name': u'sample',
121             # u'size': 20,
122         })
123
124
125     @temprepo('simple')
126     def test_document_text_with_login(self):
127         self.assertTrue(self.client.login(username='admin', password='admin'))
128
129         self.response = self.client.get( \
130             reverse("document_view", args=['sample']) )
131
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']),
137             u'name': u'sample',
138             # u'size': 20,
139         })
140
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")
144 #
145 #
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"
150 #
151 #        self.response = self.client.put( \
152 #            reverse("doctext_view", args=['testfile']), {'contents': TEXT })
153 #        self.assertEqual(self.response.status_code, 200)
154 #
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)
159
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)
163
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)
167
168         if exclude is True:
169             for (k,v) in result.items():
170                 self.assertTrue(must_have.has_key(k))
171                 self.assertEqual(must_have[k], v)
172
173         for key in exclude:
174             self.assertFalse(result.has_key(key))
175             
176         return result   
177