Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

284 строки
11 KiB

  1. import mock
  2. from django_images.models import Thumbnail
  3. from taggit.models import Tag
  4. from tastypie.exceptions import Unauthorized
  5. from tastypie.test import ResourceTestCase
  6. from .helpers import ImageFactory, PinFactory, UserFactory
  7. from core.models import Pin, Image
  8. from users.models import User
  9. __all__ = ['ImageResourceTest', 'PinResourceTest']
  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):
  15. response = mock.Mock(content=open('logo.png', 'rb').read())
  16. return response
  17. class ImageResourceTest(ResourceTestCase):
  18. def test_post_create_unsupported(self):
  19. """Make sure that new images can't be created using API"""
  20. response = self.api_client.post('/api/v1/image/', format='json', data={})
  21. self.assertHttpUnauthorized(response)
  22. def test_list_detail(self):
  23. image = ImageFactory()
  24. thumbnail = filter_generator_for('thumbnail')(image)
  25. standard = filter_generator_for('standard')(image)
  26. square = filter_generator_for('square')(image)
  27. response = self.api_client.get('/api/v1/image/', format='json')
  28. self.assertDictEqual(self.deserialize(response)['objects'][0], {
  29. u'image': unicode(image.image.url),
  30. u'height': image.height,
  31. u'width': image.width,
  32. u'standard': {
  33. u'image': unicode(standard.image.url),
  34. u'width': standard.width,
  35. u'height': standard.height,
  36. },
  37. u'thumbnail': {
  38. u'image': unicode(thumbnail.image.url),
  39. u'width': thumbnail.width,
  40. u'height': thumbnail.height,
  41. },
  42. u'square': {
  43. u'image': unicode(square.image.url),
  44. u'width': square.width,
  45. u'height': square.height,
  46. },
  47. })
  48. class PinResourceTest(ResourceTestCase):
  49. def setUp(self):
  50. super(PinResourceTest, self).setUp()
  51. self.user = UserFactory(password='password')
  52. self.api_client.client.login(username=self.user.username, password='password')
  53. @mock.patch('requests.get', mock_requests_get)
  54. def test_post_create_url(self):
  55. url = 'http://testserver/mocked/logo.png'
  56. referer = 'http://testserver/'
  57. post_data = {
  58. 'submitter': '/api/v1/user/{}/'.format(self.user.pk),
  59. 'url': url,
  60. 'referer': referer,
  61. 'description': 'That\'s an Apple!'
  62. }
  63. response = self.api_client.post('/api/v1/pin/', data=post_data)
  64. self.assertHttpCreated(response)
  65. self.assertEqual(Pin.objects.count(), 1)
  66. self.assertEqual(Image.objects.count(), 1)
  67. # submitter is optional, current user will be used by default
  68. post_data = {
  69. 'url': url,
  70. 'description': 'That\'s an Apple!',
  71. 'origin': None
  72. }
  73. response = self.api_client.post('/api/v1/pin/', data=post_data)
  74. self.assertHttpCreated(response)
  75. @mock.patch('requests.get', mock_requests_get)
  76. def test_post_create_url_with_empty_tags(self):
  77. url = 'http://testserver/mocked/logo.png'
  78. referer = 'http://testserver/'
  79. post_data = {
  80. 'submitter': '/api/v1/user/{}/'.format(self.user.pk),
  81. 'url': url,
  82. 'referer': referer,
  83. 'description': 'That\'s an Apple!',
  84. 'tags': []
  85. }
  86. response = self.api_client.post('/api/v1/pin/', data=post_data)
  87. self.assertHttpCreated(response)
  88. self.assertEqual(Pin.objects.count(), 1)
  89. self.assertEqual(Image.objects.count(), 1)
  90. pin = Pin.objects.get(url=url)
  91. self.assertEqual(pin.tags.count(), 0)
  92. @mock.patch('requests.get', mock_requests_get)
  93. def test_post_create_url_unauthorized(self):
  94. url = 'http://testserver/mocked/logo.png'
  95. referer = 'http://testserver/'
  96. post_data = {
  97. 'submitter': '/api/v1/user/2/',
  98. 'url': url,
  99. 'referer': referer,
  100. 'description': 'That\'s an Apple!',
  101. 'tags': []
  102. }
  103. with self.assertRaises(Unauthorized):
  104. response = self.api_client.post('/api/v1/pin/', data=post_data)
  105. self.assertEqual(Pin.objects.count(), 0)
  106. self.assertEqual(Image.objects.count(), 0)
  107. @mock.patch('requests.get', mock_requests_get)
  108. def test_post_create_url_with_empty_origin(self):
  109. url = 'http://testserver/mocked/logo.png'
  110. referer = 'http://testserver/'
  111. post_data = {
  112. 'submitter': '/api/v1/user/{}/'.format(self.user.pk),
  113. 'url': url,
  114. 'referer': referer,
  115. 'description': 'That\'s an Apple!',
  116. 'origin': None
  117. }
  118. response = self.api_client.post('/api/v1/pin/', data=post_data)
  119. self.assertHttpCreated(response)
  120. self.assertEqual(Pin.objects.count(), 1)
  121. self.assertEqual(Image.objects.count(), 1)
  122. self.assertEqual(Pin.objects.get(url=url).origin, None)
  123. @mock.patch('requests.get', mock_requests_get)
  124. def test_post_create_url_with_origin(self):
  125. origin = 'http://testserver/mocked/'
  126. url = origin + 'logo.png'
  127. referer = 'http://testserver/'
  128. post_data = {
  129. 'submitter': '/api/v1/user/{}/'.format(self.user.pk),
  130. 'url': url,
  131. 'referer': referer,
  132. 'description': 'That\'s an Apple!',
  133. 'origin': origin
  134. }
  135. response = self.api_client.post('/api/v1/pin/', data=post_data)
  136. self.assertHttpCreated(response)
  137. self.assertEqual(Pin.objects.count(), 1)
  138. self.assertEqual(Image.objects.count(), 1)
  139. self.assertEqual(Pin.objects.get(url=url).origin, origin)
  140. def test_post_create_obj(self):
  141. image = ImageFactory()
  142. referer = 'http://testserver/'
  143. post_data = {
  144. 'submitter': '/api/v1/user/{}/'.format(self.user.pk),
  145. 'referer': referer,
  146. 'image': '/api/v1/image/{}/'.format(image.pk),
  147. 'description': 'That\'s something else (probably a CC logo)!',
  148. 'tags': ['random', 'tags'],
  149. }
  150. response = self.api_client.post('/api/v1/pin/', data=post_data)
  151. self.assertEqual(
  152. self.deserialize(response)['description'],
  153. 'That\'s something else (probably a CC logo)!'
  154. )
  155. self.assertHttpCreated(response)
  156. # A number of Image objects should stay the same as we are using an existing image
  157. self.assertEqual(Image.objects.count(), 1)
  158. self.assertEqual(Pin.objects.count(), 1)
  159. self.assertEquals(Tag.objects.count(), 2)
  160. def test_put_detail_unauthenticated(self):
  161. self.api_client.client.logout()
  162. uri = '/api/v1/pin/{}/'.format(PinFactory().pk)
  163. response = self.api_client.put(uri, format='json', data={})
  164. self.assertHttpUnauthorized(response)
  165. def test_put_detail_unauthorized(self):
  166. uri = '/api/v1/pin/{}/'.format(PinFactory(submitter=self.user).pk)
  167. user = UserFactory(password='password')
  168. self.api_client.client.login(username=user.username, password='password')
  169. response = self.api_client.put(uri, format='json', data={})
  170. self.assertHttpUnauthorized(response)
  171. def test_put_detail(self):
  172. pin = PinFactory(submitter=self.user)
  173. uri = '/api/v1/pin/{}/'.format(pin.pk)
  174. new = {'description': 'Updated description'}
  175. response = self.api_client.put(uri, format='json', data=new)
  176. self.assertHttpAccepted(response)
  177. self.assertEqual(Pin.objects.count(), 1)
  178. self.assertEqual(Pin.objects.get(pk=pin.pk).description, new['description'])
  179. def test_delete_detail_unauthenticated(self):
  180. uri = '/api/v1/pin/{}/'.format(PinFactory(submitter=self.user).pk)
  181. self.api_client.client.logout()
  182. self.assertHttpUnauthorized(self.api_client.delete(uri))
  183. def test_delete_detail_unauthorized(self):
  184. uri = '/api/v1/pin/{}/'.format(PinFactory(submitter=self.user).pk)
  185. User.objects.create_user('test', 'test@example.com', 'test')
  186. self.api_client.client.login(username='test', password='test')
  187. self.assertHttpUnauthorized(self.api_client.delete(uri))
  188. def test_delete_detail(self):
  189. uri = '/api/v1/pin/{}/'.format(PinFactory(submitter=self.user).pk)
  190. self.assertHttpAccepted(self.api_client.delete(uri))
  191. self.assertEqual(Pin.objects.count(), 0)
  192. def test_get_list_json_ordered(self):
  193. _, pin = PinFactory(), PinFactory()
  194. response = self.api_client.get('/api/v1/pin/', format='json', data={'order_by': '-id'})
  195. self.assertValidJSONResponse(response)
  196. self.assertEqual(self.deserialize(response)['objects'][0]['id'], pin.id)
  197. def test_get_list_json_filtered_by_tags(self):
  198. pin = PinFactory()
  199. response = self.api_client.get('/api/v1/pin/', format='json', data={'tag': pin.tags.all()[0]})
  200. self.assertValidJSONResponse(response)
  201. self.assertEqual(self.deserialize(response)['objects'][0]['id'], pin.pk)
  202. def test_get_list_json_filtered_by_submitter(self):
  203. pin = PinFactory(submitter=self.user)
  204. response = self.api_client.get('/api/v1/pin/', format='json', data={'submitter__username': self.user.username})
  205. self.assertValidJSONResponse(response)
  206. self.assertEqual(self.deserialize(response)['objects'][0]['id'], pin.pk)
  207. def test_get_list_json(self):
  208. image = ImageFactory()
  209. pin = PinFactory(**{
  210. 'submitter': self.user,
  211. 'image': image,
  212. 'referer': 'http://testserver/mocked/',
  213. 'url': 'http://testserver/mocked/logo.png',
  214. 'description': u'Mocked Description',
  215. 'origin': None
  216. })
  217. standard = filter_generator_for('standard')(image)
  218. thumbnail = filter_generator_for('thumbnail')(image)
  219. square = filter_generator_for('square')(image)
  220. response = self.api_client.get('/api/v1/pin/', format='json')
  221. self.assertValidJSONResponse(response)
  222. self.assertDictEqual(self.deserialize(response)['objects'][0], {
  223. u'id': pin.id,
  224. u'submitter': {
  225. u'username': unicode(self.user.username),
  226. u'gravatar': unicode(self.user.gravatar)
  227. },
  228. u'image': {
  229. u'image': unicode(image.image.url),
  230. u'width': image.width,
  231. u'height': image.height,
  232. u'standard': {
  233. u'image': unicode(standard.image.url),
  234. u'width': standard.width,
  235. u'height': standard.height,
  236. },
  237. u'thumbnail': {
  238. u'image': unicode(thumbnail.image.url),
  239. u'width': thumbnail.width,
  240. u'height': thumbnail.height,
  241. },
  242. u'square': {
  243. u'image': unicode(square.image.url),
  244. u'width': square.width,
  245. u'height': square.height,
  246. },
  247. },
  248. u'url': pin.url,
  249. u'origin': pin.origin,
  250. u'description': pin.description,
  251. u'tags': [tag.name for tag in pin.tags.all()]
  252. })