|
| 1 | +# Copyright (c) 2016 Adobe Systems Incorporated. All rights reserved. |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +# of this software and associated documentation files (the "Software"), to deal |
| 5 | +# in the Software without restriction, including without limitation the rights |
| 6 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +# copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in all |
| 11 | +# copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | +# SOFTWARE. |
| 20 | + |
| 21 | +import mock |
| 22 | +import json |
| 23 | +import pytest |
| 24 | + |
| 25 | +from conftest import MockResponse |
| 26 | +from umapi_client.legacy import UMAPI, Action |
| 27 | +from umapi_client.legacy import UMAPIError, UMAPIRetryError, UMAPIRequestError, ActionFormatError |
| 28 | + |
| 29 | + |
| 30 | +# These values will be returned in the various cases by get/post |
| 31 | +success_response = MockResponse(200, {"result": "success"}) |
| 32 | +error_response = MockResponse(200, {"result": "error", "errors": [{"errorCode": "test.error"}]}) |
| 33 | +retry_response = MockResponse(429, headers={"Retry-After": "1"}) |
| 34 | +not_found_response = MockResponse(404, text="404 Object Not Found") |
| 35 | + |
| 36 | +@mock.patch('umapi_client.legacy.requests.get', return_value=success_response) |
| 37 | +def test_list_users_success(_): |
| 38 | + """Test Users List - SUCCESS""" |
| 39 | + api = UMAPI('http://example.com/success', "N/A", retry_max_attempts=1) |
| 40 | + assert api.users(None) == {"result": "success"} |
| 41 | + |
| 42 | + |
| 43 | +@mock.patch('umapi_client.legacy.requests.get', return_value=error_response) |
| 44 | +def test_list_users_error(_): |
| 45 | + """Test Users List - ERROR""" |
| 46 | + api = UMAPI('http://example.com/error', "N/A", retry_max_attempts=1) |
| 47 | + pytest.raises(UMAPIRequestError, api.users, None) |
| 48 | + |
| 49 | + |
| 50 | +@mock.patch('umapi_client.legacy.requests.get', return_value=not_found_response) |
| 51 | +def test_list_users_failure(patch): |
| 52 | + """Test Users List - FAILURE""" |
| 53 | + api = UMAPI('http://example.com/failure', "N/A", retry_max_attempts=1) |
| 54 | + pytest.raises(UMAPIError, api.users, None) |
| 55 | + patch.return_value = retry_response |
| 56 | + api = UMAPI('http://example.com/retry', "N/A", retry_max_attempts=1) |
| 57 | + pytest.raises(UMAPIRetryError, api.users, None) |
| 58 | + |
| 59 | + |
| 60 | +@mock.patch('umapi_client.legacy.requests.get', return_value=success_response) |
| 61 | +def test_list_groups_success(_): |
| 62 | + """Test Groups List - SUCCESS""" |
| 63 | + api = UMAPI('http://example.com/success', "N/A", retry_max_attempts=1) |
| 64 | + assert api.groups(None) == {"result": "success"} |
| 65 | + |
| 66 | + |
| 67 | +@mock.patch('umapi_client.legacy.requests.post', return_value=success_response) |
| 68 | +def test_user_create_success(_): |
| 69 | + """Test User Creation - SUCCESS""" |
| 70 | + api = UMAPI('http://example.com/success', "N/A", retry_max_attempts=1) |
| 71 | + |
| 72 | + action = Action( user_key="[email protected]"). do( |
| 73 | + addAdobeID={ "email": "[email protected]"} |
| 74 | + ) |
| 75 | + |
| 76 | + assert api.action(None, action) == {"result": "success"} |
| 77 | + |
| 78 | + |
| 79 | +@mock.patch('umapi_client.legacy.requests.post', return_value=error_response) |
| 80 | +def test_user_create_error(_): |
| 81 | + """Test User Creation - ERROR""" |
| 82 | + api = UMAPI('http://example.com/error', "N/A", retry_max_attempts=1) |
| 83 | + |
| 84 | + action = Action( user_key="[email protected]"). do( |
| 85 | + addAdobeID={ "email": "[email protected]"} |
| 86 | + ) |
| 87 | + pytest.raises(UMAPIRequestError, api.action, None, action) |
| 88 | + |
| 89 | + |
| 90 | +@mock.patch('umapi_client.legacy.requests.post', return_value=not_found_response) |
| 91 | +def test_user_create_failure(patch): |
| 92 | + """Test User Creation - FAILURE""" |
| 93 | + action = Action( user_key="[email protected]"). do( |
| 94 | + addAdobeID={ "email": "[email protected]"} |
| 95 | + ) |
| 96 | + api = UMAPI('http://example.com/failure', "N/A", retry_max_attempts=1) |
| 97 | + pytest.raises(UMAPIError, api.action, None, action) |
| 98 | + patch.return_value = retry_response |
| 99 | + api = UMAPI('http://example.com/retry', "N/A", retry_max_attempts=1) |
| 100 | + pytest.raises(UMAPIRetryError, api.action, None, action) |
| 101 | + |
| 102 | + |
| 103 | +@mock.patch('umapi_client.legacy.requests.post', return_value=success_response) |
| 104 | +def test_product_add(_): |
| 105 | + """Test Product Add - SUCCESS""" |
| 106 | + api = UMAPI('http://example.com/success', "N/A", retry_max_attempts=1) |
| 107 | + |
| 108 | + action = Action( user_key="[email protected]"). do( |
| 109 | + add=["product1", "product2"] |
| 110 | + ) |
| 111 | + |
| 112 | + assert api.action(None, action) == {"result": "success"} |
| 113 | + |
| 114 | + |
| 115 | +@mock.patch('umapi_client.legacy.requests.post', return_value=success_response) |
| 116 | +def test_action_format_error(_): |
| 117 | + """Test Action Format Error""" |
| 118 | + api = UMAPI('http://example.com/success', "N/A", retry_max_attempts=1) |
| 119 | + action = '' |
| 120 | + pytest.raises(ActionFormatError, api.action, None, action) |
| 121 | + |
| 122 | + |
| 123 | +def test_action_obj_create(): |
| 124 | + """"Create a user creation action object and make sure that we can serialize it in the expected format""" |
| 125 | + action = Action( user="[email protected]"). do( |
| 126 | + addAdobeID={ "email": "[email protected]"} |
| 127 | + ) |
| 128 | + assert json.dumps(action.wire_dict(), sort_keys=True) == \ |
| 129 | + '{"do": [{"addAdobeID": {"email": "[email protected]"}}], "user": "[email protected]"}' |
| 130 | + action = Action( user_key="[email protected]"). do( |
| 131 | + addAdobeID={ "email": "[email protected]"} |
| 132 | + ) |
| 133 | + assert json.dumps(action.wire_dict(), sort_keys=True) == \ |
| 134 | + '{"do": [{"addAdobeID": {"email": "[email protected]"}}], "user": "[email protected]"}' |
| 135 | + |
| 136 | + |
| 137 | +def test_action_obj_remove(): |
| 138 | + """"Create a user removal action object""" |
| 139 | + action = Action( user="[email protected]"). do( |
| 140 | + removeFromOrg={} |
| 141 | + ) |
| 142 | + assert json.dumps(action.wire_dict(), sort_keys=True) == \ |
| 143 | + '{"do": [{"removeFromOrg": {}}], "user": "[email protected]"}' |
| 144 | + action = Action( user_key="[email protected]"). do( |
| 145 | + removeFromOrg={} |
| 146 | + ) |
| 147 | + assert json.dumps(action.wire_dict(), sort_keys=True) == \ |
| 148 | + '{"do": [{"removeFromOrg": {}}], "user": "[email protected]"}' |
| 149 | + |
| 150 | + |
| 151 | +def test_action_obj_update(): |
| 152 | + """Create a user update action object""" |
| 153 | + action = Action( user="[email protected]"). do( |
| 154 | + update={"firstname": "example", "lastname": "user"} |
| 155 | + ) |
| 156 | + assert json.dumps(action.wire_dict(), sort_keys=True) == \ |
| 157 | + '{"do": [{"update": {"firstname": "example", "lastname": "user"}}], "user": "[email protected]"}' |
| 158 | + action = Action( user_key="[email protected]"). do( |
| 159 | + update={"firstname": "example", "lastname": "user"} |
| 160 | + ) |
| 161 | + assert json.dumps(action.wire_dict(), sort_keys=True) == \ |
| 162 | + '{"do": [{"update": {"firstname": "example", "lastname": "user"}}], "user": "[email protected]"}' |
| 163 | + |
| 164 | + |
| 165 | +def test_action_obj_multi(): |
| 166 | + """Create a multi-action action object""" |
| 167 | + action = Action( user="[email protected]"). do( |
| 168 | + addAdobeID={ "email": "[email protected]"}, |
| 169 | + add=["product1", "product2"], |
| 170 | + remove=["product3"] |
| 171 | + ) |
| 172 | + assert json.dumps(action.wire_dict(), sort_keys=True) == \ |
| 173 | + '{"do": [{"addAdobeID": {"email": "[email protected]"}}, {"add": {"product": ["product1", "product2"]}}, {"remove": {"product": ["product3"]}}], "user": "[email protected]"}' |
| 174 | + |
| 175 | + |
| 176 | +def test_action_obj_requestid(): |
| 177 | + """Include a request ID in action object""" |
| 178 | + action = Action( user="[email protected]", requestID="abc123"). do( |
| 179 | + add=["product1"] |
| 180 | + ) |
| 181 | + assert json.dumps(action.wire_dict(), sort_keys=True) == \ |
| 182 | + '{"do": [{"add": {"product": ["product1"]}}], "requestID": "abc123", "user": "[email protected]"}' |
0 commit comments