/test/tinify_source_test.py

https://github.com/tinify/tinify-python · Python · 152 lines · 119 code · 32 blank · 1 comment · 14 complexity · d5bf31513e69a82643958f38cbdd73bf MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, division, print_function, unicode_literals
  3. import os
  4. import json
  5. import tempfile
  6. import tinify
  7. from tinify import Source, Result, ResultMeta, AccountError, ClientError
  8. from helper import *
  9. class TinifySourceWithInvalidApiKey(TestHelper):
  10. def setUp(self):
  11. super(type(self), self).setUp()
  12. tinify.key = 'invalid'
  13. httpretty.register_uri(httpretty.POST, 'https://api.tinify.com/shrink', **{
  14. 'status': 401
  15. })
  16. def test_from_file_should_raise_account_error(self):
  17. with self.assertRaises(AccountError):
  18. Source.from_file(dummy_file)
  19. def test_from_buffer_should_raise_account_error(self):
  20. with self.assertRaises(AccountError):
  21. Source.from_buffer('png file')
  22. def test_from_url_should_raise_account_error(self):
  23. with self.assertRaises(AccountError):
  24. Source.from_url('http://example.com/test.jpg')
  25. class TinifySourceWithValidApiKey(TestHelper):
  26. def setUp(self):
  27. super(type(self), self).setUp()
  28. tinify.key = 'valid'
  29. httpretty.register_uri(httpretty.POST, 'https://api.tinify.com/shrink', **{
  30. 'status': 201,
  31. 'location': 'https://api.tinify.com/some/location'
  32. })
  33. httpretty.register_uri(httpretty.GET, 'https://api.tinify.com/some/location', body=self.return_file)
  34. httpretty.register_uri(httpretty.POST, 'https://api.tinify.com/some/location', body=self.return_file)
  35. @staticmethod
  36. def return_file(request, uri, headers):
  37. if request.body:
  38. data = json.loads(request.body.decode('utf-8'))
  39. else:
  40. data = {}
  41. response = None
  42. if 'store' in data:
  43. headers['location'] = 'https://bucket.s3-region.amazonaws.com/some/location'
  44. response = json.dumps({'status': 'success'}).encode('utf-8')
  45. elif 'preserve' in data:
  46. response = b'copyrighted file'
  47. elif 'resize' in data:
  48. response = b'small file'
  49. else:
  50. response = b'compressed file'
  51. return (200, headers, response)
  52. def test_from_file_with_path_should_return_source(self):
  53. self.assertIsInstance(Source.from_file(dummy_file), Source)
  54. def test_from_file_with_path_should_return_source_with_data(self):
  55. self.assertEqual(b'compressed file', Source.from_file(dummy_file).to_buffer())
  56. def test_from_file_with_file_object_should_return_source(self):
  57. with open(dummy_file, 'rb') as f:
  58. self.assertIsInstance(Source.from_file(f), Source)
  59. def test_from_file_with_file_object_should_return_source_with_data(self):
  60. with open(dummy_file, 'rb') as f:
  61. self.assertEqual(b'compressed file', Source.from_file(f).to_buffer())
  62. def test_from_buffer_should_return_source(self):
  63. self.assertIsInstance(Source.from_buffer('png file'), Source)
  64. def test_from_buffer_should_return_source_with_data(self):
  65. self.assertEqual(b'compressed file', Source.from_buffer('png file').to_buffer())
  66. def test_from_url_should_return_source(self):
  67. self.assertIsInstance(Source.from_url('http://example.com/test.jpg'), Source)
  68. def test_from_url_should_return_source_with_data(self):
  69. self.assertEqual(b'compressed file', Source.from_url('http://example.com/test.jpg').to_buffer())
  70. def test_from_url_should_raise_error_when_server_doesnt_return_a_success(self):
  71. httpretty.register_uri(httpretty.POST, 'https://api.tinify.com/shrink',
  72. body='{"error":"Source not found","message":"Cannot parse URL"}',
  73. status=400,
  74. )
  75. with self.assertRaises(ClientError):
  76. Source.from_url('file://wrong')
  77. def test_result_should_return_result(self):
  78. self.assertIsInstance(Source.from_buffer('png file').result(), Result)
  79. def test_preserve_should_return_source(self):
  80. self.assertIsInstance(Source.from_buffer('png file').preserve("copyright", "location"), Source)
  81. self.assertEqual(b'png file', httpretty.last_request().body)
  82. def test_preserve_should_return_source_with_data(self):
  83. self.assertEqual(b'copyrighted file', Source.from_buffer('png file').preserve("copyright", "location").to_buffer())
  84. self.assertJsonEqual('{"preserve":["copyright","location"]}', httpretty.last_request().body.decode('utf-8'))
  85. def test_preserve_should_return_source_with_data_for_array(self):
  86. self.assertEqual(b'copyrighted file', Source.from_buffer('png file').preserve(["copyright", "location"]).to_buffer())
  87. self.assertJsonEqual('{"preserve":["copyright","location"]}', httpretty.last_request().body.decode('utf-8'))
  88. def test_preserve_should_return_source_with_data_for_tuple(self):
  89. self.assertEqual(b'copyrighted file', Source.from_buffer('png file').preserve(("copyright", "location")).to_buffer())
  90. self.assertJsonEqual('{"preserve":["copyright","location"]}', httpretty.last_request().body.decode('utf-8'))
  91. def test_preserve_should_include_other_options_if_set(self):
  92. self.assertEqual(b'copyrighted file', Source.from_buffer('png file').resize(width=400).preserve("copyright", "location").to_buffer())
  93. self.assertJsonEqual('{"preserve":["copyright","location"],"resize":{"width":400}}', httpretty.last_request().body.decode('utf-8'))
  94. def test_resize_should_return_source(self):
  95. self.assertIsInstance(Source.from_buffer('png file').resize(width=400), Source)
  96. self.assertEqual(b'png file', httpretty.last_request().body)
  97. def test_resize_should_return_source_with_data(self):
  98. self.assertEqual(b'small file', Source.from_buffer('png file').resize(width=400).to_buffer())
  99. self.assertJsonEqual('{"resize":{"width":400}}', httpretty.last_request().body.decode('utf-8'))
  100. def test_store_should_return_result_meta(self):
  101. self.assertIsInstance(Source.from_buffer('png file').store(service='s3'), ResultMeta)
  102. self.assertJsonEqual('{"store":{"service":"s3"}}', httpretty.last_request().body.decode('utf-8'))
  103. def test_store_should_return_result_meta_with_location(self):
  104. self.assertEqual('https://bucket.s3-region.amazonaws.com/some/location',
  105. Source.from_buffer('png file').store(service='s3').location)
  106. self.assertJsonEqual('{"store":{"service":"s3"}}', httpretty.last_request().body.decode('utf-8'))
  107. def test_store_should_include_other_options_if_set(self):
  108. self.assertEqual('https://bucket.s3-region.amazonaws.com/some/location', Source.from_buffer('png file').resize(width=400).store(service='s3').location)
  109. self.assertJsonEqual('{"store":{"service":"s3"},"resize":{"width":400}}', httpretty.last_request().body.decode('utf-8'))
  110. def test_to_buffer_should_return_image_data(self):
  111. self.assertEqual(b'compressed file', Source.from_buffer('png file').to_buffer())
  112. def test_to_file_with_path_should_store_image_data(self):
  113. with tempfile.TemporaryFile() as tmp:
  114. Source.from_buffer('png file').to_file(tmp)
  115. tmp.seek(0)
  116. self.assertEqual(b'compressed file', tmp.read())
  117. def test_to_file_with_file_object_should_store_image_data(self):
  118. with tempfile.NamedTemporaryFile() as tmp:
  119. Source.from_buffer('png file').to_file(tmp.name)
  120. self.assertEqual(b'compressed file', tmp.read())