def test_post_body_must_be_valid_json(self): request = self.get_mock_request(content='\invalid json') with self.assertRaises(HTTPException) as cm: get_metadata(self.metadata_client, self.get_config(), request) self.assertEqual(cm.exception.response.status_code, 400)
def test_valid_json_post_body_must_include_urls(self): request = self.get_mock_request(content=json.dumps({})) with self.assertRaises(HTTPException) as cm: get_metadata(self.metadata_client, self.get_config(), request) self.assertEqual(cm.exception.response.status_code, 400)
def test_wrong_content_type_raises_400(self): request = self.get_mock_request(content_type='\invalid') with self.assertRaises(HTTPException) as cm: get_metadata(self.metadata_client, self.get_config(), request) self.assertEqual(cm.exception.response.status_code, 400)
def test_rejects_calls_null_urls(self): request = self.get_mock_request(urls=self.sample_urls + [None]) with self.assertRaises(HTTPException) as cm: get_metadata(self.metadata_client, self.get_config(), request) self.assertEqual(cm.exception.response.status_code, 400)
def test_rejects_calls_with_too_many_urls(self): config = self.get_config(MAXIMUM_POST_URLS=1) request = self.get_mock_request(urls=self.sample_urls) with self.assertRaises(HTTPException) as cm: get_metadata(self.metadata_client, config, request) self.assertEqual(cm.exception.response.status_code, 400)
def test_urlextractorexception_returns_error(self): self.mock_redis.get.side_effect = redis.RedisError() request = self.get_mock_request(urls=self.sample_urls) with self.assertRaises(HTTPException) as cm: get_metadata(self.metadata_client, self.get_config(), request) self.assertEqual(cm.exception.response.status_code, 500)
def test_extract_returns_cached_data(self): cached_urls = self.sample_urls def fake_cache(urls): def mock_cache_get(cache_key): for url in urls: if url in cache_key: return json.dumps(self.get_mock_url_data(url)) return mock_cache_get self.mock_redis.get.side_effect = fake_cache(cached_urls) request = self.get_mock_request(urls=self.sample_urls) response = get_metadata(self.metadata_client, self.get_config(), request) self.assertEqual(response.status_code, 200) self.assertEqual(self.mock_redis.get.call_count, len(self.sample_urls)) self.assertEqual(self.mock_redis.setex.call_count, 0) self.assertEqual(self.mock_requests_get.call_count, 0) response_data = json.loads(response.data) self.assertEqual(response_data, { 'urls': self.expected_response, 'error': '', })
def test_extract_returns_cached_data(self): cached_urls = self.sample_urls def fake_cache(urls): def mock_cache_get(cache_key): for url in urls: if url in cache_key: return json.dumps(self.get_mock_url_data(url)) return mock_cache_get self.mock_redis.get.side_effect = fake_cache(cached_urls) request = self.get_mock_request(urls=self.sample_urls) response = get_metadata( self.metadata_client, self.get_config(), request) self.assertEqual(response.status_code, 200) self.assertEqual(self.mock_redis.get.call_count, len(self.sample_urls)) self.assertEqual(self.mock_redis.setex.call_count, 0) self.assertEqual(self.mock_requests_get.call_count, 0) response_data = json.loads(response.data) self.assertEqual(response_data, { 'urls': self.expected_response, 'error': '', })
def test_empty_urls_param_returns_200(self): request = self.get_mock_request() response = get_metadata(self.metadata_client, self.get_config(), request) self.assertEqual(response.status_code, 200) response_data = json.loads(response.data) self.assertEqual(response_data, { 'urls': {}, 'error': '', })
def test_empty_urls_param_returns_200(self): request = self.get_mock_request() response = get_metadata( self.metadata_client, self.get_config(), request) self.assertEqual(response.status_code, 200) response_data = json.loads(response.data) self.assertEqual(response_data, { 'urls': {}, 'error': '', })