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))
38 def assert_slugs(self, url, slugs):
39 have_slugs = [x['slug'] for x in self.load_json(url)]
40 self.assertEqual(have_slugs, slugs, have_slugs)
43 class BookTests(ApiTest):
46 self.tag = Tag.objects.create(category='author', slug='joe')
47 self.book = Book.objects.create(title='A Book', slug='a-book')
48 self.book_tagged = Book.objects.create(title='Tagged Book', slug='tagged-book')
49 self.book_tagged.tags = [self.tag]
50 self.book_tagged.save()
52 def test_book_list(self):
53 books = self.load_json('/api/books/')
54 self.assertEqual(len(books), 2,
57 def test_tagged_books(self):
58 books = self.load_json('/api/authors/joe/books/')
60 self.assertEqual([b['title'] for b in books], [self.book_tagged.title],
61 'Wrong tagged book list.')
63 def test_detail(self):
64 book = self.load_json('/api/books/a-book/')
65 self.assertEqual(book['title'], self.book.title,
66 'Wrong book details.')
69 class TagTests(ApiTest):
72 self.tag = Tag.objects.create(category='author', slug='joe', name='Joe')
73 self.book = Book.objects.create(title='A Book', slug='a-book')
74 self.book.tags = [self.tag]
77 def test_tag_list(self):
78 tags = self.load_json('/api/authors/')
79 self.assertEqual(len(tags), 1,
82 def test_tag_detail(self):
83 tag = self.load_json('/api/authors/joe/')
84 self.assertEqual(tag['name'], self.tag.name,
88 class PictureTests(ApiTest):
89 def test_publish(self):
90 slug = "kandinsky-composition-viii"
91 xml = SimpleUploadedFile(
92 'composition8.xml', open(path.join(picture.tests.__path__[0], "files", slug + ".xml")).read())
93 img = SimpleUploadedFile(
94 'kompozycja-8.png', open(path.join(picture.tests.__path__[0], "files", slug + ".png")).read())
96 import_form = PictureImportForm({}, {
97 'picture_xml_file': xml,
98 'picture_image_file': img
101 assert import_form.is_valid()
102 if import_form.is_valid():
105 Picture.objects.get(slug=slug)
108 class BooksTests(ApiTest):
109 fixtures = ['test-books.yaml']
111 def test_books(self):
112 self.assert_json_response('/api/books/', 'books.json')
113 self.assert_json_response('/api/books/?new_api=true', 'books.json')
115 self.assert_slugs('/api/audiobooks/', ['parent'])
116 self.assert_slugs('/api/daisy/', ['parent'])
117 self.assert_slugs('/api/newest/', ['parent'])
118 self.assert_slugs('/api/parent_books/', ['parent'])
119 self.assert_slugs('/api/recommended/', ['parent'])
122 self.assert_slugs('/api/books/after/grandchild/count/1/', ['parent'])
123 self.assert_slugs('/api/books/?new_api=true&after=$grandchild$3&count=1', ['parent'])
126 self.assert_slugs('/api/authors/john-doe/books/', ['parent'])
127 self.assert_slugs('/api/genres/sonet/books/?authors=john-doe', ['parent'])
128 # It is probably a mistake that this doesn't filter:
129 self.assert_slugs('/api/books/?authors=john-doe', ['child', 'grandchild', 'parent'])
131 # Parent books by tag.
132 # Notice this contains a grandchild, if a child doesn't have the tag.
133 # This probably isn't really intended behavior and should be redefined.
134 self.assert_slugs('/api/genres/sonet/parent_books/', ['grandchild', 'parent'])
136 def test_ebooks(self):
137 self.assert_json_response('/api/ebooks/', 'ebooks.json')
139 def test_filter_books(self):
140 self.assert_json_response('/api/filter-books/', 'filter-books.json')
142 '/api/filter-books/?lektura=false&preview=false',
143 ['child', 'grandchild', 'parent'])
145 '/api/filter-books/?lektura=true',
148 Book.objects.filter(slug='child').update(preview=True)
149 self.assert_slugs('/api/filter-books/?preview=true', ['child'])
150 self.assert_slugs('/api/filter-books/?preview=false', ['grandchild', 'parent'])
152 self.assert_slugs('/api/filter-books/?audiobook=true', ['parent'])
153 self.assert_slugs('/api/filter-books/?audiobook=false', ['child', 'grandchild'])
155 self.assert_slugs('/api/filter-books/?genres=wiersz', ['child'])
157 self.assert_slugs('/api/filter-books/?search=parent', ['parent'])
159 def test_collections(self):
160 self.assert_json_response('/api/collections/', 'collections.json')
161 self.assert_json_response('/api/collections/a-collection/', 'collection.json')
164 self.assert_json_response('/api/books/parent/', 'books-parent.json')
165 self.assert_json_response('/api/books/child/', 'books-child.json')
166 self.assert_json_response('/api/books/grandchild/', 'books-grandchild.json')
169 # List of tags by category.
170 self.assert_json_response('/api/genres/', 'tags.json')
172 def test_fragments(self):
173 # This is not supported, though it probably should be.
174 #self.assert_json_response('/api/books/child/fragments/', 'fragments.json')
176 self.assert_json_response('/api/genres/wiersz/fragments/', 'fragments.json')
177 self.assert_json_response('/api/genres/wiersz/fragments/', 'fragments.json')
179 self.assert_json_response('/api/books/child/fragments/an-anchor/', 'fragment.json')
182 class BlogTests(ApiTest):
184 self.assertEqual(self.load_json('/api/blog/'), [])
187 class PreviewTests(ApiTest):
189 self.assert_json_response('/api/preview/', 'preview.json')