|
61 | 61 | MOCK_UPDATE_EVENT_404 = '404_request_not_found.json' # errors/
|
62 | 62 | MOCK_UPDATE_EVENT_409 = '409_state_not_ready.json' # errors/
|
63 | 63 |
|
64 |
| -MOCK_GET_RELATED_VISITORS_200= 'related-visitors/get_related_visitors_200.json' |
| 64 | +MOCK_GET_RELATED_VISITORS_200 = 'related-visitors/get_related_visitors_200.json' |
| 65 | +MOCK_GET_RELATED_VISITORS_400 = '400_visitor_id_invalid.json' # errors/ |
| 66 | +MOCK_GET_RELATED_VISITORS_403 = '403_feature_not_enabled.json' # errors/ |
| 67 | +MOCK_GET_RELATED_VISITORS_404 = '404_visitor_not_found.json' # errors/ |
| 68 | +MOCK_GET_RELATED_VISITORS_429 = '429_too_many_requests.json' # errors/ |
65 | 69 |
|
66 |
| -class MockPoolManager(object): |
67 | 70 |
|
| 71 | +class MockPoolManager(object): |
68 | 72 |
|
69 | 73 | def __init__(self, tc, request_headers=None):
|
70 | 74 | if request_headers is None:
|
@@ -126,7 +130,8 @@ def request(self, *args, **kwargs):
|
126 | 130 | return urllib3.HTTPResponse(status=status, body=answer_mock, headers=self.request_headers)
|
127 | 131 | except IOError as e:
|
128 | 132 | print(e)
|
129 |
| - return urllib3.HTTPResponse(status=200, body='{"visitorId": "%s", "visits": []}' % mock_file_by_first_argument) |
| 133 | + return urllib3.HTTPResponse(status=200, |
| 134 | + body='{"visitorId": "%s", "visits": []}' % mock_file_by_first_argument) |
130 | 135 | pass
|
131 | 136 |
|
132 | 137 |
|
@@ -312,7 +317,6 @@ def test_get_event_empty_data(self):
|
312 | 317 | value = getattr(response.products, field)
|
313 | 318 | self.assertIsNone(value, f"Signal '{field}' is not empty")
|
314 | 319 |
|
315 |
| - |
316 | 320 | def test_get_visits_empty_answer(self):
|
317 | 321 | """Test checks correct code running in case of there is no visits"""
|
318 | 322 | mock_pool = MockPoolManager(self)
|
@@ -578,12 +582,82 @@ def test_get_related_visitors(self):
|
578 | 582 | mock_pool = MockPoolManager(self)
|
579 | 583 | self.api.api_client.rest_client.pool_manager = mock_pool
|
580 | 584 | mock_pool.expect_request('GET', TestFingerprintApi.get_related_visitors_path(),
|
581 |
| - fields=[self.integration_info, ('visitor_id', MOCK_GET_RELATED_VISITORS_200)], headers=self.request_headers, |
| 585 | + fields=[self.integration_info, ('visitor_id', MOCK_GET_RELATED_VISITORS_200)], |
| 586 | + headers=self.request_headers, |
582 | 587 | preload_content=True, timeout=None)
|
583 | 588 |
|
584 | 589 | response = self.api.get_related_visitors(MOCK_GET_RELATED_VISITORS_200)
|
585 | 590 | self.assertIsInstance(response, RelatedVisitorsResponse)
|
586 | 591 |
|
| 592 | + def test_get_related_visitors_400(self): |
| 593 | + """Test that related visitors returns 400 error""" |
| 594 | + mock_pool = MockPoolManager(self) |
| 595 | + self.api.api_client.rest_client.pool_manager = mock_pool |
| 596 | + |
| 597 | + mock_pool.expect_request('GET', |
| 598 | + TestFingerprintApi.get_related_visitors_path(), |
| 599 | + fields=[self.integration_info, ('visitor_id', MOCK_GET_RELATED_VISITORS_400)], |
| 600 | + headers=self.request_headers, preload_content=True, timeout=None, status=400, |
| 601 | + ) |
| 602 | + |
| 603 | + with self.assertRaises(KnownApiException) as context: |
| 604 | + self.api.get_related_visitors(MOCK_GET_RELATED_VISITORS_400) |
| 605 | + self.assertEqual(context.exception.status, 400) |
| 606 | + self.assertIsInstance(context.exception.structured_error, ErrorResponse) |
| 607 | + self.assertEqual(context.exception.structured_error.error.code, ErrorCode.REQUESTCANNOTBEPARSED) |
| 608 | + |
| 609 | + def test_get_related_visitors_403(self): |
| 610 | + """Test that related visitors returns 403 error""" |
| 611 | + mock_pool = MockPoolManager(self) |
| 612 | + self.api.api_client.rest_client.pool_manager = mock_pool |
| 613 | + |
| 614 | + mock_pool.expect_request('GET', |
| 615 | + TestFingerprintApi.get_related_visitors_path(), |
| 616 | + fields=[self.integration_info, ('visitor_id', MOCK_GET_RELATED_VISITORS_403)], |
| 617 | + headers=self.request_headers, preload_content=True, timeout=None, status=403, |
| 618 | + ) |
| 619 | + |
| 620 | + with self.assertRaises(KnownApiException) as context: |
| 621 | + self.api.get_related_visitors(MOCK_GET_RELATED_VISITORS_403) |
| 622 | + self.assertEqual(context.exception.status, 403) |
| 623 | + self.assertIsInstance(context.exception.structured_error, ErrorResponse) |
| 624 | + self.assertEqual(context.exception.structured_error.error.code, ErrorCode.FEATURENOTENABLED) |
| 625 | + |
| 626 | + def test_get_related_visitors_404(self): |
| 627 | + """Test that related visitors returns 404 error""" |
| 628 | + mock_pool = MockPoolManager(self) |
| 629 | + self.api.api_client.rest_client.pool_manager = mock_pool |
| 630 | + |
| 631 | + mock_pool.expect_request('GET', |
| 632 | + TestFingerprintApi.get_related_visitors_path(), |
| 633 | + fields=[self.integration_info, ('visitor_id', MOCK_GET_RELATED_VISITORS_404)], |
| 634 | + headers=self.request_headers, preload_content=True, timeout=None, status=404, |
| 635 | + ) |
| 636 | + |
| 637 | + with self.assertRaises(KnownApiException) as context: |
| 638 | + self.api.get_related_visitors(MOCK_GET_RELATED_VISITORS_404) |
| 639 | + self.assertEqual(context.exception.status, 404) |
| 640 | + self.assertIsInstance(context.exception.structured_error, ErrorResponse) |
| 641 | + self.assertEqual(context.exception.structured_error.error.code, ErrorCode.VISITORNOTFOUND) |
| 642 | + |
| 643 | + def test_get_related_visitors_429(self): |
| 644 | + """Test that related visitors returns 429 error""" |
| 645 | + mock_pool = MockPoolManager(self, request_headers={'Retry-After': '4'}) |
| 646 | + self.api.api_client.rest_client.pool_manager = mock_pool |
| 647 | + |
| 648 | + mock_pool.expect_request('GET', |
| 649 | + TestFingerprintApi.get_related_visitors_path(), |
| 650 | + fields=[self.integration_info, ('visitor_id', MOCK_GET_RELATED_VISITORS_429)], |
| 651 | + headers=self.request_headers, preload_content=True, timeout=None, status=429, |
| 652 | + ) |
| 653 | + |
| 654 | + with self.assertRaises(KnownApiException) as context: |
| 655 | + self.api.get_related_visitors(MOCK_GET_RELATED_VISITORS_429) |
| 656 | + self.assertEqual(context.exception.status, 429) |
| 657 | + self.assertIsInstance(context.exception.structured_error, ErrorResponse) |
| 658 | + self.assertEqual(context.exception.structured_error.error.code, ErrorCode.TOOMANYREQUESTS) |
| 659 | + self.assertEqual(context.exception.structured_error.retry_after, 4) |
| 660 | + |
587 | 661 |
|
588 | 662 | if __name__ == '__main__':
|
589 | 663 | unittest.main()
|
0 commit comments