def test_raises(self, request): request.return_value = MockResponse(404, '{}') ucare = UploadCare('pub', 'secret') with self.assertRaises(UploadCareException): ucare.make_request('GET', '/files/') request.return_value = MockResponse(200, 'meh') with self.assertRaises(ValueError) as cm: ucare.make_request('GET', '/files/') self.assertEqual('no json in response', cm.exception.message)
def test_ensure_on_cdn_raises(self, request, head): ucare = UploadCare('pub', 'secret') uuid = '6c5e9526-b0fe-4739-8975-72e8d5ee6342' f = ucare.file(uuid) api_response = MockResponse(200, '{"on_s3": true, "last_keep_claim": true}') request.return_value = api_response cdn_response = MockResponse(404) head.return_value = cdn_response with self.assertRaises(Exception) as cm: f.store(wait=True, timeout=0.1) self.assertEqual('timed out waiting for file appear on cdn', cm.exception.message)
def test_url_caching(self, request): """Test that known url is cached and no requests are made""" ucare = UploadCare('pub', 'secret') uuid = '6c5e9526-b0fe-4739-8975-72e8d5ee6342' request.return_value = MockResponse(200, '{"original_file_url": "meh"}') self.assertEqual(0, len(request.mock_calls)) f = ucare.file(uuid) self.assertEqual('meh', f.url) self.assertEqual(1, len(request.mock_calls)) fake_url = 'http://i-am-the-file/{0}/'.format(uuid) f = ucare.file(fake_url) self.assertEqual(fake_url, f.url) # no additional calls are made self.assertEqual(1, len(request.mock_calls))
def test_cdn_urls(self, request, head): ucare = UploadCare('pub', 'secret') uuid = '6c5e9526-b0fe-4739-8975-72e8d5ee6342' f = ucare.file(uuid) api_response = MockResponse(200, '{"on_s3": true, "last_keep_claim": true}') request.return_value = api_response cdn_response = MockResponse(200) head.return_value = cdn_response f.store(wait=True, timeout=1) self.assertEqual(f.cdn_url, 'https://ucarecdn.com/6c5e9526-b0fe-4739-8975-72e8d5ee6342/') self.assertEqual(f.resized_40x40, 'https://ucarecdn.com/6c5e9526-b0fe-4739-8975-72e8d5ee6342/-/resize/40x40/') self.assertEqual(f.resized_x40, 'https://ucarecdn.com/6c5e9526-b0fe-4739-8975-72e8d5ee6342/-/resize/x40/') self.assertEqual(f.resized_40x, 'https://ucarecdn.com/6c5e9526-b0fe-4739-8975-72e8d5ee6342/-/resize/40/') self.assertEqual(f.cropped_40x40, 'https://ucarecdn.com/6c5e9526-b0fe-4739-8975-72e8d5ee6342/-/crop/40x40/') with self.assertRaises(ValueError): f.cropped_40 with self.assertRaises(ValueError): f.resized_ with self.assertRaises(ValueError): f.resized_1x1x1
def test_uri_builders(self): ucare = UploadCare('pub', 'secret') path = ucare._build_api_path('/files/?asd=1') uri = ucare._build_api_uri(path) self.assertEqual(path, '/files/?asd=1') self.assertEqual(uri, 'https://api.uploadcare.com/files/?asd=1') ucare = UploadCare('pub', 'secret', api_base='http://example.com/api') path = ucare._build_api_path('/files/?asd=1') uri = ucare._build_api_uri(path) self.assertEqual(path, '/api/files/?asd=1') self.assertEqual(uri, 'http://example.com/api/files/?asd=1')
def test_request_headers(self, request): request.return_value = MockResponse(200, '[]') ucare = UploadCare('pub', 'secret') ucare.make_request('GET', '/files/') headers = request.call_args[1]['headers'] self.assertIn('Accept', headers) self.assertIn('User-Agent', headers) self.assertEqual(headers['Accept'], 'application/vnd.uploadcare-v0.2+json') self.assertEqual(headers['User-Agent'], 'pyuploadcare/0.12') ucare = UploadCare('pub', 'secret', api_version='0.1') ucare.make_request('GET', '/files/') headers = request.call_args[1]['headers'] self.assertIn('Accept', headers) self.assertIn('User-Agent', headers) self.assertEqual(headers['Accept'], 'application/json') self.assertEqual(headers['User-Agent'], 'pyuploadcare/0.12')