Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
    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)
Ejemplo n.º 6
0
    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)
Ejemplo n.º 7
0
    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)
Ejemplo n.º 8
0
    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)
Ejemplo n.º 9
0
    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)
Ejemplo n.º 10
0
    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)
Ejemplo n.º 11
0
    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)
Ejemplo n.º 12
0
    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)
Ejemplo n.º 13
0
    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': '',
        })
Ejemplo n.º 14
0
    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': '',
        })
Ejemplo n.º 15
0
    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': '',
        })
Ejemplo n.º 16
0
    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': '',
        })