Completed
Push — master ( a78a43...15b619 )
by Camille
52s
created

ImageTests.test_delete_forbidden()   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 7
rs 9.4285
1
import json
2
from PIL import Image as PIL_Image
3
4
from django.core.files.uploadedfile import SimpleUploadedFile
5
6
from rest_framework import status
7
from rest_framework.test import APITestCase, force_authenticate
8
9
from sigma_core.tests.factories import UserFactory
10
from sigma_core.serializers.user import DetailedUserSerializer as UserSerializer
11
from sigma_files.models import Image
12
from sigma_files.serializers import ImageSerializer_WithoutPerms, ImageSerializer
13
14
15
class ImageTests(APITestCase):
16
    @classmethod
17
    def setUpTestData(self):
18
        super(ImageTests, self).setUpTestData()
19
20
        self.user = UserFactory()
21
        self.other_user = UserFactory()
22
        file = SimpleUploadedFile(name='test.jpg', content=PIL_Image.new('RGB', (15, 15)).tobytes(), content_type='image/jpeg')
23
        self.profile_image = Image.objects.create(file=file, owner=self.user)
24
25
        self.user.photo = self.profile_image
26
        self.user.save()
27
28
        serializer = ImageSerializer_WithoutPerms(self.profile_image)
29
        self.profile_data = serializer.data
30
        self.profiles_url = '/image/'
31
        self.profile_url = self.profiles_url + '%d/' % self.profile_image.id
32
33
    def test_get_list_unauthed(self):
34
        # Client not authenticated
35
        response = self.client.get(self.profiles_url)
36
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
37
38
    def test_get_list_ok(self):
39
        self.client.force_authenticate(user=self.user)
40
        response = self.client.get(self.profiles_url)
41
        self.assertEqual(response.status_code, status.HTTP_200_OK)
42
43
    def test_get_one_ok(self):
44
        self.client.force_authenticate(user=self.user)
45
        response = self.client.get(self.profile_url)
46
        self.assertEqual(response.status_code, status.HTTP_200_OK)
47
48
    def test_create_unauthed(self):
49
        with open("sigma_files/test_img.png", "rb") as img:
50
             response = self.client.post(self.profiles_url, {'file': img}, format='multipart')
51
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
52
53
    def test_create_ok(self):
54
        self.client.force_authenticate(user=self.user)
55
        with open("sigma_files/test_img.png", "rb") as img:
56
             response = self.client.post(self.profiles_url, {'file': img}, format='multipart')
57
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
58
59
    def test_delete_forbidden(self):
60
        # Client wants to delete an image that he does not own
61
        self.client.force_authenticate(user=self.other_user)
62
        file = SimpleUploadedFile(name='test.jpg', content=PIL_Image.new('RGB', (15, 15)).tobytes(), content_type='image/jpeg')
63
        profile_image = Image.objects.create(file=file, owner=self.user)
64
        response = self.client.delete(self.profiles_url + "%d/" % profile_image.id)
65
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
66
67
    def test_delete_ok(self):
68
        # Client wants to delete an image that he owns
69
        self.client.force_authenticate(user=self.user)
70
        file = SimpleUploadedFile(name='test.jpg', content=PIL_Image.new('RGB', (15, 15)).tobytes(), content_type='image/jpeg')
71
        profile_image = Image.objects.create(file=file, owner=self.user)
72
        response = self.client.delete(self.profiles_url + "%d/" % profile_image.id)
73
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
74
        try:
75
            img = Image.objects.get(pk=profile_image.id)
76
        except Image.DoesNotExist:
77
            img = None # File has been deleted
78
        self.assertEqual(img, None)
79