Always attempt to build HTML and fix_tree_tags when publishing a book. Also, some...
[wolnelektury.git] / apps / 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     API_WAIT=-1,
20     NO_SEARCH_INDEX = True,
21     CACHES = {'api': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'},
22               'default': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'},
23               'permanent': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}}
24 )
25 class ApiTest(TestCase):
26     pass
27
28
29 class ChangesTest(ApiTest):
30
31     def test_basic(self):
32         book = Book(title='A Book')
33         book.save()
34         tag = Tag.objects.create(category='author', name='Author')
35         book.tags = [tag]
36         book.save()
37
38         changes = json.loads(self.client.get('/api/changes/0.json?book_fields=title&tag_fields=name').content)
39         self.assertEqual(changes['updated']['books'],
40                          [{'id': book.id, 'title': book.title}],
41                          'Invalid book format in changes')
42         self.assertEqual(changes['updated']['tags'],
43                          [{'id': tag.id, 'name': tag.name}],
44                          'Invalid tag format in changes')
45
46
47 class BookChangesTests(ApiTest):
48
49     def setUp(self):
50         super(BookChangesTests, self).setUp()
51         self.book = Book.objects.create(slug='slug')
52
53     def test_basic(self):
54         # test book in book_changes.added
55         changes = json.loads(self.client.get('/api/book_changes/0.json').content)
56         self.assertEqual(len(changes['updated']),
57                          1,
58                          'Added book not in book_changes.updated')
59
60     def test_deleted_disappears(self):
61         # test deleted book disappears
62         Book.objects.all().delete()
63         changes = json.loads(self.client.get('/api/book_changes/0.json').content)
64         self.assertEqual(len(changes), 1,
65                          'Deleted book should disappear.')
66
67     def test_shelf(self):
68         changed_at = self.book.changed_at
69
70         # putting on a shelf should not update changed_at
71         shelf = Tag.objects.create(category='set', slug='shelf')
72         self.book.tags = [shelf]
73         self.assertEqual(self.book.changed_at,
74                          changed_at)
75
76 class TagChangesTests(ApiTest):
77
78     def setUp(self):
79         super(TagChangesTests, self).setUp()
80         self.tag = Tag.objects.create(category='author')
81         self.book = Book.objects.create()
82         self.book.tags = [self.tag]
83         self.book.save()
84
85     def test_added(self):
86         # test tag in tag_changes.added
87         changes = json.loads(self.client.get('/api/tag_changes/0.json').content)
88         self.assertEqual(len(changes['updated']),
89                          1,
90                          'Added tag not in tag_changes.updated')
91
92     def test_empty_disappears(self):
93         self.book.tags = []
94         self.book.save()
95         changes = json.loads(self.client.get('/api/tag_changes/0.json').content)
96         self.assertEqual(len(changes), 1,
97                          'Empty or deleted tag should disappear.')
98
99
100
101 class BookTests(TestCase):
102
103     def setUp(self):
104         self.tag = Tag.objects.create(category='author', slug='joe')
105         self.book = Book.objects.create(title='A Book', slug='a-book')
106         self.book_tagged = Book.objects.create(title='Tagged Book', slug='tagged-book')
107         self.book_tagged.tags = [self.tag]
108         self.book_tagged.save()
109
110     def test_book_list(self):
111         books = json.loads(self.client.get('/api/books/').content)
112         self.assertEqual(len(books), 2,
113                          'Wrong book list.')
114
115     def test_tagged_books(self):
116         books = json.loads(self.client.get('/api/authors/joe/books/').content)
117
118         self.assertEqual([b['title'] for b in books], [self.book_tagged.title],
119                         'Wrong tagged book list.')
120
121     def test_detail(self):
122         book = json.loads(self.client.get('/api/books/a-book/').content)
123         self.assertEqual(book['title'], self.book.title,
124                         'Wrong book details.')
125
126
127 class TagTests(TestCase):
128
129     def setUp(self):
130         self.tag = Tag.objects.create(category='author', slug='joe', name='Joe')
131         self.book = Book.objects.create(title='A Book', slug='a-book')
132         self.book.tags = [self.tag]
133         self.book.save()
134
135     def test_tag_list(self):
136         tags = json.loads(self.client.get('/api/authors/').content)
137         self.assertEqual(len(tags), 1,
138                         'Wrong tag list.')
139
140     def test_tag_detail(self):
141         tag = json.loads(self.client.get('/api/authors/joe/').content)
142         self.assertEqual(tag['name'], self.tag.name,
143                         'Wrong tag details.')
144
145
146 class PictureTests(ApiTest):
147     def test_publish(self):
148         slug = "kandinsky-composition-viii"
149         xml = SimpleUploadedFile('composition8.xml', open(path.join(picture.tests.__path__[0], "files", slug + ".xml")).read())
150         img = SimpleUploadedFile('kompozycja-8.png', open(path.join(picture.tests.__path__[0], "files", slug + ".png")).read())
151
152         import_form = PictureImportForm({}, {
153             'picture_xml_file': xml,
154             'picture_image_file': img
155             })
156
157         assert import_form.is_valid()
158         if import_form.is_valid():
159             import_form.save()
160
161         Picture.objects.get(slug=slug)