Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 56 additions & 35 deletions web/packages/teleport/src/Login/Login.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,41 +114,62 @@ test('login with private key policy enabled through role setting', async () => {
expect(screen.getByText(/login disabled/i)).toBeInTheDocument();
});

test('show motd only if motd is set', async () => {
// default login form
const { unmount } = render(<Login />);
expect(screen.getByPlaceholderText(/username/i)).toBeInTheDocument();
expect(
screen.queryByText('Welcome to cluster, your activity will be recorded.')
).not.toBeInTheDocument();
unmount();

// now set motd
jest
.spyOn(cfg, 'getMotd')
.mockImplementation(
() => 'Welcome to cluster, your activity will be recorded.'
);

render(<Login />);

expect(
screen.getByText('Welcome to cluster, your activity will be recorded.')
).toBeInTheDocument();
expect(screen.queryByPlaceholderText(/username/i)).not.toBeInTheDocument();
});
describe('test MOTD', () => {
test('show motd only if motd is set', async () => {
// default login form
const { unmount } = render(<Login />);
expect(screen.getByPlaceholderText(/username/i)).toBeInTheDocument();
expect(
screen.queryByText('Welcome to cluster, your activity will be recorded.')
).not.toBeInTheDocument();
unmount();

// now set motd
jest
.spyOn(cfg, 'getMotd')
.mockImplementation(
() => 'Welcome to cluster, your activity will be recorded.'
);

render(<Login />);

expect(
screen.getByText('Welcome to cluster, your activity will be recorded.')
).toBeInTheDocument();
expect(screen.queryByPlaceholderText(/username/i)).not.toBeInTheDocument();
});

test('show login form after modt acknowledge', async () => {
jest
.spyOn(cfg, 'getMotd')
.mockImplementation(
() => 'Welcome to cluster, your activity will be recorded.'
);
render(<Login />);
expect(
screen.getByText('Welcome to cluster, your activity will be recorded.')
).toBeInTheDocument();
test('show login form after modt acknowledge', async () => {
jest
.spyOn(cfg, 'getMotd')
.mockImplementation(
() => 'Welcome to cluster, your activity will be recorded.'
);
render(<Login />);
expect(
screen.getByText('Welcome to cluster, your activity will be recorded.')
).toBeInTheDocument();

fireEvent.click(screen.getByText('Acknowledge'));
expect(screen.getByPlaceholderText(/username/i)).toBeInTheDocument();
});

fireEvent.click(screen.getByText('Acknowledge'));
expect(screen.getByPlaceholderText(/username/i)).toBeInTheDocument();
test('skip motd if login initiated from headless auth', async () => {
jest
.spyOn(cfg, 'getMotd')
.mockImplementation(
() => 'Welcome to cluster, your activity will be recorded.'
);
jest
.spyOn(history, 'getRedirectParam')
.mockReturnValue(
'https://teleport.example.com/web/headless/5c5c1f73-ac5c-52ee-bc9e-0353094dcb4a'
);

render(<Login />);

expect(
screen.queryByText('Welcome to cluster, your activity will be recorded.')
).not.toBeInTheDocument();
});
});
9 changes: 8 additions & 1 deletion web/packages/teleport/src/Login/useLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ export default function useLogin() {
const auth2faType = cfg.getAuth2faType();
const isLocalAuthEnabled = cfg.getLocalAuthFlag();
const motd = cfg.getMotd();
const [showMotd, setShowMotd] = useState(!!motd);
const [showMotd, setShowMotd] = useState<boolean>(() => {
const redirectUri = history.getRedirectParam();

if (redirectUri?.includes('headless')) {
return false;
}
Comment thread
flyinghermit marked this conversation as resolved.
return !!cfg.getMotd();
});

function acknowledgeMotd() {
setShowMotd(false);
Expand Down