Completed
Pull Request — master (#21)
by Camille
50s
created

ProfileImageTests.test_create_ok()   A

Complexity

Conditions 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
dl 0
loc 5
rs 9.4285
1
import json
2
from PIL import 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 ProfileImage
12
from sigma_files.serializers import ProfileImageSerializer
13
14
15
class ProfileImageTests(APITestCase):
16
    @classmethod
17
    def setUpTestData(self):
18
        super(ProfileImageTests, self).setUpTestData()
19
20
        self.user = UserFactory()
21
        file = SimpleUploadedFile(name='test.jpg', content=Image.new('RGB', (15, 15)).tobytes(), content_type='image/jpeg')
22
        self.profile_image = ProfileImage.objects.create(file=file)
23
24
        self.user.photo = self.profile_image
25
        self.user.save()
26
27
        serializer = ProfileImageSerializer(self.profile_image)
28
        self.profile_data = serializer.data
29
        self.profiles_url = '/profile-image/'
30
        self.profile_url = self.profiles_url + '%d/' % self.profile_image.id
31
32
    def test_get_list_unauthed(self):
33
        # Client not authenticated
34
        response = self.client.get(self.profiles_url)
35
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
36
37
    def test_get_list_ok(self):
38
        self.client.force_authenticate(user=self.user)
39
        response = self.client.get(self.profiles_url)
40
        self.assertEqual(response.status_code, status.HTTP_200_OK)
41
42
    def test_get_one_ok(self):
43
        self.client.force_authenticate(user=self.user)
44
        response = self.client.get(self.profile_url)
45
        self.assertEqual(response.status_code, status.HTTP_200_OK)
46
47
    def test_create_unauthed(self):
48
        with open("sigma_files/test_img.png", "rb") as img:
49
             response = self.client.post(self.profiles_url, {'file': img}, format='multipart')
50
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
51
52
    def test_create_ok(self):
53
        self.client.force_authenticate(user=self.user)
54
        with open("sigma_files/test_img.png", "rb") as img:
55
             response = self.client.post(self.profiles_url, {'file': img}, format='multipart')
56
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
57