|
| 1 | +# Copyright 2022, Optimizely |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http:#www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | + |
| 14 | +import json |
| 15 | +from unittest import mock |
| 16 | + |
| 17 | +from requests import exceptions as request_exception |
| 18 | + |
| 19 | +from optimizely.helpers.enums import OdpRestApiConfig |
| 20 | +from optimizely.odp.zaius_rest_api_manager import ZaiusRestApiManager |
| 21 | +from . import base |
| 22 | + |
| 23 | + |
| 24 | +class ZaiusRestApiManagerTest(base.BaseTest): |
| 25 | + user_key = "vuid" |
| 26 | + user_value = "test-user-value" |
| 27 | + api_key = "test-api-key" |
| 28 | + api_host = "test-host" |
| 29 | + |
| 30 | + events = [ |
| 31 | + {"type": "t1", "action": "a1", "identifiers": {"id-key-1": "id-value-1"}, "data": {"key-1": "value1"}}, |
| 32 | + {"type": "t2", "action": "a2", "identifiers": {"id-key-2": "id-value-2"}, "data": {"key-2": "value2"}}, |
| 33 | + ] |
| 34 | + |
| 35 | + def test_send_odp_events__valid_request(self): |
| 36 | + with mock.patch('requests.post') as mock_request_post: |
| 37 | + api = ZaiusRestApiManager() |
| 38 | + api.send_odp_events(api_key=self.api_key, |
| 39 | + api_host=self.api_host, |
| 40 | + events=self.events) |
| 41 | + |
| 42 | + request_headers = {'content-type': 'application/json', 'x-api-key': self.api_key} |
| 43 | + mock_request_post.assert_called_once_with(url=self.api_host + "/v3/events", |
| 44 | + headers=request_headers, |
| 45 | + data=json.dumps(self.events), |
| 46 | + timeout=OdpRestApiConfig.REQUEST_TIMEOUT) |
| 47 | + |
| 48 | + def test_send_odp_ovents_success(self): |
| 49 | + with mock.patch('requests.post') as mock_request_post: |
| 50 | + # no need to mock url and content because we're not returning the response |
| 51 | + mock_request_post.return_value = self.fake_server_response(status_code=200) |
| 52 | + |
| 53 | + api = ZaiusRestApiManager() |
| 54 | + should_retry = api.send_odp_events(api_key=self.api_key, |
| 55 | + api_host=self.api_host, |
| 56 | + events=self.events) # content of events doesn't matter for the test |
| 57 | + |
| 58 | + self.assertFalse(should_retry) |
| 59 | + |
| 60 | + def test_send_odp_events_invalid_json_no_retry(self): |
| 61 | + events = {1, 2, 3} # using a set to trigger JSON-not-serializable error |
| 62 | + |
| 63 | + with mock.patch('requests.post') as mock_request_post, \ |
| 64 | + mock.patch('optimizely.logger') as mock_logger: |
| 65 | + api = ZaiusRestApiManager(logger=mock_logger) |
| 66 | + should_retry = api.send_odp_events(api_key=self.api_key, |
| 67 | + api_host=self.api_host, |
| 68 | + events=events) |
| 69 | + |
| 70 | + self.assertFalse(should_retry) |
| 71 | + mock_request_post.assert_not_called() |
| 72 | + mock_logger.error.assert_called_once_with( |
| 73 | + 'ODP event send failed (Object of type set is not JSON serializable).') |
| 74 | + |
| 75 | + def test_send_odp_events_invalid_url_no_retry(self): |
| 76 | + invalid_url = 'https://*api.zaius.com' |
| 77 | + |
| 78 | + with mock.patch('requests.post', |
| 79 | + side_effect=request_exception.InvalidURL('Invalid URL')) as mock_request_post, \ |
| 80 | + mock.patch('optimizely.logger') as mock_logger: |
| 81 | + api = ZaiusRestApiManager(logger=mock_logger) |
| 82 | + should_retry = api.send_odp_events(api_key=self.api_key, |
| 83 | + api_host=invalid_url, |
| 84 | + events=self.events) |
| 85 | + |
| 86 | + self.assertFalse(should_retry) |
| 87 | + mock_request_post.assert_called_once() |
| 88 | + mock_logger.error.assert_called_once_with('ODP event send failed (Invalid URL).') |
| 89 | + |
| 90 | + def test_send_odp_events_network_error_retry(self): |
| 91 | + with mock.patch('requests.post', |
| 92 | + side_effect=request_exception.ConnectionError('Connection error')) as mock_request_post, \ |
| 93 | + mock.patch('optimizely.logger') as mock_logger: |
| 94 | + api = ZaiusRestApiManager(logger=mock_logger) |
| 95 | + should_retry = api.send_odp_events(api_key=self.api_key, |
| 96 | + api_host=self.api_host, |
| 97 | + events=self.events) |
| 98 | + |
| 99 | + self.assertTrue(should_retry) |
| 100 | + mock_request_post.assert_called_once() |
| 101 | + mock_logger.error.assert_called_once_with('ODP event send failed (network error).') |
| 102 | + |
| 103 | + def test_send_odp_events_400_no_retry(self): |
| 104 | + with mock.patch('requests.post') as mock_request_post, \ |
| 105 | + mock.patch('optimizely.logger') as mock_logger: |
| 106 | + mock_request_post.return_value = self.fake_server_response(status_code=400, |
| 107 | + url=self.api_host, |
| 108 | + content=self.failure_response_data) |
| 109 | + |
| 110 | + api = ZaiusRestApiManager(logger=mock_logger) |
| 111 | + should_retry = api.send_odp_events(api_key=self.api_key, |
| 112 | + api_host=self.api_host, |
| 113 | + events=self.events) |
| 114 | + |
| 115 | + self.assertFalse(should_retry) |
| 116 | + mock_request_post.assert_called_once() |
| 117 | + mock_logger.error.assert_called_once_with('ODP event send failed ({"title":"Bad Request","status":400,' |
| 118 | + '"timestamp":"2022-07-01T20:44:00.945Z","detail":{"invalids":' |
| 119 | + '[{"event":0,"message":"missing \'type\' field"}]}}).') |
| 120 | + |
| 121 | + def test_send_odp_events_500_retry(self): |
| 122 | + with mock.patch('requests.post') as mock_request_post, \ |
| 123 | + mock.patch('optimizely.logger') as mock_logger: |
| 124 | + mock_request_post.return_value = self.fake_server_response(status_code=500, url=self.api_host) |
| 125 | + |
| 126 | + api = ZaiusRestApiManager(logger=mock_logger) |
| 127 | + should_retry = api.send_odp_events(api_key=self.api_key, |
| 128 | + api_host=self.api_host, |
| 129 | + events=self.events) |
| 130 | + |
| 131 | + self.assertTrue(should_retry) |
| 132 | + mock_request_post.assert_called_once() |
| 133 | + mock_logger.error.assert_called_once_with('ODP event send failed (500 Server Error: None for url: test-host).') |
| 134 | + |
| 135 | + # test json responses |
| 136 | + success_response_data = '{"title":"Accepted","status":202,"timestamp":"2022-07-01T16:04:06.786Z"}' |
| 137 | + |
| 138 | + failure_response_data = '{"title":"Bad Request","status":400,"timestamp":"2022-07-01T20:44:00.945Z",' \ |
| 139 | + '"detail":{"invalids":[{"event":0,"message":"missing \'type\' field"}]}}' |
0 commit comments