Skip to content

Commit

Permalink
Always show Bots page with conditional permissions error
Browse files Browse the repository at this point in the history
This will make the bots page always visible to users. If they do not
have permissions, they will see the empty state as well as an error of
what permissions are missing.
  • Loading branch information
avatus authored and github-actions committed Dec 12, 2024
1 parent d2d67f0 commit ba2534a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
27 changes: 24 additions & 3 deletions web/packages/teleport/src/Bots/List/Bots.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,20 @@ import { render, screen, userEvent, waitFor } from 'design/utils/testing';

import api from 'teleport/services/api';
import { botsApiResponseFixture } from 'teleport/Bots/fixtures';
import { createTeleportContext } from 'teleport/mocks/contexts';
import {
allAccessAcl,
createTeleportContext,
noAccess,
} from 'teleport/mocks/contexts';
import { ContextProvider } from 'teleport/index';
import TeleportContext from 'teleport/teleportContext';

import { Bots } from './Bots';

function renderWithContext(element) {
const ctx = createTeleportContext();
function renderWithContext(element, ctx?: TeleportContext) {
if (!ctx) {
ctx = createTeleportContext();
}
return render(
<MemoryRouter>
<ContextProvider ctx={ctx}>{element}</ContextProvider>
Expand Down Expand Up @@ -57,6 +64,20 @@ test('shows empty state when bots are empty', async () => {
});
});

test('shows missing permissions error if user lacks permissions to list', async () => {
jest.spyOn(api, 'get').mockResolvedValue({ items: [] });
const ctx = createTeleportContext();
ctx.storeUser.setState({ acl: { ...allAccessAcl, bots: noAccess } });
renderWithContext(<Bots />, ctx);

await waitFor(() => {
expect(screen.getByTestId('bots-empty-state')).toBeInTheDocument();
});
expect(
screen.getByText(/You do not have permission to access Bots/i)
).toBeInTheDocument();
});

test('calls edit endpoint', async () => {
jest
.spyOn(api, 'get')
Expand Down
25 changes: 18 additions & 7 deletions web/packages/teleport/src/Bots/List/Bots.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,15 @@ export function Bots() {
const ctx = useTeleport();
const flags = ctx.getFeatureFlags();
const hasAddBotPermissions = flags.addBots;
const canListBots = flags.listBots;

const [bots, setBots] = useState<FlatBot[]>([]);
const [selectedBot, setSelectedBot] = useState<FlatBot>();
const [selectedRoles, setSelectedRoles] = useState<string[]>();
const { attempt: crudAttempt, run: crudRun } = useAttemptNext();
const { attempt: fetchAttempt, run: fetchRun } = useAttemptNext('processing');
const { attempt: fetchAttempt, run: fetchRun } = useAttemptNext(
canListBots ? 'processing' : 'success'
);

useEffect(() => {
const signal = new AbortController();
Expand All @@ -60,15 +63,17 @@ export function Bots() {
return await fetchBots(signal, flags);
}

fetchRun(() =>
bots(signal.signal).then(botRes => {
setBots(botRes.bots);
})
);
if (canListBots) {
fetchRun(() =>
bots(signal.signal).then(botRes => {
setBots(botRes.bots);
})
);
}
return () => {
signal.abort();
};
}, [ctx, fetchRun]);
}, [ctx, fetchRun, canListBots]);

async function fetchRoleNames(search: string): Promise<string[]> {
const flags = ctx.getFeatureFlags();
Expand Down Expand Up @@ -122,6 +127,12 @@ export function Bots() {
if (fetchAttempt.status === 'success' && bots.length === 0) {
return (
<FeatureBox>
{!canListBots && (
<Alert kind="info" mt={4}>
You do not have permission to access Bots. Missing role permissions:{' '}
<code>bot.list</code>
</Alert>
)}
<EmptyState />
</FeatureBox>
);
Expand Down
12 changes: 10 additions & 2 deletions web/packages/teleport/src/Bots/List/EmptyState/EmptyState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,20 @@ export const BotTiles = () => {
<PreviewBox>
<Flex>
{integrationsTop.map(integration => (
<DisplayTile icon={integration.icon} title={integration.title} />
<DisplayTile
key={integration.title}
icon={integration.icon}
title={integration.title}
/>
))}
</Flex>
<Flex>
{integrationsBottom.map(integration => (
<DisplayTile icon={integration.icon} title={integration.title} />
<DisplayTile
key={integration.title}
icon={integration.icon}
title={integration.title}
/>
))}
</Flex>
</PreviewBox>
Expand Down
4 changes: 2 additions & 2 deletions web/packages/teleport/src/features.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ export class FeatureBots implements TeleportFeature {
component: Bots,
};

hasAccess(flags: FeatureFlags) {
return flags.listBots;
hasAccess() {
return true;
}

navigationItem = {
Expand Down

0 comments on commit ba2534a

Please sign in to comment.