32d394e7059c4cd8174b01f9d5e4cd5486794e63
[wolnelektury.git] / src / api / tests / tests.py
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.
4 #
5 from os import path
6 import json
7
8 from django.core.files.uploadedfile import SimpleUploadedFile
9 from django.test import TestCase
10 from django.test.utils import override_settings
11
12 from catalogue.models import Book, Tag
13 from picture.forms import PictureImportForm
14 from picture.models import Picture
15 import picture.tests
16
17
18 @override_settings(
19     NO_SEARCH_INDEX=True,
20     CACHES={'default': {
21         'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}},
22 )
23 class ApiTest(TestCase):
24     def load_json(self, url):
25         content = self.client.get(url).content
26         try:
27             data = json.loads(content)
28         except ValueError:
29             self.fail('No JSON could be decoded: %s' % content)
30         return data
31
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))
37
38
39 class BookTests(ApiTest):
40
41     def setUp(self):
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()
47
48     def test_book_list(self):
49         books = self.load_json('/api/books/')
50         self.assertEqual(len(books), 2,
51                          'Wrong book list.')
52
53     def test_tagged_books(self):
54         books = self.load_json('/api/authors/joe/books/')
55
56         self.assertEqual([b['title'] for b in books], [self.book_tagged.title],
57                          'Wrong tagged book list.')
58
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.')
63
64
65 class TagTests(ApiTest):
66
67     def setUp(self):
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]
71         self.book.save()
72
73     def test_tag_list(self):
74         tags = self.load_json('/api/authors/')
75         self.assertEqual(len(tags), 1,
76                          'Wrong tag list.')
77
78     def test_tag_detail(self):
79         tag = self.load_json('/api/authors/joe/')
80         self.assertEqual(tag['name'], self.tag.name,
81                          'Wrong tag details.')
82
83
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())
91
92         import_form = PictureImportForm({}, {
93             'picture_xml_file': xml,
94             'picture_image_file': img
95             })
96
97         assert import_form.is_valid()
98         if import_form.is_valid():
99             import_form.save()
100
101         Picture.objects.get(slug=slug)
102
103
104 class URLTests(ApiTest):
105     fixtures = ['test-books.yaml']
106
107     def test_get(self):
108         # book lists
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')
114
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')
118
119         self.assert_json_response('/api/collections/', 'collections.json')