|
- import json
-
- from django.urls import reverse
- import mock
- from rest_framework import status
- from rest_framework.test import APITestCase
-
- from django_images.models import Thumbnail
- from taggit.models import Tag
-
- from .helpers import create_image, create_user, create_pin
- from core.models import Pin, Image
-
-
- def mock_requests_get(url, **kwargs):
- response = mock.Mock(content=open('docs/src/imgs/logo-dark.png', 'rb').read())
- return response
-
-
- class ImageTests(APITestCase):
- def test_post_create_unsupported(self):
- url = reverse("image-list")
- data = {}
- response = self.client.post(
- url,
- data=data,
- format='json',
- )
- self.assertEqual(response.status_code, 403, response.data)
-
-
- class PinTests(APITestCase):
- _JSON_TYPE = "application/json"
-
- def setUp(self):
- super(PinTests, self).setUp()
- self.user = create_user("default")
- self.client.login(username=self.user.username, password='password')
-
- def tearDown(self):
- Pin.objects.all().delete()
- Image.objects.all().delete()
- Tag.objects.all().delete()
-
- @mock.patch('requests.get', mock_requests_get)
- def test_should_create_pin(self):
- url = 'http://testserver.com/mocked/logo-01.png'
- create_url = reverse("pin-list")
- referer = 'http://testserver.com/'
- post_data = {
- 'url': url,
- 'referer': referer,
- 'description': 'That\'s an Apple!'
- }
- response = self.client.post(create_url, data=post_data, format="json")
- self.assertEqual(response.status_code, status.HTTP_201_CREATED)
- pin = Pin.objects.get(url=url)
- self.assertIsNotNone(pin.image.image)
-
- @mock.patch('requests.get', mock_requests_get)
- def test_post_create_url_with_empty_tags(self):
- url = 'http://testserver.com/mocked/logo-02.png'
- create_url = reverse("pin-list")
- referer = 'http://testserver.com/'
- post_data = {
- 'url': url,
- 'referer': referer,
- 'description': 'That\'s an Apple!',
- 'tags': []
- }
- response = self.client.post(create_url, data=post_data, format="json")
- self.assertEqual(
- response.status_code, status.HTTP_201_CREATED, response.json()
- )
- self.assertEqual(Image.objects.count(), 1)
- pin = Pin.objects.get(url=url)
- self.assertIsNotNone(pin.image.image)
- self.assertEqual(pin.tags.count(), 0)
-
- def test_should_post_create_pin_with_existed_image(self):
- image = create_image()
- create_pin(self.user, image=image, tags=[])
- create_url = reverse("pin-list")
- referer = 'http://testserver.com/'
- post_data = {
- 'referer': referer,
- 'image_by_id': image.pk,
- 'description': 'That\'s something else (probably a CC logo)!',
- 'tags': ['random', 'tags'],
- }
- response = self.client.post(create_url, data=post_data, format="json")
- resp_data = response.json()
- self.assertEqual(response.status_code, status.HTTP_201_CREATED, resp_data)
- self.assertEqual(
- resp_data['description'],
- 'That\'s something else (probably a CC logo)!',
- resp_data
- )
- self.assertEquals(Pin.objects.count(), 2)
-
- def test_patch_detail_unauthenticated(self):
- image = create_image()
- pin = create_pin(self.user, image, [])
- self.client.logout()
- uri = reverse("pin-detail", kwargs={"pk": pin.pk})
- response = self.client.patch(uri, format='json', data={})
- self.assertEqual(response.status_code, 403)
-
- def test_patch_detail(self):
- image = create_image()
- pin = create_pin(self.user, image, [])
- uri = reverse("pin-detail", kwargs={"pk": pin.pk})
- new = {'description': 'Updated description'}
-
- response = self.client.patch(
- uri, new, format="json",
- )
- self.assertEqual(response.status_code, status.HTTP_200_OK, response.json())
- self.assertEqual(Pin.objects.count(), 1)
- self.assertEqual(Pin.objects.get(pk=pin.pk).description, new['description'])
-
- def test_delete_detail_unauthenticated(self):
- image = create_image()
- pin = create_pin(self.user, image, [])
- uri = reverse("pin-detail", kwargs={"pk": pin.pk})
- self.client.logout()
- self.assertEqual(self.client.delete(uri).status_code, 403)
-
- def test_delete_detail(self):
- image = create_image()
- pin = create_pin(self.user, image, [])
- uri = reverse("pin-detail", kwargs={"pk": pin.pk})
- self.client.delete(uri)
- self.assertEqual(Pin.objects.count(), 0)
|