-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_identity_map_client_unit_tests.py
More file actions
54 lines (48 loc) · 2.92 KB
/
test_identity_map_client_unit_tests.py
File metadata and controls
54 lines (48 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import base64
import unittest
import datetime as dt
from unittest.mock import patch, MagicMock
from uid2_client import IdentityMapClient, get_datetime_utc_iso_format, Uid2Response, Envelope
class IdentityMapUnitTests(unittest.TestCase):
UID2_SECRET_KEY = base64.b64encode(b"UID2_CLIENT_SECRET").decode()
identity_map_client = IdentityMapClient("UID2_BASE_URL", "UID2_API_KEY", UID2_SECRET_KEY)
def test_identity_buckets_invalid_timestamp(self):
test_cases = ["1234567890",
1234567890,
2024.7,
"2024-7-1",
"2024-07-01T12:00:00",
[2024, 7, 1, 12, 0, 0],
None]
for timestamp in test_cases:
self.assertRaises(AttributeError, self.identity_map_client.get_identity_buckets,
timestamp)
def test_get_datetime_utc_iso_format_timestamp(self):
expected_timestamp = "2024-07-02T14:30:15.123456"
test_cases = ["2024-07-02T14:30:15.123456+00:00", "2024-07-02 09:30:15.123456-05:00",
"2024-07-02T08:30:15.123456-06:00", "2024-07-02T10:30:15.123456-04:00",
"2024-07-02T06:30:15.123456-08:00", "2024-07-02T23:30:15.123456+09:00",
"2024-07-03T00:30:15.123456+10:00", "2024-07-02T20:00:15.123456+05:30"]
for timestamp_str in test_cases:
timestamp = dt.datetime.fromisoformat(timestamp_str)
iso_format_timestamp = get_datetime_utc_iso_format(timestamp)
self.assertEqual(expected_timestamp, iso_format_timestamp)
@patch('uid2_client.identity_map_client.create_envelope')
@patch('uid2_client.identity_map_client.make_request')
@patch('uid2_client.identity_map_client.parse_response')
def test_identity_buckets_request(self, mock_parse_response, mock_make_request, mock_create_envelope):
expected_req = b'{"since_timestamp": "2024-07-02T14:30:15.123456"}'
test_cases = ["2024-07-02T14:30:15.123456+00:00", "2024-07-02 09:30:15.123456-05:00",
"2024-07-02T08:30:15.123456-06:00", "2024-07-02T10:30:15.123456-04:00",
"2024-07-02T06:30:15.123456-08:00", "2024-07-02T23:30:15.123456+09:00",
"2024-07-03T00:30:15.123456+10:00", "2024-07-02T20:00:15.123456+05:30"]
mock_req = b'mocked_request_data'
mock_nonce = 'mocked_nonce'
mock_create_envelope.return_value = Envelope(mock_req, mock_nonce)
mock_response = '{"mocked": "response"}'
mock_make_request.return_value = Uid2Response.from_string(mock_response)
mock_parse_response.return_value = b'{"body":[],"status":"success"}'
for timestamp in test_cases:
self.identity_map_client.get_identity_buckets(dt.datetime.fromisoformat(timestamp))
called_args, called_kwargs = mock_create_envelope.call_args
self.assertEqual(expected_req, called_args[2])