Skip to content

Commit ab115e8

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

File tree

1 file changed

+88
-3
lines changed

1 file changed

+88
-3
lines changed

test/test_fingerprint_api.py

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111
"""
1212

1313
import io
14-
import json
1514
import os
1615
import unittest
17-
from datetime import datetime
1816

1917
import urllib3
2018

2119
from fingerprint_pro_server_api_sdk import (Configuration, ErrorResponse, ErrorPlainResponse, ErrorCode,
22-
RawDeviceAttributes, EventsUpdateRequest, RelatedVisitorsResponse)
20+
RawDeviceAttributes, EventsUpdateRequest, RelatedVisitorsResponse,
21+
SearchEventsResponse, SearchEventsResponseEvents, Products)
2322
from fingerprint_pro_server_api_sdk.api.fingerprint_api import FingerprintApi # noqa: E501
2423
from fingerprint_pro_server_api_sdk.rest import KnownApiException, ApiException
2524
from urllib.parse import urlencode
@@ -69,6 +68,9 @@
6968
MOCK_GET_RELATED_VISITORS_404 = '404_visitor_not_found.json' # errors/
7069
MOCK_GET_RELATED_VISITORS_429 = '429_too_many_requests.json' # errors/
7170

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

7375
class MockPoolManager(object):
7476

@@ -120,6 +122,11 @@ def request(self, *args, **kwargs):
120122
if mock_file_by_first_argument == 'related-visitors':
121123
# Extract file name from visitor_id param
122124
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]
123130

124131
path = './test/mocks/' + mock_file_by_first_argument
125132

@@ -182,6 +189,15 @@ def get_related_visitors_path(region='us'):
182189
}.get(region, "api.fpjs.io")
183190
return 'https://%s/related-visitors' % domain
184191

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+
185201
def test_get_visits_correct_data(self):
186202
"""Test checks correct code run result in default scenario"""
187203
mock_pool = MockPoolManager(self)
@@ -663,6 +679,75 @@ def test_get_related_visitors_429(self):
663679
self.assertEqual(context.exception.structured_error.error.code, ErrorCode.TOOMANYREQUESTS)
664680
self.assertEqual(context.exception.structured_error.retry_after, 4)
665681

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

667752
if __name__ == '__main__':
668753
unittest.main()

0 commit comments

Comments
 (0)