def test_query_multiple_user_empty(): with mock.patch("umapi_client.connection.requests.Session.get") as mock_get: mock_get.return_value = MockResponse(200, {"result": "success", "lastPage": True, "users": []}) conn = Connection(**mock_connection_params) assert conn.query_multiple("user") == ([], True)
def test_execute_single_success_immediate(): with mock.patch( "umapi_client.connection.requests.Session.post") as mock_post: mock_post.return_value = MockResponse(200, {"result": "success"}) conn = Connection(**mock_connection_params) action = Action(top="top").append(a="a") assert conn.execute_single(action, immediate=True) == (0, 1, 1)
def test_remote_status_failure(): with mock.patch( "umapi_client.connection.requests.Session.get") as mock_get: mock_get.return_value = MockResponse(404, text="404 Not Found") conn = Connection(**mock_connection_params) _, remote_status = conn.status(remote=True) assert remote_status["status"].startswith("Unexpected")
def test_remote_status_error(): with mock.patch( "umapi_client.connection.requests.Session.get") as mock_get: mock_get.side_effect = requests.ConnectionError conn = Connection(**mock_connection_params) _, remote_status = conn.status(remote=True) assert remote_status["status"].startswith("Unreachable")
def test_execute_single_error_immediate_throttled(): with mock.patch( "umapi_client.connection.requests.Session.post") as mock_post: mock_post.return_value = MockResponse( 200, { "result": "partial", "completed": 1, "notCompleted": 1, "errors": [{ "index": 1, "step": 0, "errorCode": "test" }] }) conn = Connection(throttle_commands=2, **mock_connection_params) action = Action(top="top0").append(a="a0").append(a="a1").append( a="a2") assert conn.execute_single(action, immediate=True) == (0, 2, 1) assert action.execution_errors() == [{ "command": { "a": "a2" }, "target": { "top": "top0" }, "errorCode": "test" }]
def test_execute_single_dofirst_error_immediate(): with mock.patch( "umapi_client.connection.requests.Session.post") as mock_post: mock_post.return_value = MockResponse( 200, { "result": "error", "errors": [{ "index": 0, "step": 0, "errorCode": "test.error", "message": "Test error message" }] }) conn = Connection(**mock_connection_params) action = Action(top="top").insert(a="a") assert conn.execute_single(action, immediate=True) == (0, 1, 0) assert action.execution_errors() == [{ "command": { "a": "a" }, "target": { "top": "top" }, "errorCode": "test.error", "message": "Test error message" }]
def test_execute_multiple_dofirst_error(): with mock.patch( "umapi_client.connection.requests.Session.post") as mock_post: mock_post.return_value = MockResponse( 200, { "result": "error", "completed": 1, "notCompleted": 1, "errors": [{ "index": 1, "step": 1, "errorCode": "test.error", "message": "Test error message" }] }) conn = Connection(**mock_connection_params) action0 = Action(top="top0").append(a="a0") action1 = Action(top="top1").append(a="a1").insert(b="b") assert conn.execute_multiple([action0, action1]) == (0, 2, 1) assert action0.execution_errors() == [] assert action1.execution_errors() == [{ "command": { "a": "a1" }, "target": { "top": "top1" }, "errorCode": "test.error", "message": "Test error message" }]
def test_query_multiple_user_success(): with mock.patch( "umapi_client.connection.requests.Session.get") as mock_get: mock_get.return_value = MockResponse( 200, { "result": "success", "lastPage": False, "users": [{ "name": "n1", "type": "user" }, { "name": "n2", "type": "user" }] }) conn = Connection(**mock_connection_params) assert conn.query_multiple("user") == ([{ "name": "n1", "type": "user" }, { "name": "n2", "type": "user" }], False, 0, 0, 1, 0)
def test_execute_multiple_queued_throttle_actions_error(): with mock.patch( "umapi_client.connection.requests.Session.post") as mock_post: mock_post.side_effect = [ MockResponse(500), MockResponse(200, {"result": "success"}), MockResponse(200, {"result": "success"}) ] conn = Connection(throttle_actions=2, **mock_connection_params) action0 = Action(top="top0").append(a="a0") action1 = Action(top="top1").append(a="a1") action2 = Action(top="top2").append(a="a2") action3 = Action(top="top3").append(a="a3") action4 = Action(top="top4").append(a="a4") action5 = Action(top="top5").append(a="a5") pytest.raises(BatchError, conn.execute_multiple, [action0, action1, action2, action3, action4, action5], immediate=False) local_status, _ = conn.status(remote=False) assert local_status == { "multiple-query-count": 0, "single-query-count": 0, "actions-sent": 6, "actions-completed": 4, "actions-queued": 0 }
def test_mock_proxy_get(): with mock.patch("umapi_client.connection.requests.Session.get") as mock_get: mock_get.return_value = MockResponse(200, body=["test", "body"]) with mock.patch("umapi_client.connection.os.getenv") as mock_getenv: mock_getenv.return_value = "proxy" conn = Connection(**mock_connection_params) conn.make_call("").json() mock_get.assert_called_with('http://test/', auth='N/A', timeout=120.0)
def test_execute_single_success_queued(): with mock.patch( "umapi_client.connection.requests.Session.post") as mock_post: mock_post.return_value = MockResponse(200, {"result": "success"}) conn = Connection(throttle_actions=2, **mock_connection_params) action = Action(top="top").append(a="a") assert conn.execute_single(action) == (1, 0, 0) assert conn.execute_single(action) == (0, 2, 2)
def test_execute_multiple_dofirst_success(): with mock.patch( "umapi_client.connection.requests.Session.post") as mock_post: mock_post.return_value = MockResponse(200, {"result": "success"}) conn = Connection(**mock_connection_params) action0 = Action(top="top0").append(a="a0").insert(b="b") action1 = Action(top="top1").append(a="a1") assert conn.execute_multiple([action0, action1]) == (0, 2, 2)
def test_mock_playback_get(): with mock.patch("umapi_client.connection.requests.Session.get") as mock_get: mock_get.return_value = MockResponse(200, body=["test", "body"]) with mock.patch("umapi_client.connection.os.getenv") as mock_getenv: mock_getenv.return_value = "playback" conn = Connection(**mock_connection_params) conn.make_call("").json() assert mock_get.call_args[0][0] == 'http://test/' assert isinstance(mock_get.call_args[1]['auth'], Auth)
def test_query_multiple_usergroup_empty(): with mock.patch("umapi_client.connection.requests.Session.get") as mock_get: mock_get.return_value = MockResponse(200, [], {"X-Total-Count": "0", "X-Page-Count": "1", "X-Current-Page": "1", "X-Page-Size:": "0"}) conn = Connection(**mock_connection_params) assert conn.query_multiple("user-group") == ([], True)
def test_query_multiple_usergroup_success(): with mock.patch("umapi_client.connection.requests.Session.get") as mock_get: mock_get.return_value = MockResponse(200, [{"name": "n1", "type": "user-group"}, {"name": "n2", "type": "user-group"}], {"X-Total-Count": "4", "X-Page-Count": "2", "X-Current-Page": "1", "X-Page-Size:": "2"}) conn = Connection(**mock_connection_params) assert conn.query_multiple("user-group") == ([{"name": "n1", "type": "user-group"}, {"name": "n2", "type": "user-group"}], False)
def test_query_single_success(): with mock.patch( "umapi_client.connection.requests.Session.get") as mock_get: mock_get.return_value = MockResponse(200, { "result": "success", "user": { "name": "n1", "type": "user" } }) conn = Connection(**mock_connection_params) assert conn.query_single("user", ["n1"]) == { "name": "n1", "type": "user" }
def test_query_multiple_user_paged(): with mock.patch( "umapi_client.connection.requests.Session.get") as mock_get: mock_get.side_effect = [ MockResponse( 200, { "result": "success", "lastPage": False, "users": [{ "name": "n1", "type": "user" }, { "name": "n2", "type": "user" }] }), MockResponse( 200, { "result": "success", "lastPage": True, "users": [{ "name": "n3", "type": "user" }, { "name": "n4", "type": "user" }] }) ] conn = Connection(**mock_connection_params) assert conn.query_multiple("user", 0) == ([{ "name": "n1", "type": "user" }, { "name": "n2", "type": "user" }], False, 0, 0, 1, 0) assert conn.query_multiple("user", 1) == ([{ "name": "n3", "type": "user" }, { "name": "n4", "type": "user" }], True, 0, 0, 1, 0)
def test_get_server_fail(): with mock.patch( "umapi_client.connection.requests.Session.get") as mock_get: mock_get.return_value = MockResponse(500, text="500 test server failure") conn = Connection(**mock_connection_params) pytest.raises(ServerError, conn.make_call, "")
def test_post_request_fail(): with mock.patch( "umapi_client.connection.requests.Session.post") as mock_post: mock_post.return_value = MockResponse(400, text="400 test request failure") conn = Connection(**mock_connection_params) pytest.raises(RequestError, conn.make_call, "", "[3, 5]")
def test_large_group_action_split(): """ Ensure that very large group lists (100+) will be handled appropriately Connection.execute_multiple splits commands and splits actions Result should be 2 actions, even though we only created one action :return: """ with mock.patch("umapi_client.connection.requests.Session.post") as mock_post: mock_post.return_value = MockResponse(200, {"result": "success"}) conn = Connection(**mock_connection_params) group_prefix = "G" add_groups = [group_prefix+six.text_type(n+1) for n in range(0, 150)] user = UserAction(id_type=IdentityTypes.enterpriseID, email="*****@*****.**") user.add_to_groups(groups=add_groups, group_type=GroupTypes.usergroup) assert conn.execute_single(user, immediate=True) == (0, 2, 2)
def test_qs_success(): with mock.patch("umapi_client.connection.requests.Session.get") as mock_get: mock_get.return_value = MockResponse(200, {"result": "success", "user": {"user": "******", "type": "adobeID"}}) conn = Connection(**mock_connection_params) qs = QuerySingle(conn, "user", ["*****@*****.**"]) assert qs.result() == {"user": "******", "type": "adobeID"}
def test_qm_usergroup_iterate_partial(): with mock.patch("umapi_client.connection.requests.Session.get") as mock_get: mock_get.side_effect = [MockResponse(200, [{"name": "n1", "type": "user-group"}, {"name": "n2", "type": "user-group"}], {"X-Total-Count": "6", "X-Page-Count": "3", "X-Current-Page": "1", "X-Page-Size:": "2"}), MockResponse(200, [{"name": "n1", "type": "user-group"}, {"name": "n2", "type": "user-group"}], {"X-Total-Count": "6", "X-Page-Count": "3", "X-Current-Page": "2", "X-Page-Size:": "2"}), MockResponse(400, text="400 bad request")] conn = Connection(**mock_connection_params) qm = QueryMultiple(conn, "user-group") for obj in qm: if obj["name"] == "n2": break for obj in qm: if obj["name"] == "n2": break pytest.raises(RequestError, qm.all_results)
def test_qm_user_reload(): with mock.patch("umapi_client.connection.requests.Session.get") as mock_get: mock_get.side_effect = [MockResponse(200, {"result": "success", "lastPage": False, "users": [{"name": "n1", "type": "user"}, {"name": "n2", "type": "user"}]}), MockResponse(200, {"result": "success", "lastPage": True, "users": [{"name": "n3", "type": "user"}, {"name": "n4", "type": "user"}]}), MockResponse(200, {"result": "success", "lastPage": False, "users": [{"name": "n5", "type": "user"}, {"name": "n6", "type": "user"}]}), MockResponse(200, {"result": "success", "lastPage": True, "users": [{"name": "n7", "type": "user"}, {"name": "n8", "type": "user"}]})] conn = Connection(**mock_connection_params) qm = QueryMultiple(conn, "user") assert list(qm) == [{"name": "n1", "type": "user"}, {"name": "n2", "type": "user"}, {"name": "n3", "type": "user"}, {"name": "n4", "type": "user"}] qm.reload() assert qm.all_results() == [{"name": "n5", "type": "user"}, {"name": "n6", "type": "user"}, {"name": "n7", "type": "user"}, {"name": "n8", "type": "user"}]
def test_get_retry_header_time_2(): with mock.patch( "umapi_client.connection.requests.Session.get") as mock_get: mock_get.return_value = MockResponse( 502, headers={"Retry-After": formatdate(time.time() + 2.5)}) conn = Connection(**mock_connection_params) pytest.raises(UnavailableError, conn.make_call, "")
def test_group_size_limit(): """ Test with different 'throttle_groups' value, which governs the max size of the group list before commands are split :return: """ with mock.patch("umapi_client.connection.requests.Session.post") as mock_post: mock_post.return_value = MockResponse(200, {"result": "success"}) params = mock_connection_params params['throttle_groups'] = 5 conn = Connection(**params) group_prefix = "G" add_groups = [group_prefix+six.text_type(n+1) for n in range(0, 150)] user = UserAction(id_type=IdentityTypes.enterpriseID, email="*****@*****.**") user.add_to_groups(groups=add_groups, group_type=GroupTypes.usergroup) assert conn.execute_single(user, immediate=True) == (0, 3, 3)
def test_post_retry_header_0(): with mock.patch( "umapi_client.connection.requests.Session.post") as mock_post: mock_post.return_value = MockResponse(503, headers={"Retry-After": "0"}) conn = Connection(**mock_connection_params) pytest.raises(UnavailableError, conn.make_call, "", "[3, 5]")
def test_qs_not_found(): with mock.patch( "umapi_client.connection.requests.Session.get") as mock_get: mock_get.return_value = MockResponse(404, text="404 Object not found") conn = Connection(**mock_connection_params) qs = QuerySingle(conn, "user", ["*****@*****.**"]) assert qs.result() == {}
def test_qm_usergroup_iterate_complete(): with mock.patch("umapi_client.connection.requests.Session.get") as mock_get: mock_get.side_effect = [MockResponse(200, [{"name": "n1", "type": "user-group"}, {"name": "n2", "type": "user-group"}], {"X-Total-Count": "4", "X-Page-Count": "2", "X-Current-Page": "1", "X-Page-Size:": "2"}), MockResponse(200, [{"name": "n3", "type": "user-group"}, {"name": "n4", "type": "user-group"}], {"X-Total-Count": "4", "X-Page-Count": "2", "X-Current-Page": "2", "X-Page-Size:": "2"})] conn = Connection(**mock_connection_params) qm = QueryMultiple(conn, "user-group") for obj in qm: if obj["name"] == "n3": break assert qm.all_results() == [{"name": "n1", "type": "user-group"}, {"name": "n2", "type": "user-group"}, {"name": "n3", "type": "user-group"}, {"name": "n4", "type": "user-group"}]
def test_qs_reload(): with mock.patch( "umapi_client.connection.requests.Session.get") as mock_get: mock_get.side_effect = [ MockResponse( 200, { "result": "success", "user": { "user": "******", "type": "adobeID" } }), MockResponse( 200, { "result": "success", "user": { "user": "******", "type": "adobeID" } }) ] conn = Connection(**mock_connection_params) qs = QuerySingle(conn, "user", ["*****@*****.**"]) assert qs.result() == {"user": "******", "type": "adobeID"} # second verse, same as the first assert qs.result() == {"user": "******", "type": "adobeID"} qs.reload() assert qs.result() == {"user": "******", "type": "adobeID"}
def test_post_timeout(): with mock.patch( "umapi_client.connection.requests.Session.post") as mock_post: mock_post.side_effect = requests.Timeout conn = Connection(**dict(mock_connection_params, retry_max_attempts=2)) pytest.raises(UnavailableError, conn.make_call, "", [3, 5]) assert mock_post.call_count == 2