2019-02-22 15:26:15 +08:00
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
from django.urls import reverse
|
2013-03-02 11:06:53 -08:00
|
|
|
import mock
|
2019-02-22 15:26:15 +08:00
|
|
|
from rest_framework import status
|
|
|
|
|
from rest_framework.test import APITestCase
|
2013-02-26 23:20:50 +00:00
|
|
|
|
|
|
|
|
from django_images.models import Thumbnail
|
|
|
|
|
from taggit.models import Tag
|
2012-05-10 02:34:15 +00:00
|
|
|
|
2019-02-22 15:26:15 +08:00
|
|
|
from .helpers import create_image, create_user, create_pin
|
2018-02-08 22:44:55 -05:00
|
|
|
from core.models import Pin, Image
|
2012-05-10 02:34:15 +00:00
|
|
|
|
|
|
|
|
|
2019-02-22 15:26:15 +08:00
|
|
|
def mock_requests_get(url, **kwargs):
|
2019-12-08 21:02:49 +00:00
|
|
|
response = mock.Mock(content=open('docs/src/imgs/logo-dark.png', 'rb').read())
|
2013-03-19 15:41:55 +01:00
|
|
|
return response
|
2013-03-02 11:06:53 -08:00
|
|
|
|
|
|
|
|
|
2019-02-22 15:26:15 +08:00
|
|
|
class ImageTests(APITestCase):
|
2013-03-03 04:53:44 -08:00
|
|
|
def test_post_create_unsupported(self):
|
2019-02-22 15:26:15 +08:00
|
|
|
url = reverse("image-list")
|
|
|
|
|
data = {}
|
|
|
|
|
response = self.client.post(
|
|
|
|
|
url,
|
|
|
|
|
data=data,
|
|
|
|
|
format='json',
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(response.status_code, 403, response.data)
|
2012-05-10 02:34:15 +00:00
|
|
|
|
|
|
|
|
|
2019-02-22 15:26:15 +08:00
|
|
|
class PinTests(APITestCase):
|
|
|
|
|
_JSON_TYPE = "application/json"
|
2012-05-10 02:34:15 +00:00
|
|
|
|
|
|
|
|
def setUp(self):
|
2019-02-22 15:26:15 +08:00
|
|
|
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()
|
2013-02-26 23:20:50 +00:00
|
|
|
|
2013-03-19 15:41:55 +01:00
|
|
|
@mock.patch('requests.get', mock_requests_get)
|
2019-02-22 15:26:15 +08:00
|
|
|
def test_should_create_pin(self):
|
|
|
|
|
url = 'http://testserver.com/mocked/logo-01.png'
|
|
|
|
|
create_url = reverse("pin-list")
|
|
|
|
|
referer = 'http://testserver.com/'
|
2013-02-26 23:20:50 +00:00
|
|
|
post_data = {
|
2013-04-05 19:34:31 +02:00
|
|
|
'url': url,
|
2018-08-21 08:14:17 -07:00
|
|
|
'referer': referer,
|
2013-02-26 23:20:50 +00:00
|
|
|
'description': 'That\'s an Apple!'
|
|
|
|
|
}
|
2019-02-22 15:26:15 +08:00
|
|
|
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)
|
2013-04-05 19:34:31 +02:00
|
|
|
|
2013-03-19 15:41:55 +01:00
|
|
|
@mock.patch('requests.get', mock_requests_get)
|
2013-03-04 12:03:58 -08:00
|
|
|
def test_post_create_url_with_empty_tags(self):
|
2019-02-22 15:26:15 +08:00
|
|
|
url = 'http://testserver.com/mocked/logo-02.png'
|
|
|
|
|
create_url = reverse("pin-list")
|
|
|
|
|
referer = 'http://testserver.com/'
|
2013-03-03 06:20:50 -08:00
|
|
|
post_data = {
|
|
|
|
|
'url': url,
|
2018-08-21 08:14:17 -07:00
|
|
|
'referer': referer,
|
2013-03-03 06:20:50 -08:00
|
|
|
'description': 'That\'s an Apple!',
|
|
|
|
|
'tags': []
|
|
|
|
|
}
|
2019-02-22 15:26:15 +08:00
|
|
|
response = self.client.post(create_url, data=post_data, format="json")
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
response.status_code, status.HTTP_201_CREATED, response.json()
|
|
|
|
|
)
|
2013-03-04 12:03:58 -08:00
|
|
|
self.assertEqual(Image.objects.count(), 1)
|
2013-03-03 06:20:50 -08:00
|
|
|
pin = Pin.objects.get(url=url)
|
2019-02-22 15:26:15 +08:00
|
|
|
self.assertIsNotNone(pin.image.image)
|
2013-03-03 06:20:50 -08:00
|
|
|
self.assertEqual(pin.tags.count(), 0)
|
|
|
|
|
|
2019-02-22 15:26:15 +08:00
|
|
|
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/'
|
2013-03-03 08:24:26 -08:00
|
|
|
post_data = {
|
2018-08-21 08:14:17 -07:00
|
|
|
'referer': referer,
|
2019-02-22 15:26:15 +08:00
|
|
|
'image_by_id': image.pk,
|
2013-02-26 23:20:50 +00:00
|
|
|
'description': 'That\'s something else (probably a CC logo)!',
|
|
|
|
|
'tags': ['random', 'tags'],
|
|
|
|
|
}
|
2019-02-22 15:26:15 +08:00
|
|
|
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)
|
2014-04-03 11:20:33 +02:00
|
|
|
self.assertEqual(
|
2019-02-22 15:26:15 +08:00
|
|
|
resp_data['description'],
|
|
|
|
|
'That\'s something else (probably a CC logo)!',
|
|
|
|
|
resp_data
|
2014-04-03 11:20:33 +02:00
|
|
|
)
|
2019-02-22 15:26:15 +08:00
|
|
|
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})
|
2013-03-02 17:00:58 -08:00
|
|
|
new = {'description': 'Updated description'}
|
|
|
|
|
|
2019-02-22 15:26:15 +08:00
|
|
|
response = self.client.patch(
|
|
|
|
|
uri, new, format="json",
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK, response.json())
|
2013-03-04 12:03:58 -08:00
|
|
|
self.assertEqual(Pin.objects.count(), 1)
|
|
|
|
|
self.assertEqual(Pin.objects.get(pk=pin.pk).description, new['description'])
|
2013-03-02 17:00:58 -08:00
|
|
|
|
|
|
|
|
def test_delete_detail_unauthenticated(self):
|
2019-02-22 15:26:15 +08:00
|
|
|
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)
|
2013-03-02 17:00:58 -08:00
|
|
|
|
|
|
|
|
def test_delete_detail(self):
|
2019-02-22 15:26:15 +08:00
|
|
|
image = create_image()
|
|
|
|
|
pin = create_pin(self.user, image, [])
|
|
|
|
|
uri = reverse("pin-detail", kwargs={"pk": pin.pk})
|
|
|
|
|
self.client.delete(uri)
|
2013-03-04 12:03:58 -08:00
|
|
|
self.assertEqual(Pin.objects.count(), 0)
|