Support civicrm newsletters.
[wolnelektury.git] / src / catalogue / tests / test_bookmedia.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 from os.path import basename, exists
5 from unittest import skip
6
7 from django.core.files.base import ContentFile
8
9 from catalogue.test_utils import *
10 from catalogue import models, utils
11
12
13 class BookMediaTests(WLTestCase):
14
15     def setUp(self):
16         WLTestCase.setUp(self)
17         self.file = ContentFile(b'X')
18         self.file2 = ContentFile(b'Y')
19         self.book = models.Book.objects.create(slug='test-book', title='Test')
20
21     def set_title(self, title):
22         self.book.title = title
23         self.book.save()
24
25     def test_overwrite(self):
26         """
27             File gets overwritten with same filename on update.
28         """
29
30         bm = models.BookMedia(book=self.book, type='ogg', name="Some media")
31         self.set_title(bm.name)
32         bm.file.save(None, self.file)
33         bm.file.save(None, self.file2)
34
35         self.assertEqual(bm.file.read(), b'Y')
36         self.assertEqual(basename(bm.file.name), 'test-book.ogg')
37
38     def test_zip_audiobooks(self):
39         paths = [
40             (None, join(dirname(__file__), "files/fraszka-do-anusie.xml")),
41             (None, join(dirname(__file__), "files/fraszki.xml")),
42             ]
43
44         url = utils.create_zip(paths, 'test-zip-slug')
45         self.assertEqual("zip/test-zip-slug.zip", url)
46         self.assertTrue(exists(join(settings.MEDIA_ROOT, url)))
47
48         utils.remove_zip('test-zip-slug')
49         self.assertFalse(exists(join(settings.MEDIA_ROOT, url)))
50
51     def test_remove_zip_on_media_change(self):
52         bm = models.BookMedia(book=self.book, type='ogg', name="Title")
53         self.set_title(bm.name)
54         bm.file.save(None, self.file)
55         bm.save()
56
57         zip_url = self.book.zip_audiobooks('ogg')
58         self.assertEqual('zip/'+self.book.slug+'_ogg.zip', zip_url)
59         self.assertTrue(exists(join(settings.MEDIA_ROOT, zip_url)))
60
61         bm2 = models.BookMedia(book=self.book, type='ogg', name="Other title")
62         self.set_title(bm2.name)
63         bm2.file.save(None, self.file2)
64         self.set_title("Title")
65         bm2.save()
66         # was the audiobook zip deleted?
67         self.assertFalse(exists(join(settings.MEDIA_ROOT, zip_url)))