Skip to content

Commit

Permalink
test: add tests for error cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ilfa committed Jan 30, 2023
1 parent d78d12b commit 2c2bff7
Show file tree
Hide file tree
Showing 9 changed files with 292 additions and 4 deletions.
8 changes: 8 additions & 0 deletions sync.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ curl -o ./res/fingerprint-server-api.yaml https://fingerprintjs.github.io/finger
examplesList=(
'visits_limit_1.json'
'visits_limit_500.json'
'visits_403_error.json'
'visits_too_many_requests_error.json'
'webhook.json'
'get_event.json'
'get_event_403_error.json'
'get_event_404_error.json'
'get_event_botd_failed_error.json'
'get_event_botd_too_many_requests_error.json'
'get_event_identification_failed_error.json'
'get_event_identification_too_many_requests_error.json'
)

for example in ${examplesList[*]}; do
Expand Down
6 changes: 6 additions & 0 deletions test/mocks/get_event_403_error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"error": {
"code": "TokenRequired",
"message": "secret key is required"
}
}
6 changes: 6 additions & 0 deletions test/mocks/get_event_404_error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"error": {
"code": "RequestNotFound",
"message": "request id is not found"
}
}
67 changes: 67 additions & 0 deletions test/mocks/get_event_botd_failed_error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"products": {
"identification": {
"data": {
"visitorId": "Ibk1527CUFmcnjLwIs4A9",
"requestId": "0KSh65EnVoB85JBmloQK",
"incognito": true,
"linkedId": "somelinkedId",
"time": "2019-05-21T16:40:13Z",
"timestamp": 1582299576512,
"url": "https://www.example.com/login",
"ip": "61.127.217.15",
"ipLocation": {
"accuracyRadius": 10,
"latitude": 49.982,
"longitude": 36.2566,
"postalCode": "61202",
"timezone": "Europe/Dusseldorf",
"city": {
"name": "Dusseldorf"
},
"continent": {
"code": "EU",
"name": "Europe"
},
"country": {
"code": "DE",
"name": "Germany"
},
"subdivisions": [
{
"isoCode": "63",
"name": "North Rhine-Westphalia"
}
]
},
"browserDetails": {
"browserName": "Chrome",
"browserMajorVersion": "74",
"browserFullVersion": "74.0.3729",
"os": "Windows",
"osVersion": "7",
"device": "Other",
"userAgent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) ...."
},
"confidence": {
"score": 0.97
},
"visitorFound": true,
"firstSeenAt": {
"global": "2022-03-16T11:26:45.362Z",
"subscription": "2022-03-16T11:31:01.101Z"
},
"lastSeenAt": {
"global": "2022-03-16T11:28:34.023Z",
"subscription": null
}
}
},
"botd": {
"error": {
"code": "Failed",
"message": "internal server error"
}
}
}
}
67 changes: 67 additions & 0 deletions test/mocks/get_event_botd_too_many_requests_error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"products": {
"identification": {
"data": {
"visitorId": "Ibk1527CUFmcnjLwIs4A9",
"requestId": "0KSh65EnVoB85JBmloQK",
"incognito": true,
"linkedId": "somelinkedId",
"time": "2019-05-21T16:40:13Z",
"timestamp": 1582299576512,
"url": "https://www.example.com/login",
"ip": "61.127.217.15",
"ipLocation": {
"accuracyRadius": 10,
"latitude": 49.982,
"longitude": 36.2566,
"postalCode": "61202",
"timezone": "Europe/Dusseldorf",
"city": {
"name": "Dusseldorf"
},
"continent": {
"code": "EU",
"name": "Europe"
},
"country": {
"code": "DE",
"name": "Germany"
},
"subdivisions": [
{
"isoCode": "63",
"name": "North Rhine-Westphalia"
}
]
},
"browserDetails": {
"browserName": "Chrome",
"browserMajorVersion": "74",
"browserFullVersion": "74.0.3729",
"os": "Windows",
"osVersion": "7",
"device": "Other",
"userAgent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) ...."
},
"confidence": {
"score": 0.97
},
"visitorFound": true,
"firstSeenAt": {
"global": "2022-03-16T11:26:45.362Z",
"subscription": "2022-03-16T11:31:01.101Z"
},
"lastSeenAt": {
"global": "2022-03-16T11:28:34.023Z",
"subscription": null
}
}
},
"botd": {
"error": {
"code": "TooManyRequests",
"message": "too many requests"
}
}
}
}
20 changes: 20 additions & 0 deletions test/mocks/get_event_identification_failed_error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"products": {
"identification": {
"error": {
"code": "Failed",
"message": "failed"
}
},
"botd": {
"data": {
"url": "https://example.com/login",
"bot": {
"result": "notDetected"
},
"ip": "61.127.217.15",
"time": "2019-05-21T16:40:13Z"
}
}
}
}
20 changes: 20 additions & 0 deletions test/mocks/get_event_identification_too_many_requests_error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"products": {
"identification": {
"error": {
"code": "429 Too Many Requests",
"message": "too many requests"
}
},
"botd": {
"data": {
"url": "https://example.com/login",
"bot": {
"result": "notDetected"
},
"ip": "61.127.217.15",
"time": "2019-05-21T16:40:13Z"
}
}
}
}
3 changes: 3 additions & 0 deletions test/mocks/visits_403_error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"error": "Forbidden (HTTP 403)"
}
99 changes: 95 additions & 4 deletions test/test_fingerprint_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@

import urllib3

import fingerprint_pro_server_api_sdk
from fingerprint_pro_server_api_sdk import Configuration
from fingerprint_pro_server_api_sdk import Configuration, ManyRequestsResponse, ErrorVisits403, ErrorEvent403Response, \
ErrorEvent404Response
from fingerprint_pro_server_api_sdk.api.fingerprint_api import FingerprintApi # noqa: E501
from fingerprint_pro_server_api_sdk.rest import ApiException
from fingerprint_pro_server_api_sdk.rest import KnownApiException

API_KEY = 'private_key'

VERSION = '2.0.0'


class MockPoolManager(object):
def __init__(self, tc):
self._tc = tc
Expand All @@ -41,6 +42,10 @@ def get_mock_from_path(path):
def request(self, *args, **kwargs):
self._tc.assertTrue(len(self._reqs) > 0)
r = self._reqs.pop(0)
status = 200
if r[1].get('status') is not None:
status = r[1].get('status')
r[1].pop('status')
self._tc.maxDiff = None
self._tc.assertEqual(r[0], args)
self._tc.assertEqual(r[1], kwargs)
Expand All @@ -56,7 +61,11 @@ def request(self, *args, **kwargs):
with io.open('./test/mocks/' + mock_file_by_visitor_id, 'r', encoding='utf-8') as mock_file:
answer_mock = mock_file.read()
mock_file.close()
return urllib3.HTTPResponse(status=200, body=answer_mock)
headers = {}
if mock_file_by_visitor_id == 'visits_too_many_requests_error.json':
headers.update({'Retry-After': '4'})

return urllib3.HTTPResponse(status=status, body=answer_mock, headers=headers)
except IOError as e:
print(e)
return urllib3.HTTPResponse(status=200, body='{"visitorId": "%s", "visits": []}' % mock_file_by_visitor_id)
Expand Down Expand Up @@ -113,6 +122,34 @@ def test_get_visits_correct_data(self):
self.api.get_visits(mock_file1)
self.api.get_visits(mock_file2)

def test_get_visits_error_403(self):
"""Test checks correct code run result in case of 403 error"""
mock_pool = MockPoolManager(self)
self.api.api_client.rest_client.pool_manager = mock_pool
mock_file = 'visits_403_error.json'
mock_pool.expect_request('GET', TestFingerprintApi.get_get_visits_method_path(visitor_id=mock_file),
fields=[self.integration_info], headers=self.request_headers,
preload_content=True, timeout=None, status=403)
with self.assertRaises(KnownApiException) as context:
self.api.get_visits(mock_file)
self.assertEqual(context.exception.status, 403)
structured_error = context.exception.structured_error
self.assertIsInstance(context.exception.structured_error, ErrorVisits403)

def test_get_visits_error_429(self):
"""Test checks correct code run result in case of 429 error"""
mock_pool = MockPoolManager(self)
self.api.api_client.rest_client.pool_manager = mock_pool
mock_file = 'visits_too_many_requests_error.json'
mock_pool.expect_request('GET', TestFingerprintApi.get_get_visits_method_path(visitor_id=mock_file),
fields=[self.integration_info], headers=self.request_headers,
preload_content=True, timeout=None, status=429)
with self.assertRaises(KnownApiException) as context:
self.api.get_visits(mock_file)
self.assertEqual(context.exception.status, 429)
self.assertIsNotNone(context.exception.headers.get('retry-after'))
self.assertIsInstance(context.exception.structured_error, ManyRequestsResponse)

def test_get_event_correct_data(self):
"""Test checks correct code run result in default scenario"""
mock_pool = MockPoolManager(self)
Expand All @@ -124,6 +161,60 @@ def test_get_event_correct_data(self):

self.api.get_event(mock_file1)

def test_get_event_errors_200(self):
"""Test checks correct code run result in scenario of arrors in BotD or identification API"""
mock_pool = MockPoolManager(self)
self.api.api_client.rest_client.pool_manager = mock_pool
mock_file_botd_fail = 'get_event_botd_failed_error.json'
mock_file_botd_429 = 'get_event_botd_too_many_requests_error.json'
mock_file_identification_fail = 'get_event_identification_failed_error.json'
mock_file_identification_429 = 'get_event_identification_too_many_requests_error.json'
mock_pool.expect_request('GET', TestFingerprintApi.get_get_event_method_path(request_id=mock_file_botd_fail),
fields=[self.integration_info], headers=self.request_headers,
preload_content=True, timeout=None)
mock_pool.expect_request('GET', TestFingerprintApi.get_get_event_method_path(request_id=mock_file_botd_429),
fields=[self.integration_info], headers=self.request_headers,
preload_content=True, timeout=None)
mock_pool.expect_request('GET',
TestFingerprintApi.get_get_event_method_path(request_id=mock_file_identification_fail),
fields=[self.integration_info], headers=self.request_headers,
preload_content=True, timeout=None)
mock_pool.expect_request('GET',
TestFingerprintApi.get_get_event_method_path(request_id=mock_file_identification_429),
fields=[self.integration_info], headers=self.request_headers,
preload_content=True, timeout=None)

self.api.get_event(mock_file_botd_fail)
self.api.get_event(mock_file_botd_429)
self.api.get_event(mock_file_identification_fail)
self.api.get_event(mock_file_identification_429)

def test_get_event_error_403(self):
"""Test checks correct code run result in case of 403 error"""
mock_pool = MockPoolManager(self)
self.api.api_client.rest_client.pool_manager = mock_pool
mock_file = 'get_event_403_error.json'
mock_pool.expect_request('GET', TestFingerprintApi.get_get_event_method_path(request_id=mock_file),
fields=[self.integration_info], headers=self.request_headers,
preload_content=True, timeout=None, status=403)
with self.assertRaises(KnownApiException) as context:
self.api.get_event(mock_file)
self.assertEqual(context.exception.status, 403)
self.assertIsInstance(context.exception.structured_error, ErrorEvent403Response)

def test_get_event_error_404(self):
"""Test checks correct code run result in case of 403 error"""
mock_pool = MockPoolManager(self)
self.api.api_client.rest_client.pool_manager = mock_pool
mock_file = 'get_event_404_error.json'
mock_pool.expect_request('GET', TestFingerprintApi.get_get_event_method_path(request_id=mock_file),
fields=[self.integration_info], headers=self.request_headers,
preload_content=True, timeout=None, status=404)
with self.assertRaises(KnownApiException) as context:
self.api.get_event(mock_file)
self.assertEqual(context.exception.status, 404)
self.assertIsInstance(context.exception.structured_error, ErrorEvent404Response)

def test_get_event_empty_data(self):
"""Test checks correct code running in case of there is no events"""
mock_pool = MockPoolManager(self)
Expand Down

0 comments on commit 2c2bff7

Please sign in to comment.