No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import json
  2. from django.urls import reverse
  3. import mock
  4. from rest_framework import status
  5. from rest_framework.test import APITestCase
  6. from django_images.models import Thumbnail
  7. from taggit.models import Tag
  8. from .helpers import create_image, create_user, create_pin
  9. from core.models import Pin, Image
  10. def filter_generator_for(size):
  11. def wrapped_func(obj):
  12. return Thumbnail.objects.get_or_create_at_size(obj.pk, size)
  13. return wrapped_func
  14. def mock_requests_get(url, **kwargs):
  15. response = mock.Mock(content=open('logo.png', 'rb').read())
  16. return response
  17. class ImageTests(APITestCase):
  18. def test_post_create_unsupported(self):
  19. url = reverse("image-list")
  20. data = {}
  21. response = self.client.post(
  22. url,
  23. data=data,
  24. format='json',
  25. )
  26. self.assertEqual(response.status_code, 403, response.data)
  27. class PinTests(APITestCase):
  28. _JSON_TYPE = "application/json"
  29. def setUp(self):
  30. super(PinTests, self).setUp()
  31. self.user = create_user("default")
  32. self.client.login(username=self.user.username, password='password')
  33. def tearDown(self):
  34. Pin.objects.all().delete()
  35. Image.objects.all().delete()
  36. Tag.objects.all().delete()
  37. @mock.patch('requests.get', mock_requests_get)
  38. def test_should_create_pin(self):
  39. url = 'http://testserver.com/mocked/logo-01.png'
  40. create_url = reverse("pin-list")
  41. referer = 'http://testserver.com/'
  42. post_data = {
  43. 'url': url,
  44. 'referer': referer,
  45. 'description': 'That\'s an Apple!'
  46. }
  47. response = self.client.post(create_url, data=post_data, format="json")
  48. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  49. pin = Pin.objects.get(url=url)
  50. self.assertIsNotNone(pin.image.image)
  51. @mock.patch('requests.get', mock_requests_get)
  52. def test_post_create_url_with_empty_tags(self):
  53. url = 'http://testserver.com/mocked/logo-02.png'
  54. create_url = reverse("pin-list")
  55. referer = 'http://testserver.com/'
  56. post_data = {
  57. 'url': url,
  58. 'referer': referer,
  59. 'description': 'That\'s an Apple!',
  60. 'tags': []
  61. }
  62. response = self.client.post(create_url, data=post_data, format="json")
  63. self.assertEqual(
  64. response.status_code, status.HTTP_201_CREATED, response.json()
  65. )
  66. self.assertEqual(Image.objects.count(), 1)
  67. pin = Pin.objects.get(url=url)
  68. self.assertIsNotNone(pin.image.image)
  69. self.assertEqual(pin.tags.count(), 0)
  70. def test_should_post_create_pin_with_existed_image(self):
  71. image = create_image()
  72. create_pin(self.user, image=image, tags=[])
  73. create_url = reverse("pin-list")
  74. referer = 'http://testserver.com/'
  75. post_data = {
  76. 'referer': referer,
  77. 'image_by_id': image.pk,
  78. 'description': 'That\'s something else (probably a CC logo)!',
  79. 'tags': ['random', 'tags'],
  80. }
  81. response = self.client.post(create_url, data=post_data, format="json")
  82. resp_data = response.json()
  83. self.assertEqual(response.status_code, status.HTTP_201_CREATED, resp_data)
  84. self.assertEqual(
  85. resp_data['description'],
  86. 'That\'s something else (probably a CC logo)!',
  87. resp_data
  88. )
  89. self.assertEquals(Pin.objects.count(), 2)
  90. def test_patch_detail_unauthenticated(self):
  91. image = create_image()
  92. pin = create_pin(self.user, image, [])
  93. self.client.logout()
  94. uri = reverse("pin-detail", kwargs={"pk": pin.pk})
  95. response = self.client.patch(uri, format='json', data={})
  96. self.assertEqual(response.status_code, 403)
  97. def test_patch_detail(self):
  98. image = create_image()
  99. pin = create_pin(self.user, image, [])
  100. uri = reverse("pin-detail", kwargs={"pk": pin.pk})
  101. new = {'description': 'Updated description'}
  102. response = self.client.patch(
  103. uri, new, format="json",
  104. )
  105. self.assertEqual(response.status_code, status.HTTP_200_OK, response.json())
  106. self.assertEqual(Pin.objects.count(), 1)
  107. self.assertEqual(Pin.objects.get(pk=pin.pk).description, new['description'])
  108. def test_delete_detail_unauthenticated(self):
  109. image = create_image()
  110. pin = create_pin(self.user, image, [])
  111. uri = reverse("pin-detail", kwargs={"pk": pin.pk})
  112. self.client.logout()
  113. self.assertEqual(self.client.delete(uri).status_code, 403)
  114. def test_delete_detail(self):
  115. image = create_image()
  116. pin = create_pin(self.user, image, [])
  117. uri = reverse("pin-detail", kwargs={"pk": pin.pk})
  118. self.client.delete(uri)
  119. self.assertEqual(Pin.objects.count(), 0)