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.
8 from django.core.files.uploadedfile import SimpleUploadedFile
9 from django.test import TestCase
10 from django.test.utils import override_settings
12 from catalogue.models import Book, Tag
13 from picture.forms import PictureImportForm
14 from picture.models import Picture
21 'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}},
23 class ApiTest(TestCase):
24 def load_json(self, url):
25 content = self.client.get(url).content
27 data = json.loads(content)
29 self.fail('No JSON could be decoded: %s' % content)
32 def assert_json_response(self, url, name):
33 data = self.load_json(url)
34 with open(path.join(path.dirname(__file__), 'res', 'responses', name)) as f:
35 good_data = json.load(f)
36 self.assertEqual(data, good_data, json.dumps(data, indent=4))
39 class BookTests(ApiTest):
42 self.tag = Tag.objects.create(category='author', slug='joe')
43 self.book = Book.objects.create(title='A Book', slug='a-book')
44 self.book_tagged = Book.objects.create(title='Tagged Book', slug='tagged-book')
45 self.book_tagged.tags = [self.tag]
46 self.book_tagged.save()
48 def test_book_list(self):
49 books = self.load_json('/api/books/')
50 self.assertEqual(len(books), 2,
53 def test_tagged_books(self):
54 books = self.load_json('/api/authors/joe/books/')
56 self.assertEqual([b['title'] for b in books], [self.book_tagged.title],
57 'Wrong tagged book list.')
59 def test_detail(self):
60 book = self.load_json('/api/books/a-book/')
61 self.assertEqual(book['title'], self.book.title,
62 'Wrong book details.')
65 class TagTests(ApiTest):
68 self.tag = Tag.objects.create(category='author', slug='joe', name='Joe')
69 self.book = Book.objects.create(title='A Book', slug='a-book')
70 self.book.tags = [self.tag]
73 def test_tag_list(self):
74 tags = self.load_json('/api/authors/')
75 self.assertEqual(len(tags), 1,
78 def test_tag_detail(self):
79 tag = self.load_json('/api/authors/joe/')
80 self.assertEqual(tag['name'], self.tag.name,
84 class PictureTests(ApiTest):
85 def test_publish(self):
86 slug = "kandinsky-composition-viii"
87 xml = SimpleUploadedFile(
88 'composition8.xml', open(path.join(picture.tests.__path__[0], "files", slug + ".xml")).read())
89 img = SimpleUploadedFile(
90 'kompozycja-8.png', open(path.join(picture.tests.__path__[0], "files", slug + ".png")).read())
92 import_form = PictureImportForm({}, {
93 'picture_xml_file': xml,
94 'picture_image_file': img
97 assert import_form.is_valid()
98 if import_form.is_valid():
101 Picture.objects.get(slug=slug)
104 class URLTests(ApiTest):
105 fixtures = ['test-books.yaml']
109 self.assert_json_response('/api/audiobooks/', 'audiobooks.json')
110 self.assert_json_response('/api/daisy/', 'daisy.json')
111 self.assert_json_response('/api/ebooks/', 'ebooks.json')
112 self.assert_json_response('/api/filter-books/', 'filter-books.json')
113 self.assert_json_response('/api/newest/', 'newest.json')
115 self.assert_json_response('/api/blog/', 'blog.json')
116 self.assert_json_response('/api/preview/', 'preview.json')
117 self.assert_json_response('/api/recommended/', 'recommended.json')
119 self.assert_json_response('/api/collections/', 'collections.json')