Skip to content

Commit

Permalink
Merge pull request #104819 from cockroachdb/blathers/backport-release…
Browse files Browse the repository at this point in the history
…-23.1-104622

release-23.1: ui: hide tenant dropdown when unneeded
  • Loading branch information
Santamaura authored Jun 14, 2023
2 parents ccb95c8 + c2eba7c commit 6c40223
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
1 change: 1 addition & 0 deletions pkg/ui/workspaces/db-console/src/redux/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// licenses/APL.txt.

export const MULTITENANT_SESSION_COOKIE_NAME = "session";
export const SYSTEM_TENANT_NAME = "system";

export const getAllCookies = (): Map<string, string> => {
const cookieMap: Map<string, string> = new Map();
Expand Down
17 changes: 17 additions & 0 deletions pkg/ui/workspaces/db-console/src/redux/tenants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2023 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

// isSystemTenant checks whether the provided tenant name is the
// system tenant.
import { SYSTEM_TENANT_NAME } from "./cookies";

export const isSystemTenant = (tenantName: string): boolean => {
return tenantName === SYSTEM_TENANT_NAME;
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,32 @@ jest.mock("src/redux/cookies", () => ({
}));

describe("TenantDropdown", () => {
it("returns null if there are no tenants in the session cookie", () => {
it("returns null if there's no current tenant", () => {
(
selectTenantsFromMultitenantSessionCookie as jest.MockedFn<
typeof selectTenantsFromMultitenantSessionCookie
>
).mockReturnValueOnce([]);
(
getCookieValue as jest.MockedFn<typeof getCookieValue>
).mockReturnValueOnce(null);
const wrapper = shallow(<TenantDropdown />);
expect(wrapper.isEmptyRender());
});
// Mutli-tenant scenarios
it("returns null if there are no tenants or less than 2 tenants in the session cookie", () => {
(
selectTenantsFromMultitenantSessionCookie as jest.MockedFn<
typeof selectTenantsFromMultitenantSessionCookie
>
).mockReturnValueOnce(["system"]);
(
getCookieValue as jest.MockedFn<typeof getCookieValue>
).mockReturnValueOnce("system");
const wrapper = shallow(<TenantDropdown />);
expect(wrapper.isEmptyRender());
});
it("returns a dropdown list of tenant options if there are tenant in the session cookie", () => {
it("returns a dropdown list of tenant options if there are multiple tenant in the session cookie", () => {
(
selectTenantsFromMultitenantSessionCookie as jest.MockedFn<
typeof selectTenantsFromMultitenantSessionCookie
Expand All @@ -40,6 +56,18 @@ describe("TenantDropdown", () => {
getCookieValue as jest.MockedFn<typeof getCookieValue>
).mockReturnValueOnce("system");
const wrapper = shallow(<TenantDropdown />);
expect(wrapper.find({ children: "Tenant system" }).length).toEqual(1);
expect(wrapper.find({ children: "Tenant: system" }).length).toEqual(1);
});
it("returns a dropdown if the there is a single tenant option but isn't system tenant", () => {
(
selectTenantsFromMultitenantSessionCookie as jest.MockedFn<
typeof selectTenantsFromMultitenantSessionCookie
>
).mockReturnValueOnce(["app"]);
(
getCookieValue as jest.MockedFn<typeof getCookieValue>
).mockReturnValueOnce("app");
const wrapper = shallow(<TenantDropdown />);
expect(wrapper.find({ children: "Tenant: app" }).length).toEqual(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import React from "react";
import { Dropdown } from "@cockroachlabs/cluster-ui";
import ErrorBoundary from "../errorMessage/errorBoundary";
import "./tenantDropdown.styl";
import { isSystemTenant } from "src/redux/tenants";

const tenantIDKey = "tenant";

Expand All @@ -26,7 +27,7 @@ const TenantDropdown = () => {
const createDropdownItems = () => {
return (
tenants?.map(tenantID => {
return { name: "Tenant " + tenantID, value: tenantID };
return { name: "Tenant: " + tenantID, value: tenantID };
}) || []
);
};
Expand All @@ -38,17 +39,17 @@ const TenantDropdown = () => {
}
};

if (tenants.length == 0) {
if (!currentTenant || (tenants.length < 2 && isSystemTenant(currentTenant))) {
return null;
}

return (
<ErrorBoundary>
<Dropdown
items={createDropdownItems()}
onChange={tenantID => onTenantChange(tenantID)}
onChange={(tenantID: string) => onTenantChange(tenantID)}
>
<div className="tenant-selected">{"Tenant " + currentTenant}</div>
<div className="tenant-selected">{"Tenant: " + currentTenant}</div>
</Dropdown>
</ErrorBoundary>
);
Expand Down

0 comments on commit 6c40223

Please sign in to comment.