|
| 1 | +from django.test import TestCase |
| 2 | +from django.urls import reverse |
| 3 | +from rest_framework import status |
| 4 | +from rest_framework.test import APIClient, APITestCase |
| 5 | +from rest_framework_simplejwt.tokens import AccessToken |
| 6 | + |
| 7 | +from apps.category.models import Category |
| 8 | +from apps.like.models import Like |
| 9 | +from apps.product.models import Product |
| 10 | +from apps.user.models import Account |
| 11 | + |
| 12 | + |
| 13 | +class TestLikeListCreateView(APITestCase): |
| 14 | + def setUp(self) -> None: |
| 15 | + # self.client = APIClient() |
| 16 | + self.url = reverse("likes") |
| 17 | + data = { |
| 18 | + |
| 19 | + "password": "fels3570", |
| 20 | + "nickname": "nick", |
| 21 | + "phone": "1234", |
| 22 | + } |
| 23 | + self.user = Account.objects.create_user(**data) |
| 24 | + # self.client.force_login(user=self.user) |
| 25 | + # self.token = AccessToken.for_user(self.user) |
| 26 | + self.category = Category.objects.create(name="test category") |
| 27 | + self.product = Product.objects.create( |
| 28 | + name="test product", |
| 29 | + lender=self.user, |
| 30 | + brand="test brand", |
| 31 | + condition="good", |
| 32 | + purchase_date="2024-01-01", |
| 33 | + purchase_price=10000, |
| 34 | + rental_fee=1000, |
| 35 | + size="xs", |
| 36 | + product_category=self.category, |
| 37 | + ) |
| 38 | + |
| 39 | + def test_list_likes(self) -> None: |
| 40 | + self.client.force_authenticate(user=self.user) |
| 41 | + Like.objects.create(user=self.user, product=self.product) |
| 42 | + res = self.client.get(self.url) |
| 43 | + self.assertEqual(res.status_code, status.HTTP_200_OK) |
| 44 | + self.assertEqual(res.data.get("count"), 1) |
| 45 | + |
| 46 | + def test_create_like(self) -> None: |
| 47 | + self.client.force_authenticate(user=self.user) |
| 48 | + data = {"product_id": self.product.uuid} |
| 49 | + res = self.client.post(self.url, data, format="json") |
| 50 | + self.assertEqual(res.status_code, status.HTTP_201_CREATED) |
| 51 | + self.assertTrue(Like.objects.filter(user=self.user, product=self.product).exists()) |
| 52 | + |
| 53 | + def test_create_like_without_product_id(self) -> None: |
| 54 | + self.client.force_authenticate(user=self.user) |
| 55 | + res = self.client.post(self.url, {}) |
| 56 | + self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) |
| 57 | + self.assertFalse(Like.objects.filter(user=self.user, product=self.product).exists()) |
| 58 | + |
| 59 | + def test_create_like_already_exists(self) -> None: |
| 60 | + self.client.force_authenticate(user=self.user) |
| 61 | + Like.objects.create(user=self.user, product=self.product) |
| 62 | + data = {"product_id": self.product.uuid} |
| 63 | + res = self.client.post(self.url, data, format="json") |
| 64 | + self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) |
| 65 | + self.assertEqual(res.data, ["Already liked this product."]) |
| 66 | + |
| 67 | + def test_create_list_without_login(self) -> None: |
| 68 | + data = {"product_id": self.product.uuid} |
| 69 | + res = self.client.post(self.url, data, format="json") |
| 70 | + self.assertEqual(res.status_code, status.HTTP_401_UNAUTHORIZED) |
| 71 | + |
| 72 | + |
| 73 | +class TestLikeDestroyView(APITestCase): |
| 74 | + def setUp(self) -> None: |
| 75 | + data = { |
| 76 | + |
| 77 | + "password": "fels3570", |
| 78 | + "nickname": "nick", |
| 79 | + "phone": "1234", |
| 80 | + } |
| 81 | + self.user = Account.objects.create_user(**data) |
| 82 | + self.category = Category.objects.create(name="test category") |
| 83 | + self.product = Product.objects.create( |
| 84 | + name="test product", |
| 85 | + lender=self.user, |
| 86 | + brand="test brand", |
| 87 | + condition="good", |
| 88 | + purchase_date="2024-01-01", |
| 89 | + purchase_price=10000, |
| 90 | + rental_fee=1000, |
| 91 | + size="xs", |
| 92 | + product_category=self.category, |
| 93 | + likes=1, |
| 94 | + ) |
| 95 | + self.like = Like.objects.create(user=self.user, product=self.product) |
| 96 | + self.url = reverse("like_delete", kwargs={"pk": self.product.pk}) |
| 97 | + |
| 98 | + def test_delete_like(self) -> None: |
| 99 | + self.client.force_authenticate(user=self.user) |
| 100 | + res = self.client.delete(self.url) |
| 101 | + self.assertEqual(res.status_code, status.HTTP_204_NO_CONTENT) |
| 102 | + self.assertFalse(Like.objects.filter(user=self.user, product=self.product).exists()) |
| 103 | + |
| 104 | + # def test_like_count_decreased(self) -> None: |
| 105 | + # self.client.force_authenticate(user=self.user) |
| 106 | + # initial_count = self.product.likes |
| 107 | + # res = self.client.delete(self.url) |
| 108 | + # self.assertEqual(res.status_code, status.HTTP_204_NO_CONTENT) |
| 109 | + # self.assertEqual(self.product.likes, initial_count - 1) |
| 110 | + |
| 111 | + def test_delete_like_not_found(self) -> None: |
| 112 | + self.client.force_authenticate(user=self.user) |
| 113 | + self.like.delete() |
| 114 | + res = self.client.delete(self.url) |
| 115 | + self.assertEqual(res.status_code, status.HTTP_404_NOT_FOUND) |
| 116 | + |
| 117 | + def test_delete_like_by_other_user(self) -> None: |
| 118 | + data = { |
| 119 | + |
| 120 | + "password": "fels3570", |
| 121 | + "nickname": "dfk", |
| 122 | + "phone": "1234", |
| 123 | + } |
| 124 | + other_user = Account.objects.create_user(**data) |
| 125 | + self.client.force_authenticate(other_user) |
| 126 | + res = self.client.delete(self.url) |
| 127 | + self.assertEqual(res.status_code, status.HTTP_404_NOT_FOUND) |
| 128 | + |
| 129 | + |
| 130 | +class TestPermission(APITestCase): |
| 131 | + pass |
0 commit comments