|
1
|
|
|
import json |
|
2
|
|
|
import tempfile |
|
3
|
|
|
from mock import Mock |
|
4
|
|
|
from PIL import Image |
|
5
|
|
|
|
|
6
|
|
|
from django.core.files.uploadedfile import SimpleUploadedFile |
|
7
|
|
|
|
|
8
|
|
|
from rest_framework import status |
|
9
|
|
|
from rest_framework.test import APITestCase, force_authenticate |
|
10
|
|
|
|
|
11
|
|
|
from sigma_core.tests.factories import UserFactory |
|
12
|
|
|
from sigma_core.serializers.user import DetailedUserSerializer as UserSerializer |
|
13
|
|
|
from sigma_files.models import ProfileImage |
|
14
|
|
|
from sigma_files.serializers import ProfileImageSerializer |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
class ProfileImageTests(APITestCase): |
|
18
|
|
|
@classmethod |
|
19
|
|
|
def setUpTestData(self): |
|
20
|
|
|
super(ProfileImageTests, self).setUpTestData() |
|
21
|
|
|
|
|
22
|
|
|
self.user = UserFactory() |
|
23
|
|
|
file = SimpleUploadedFile(name='test.jpg', content=Image.new('RGB', (15, 15)).tobytes(), content_type='image/jpeg') |
|
24
|
|
|
self.profile_image = ProfileImage.objects.create(file=file) |
|
25
|
|
|
|
|
26
|
|
|
self.user.photo = self.profile_image |
|
27
|
|
|
self.user.save() |
|
28
|
|
|
|
|
29
|
|
|
serializer = ProfileImageSerializer(self.profile_image) |
|
30
|
|
|
self.profile_data = serializer.data |
|
31
|
|
|
self.profiles_url = '/profile-image/' |
|
32
|
|
|
self.profile_url = self.profiles_url + '%d/' % self.profile_image.id |
|
33
|
|
|
|
|
34
|
|
|
def test_get_list_unauthed(self): |
|
35
|
|
|
# Client not authenticated |
|
36
|
|
|
response = self.client.get(self.profiles_url) |
|
37
|
|
|
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) |
|
38
|
|
|
|
|
39
|
|
|
def test_get_list_ok(self): |
|
40
|
|
|
self.client.force_authenticate(user=self.user) |
|
41
|
|
|
response = self.client.get(self.profiles_url) |
|
42
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK) |
|
43
|
|
|
|
|
44
|
|
|
def test_get_one_ok(self): |
|
45
|
|
|
self.client.force_authenticate(user=self.user) |
|
46
|
|
|
response = self.client.get(self.profile_url) |
|
47
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK) |
|
48
|
|
|
|
|
49
|
|
|
def test_create_unauthed(self): |
|
50
|
|
|
with open("sigma_files/test_img.png", "rb") as img: |
|
51
|
|
|
response = self.client.post(self.profiles_url, {'file': img}, format='multipart') |
|
52
|
|
|
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) |
|
53
|
|
|
|
|
54
|
|
|
def test_create_ok(self): |
|
55
|
|
|
self.client.force_authenticate(user=self.user) |
|
56
|
|
|
with open("sigma_files/test_img.png", "rb") as img: |
|
57
|
|
|
response = self.client.post(self.profiles_url, {'file': img}, format='multipart') |
|
58
|
|
|
self.assertEqual(response.status_code, status.HTTP_201_CREATED) |
|
59
|
|
|
|