94abe53b5b6fb088913e4e6318296aaeb4202997
[wolnelektury.git] / src / api / 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
7 from django.core.files.uploadedfile import SimpleUploadedFile
8 from django.test import TestCase
9 from django.test.utils import override_settings
10 import json
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     SSIFY_CACHE_ALIASES=['default'],
23     SSIFY_RENDER=True,
24 )
25 class ApiTest(TestCase):
26     def load_json(self, url):
27         content = self.client.get(url).content
28         try:
29             data = json.loads(content)
30         except ValueError:
31             self.fail('No JSON could be decoded:', content)
32         return data
33
34
35 class BookTests(ApiTest):
36
37     def setUp(self):
38         self.tag = Tag.objects.create(category='author', slug='joe')
39         self.book = Book.objects.create(title='A Book', slug='a-book')
40         self.book_tagged = Book.objects.create(title='Tagged Book', slug='tagged-book')
41         self.book_tagged.tags = [self.tag]
42         self.book_tagged.save()
43
44     def test_book_list(self):
45         books = self.load_json('/api/books/')
46         self.assertEqual(len(books), 2,
47                          'Wrong book list.')
48
49     def test_tagged_books(self):
50         books = self.load_json('/api/authors/joe/books/')
51
52         self.assertEqual([b['title'] for b in books], [self.book_tagged.title],
53                         'Wrong tagged book list.')
54
55     def test_detail(self):
56         book = self.load_json('/api/books/a-book/')
57         self.assertEqual(book['title'], self.book.title,
58                         'Wrong book details.')
59
60
61 class TagTests(ApiTest):
62
63     def setUp(self):
64         self.tag = Tag.objects.create(category='author', slug='joe', name='Joe')
65         self.book = Book.objects.create(title='A Book', slug='a-book')
66         self.book.tags = [self.tag]
67         self.book.save()
68
69     def test_tag_list(self):
70         tags = self.load_json('/api/authors/')
71         self.assertEqual(len(tags), 1,
72                         'Wrong tag list.')
73
74     def test_tag_detail(self):
75         tag = self.load_json('/api/authors/joe/')
76         self.assertEqual(tag['name'], self.tag.name,
77                         'Wrong tag details.')
78
79
80 class PictureTests(ApiTest):
81     def test_publish(self):
82         slug = "kandinsky-composition-viii"
83         xml = SimpleUploadedFile('composition8.xml', open(path.join(picture.tests.__path__[0], "files", slug + ".xml")).read())
84         img = SimpleUploadedFile('kompozycja-8.png', open(path.join(picture.tests.__path__[0], "files", slug + ".png")).read())
85
86         import_form = PictureImportForm({}, {
87             'picture_xml_file': xml,
88             'picture_image_file': img
89             })
90
91         assert import_form.is_valid()
92         if import_form.is_valid():
93             import_form.save()
94
95         Picture.objects.get(slug=slug)