Fixes #3211, fixes #3211: related books and pictures bars.
[wolnelektury.git] / apps / api / tests.py
index e3dd71a..94abe53 100644 (file)
 # -*- coding: utf-8 -*-
+# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
+#
+from os import path
 
-from datetime import datetime
-
+from django.core.files.uploadedfile import SimpleUploadedFile
 from django.test import TestCase
-from django.utils import simplejson as json
+from django.test.utils import override_settings
+import json
 
-from api.helpers import timestamp
 from catalogue.models import Book, Tag
+from picture.forms import PictureImportForm
+from picture.models import Picture
+import picture.tests
+
+
+@override_settings(
+    NO_SEARCH_INDEX=True,
+    CACHES={'default': {
+        'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}},
+    SSIFY_CACHE_ALIASES=['default'],
+    SSIFY_RENDER=True,
+)
+class ApiTest(TestCase):
+    def load_json(self, url):
+        content = self.client.get(url).content
+        try:
+            data = json.loads(content)
+        except ValueError:
+            self.fail('No JSON could be decoded:', content)
+        return data
+
+
+class BookTests(ApiTest):
+
+    def setUp(self):
+        self.tag = Tag.objects.create(category='author', slug='joe')
+        self.book = Book.objects.create(title='A Book', slug='a-book')
+        self.book_tagged = Book.objects.create(title='Tagged Book', slug='tagged-book')
+        self.book_tagged.tags = [self.tag]
+        self.book_tagged.save()
 
+    def test_book_list(self):
+        books = self.load_json('/api/books/')
+        self.assertEqual(len(books), 2,
+                         'Wrong book list.')
 
-class ChangesTests(TestCase):
+    def test_tagged_books(self):
+        books = self.load_json('/api/authors/joe/books/')
 
-    def test_basic(self):
-        book = Book.objects.create(slug='a-book', title='A Book')
-        tag = Tag.objects.create(category='author', slug='author', name='Author')
+        self.assertEqual([b['title'] for b in books], [self.book_tagged.title],
+                        'Wrong tagged book list.')
 
-        print self.client.get('/api/changes/0.json?book_fields=slug&tag_fields=slug').content
-        changes = json.loads(self.client.get('/api/changes/0.json?book_fields=slug&tag_fields=slug').content)
-        self.assertEqual(changes['added']['books'], 
-                         [{'id': book.id, 'slug': book.slug}],
-                         'Invalid book format in changes')
-        self.assertEqual(changes['added']['tags'], 
-                         [{'id': tag.id, 'slug': tag.slug}],
-                         'Invalid tag format in changes')
+    def test_detail(self):
+        book = self.load_json('/api/books/a-book/')
+        self.assertEqual(book['title'], self.book.title,
+                        'Wrong book details.')
 
 
-class BookChangesTests(TestCase):
+class TagTests(ApiTest):
 
     def setUp(self):
-        self.book = Book.objects.create()
+        self.tag = Tag.objects.create(category='author', slug='joe', name='Joe')
+        self.book = Book.objects.create(title='A Book', slug='a-book')
+        self.book.tags = [self.tag]
+        self.book.save()
 
-    def test_basic(self):
-        # test book in book_changes.added
-        changes = json.loads(self.client.get('/api/book_changes/0.json').content)
-        self.assertEqual(len(changes['added']),
-                         1,
-                         'Added book not in book_changes.added')
+    def test_tag_list(self):
+        tags = self.load_json('/api/authors/')
+        self.assertEqual(len(tags), 1,
+                        'Wrong tag list.')
 
-        # test changed book in changed
-        self.book.slug = 'a-book'
-        self.book.save()
-        changes = json.loads(self.client.get('/api/book_changes/%f.json' % timestamp(self.book.created_at)).content)
-        self.assertEqual(changes['added'],
-                         [],
-                         'Changed book in book_changes.added instead of book_changes.changed.')
-        self.assertEqual(len(changes['changed']),
-                         1,
-                         'Changed book not in book_changes.changed.')
-
-        # test deleted book in deleted
-        Book.objects.all().delete()
-        changes = json.loads(self.client.get('/api/book_changes/%f.json' % timestamp(self.book.changed_at)).content)
-        self.assertEqual(changes['added'],
-                         [],
-                         'Deleted book still in book_changes.added.')
-        self.assertEqual(changes['changed'],
-                         [],
-                         'Deleted book still in book_changes.changed.')
-        self.assertEqual(len(changes['deleted']),
-                         1,
-                         'Deleted book not in book_changes.deleted.')
-
-    def test_shelf(self):
-        changed_at = self.book.changed_at
-        print changed_at
-
-        # putting on a shelf should not update changed_at
-        shelf = Tag.objects.create(category='set', slug='shelf')
-        self.book.tags = [shelf]
-        self.assertEqual(self.book.changed_at,
-                         changed_at)
-
-class TagChangesTests(TestCase):
+    def test_tag_detail(self):
+        tag = self.load_json('/api/authors/joe/')
+        self.assertEqual(tag['name'], self.tag.name,
+                        'Wrong tag details.')
 
-    def setUp(self):
-        self.tag = Tag.objects.create()
-
-    def test_basic(self):
-        # test tag in tag_changes.added
-        changes = json.loads(self.client.get('/api/tag_changes/0.json').content)
-        self.assertEqual(len(changes['added']),
-                         1,
-                         'Added tag not in tag_changes.added')
-
-        # test changed tag in changed
-        self.tag.slug = 'a-tag'
-        self.tag.save()
-        changes = json.loads(self.client.get('/api/tag_changes/%f.json' % timestamp(self.tag.created_at)).content)
-        self.assertEqual(changes['added'],
-                         [],
-                         'Changed tag in tag_changes.added instead of tag_changes.changed.')
-        self.assertEqual(len(changes['changed']),
-                         1,
-                         'Changed tag not in tag_changes.changed.')
-
-        # test deleted book in deleted
-        Tag.objects.all().delete()
-        changes = json.loads(self.client.get('/api/tag_changes/%f.json' % timestamp(self.tag.changed_at)).content)
-        self.assertEqual(changes['added'],
-                         [],
-                         'Deleted tag still in tag_changes.added.')
-        self.assertEqual(changes['changed'],
-                         [],
-                         'Deleted tag still in tag_changes.changed.')
-        self.assertEqual(len(changes['deleted']),
-                         1,
-                         'Deleted tag not in tag_changes.deleted.')
+
+class PictureTests(ApiTest):
+    def test_publish(self):
+        slug = "kandinsky-composition-viii"
+        xml = SimpleUploadedFile('composition8.xml', open(path.join(picture.tests.__path__[0], "files", slug + ".xml")).read())
+        img = SimpleUploadedFile('kompozycja-8.png', open(path.join(picture.tests.__path__[0], "files", slug + ".png")).read())
+
+        import_form = PictureImportForm({}, {
+            'picture_xml_file': xml,
+            'picture_image_file': img
+            })
+
+        assert import_form.is_valid()
+        if import_form.is_valid():
+            import_form.save()
+
+        Picture.objects.get(slug=slug)