25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

api.py 4.7 KiB

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