Skip to content

Commit

Permalink
[v13] skip motd in UI if request initiated from tsh headless auth (#3…
Browse files Browse the repository at this point in the history
…1205)

* skip motd for headless auth

* address review suggestion:
- rename redirect_uri to redirectUri
- add explicit default showMotd type and setShowMotd return type

* group motd tests

* use history.getRedirectParam()

* fix test: mock getRedirectParam, remove nested motd check
  • Loading branch information
flyinghermit committed Aug 31, 2023
1 parent 109e622 commit 576e0bc
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 36 deletions.
91 changes: 56 additions & 35 deletions web/packages/teleport/src/Login/Login.test.tsx
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
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;
}
return !!cfg.getMotd();
});

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

0 comments on commit 576e0bc

Please sign in to comment.