From 576e0bc4ff099165b218d9effae02b0377a2c478 Mon Sep 17 00:00:00 2001 From: Sakshyam Shah Date: Thu, 31 Aug 2023 16:08:22 -0400 Subject: [PATCH] [v13] skip motd in UI if request initiated from tsh headless auth (#31205) * 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 --- .../teleport/src/Login/Login.test.tsx | 91 ++++++++++++------- web/packages/teleport/src/Login/useLogin.ts | 9 +- 2 files changed, 64 insertions(+), 36 deletions(-) diff --git a/web/packages/teleport/src/Login/Login.test.tsx b/web/packages/teleport/src/Login/Login.test.tsx index 8102775cb9653..61ee3d1d1d1d2 100644 --- a/web/packages/teleport/src/Login/Login.test.tsx +++ b/web/packages/teleport/src/Login/Login.test.tsx @@ -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(); - 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(); - - 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(); + 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(); + + 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(); - 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(); + 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(); + + expect( + screen.queryByText('Welcome to cluster, your activity will be recorded.') + ).not.toBeInTheDocument(); + }); }); diff --git a/web/packages/teleport/src/Login/useLogin.ts b/web/packages/teleport/src/Login/useLogin.ts index 5ce89419d3be4..8cc60b3a751bd 100644 --- a/web/packages/teleport/src/Login/useLogin.ts +++ b/web/packages/teleport/src/Login/useLogin.ts @@ -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(() => { + const redirectUri = history.getRedirectParam(); + + if (redirectUri?.includes('headless')) { + return false; + } + return !!cfg.getMotd(); + }); function acknowledgeMotd() { setShowMotd(false);