1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from base64 import b64encode
10 from StringIO import StringIO
12 from urllib import quote, urlencode
13 from urlparse import parse_qs
15 from django.contrib.auth.models import User
16 from django.core.files.uploadedfile import SimpleUploadedFile
17 from django.test import TestCase
18 from django.test.utils import override_settings
19 from mock import patch
20 from piston.models import Consumer, Token
22 from catalogue.models import Book, Tag
23 from picture.forms import PictureImportForm
24 from picture.models import Picture
31 'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}},
33 class ApiTest(TestCase):
34 def load_json(self, url):
35 content = self.client.get(url).content
37 data = json.loads(content)
39 self.fail('No JSON could be decoded: %s' % content)
42 def assert_json_response(self, url, name):
43 data = self.load_json(url)
44 filename = path.join(path.dirname(__file__), 'res', 'responses', name)
45 with open(filename) as f:
46 good_data = json.load(f)
47 self.assertEqual(data, good_data, json.dumps(data, indent=4))
49 def assert_slugs(self, url, slugs):
50 have_slugs = [x['slug'] for x in self.load_json(url)]
51 self.assertEqual(have_slugs, slugs, have_slugs)
54 class BookTests(ApiTest):
57 self.tag = Tag.objects.create(category='author', slug='joe')
58 self.book = Book.objects.create(title='A Book', slug='a-book')
59 self.book_tagged = Book.objects.create(
60 title='Tagged Book', slug='tagged-book')
61 self.book_tagged.tags = [self.tag]
62 self.book_tagged.save()
64 def test_book_list(self):
65 books = self.load_json('/api/books/')
66 self.assertEqual(len(books), 2,
69 def test_tagged_books(self):
70 books = self.load_json('/api/authors/joe/books/')
72 self.assertEqual([b['title'] for b in books], [self.book_tagged.title],
73 'Wrong tagged book list.')
75 def test_detail(self):
76 book = self.load_json('/api/books/a-book/')
77 self.assertEqual(book['title'], self.book.title,
78 'Wrong book details.')
81 class TagTests(ApiTest):
84 self.tag = Tag.objects.create(
85 category='author', slug='joe', name='Joe')
86 self.book = Book.objects.create(title='A Book', slug='a-book')
87 self.book.tags = [self.tag]
90 def test_tag_list(self):
91 tags = self.load_json('/api/authors/')
92 self.assertEqual(len(tags), 1,
95 def test_tag_detail(self):
96 tag = self.load_json('/api/authors/joe/')
97 self.assertEqual(tag['name'], self.tag.name,
101 class PictureTests(ApiTest):
102 def test_publish(self):
103 slug = "kandinsky-composition-viii"
104 xml = SimpleUploadedFile(
107 picture.tests.__path__[0], "files", slug + ".xml"
109 img = SimpleUploadedFile(
112 picture.tests.__path__[0], "files", slug + ".png"
115 import_form = PictureImportForm({}, {
116 'picture_xml_file': xml,
117 'picture_image_file': img
120 assert import_form.is_valid()
121 if import_form.is_valid():
124 Picture.objects.get(slug=slug)
127 class BooksTests(ApiTest):
128 fixtures = ['test-books.yaml']
130 def test_books(self):
131 self.assert_json_response('/api/books/', 'books.json')
132 self.assert_json_response('/api/books/?new_api=true', 'books.json')
134 self.assert_slugs('/api/audiobooks/', ['parent'])
135 self.assert_slugs('/api/daisy/', ['parent'])
136 self.assert_slugs('/api/newest/', ['parent'])
137 self.assert_slugs('/api/parent_books/', ['parent'])
138 self.assert_slugs('/api/recommended/', ['parent'])
141 self.assert_slugs('/api/books/after/grandchild/count/1/', ['parent'])
143 '/api/books/?new_api=true&after=$grandchild$3&count=1', ['parent'])
146 self.assert_slugs('/api/authors/john-doe/books/', ['parent'])
148 '/api/genres/sonet/books/?authors=john-doe',
150 # It is probably a mistake that this doesn't filter:
152 '/api/books/?authors=john-doe',
153 ['child', 'grandchild', 'parent'])
155 # Parent books by tag.
156 # Notice this contains a grandchild, if a child doesn't have the tag.
157 # This probably isn't really intended behavior and should be redefined.
159 '/api/genres/sonet/parent_books/',
160 ['grandchild', 'parent'])
162 def test_ebooks(self):
163 self.assert_json_response('/api/ebooks/', 'ebooks.json')
165 def test_filter_books(self):
166 self.assert_json_response('/api/filter-books/', 'filter-books.json')
168 '/api/filter-books/?lektura=false',
169 ['child', 'grandchild', 'parent'])
171 '/api/filter-books/?lektura=true',
175 '/api/filter-books/?preview=true',
178 '/api/filter-books/?preview=false',
182 '/api/filter-books/?audiobook=true',
185 '/api/filter-books/?audiobook=false',
186 ['child', 'grandchild'])
188 self.assert_slugs('/api/filter-books/?genres=wiersz', ['child'])
190 self.assert_slugs('/api/filter-books/?search=parent', ['parent'])
192 def test_collections(self):
193 self.assert_json_response('/api/collections/', 'collections.json')
194 self.assert_json_response(
195 '/api/collections/a-collection/',
199 self.assert_json_response('/api/books/parent/', 'books-parent.json')
200 self.assert_json_response('/api/books/child/', 'books-child.json')
201 self.assert_json_response(
202 '/api/books/grandchild/',
203 'books-grandchild.json')
206 # List of tags by category.
207 self.assert_json_response('/api/genres/', 'tags.json')
209 def test_fragments(self):
210 # This is not supported, though it probably should be.
211 # self.assert_json_response(
212 # '/api/books/child/fragments/',
215 self.assert_json_response(
216 '/api/genres/wiersz/fragments/',
218 self.assert_json_response(
219 '/api/books/child/fragments/an-anchor/',
223 class BlogTests(ApiTest):
225 self.assertEqual(self.load_json('/api/blog/'), [])
228 class PreviewTests(ApiTest):
230 self.assert_json_response('/api/preview/', 'preview.json')
233 class OAuth1Tests(ApiTest):
236 cls.user = User.objects.create(username='test')
237 cls.consumer_secret = 'len(quote(consumer secret))>=32'
238 Consumer.objects.create(
240 secret=cls.consumer_secret
244 def tearDownClass(cls):
245 User.objects.all().delete()
247 def test_create_token(self):
248 base_query = ("oauth_consumer_key=client&oauth_nonce=123&"
249 "oauth_signature_method=HMAC-SHA1&oauth_timestamp={}&"
250 "oauth_version=1.0".format(int(time())))
253 quote('http://testserver/api/oauth/request_token/', safe=''),
254 quote(base_query, safe='')
257 quote(self.consumer_secret) + '&', raw, hashlib.sha1
259 h = b64encode(h).rstrip('\n')
261 query = "{}&oauth_signature={}".format(base_query, sign)
262 response = self.client.get('/api/oauth/request_token/?' + query)
263 request_token = parse_qs(response.content)
265 Token.objects.filter(
266 key=request_token['oauth_token'][0], token_type=Token.REQUEST
267 ).update(user=self.user, is_approved=True)
269 base_query = ("oauth_consumer_key=client&oauth_nonce=123&"
270 "oauth_signature_method=HMAC-SHA1&oauth_timestamp={}&"
271 "oauth_token={}&oauth_version=1.0".format(
272 int(time()), request_token['oauth_token'][0]))
275 quote('http://testserver/api/oauth/access_token/', safe=''),
276 quote(base_query, safe='')
279 quote(self.consumer_secret) + '&' +
280 quote(request_token['oauth_token_secret'][0], safe=''),
284 h = b64encode(h).rstrip('\n')
286 query = u"{}&oauth_signature={}".format(base_query, sign)
287 response = self.client.get(u'/api/oauth/access_token/?' + query)
288 access_token = parse_qs(response.content)
291 Token.objects.filter(
292 key=access_token['oauth_token'][0],
293 token_type=Token.ACCESS,
298 class AuthorizedTests(ApiTest):
299 fixtures = ['test-books.yaml']
303 super(AuthorizedTests, cls).setUpClass()
304 cls.user = User.objects.create(username='test')
305 cls.consumer = Consumer.objects.create(
306 key='client', secret='12345678901234567890123456789012')
307 cls.token = Token.objects.create(
308 key='123456789012345678',
309 secret='12345678901234567890123456789012',
311 consumer=cls.consumer,
312 token_type=Token.ACCESS,
314 cls.key = cls.consumer.secret + '&' + cls.token.secret
317 def tearDownClass(cls):
319 cls.consumer.delete()
320 super(AuthorizedTests, cls).tearDownClass()
322 def signed(self, url, method='GET', params=None):
324 "oauth_consumer_key": self.consumer.key,
325 "oauth_nonce": "%f" % time(),
326 "oauth_signature_method": "HMAC-SHA1",
327 "oauth_timestamp": int(time()),
328 "oauth_token": self.token.key,
329 "oauth_version": "1.0",
334 sign_params.update(params)
335 sign_params.update(auth_params)
338 quote('http://testserver' + url, safe=''),
340 quote(str(k)) + "=" + quote(str(v))
341 for (k, v) in sorted(sign_params.items())))
343 auth_params["oauth_signature"] = quote(b64encode(hmac.new(
344 self.key, raw, hashlib.sha1).digest()).rstrip('\n'))
345 auth = 'OAuth realm="API", ' + ', '.join(
346 '{}="{}"'.format(k, v) for (k, v) in auth_params.items())
349 url = url + '?' + urlencode(params)
350 return getattr(self.client, method.lower())(
352 HTTP_AUTHORIZATION=auth
355 def signed_json(self, url, method='GET', params=None):
356 return json.loads(self.signed(url, method, params).content)
358 def test_books(self):
360 self.signed_json('/api/like/parent/'),
363 self.signed('/api/like/parent/', 'POST')
365 self.signed_json('/api/like/parent/'),
368 # There are several endpoints where 'liked' appears.
369 self.assertTrue(self.signed_json('/api/parent_books/')[0]['liked'])
370 self.assertTrue(self.signed_json(
371 '/api/filter-books/', params={"search": "parent"})[0]['liked'])
372 # Liked books go on shelf.
374 [x['slug'] for x in self.signed_json('/api/shelf/likes/')],
377 self.signed('/api/like/parent/', 'POST', {"action": "unlike"})
379 self.signed_json('/api/like/parent/'),
382 self.assertFalse(self.signed_json('/api/parent_books/')[0]['liked'])
384 def test_reading(self):
386 self.signed_json('/api/reading/parent/'),
387 {"state": "not_started"}
389 self.signed('/api/reading/parent/reading/', 'post')
391 self.signed_json('/api/reading/parent/'),
395 [x['slug'] for x in self.signed_json('/api/shelf/reading/')],
398 def test_subscription(self):
399 self.assert_slugs('/api/preview/', ['grandchild'])
401 self.signed_json('/api/username/'),
402 {"username": "test", "premium": False})
404 self.signed('/api/epub/grandchild/').status_code,
405 401) # Not 403 because Piston.
407 with patch('api.handlers.user_is_subscribed', return_value=True):
409 self.signed_json('/api/username/'),
410 {"username": "test", "premium": True})
411 with patch('django.core.files.storage.Storage.open',
412 return_value=StringIO("<epub>")):
414 self.signed('/api/epub/grandchild/').content,