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_ua_string_additional(): conn = Connection(user_agent="additional/1.0", **mock_connection_params) req = conn.session.prepare_request( requests.Request('GET', "http://test.com/")) ua_header = req.headers.get("User-Agent") assert ua_header.startswith("additional/1.0 umapi-client/" + umapi_version) req = conn.session.prepare_request( requests.Request('POST', "http://test.com/", data="This is a test")) ua_header = req.headers.get("User-Agent") assert ua_header.startswith("additional/1.0 umapi-client/" + umapi_version)
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_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_query_users(): conn = Connection(**mock_connection_params) query = UsersQuery(conn) assert query.url_params == [] assert query.query_params == {"directOnly": True} query = UsersQuery(conn, in_group="test", in_domain="test.com", direct_only=False) assert query.url_params == ["test"] assert query.query_params == {"directOnly": False, "domain": "test.com"}
def test_post_retry_logging(log_stream): with mock.patch("umapi_client.connection.requests.Session.post") as mock_post: mock_post.return_value = MockResponse(429, headers={"Retry-After": "3"}) stream, logger = log_stream params = dict(mock_connection_params) params["logger"] = logger conn = Connection(**params) pytest.raises(UnavailableError, conn.make_call, "", [3, 5]) stream.flush() log = stream.getvalue() # save as a local so can do pytest -l to see exact log assert log == """UMAPI timeout...service unavailable (code 429 on try 1)
def test_ua_string(): conn = Connection(**mock_connection_params) req = conn.session.prepare_request( requests.Request('GET', "http://test.com/")) ua_header = req.headers.get("User-Agent") assert ua_header.startswith("umapi-client/" + umapi_version) assert " Python" in ua_header req = conn.session.prepare_request( requests.Request('POST', "http://test.com/", data="This is a test")) ua_header = req.headers.get("User-Agent") assert ua_header.startswith("umapi-client/" + umapi_version) assert " Python" in ua_header
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, 0, 1, 1, 0)
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_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_qm_user_iterate_complete(): 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) qm = QueryMultiple(conn, "user") for obj in qm: if obj["name"] == "n3": break assert qm.all_results() == [{ "name": "n1", "type": "user" }, { "name": "n2", "type": "user" }, { "name": "n3", "type": "user" }, { "name": "n4", "type": "user" }]
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_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_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_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_remote_status_error_logging(log_stream): with mock.patch( "umapi_client.connection.requests.Session.get") as mock_get: mock_get.side_effect = requests.ConnectionError stream, logger = log_stream params = dict(mock_connection_params) params["logger"] = logger conn = Connection(**params) pytest.raises(UnavailableError, conn.make_call, "") stream.flush() log = stream.getvalue( ) # save as a local so can do pytest -l to see exact log assert "code Error on try 1" in log assert "code Error on try 2" in log assert "code Error on try 3" in log assert "code Error on try 4" not in log
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_execute_multiple_multi_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": "error1", "message": "message1" }, { "index": 1, "step": 1, "errorCode": "error2", "message": "message2" }] }) conn = Connection(**mock_connection_params) action0 = Action(top="top0").append(a="a0") action1 = Action(top="top1").append(a="a1").append(b="b") assert conn.execute_multiple([action0, action1]) == (0, 2, 1) assert action0.execution_errors() == [] assert action1.execution_errors() == [{ "command": { "b": "b" }, "target": { "top": "top1" }, "errorCode": "error1", "message": "message1" }, { "command": { "b": "b" }, "target": { "top": "top1" }, "errorCode": "error2", "message": "message2" }]
def test_remote_status_success(): with mock.patch( "umapi_client.connection.requests.Session.get") as mock_get: mock_get.return_value = MockResponse(200, body={ "build": "2559", "version": "2.1.54", "state": "LIVE" }) conn = Connection(**mock_connection_params) _, remote_status = conn.status(remote=True) assert remote_status == { "endpoint": "https://test/", "build": "2559", "version": "2.1.54", "state": "LIVE" }
def test_query_multiple_usergroup_paged(): 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) assert conn.query_multiple("user-group", 0) == ([{ "name": "n1", "type": "user-group" }, { "name": "n2", "type": "user-group" }], False, 4, 2, 1, 2) assert conn.query_multiple("user-group", 1) == ([{ "name": "n3", "type": "user-group" }, { "name": "n4", "type": "user-group" }], True, 4, 2, 2, 2)
def test_qm_user_iterate_partial(): 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": False, "users": [{ "name": "n1", "type": "user" }, { "name": "n2", "type": "user" }] }), MockResponse(200, {"result": "error"}) ] conn = Connection(**mock_connection_params) qm = QueryMultiple(conn, "user") for obj in qm: if obj["name"] == "n2": break for obj in qm: if obj["name"] == "n2": break pytest.raises(ClientError, qm.all_results)
def test_execute_single_multi_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": "error1", "message": "message1" }, { "index": 0, "step": 0, "errorCode": "error2", "message": "message2" }] }) conn = Connection(**mock_connection_params) action = Action(top="top").append(a="a") assert conn.execute_single(action, immediate=True) == (0, 1, 0) assert action.execution_errors() == [{ "command": { "a": "a" }, "target": { "top": "top" }, "errorCode": "error1", "message": "message1" }, { "command": { "a": "a" }, "target": { "top": "top" }, "errorCode": "error2", "message": "message2" }]
def test_qm_usergroup_reload(): 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"}), MockResponse(200, [{"name": "n5", "type": "user-group"}, {"name": "n6", "type": "user-group"}], {"X-Total-Count": "4", "X-Page-Count": "2", "X-Current-Page": "1", "X-Page-Size:": "2"}), MockResponse(200, [{"name": "n7", "type": "user-group"}, {"name": "n8", "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") assert list(qm) == [{"name": "n1", "type": "user-group"}, {"name": "n2", "type": "user-group"}, {"name": "n3", "type": "user-group"}, {"name": "n4", "type": "user-group"}] qm.reload() assert qm.all_results() == [{"name": "n5", "type": "user-group"}, {"name": "n6", "type": "user-group"}, {"name": "n7", "type": "user-group"}, {"name": "n8", "type": "user-group"}]
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_execute_single_error_queued_throttled(): with mock.patch( "umapi_client.connection.requests.Session.post") as mock_post: mock_post.side_effect = [ MockResponse(200, {"result": "success"}), MockResponse( 200, { "result": "partial", "completed": 1, "notCompleted": 1, "errors": [{ "index": 1, "step": 0, "errorCode": "test.error", "message": "Test error message" }] }) ] conn = Connection(throttle_actions=2, throttle_commands=1, **mock_connection_params) action = Action(top="top").append(a="a").append(b="b").append( c="c").append(d="d") assert conn.execute_single(action) == (0, 4, 3) assert action.execution_errors() == [{ "command": { "d": "d" }, "target": { "top": "top" }, "errorCode": "test.error", "message": "Test error message" }]
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_query_single_error(): with mock.patch("umapi_client.connection.requests.Session.get") as mock_get: mock_get.return_value = MockResponse(200, {"result": "error"}) conn = Connection(**mock_connection_params) pytest.raises(ClientError, conn.query_single, "user", ["n1"])
def test_query_single_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) assert conn.query_single("user", ["n1"]) == {}
def test_query_multiple_usergroup_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) assert conn.query_multiple("user-group") == ([], True)