Skip to content

Commit d244016

Browse files
committed
test: add tests for search events method
1 parent 966b796 commit d244016

File tree

1 file changed

+80
-1
lines changed

1 file changed

+80
-1
lines changed

test/test_fingerprint_api.py

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
import urllib3
2020

2121
from fingerprint_pro_server_api_sdk import (Configuration, ErrorResponse, ErrorPlainResponse, ErrorCode,
22-
RawDeviceAttributes, EventsUpdateRequest, RelatedVisitorsResponse)
22+
RawDeviceAttributes, EventsUpdateRequest, RelatedVisitorsResponse,
23+
SearchEventsResponse)
2324
from fingerprint_pro_server_api_sdk.api.fingerprint_api import FingerprintApi # noqa: E501
2425
from fingerprint_pro_server_api_sdk.rest import KnownApiException, ApiException
2526
from urllib.parse import urlencode
@@ -69,6 +70,9 @@
6970
MOCK_GET_RELATED_VISITORS_404 = '404_visitor_not_found.json' # errors/
7071
MOCK_GET_RELATED_VISITORS_429 = '429_too_many_requests.json' # errors/
7172

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/
7276

7377
class MockPoolManager(object):
7478

@@ -120,6 +124,11 @@ def request(self, *args, **kwargs):
120124
if mock_file_by_first_argument == 'related-visitors':
121125
# Extract file name from visitor_id param
122126
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]
123132

124133
path = './test/mocks/' + mock_file_by_first_argument
125134

@@ -182,6 +191,15 @@ def get_related_visitors_path(region='us'):
182191
}.get(region, "api.fpjs.io")
183192
return 'https://%s/related-visitors' % domain
184193

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+
185203
def test_get_visits_correct_data(self):
186204
"""Test checks correct code run result in default scenario"""
187205
mock_pool = MockPoolManager(self)
@@ -663,6 +681,67 @@ def test_get_related_visitors_429(self):
663681
self.assertEqual(context.exception.structured_error.error.code, ErrorCode.TOOMANYREQUESTS)
664682
self.assertEqual(context.exception.structured_error.retry_after, 4)
665683

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)
666745

667746
if __name__ == '__main__':
668747
unittest.main()

0 commit comments

Comments
 (0)