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