async def test_list_cluster_users_default_cluster( aiohttp_server: _TestServerFactory, make_client: _MakeClient ) -> None: requested_clusters = [] async def handle_list_cluster_user(request: web.Request) -> web.StreamResponse: requested_clusters.append(request.match_info["cluster_name"]) data = [ {"user_name": "denis", "role": "admin"}, {"user_name": "andrew", "role": "manager"}, {"user_name": "ivan", "role": "user"}, ] return web.json_response(data) app = web.Application() app.router.add_get( "/apis/admin/v1/clusters/{cluster_name}/users", handle_list_cluster_user ) srv = await aiohttp_server(app) async with make_client(srv.make_url("/api/v1")) as client: resp = await client._admin.list_cluster_users() assert resp == [ _ClusterUser(user_name="denis", role=_ClusterUserRoleType("admin")), _ClusterUser(user_name="andrew", role=_ClusterUserRoleType("manager")), _ClusterUser(user_name="ivan", role=_ClusterUserRoleType("user")), ] assert requested_clusters == ["default"]
def test_cluster_list(self) -> None: formatter = ClusterUserFormatter() users = [ _ClusterUser(user_name="denis", role=_ClusterUserRoleType("admin")), _ClusterUser(user_name="andrew", role=_ClusterUserRoleType("manager")), _ClusterUser(user_name="ivan", role=_ClusterUserRoleType("user")), _ClusterUser(user_name="alex", role=_ClusterUserRoleType("user")), ] expected_out = [ "\x1b[1mName\x1b[0m \x1b[1mRole\x1b[0m ", "alex user ", "andrew manager", "denis admin ", "ivan user ", ] assert formatter(users) == expected_out
async def test_add_cluster_user(aiohttp_server: _TestServerFactory, make_client: _MakeClient) -> None: requested_clusters = [] requested_payloads = [] async def handle_add_cluster_user( request: web.Request) -> web.StreamResponse: payload = await request.json() requested_clusters.append(request.match_info["cluster_name"]) requested_payloads.append(payload) return web.json_response(payload, status=HTTPCreated.status_code) app = web.Application() app.router.add_post("/apis/admin/v1/clusters/{cluster_name}/users", handle_add_cluster_user) srv = await aiohttp_server(app) async with make_client(srv.make_url("/api/v1")) as client: resp = await client._admin.add_cluster_user("default", "ivan", "user") assert resp == _ClusterUser(user_name="ivan", role=_ClusterUserRoleType("user")) assert requested_clusters == ["default"] assert requested_payloads == [{"role": "user", "user_name": "ivan"}]
async def add_cluster_user(cluster_name: str, user_name: str, role: str) -> _ClusterUser: # NOTE: We return a different role to check that we print it to user return _ClusterUser(user_name, _ClusterUserRoleType.MANAGER)