|
19 | 19 | import urllib3 |
20 | 20 |
|
21 | 21 | from fingerprint_pro_server_api_sdk import (Configuration, ErrorResponse, ErrorPlainResponse, ErrorCode, |
22 | | - RawDeviceAttributes, EventsUpdateRequest, RelatedVisitorsResponse) |
| 22 | + RawDeviceAttributes, EventsUpdateRequest, RelatedVisitorsResponse, |
| 23 | + SearchEventsResponse) |
23 | 24 | from fingerprint_pro_server_api_sdk.api.fingerprint_api import FingerprintApi # noqa: E501 |
24 | 25 | from fingerprint_pro_server_api_sdk.rest import KnownApiException, ApiException |
25 | 26 | from urllib.parse import urlencode |
|
69 | 70 | MOCK_GET_RELATED_VISITORS_404 = '404_visitor_not_found.json' # errors/ |
70 | 71 | MOCK_GET_RELATED_VISITORS_429 = '429_too_many_requests.json' # errors/ |
71 | 72 |
|
| 73 | +MOCK_SEARCH_EVENTS_200 = 'get_event_search_200.json' |
| 74 | +MOCK_SEARCH_EVENTS_400 = '400_ip_address_invalid.json' # errors/ |
| 75 | +MOCK_SEARCH_EVENTS_403 = '403_feature_not_enabled.json' # errors/ |
72 | 76 |
|
73 | 77 | class MockPoolManager(object): |
74 | 78 |
|
@@ -120,6 +124,11 @@ def request(self, *args, **kwargs): |
120 | 124 | if mock_file_by_first_argument == 'related-visitors': |
121 | 125 | # Extract file name from visitor_id param |
122 | 126 | mock_file_by_first_argument = r[1]['fields'][1][1] |
| 127 | + if mock_file_by_first_argument == 'search': |
| 128 | + if status == 200: |
| 129 | + mock_file_by_first_argument = MOCK_SEARCH_EVENTS_200 |
| 130 | + else: |
| 131 | + mock_file_by_first_argument = r[1]['fields'][2][1] |
123 | 132 |
|
124 | 133 | path = './test/mocks/' + mock_file_by_first_argument |
125 | 134 |
|
@@ -182,6 +191,15 @@ def get_related_visitors_path(region='us'): |
182 | 191 | }.get(region, "api.fpjs.io") |
183 | 192 | return 'https://%s/related-visitors' % domain |
184 | 193 |
|
| 194 | + @staticmethod |
| 195 | + def get_search_events_path(region='us'): |
| 196 | + domain = { |
| 197 | + "us": "api.fpjs.io", |
| 198 | + "eu": "eu.api.fpjs.io", |
| 199 | + "ap": "ap.api.fpjs.io", |
| 200 | + }.get(region, "api.fpjs.io") |
| 201 | + return 'https://%s/events/search' % domain |
| 202 | + |
185 | 203 | def test_get_visits_correct_data(self): |
186 | 204 | """Test checks correct code run result in default scenario""" |
187 | 205 | mock_pool = MockPoolManager(self) |
@@ -663,6 +681,67 @@ def test_get_related_visitors_429(self): |
663 | 681 | self.assertEqual(context.exception.structured_error.error.code, ErrorCode.TOOMANYREQUESTS) |
664 | 682 | self.assertEqual(context.exception.structured_error.retry_after, 4) |
665 | 683 |
|
| 684 | + def test_search_events_only_limit(self): |
| 685 | + """Test that search events returns 200 with only limit param""" |
| 686 | + mock_pool = MockPoolManager(self) |
| 687 | + self.api.api_client.rest_client.pool_manager = mock_pool |
| 688 | + mock_pool.expect_request('GET', TestFingerprintApi.get_search_events_path(), |
| 689 | + fields=[self.integration_info, ('limit', 1)], |
| 690 | + headers=self.request_headers, preload_content=True, timeout=None) |
| 691 | + |
| 692 | + response = self.api.search_events(1) |
| 693 | + self.assertIsInstance(response, SearchEventsResponse) |
| 694 | + |
| 695 | + def test_search_events_all_params(self): |
| 696 | + """Test that search events returns 200 with all params""" |
| 697 | + LIMIT = 100 |
| 698 | + BOT = 'good' |
| 699 | + IP_ADDRESS = '10.0.0.0/24' |
| 700 | + LINKED_ID = 'some_linked_id' |
| 701 | + START = 1582299576511 |
| 702 | + END = 1582299576513 |
| 703 | + REVERSE = True |
| 704 | + SUSPECT = False |
| 705 | + mock_pool = MockPoolManager(self) |
| 706 | + self.api.api_client.rest_client.pool_manager = mock_pool |
| 707 | + mock_pool.expect_request('GET', TestFingerprintApi.get_search_events_path(), |
| 708 | + fields=[self.integration_info, ('limit', LIMIT), |
| 709 | + ('visitor_id', MOCK_SEARCH_EVENTS_200), ('bot', BOT), |
| 710 | + ('ip_address', IP_ADDRESS), ('linked_id', LINKED_ID), ('start', START), |
| 711 | + ('end', END), ('reverse', REVERSE), ('suspect', SUSPECT)], |
| 712 | + headers=self.request_headers, preload_content=True, timeout=None) |
| 713 | + |
| 714 | + response = self.api.search_events(LIMIT, visitor_id=MOCK_SEARCH_EVENTS_200, bot=BOT, ip_address=IP_ADDRESS, |
| 715 | + linked_id=LINKED_ID, start=START, end=END, reverse=REVERSE, suspect=SUSPECT) |
| 716 | + self.assertIsInstance(response, SearchEventsResponse) |
| 717 | + |
| 718 | + def test_search_events_400(self): |
| 719 | + """Test that search events returns 400 invalid ip address""" |
| 720 | + mock_pool = MockPoolManager(self) |
| 721 | + self.api.api_client.rest_client.pool_manager = mock_pool |
| 722 | + mock_pool.expect_request('GET', TestFingerprintApi.get_search_events_path(), |
| 723 | + fields=[self.integration_info, ('limit', 1), ('visitor_id', MOCK_SEARCH_EVENTS_400)], |
| 724 | + headers=self.request_headers, preload_content=True, timeout=None, status=400) |
| 725 | + |
| 726 | + with self.assertRaises(KnownApiException) as context: |
| 727 | + self.api.search_events(1, visitor_id=MOCK_SEARCH_EVENTS_400) |
| 728 | + self.assertEqual(context.exception.status, 400) |
| 729 | + self.assertIsInstance(context.exception.structured_error, ErrorResponse) |
| 730 | + self.assertEqual(context.exception.structured_error.error.code, ErrorCode.REQUESTCANNOTBEPARSED) |
| 731 | + |
| 732 | + def test_search_events_403(self): |
| 733 | + """Test that search events returns 403 feature not enabled""" |
| 734 | + mock_pool = MockPoolManager(self) |
| 735 | + self.api.api_client.rest_client.pool_manager = mock_pool |
| 736 | + mock_pool.expect_request('GET', TestFingerprintApi.get_search_events_path(), |
| 737 | + fields=[self.integration_info, ('limit', 1), ('visitor_id', MOCK_SEARCH_EVENTS_403)], |
| 738 | + headers=self.request_headers, preload_content=True, timeout=None, status=403) |
| 739 | + |
| 740 | + with self.assertRaises(KnownApiException) as context: |
| 741 | + self.api.search_events(1, visitor_id=MOCK_SEARCH_EVENTS_403) |
| 742 | + self.assertEqual(context.exception.status, 403) |
| 743 | + self.assertIsInstance(context.exception.structured_error, ErrorResponse) |
| 744 | + self.assertEqual(context.exception.structured_error.error.code, ErrorCode.FEATURENOTENABLED) |
666 | 745 |
|
667 | 746 | if __name__ == '__main__': |
668 | 747 | unittest.main() |
0 commit comments