Tests

mail@pastecode.io avatar
unknown
plain_text
a year ago
11 kB
3
Indexable
Never
import pytest
import io
from PIL import Image

from django.urls import reverse
from django.contrib.auth import get_user_model
from django.core.files.uploadedfile import SimpleUploadedFile
from rest_framework.test import APIClient, APITestCase

from photos.models import Photos


def create_user(**params):
    return get_user_model().objects.create(**params)


def generate_temporary_image(ext):
    bts = io.BytesIO()
    img = Image.new("RGB", (10, 10))
    img.save(bts, ext)
    return SimpleUploadedFile("test.jpg", bts.getvalue())


def create_photo_record(**params):
    return Photos.objects.create(**params)


@pytest.mark.django_db
class TestPhotosFunctionalitiesForNonAuthenticatedUser(APITestCase):
    def setUp(self):
        self.client = APIClient()

    def test_view_photo(self):
        response = self.client.post(reverse("photos-v1:photos-list"))
        assert response.status_code == 401

    def test_upload_new_photo_record(self):
        payload = {"photo_type": "original", "photo": generate_temporary_image("jpeg")}

        response = self.client.post(
            reverse("photos-v1:photos-list"), payload, format="multipart"
        )
        assert response.status_code == 401

    def test_partial_update_photo_record(self):
        payload = {"photo_type": "original", "photo": generate_temporary_image("jpeg")}

        response = self.client.patch(
            reverse("photos-v1:photos-detail", kwargs={"pk": "1"}),
            payload,
            format="multipart",
        )

        assert response.status_code == 401

    def test_update_photo_record(self):
        payload = {"photo_type": "original", "photo": self.temporary_image("jpeg")}

        response = self.client.put(
            reverse("photos-v1:photos-detail", kwargs={"pk": "1"}),
            payload,
            format="multipart",
        )

        assert response.status_code == 401

    def test_update_photo_record(self):
        response = self.client.delete(
            reverse("photos-v1:photos-detail", kwargs={"pk": "1"}),
            format="multipart",
        )

        assert response.status_code == 401


@pytest.mark.django_db
class TestPhotosFunctionalitiesForAuthenticatedUsers(APITestCase):
    def setUp(self):
        self.client = APIClient()
        self.user = create_user(
            name="Test",
            email="test@example.com",
            password="testpass",
            last_name="Test",
            phone_number="09300000000",
            privacy_policy=True,
            user_type="owner",
            terms_condition=True,
            username="test",
        )
        self.client.force_authenticate(self.user)
        self.photo_record1 = create_photo_record(
            user=self.user,
            photo_type="original",
            photo=generate_temporary_image("jpeg"),
        )

    def tearDown(self):
        self.photo_record1.photo.delete()

    def test_view_all_photo_records(self):
        response = self.client.get(reverse("photos-v1:photos-list"))
        assert response.status_code == 200
        assert response.data.get("count") == 1

    def test_retrieve_specific_photo_record(self):
        response = self.client.get(
            reverse("photos-v1:photos-detail", kwargs={"pk": self.photo_record1.id})
        )
        assert response.status_code == 200
        assert response.data.get("id") == self.photo_record1.id

    def test_upload_new_photo_record(self):
        payload = {"photo_type": "original", "photo": generate_temporary_image("jpeg")}

        response = self.client.post(
            reverse("photos-v1:photos-list"), payload, format="multipart"
        )
        assert response.status_code == 201
        assert response.data.get("user") == self.user.id
        assert 'photo' in response.data

        Photos.objects.get(id=response.data.get("id")).photo.delete()

        response = self.client.get(reverse("photos-v1:photos-list"))
        assert response.status_code == 200
        assert response.data.get("count") == 2

    def test_upload_without_attachment(self):
        payload = {"photo_type": "original", "photo": ""}

        response = self.client.post(
            reverse("photos-v1:photos-list"), payload, format="multipart"
        )
        assert response.status_code == 400

    def test_upload_non_image_file(self):
        with pytest.raises(KeyError) as exc_info:
            payload = {"photo_type": "original", "photo": generate_temporary_image("txt")}

            response = self.client.post(
                reverse("photos-v1:photos-list"), payload, format="multipart"
            )

        assert exc_info.type is KeyError

    def test_upload_incorrect_photo_type(self):
        payload = {
            "photo_type": "incorrect_photo_type",
            "photo": generate_temporary_image("jpeg"),
        }

        response = self.client.post(
            reverse("photos-v1:photos-list"), payload, format="multipart"
        )
        assert response.status_code == 400

    def test_partial_update_photo_record(self):
        photo_id = self.photo_record1.id
        photo_url = self.photo_record1.photo

        payload = {"photo_type": "edit", "photo": generate_temporary_image("jpeg")}

        response = self.client.patch(
            reverse("photos-v1:photos-detail", kwargs={"pk": photo_id}),
            payload,
            format="multipart",
        )

        assert response.status_code == 200
        assert response.data.get("id") == photo_id
        assert response.data.get("photo_type") == payload.get("photo_type")
        assert response.data.get("photo") != photo_url

        Photos.objects.get(id=response.data.get("id")).photo.delete()

    def test_update_photo_record(self):
        photo_id = self.photo_record1.id
        photo_url = self.photo_record1.photo

        payload = {"photo_type": "edit", "photo": generate_temporary_image("jpeg")}

        response = self.client.put(
            reverse("photos-v1:photos-detail", kwargs={"pk": photo_id}),
            payload,
            format="multipart",
        )
        assert response.status_code == 200
        assert response.data.get("id") == photo_id
        assert response.data.get("photo_type") == payload.get("photo_type")
        assert response.data.get("photo") != photo_url

        Photos.objects.get(id=response.data.get("id")).photo.delete()

    def test_delete_photo_record(self):
        photo_record_2 = create_photo_record(
            user=self.user,
            photo_type="original",
            photo=generate_temporary_image("jpeg"),
        )

        response = self.client.get(reverse("photos-v1:photos-list"))
        assert response.status_code == 200
        assert response.data.get("count") == 2

        photo_record_2.photo.delete()

        response = self.client.delete(
            reverse(
                "photos-v1:photos-detail", kwargs={"pk": photo_record_2.id}
            ),
            format="multipart",
        )
        assert response.status_code == 204

        response = self.client.get(reverse("photos-v1:photos-list"))
        assert response.status_code == 200
        assert response.data.get("count") == 1

    def test_limit_photos_to_own_user(self):
        new_user = create_user(
            name="Test2",
            email="test2@example.com",
            password="testpass2",
            last_name="Test2",
            phone_number="09300000000",
            privacy_policy=True,
            user_type="owner",
            terms_condition=True,
            username="test2username",
        )

        new_photo_record_1 = create_photo_record(
            user=new_user,
            photo_type="original",
            photo=generate_temporary_image("jpeg"),
        )

        new_photo_record_2 = create_photo_record(
            user=new_user,
            photo_type="original",
            photo=generate_temporary_image("jpeg"),
        )

        new_photo_record_1.photo.delete()
        new_photo_record_2.photo.delete()

        response = self.client.get(reverse("photos-v1:photos-list"))
        assert response.status_code == 200
        assert response.data.get("count") == 1


@pytest.mark.django_db
class TestPhotosFilter(APITestCase):
    def setUp(self):
        self.client = APIClient()
        self.user = create_user(
            name="Test",
            email="test@example.com",
            password="testpass",
            last_name="Test",
            phone_number="09300000000",
            privacy_policy=True,
            user_type="owner",
            terms_condition=True,
        )
        self.client.force_authenticate(self.user)
        self.photo_record1 = create_photo_record(
            user=self.user,
            photo_type="original",
            photo=generate_temporary_image("jpeg"),
        )
        self.photo_record2 = create_photo_record(
            user=self.user,
            photo_type="original",
            photo=generate_temporary_image("jpeg"),
        )
        self.photo_record3 = create_photo_record(
            user=self.user,
            photo_type="original",
            photo=generate_temporary_image("jpeg"),
        )
        self.photo_record4 = create_photo_record(
            user=self.user, photo_type="edit", photo=generate_temporary_image("jpeg")
        )
        self.photo_record5 = create_photo_record(
            user=self.user, photo_type="edit", photo=generate_temporary_image("jpeg")
        )
        self.photo_record6 = create_photo_record(
            user=self.user, photo_type="collage", photo=generate_temporary_image("jpeg")
        )

    def tearDown(self):
        self.photo_record1.photo.delete()
        self.photo_record2.photo.delete()
        self.photo_record3.photo.delete()
        self.photo_record4.photo.delete()
        self.photo_record5.photo.delete()
        self.photo_record6.photo.delete()

    def test_filter_by_photo_type(self):
        payload = {"photo_type": Photos.PHOTO_TYPE_ORIGINAL}
        response = self.client.get(
            reverse("photos-v1:photos-list"),
            payload,
            format="multipart",
        )
        assert response.status_code == 200
        assert response.data.get("count") == 3

        payload = {"photo_type": Photos.PHOTO_TYPE_EDIT}
        response = self.client.get(
            reverse("photos-v1:photos-list"),
            payload,
            format="multipart",
        )
        assert response.status_code == 200
        assert response.data.get("count") == 2

        payload = {"photo_type": Photos.PHOTO_TYPE_COLLAGE}
        response = self.client.get(
            reverse("photos-v1:photos-list"),
            payload,
            format="multipart",
        )
        assert response.status_code == 200
        assert response.data.get("count") == 1

    def test_filter_incorrect_photo_type(self):
        payload = {"photo_type": "incorrect_photo_type"}
        response = self.client.get(
            reverse("photos-v1:photos-list"),
            payload,
            format="multipart",
        )
        assert response.status_code == 400