tests.py 3.65 KB
Newer Older
Tom van Bussel's avatar
Tom van Bussel committed
1
2
from zipfile import ZipFile
from io import BytesIO
Thom Wiggers's avatar
Thom Wiggers committed
3

Tom van Bussel's avatar
Tom van Bussel committed
4
from django.test import Client, TestCase
Thom Wiggers's avatar
Thom Wiggers committed
5

Tom van Bussel's avatar
Tom van Bussel committed
6
7
from members.models import Member
from photos.models import Photo, Album
Thom Wiggers's avatar
Thom Wiggers committed
8

Tom van Bussel's avatar
Tom van Bussel committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

def create_zip(photos):
    output_file = BytesIO()
    with ZipFile(output_file, 'w') as zip_file:
        for photo in photos:
            zip_file.write(photo)
    output_file.seek(0)
    return output_file


class AlbumUploadTest(TestCase):
    """Tests album uploads in the admin."""

    fixtures = ['members.json']

    @classmethod
    def setUpTestData(cls):
        cls.member = Member.objects.filter(user__last_name="Wiggers").first()

    def setUp(self):
        self.client = Client()
        self.client.force_login(self.member.user)

    def test_album_upload(self):
        output_file = create_zip(["photos/fixtures/thom_assessor.png"])
        self.client.post('/admin/photos/album/add/',
                         {"title": "test album",
                          "date": "2017-04-12",
                          "slug": "2017-04-12-test-album",
                          "album_archive": output_file},
                         follow=True)

        self.assertEqual(Album.objects.all().count(), 1)
        self.assertEqual(Photo.objects.all().count(), 1)

    def test_album_create_album_twice(self):
        self.client.post('/admin/photos/album/add/',
                         {"title": "test album",
                          "date": "2017-04-12",
                          "slug": "2017-04-12-test-album"},
                         follow=True)
        self.client.post('/admin/photos/album/add/',
                         {"title": "test album",
                          "date": "2017-04-12",
                          "slug": "2017-04-12-test-album"},
                         follow=True)

        self.assertEqual(Album.objects.all().count(), 1)

    def test_album_upload_same_photo_twice_in_album(self):
        output_file = create_zip(["photos/fixtures/thom_assessor.png"])
        self.client.post('/admin/photos/album/add/',
                         {"title": "test album",
                          "date": "2017-04-12",
                          "slug": "2017-04-12-test-album",
                          "album_archive": output_file},
                         follow=True)

        pk = Photo.objects.first().pk
        self.client.post('/admin/photos/album/{}/change/'.format(pk),
                         {"title": "test album",
                          "date": "2017-04-12",
                          "slug": "2017-04-12-test-album",
                          "album_archive": output_file},
                         follow=True)

        self.assertEqual(Album.objects.all().count(), 1)
        self.assertEqual(Photo.objects.all().count(), 1)

    def test_album_upload_different_photo_in_album(self):
        output_file = create_zip(["photos/fixtures/thom_assessor.png"])
        self.client.post('/admin/photos/album/add/',
                         {"title": "test album",
                          "date": "2017-04-12",
                          "slug": "2017-04-12-test-album",
                          "album_archive": output_file},
                         follow=True)

        output_file = create_zip(["photos/fixtures/janbeleid-hoe.jpg"])
        pk = Photo.objects.first().pk
        self.client.post('/admin/photos/album/{}/change/'.format(pk),
                         {"title": "test album",
                          "date": "2017-04-12",
                          "slug": "2017-04-12-test-album",
                          "album_archive": output_file},
                         follow=True)

        self.assertEqual(Album.objects.all().count(), 1)
        self.assertEqual(Photo.objects.all().count(), 2)