-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
269 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
web/packages/teleport/src/components/AuthnDialog/AuthnDialog.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/** | ||
* Teleport | ||
* Copyright (C) 2024 Gravitational, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { render, screen, fireEvent } from 'design/utils/testing'; | ||
|
||
import { makeDefaultMfaState, MfaState } from 'teleport/lib/useMfa'; | ||
import { SSOChallenge } from 'teleport/services/auth'; | ||
|
||
import AuthnDialog from './AuthnDialog'; | ||
|
||
const mockSsoChallenge: SSOChallenge = { | ||
redirectUrl: 'url', | ||
requestId: '123', | ||
device: { | ||
displayName: 'Okta', | ||
connectorId: '123', | ||
connectorType: 'saml', | ||
}, | ||
channelId: '123', | ||
}; | ||
|
||
function makeMockState(partial: Partial<MfaState>): MfaState { | ||
const mfa = makeDefaultMfaState(); | ||
return { | ||
...mfa, | ||
...partial, | ||
}; | ||
} | ||
|
||
describe('AuthnDialog', () => { | ||
const mockOnCancel = jest.fn(); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('renders the dialog with basic content', () => { | ||
const mfa = makeMockState({ ssoChallenge: mockSsoChallenge }); | ||
render(<AuthnDialog mfa={mfa} onCancel={mockOnCancel} />); | ||
|
||
expect(screen.getByText('Multi-factor authentication')).toBeInTheDocument(); | ||
expect( | ||
screen.getByText( | ||
'Re-enter your multi-factor authentication in the browser to continue.' | ||
) | ||
).toBeInTheDocument(); | ||
expect(screen.getByText('Okta')).toBeInTheDocument(); | ||
expect(screen.getByText('Cancel')).toBeInTheDocument(); | ||
}); | ||
|
||
test('displays error text when provided', () => { | ||
const errorText = 'Authentication failed'; | ||
const mfa = makeMockState({ errorText }); | ||
render(<AuthnDialog mfa={mfa} onCancel={mockOnCancel} />); | ||
|
||
expect(screen.getByTestId('danger-alert')).toBeInTheDocument(); | ||
expect(screen.getByText(errorText)).toBeInTheDocument(); | ||
}); | ||
|
||
test('sso button renders with callback', async () => { | ||
const mfa = makeMockState({ | ||
ssoChallenge: mockSsoChallenge, | ||
onSsoAuthenticate: jest.fn(), | ||
}); | ||
render(<AuthnDialog mfa={mfa} onCancel={mockOnCancel} />); | ||
const ssoButton = screen.getByText('Okta'); | ||
fireEvent.click(ssoButton); | ||
expect(mfa.onSsoAuthenticate).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('webauthn button renders with callback', async () => { | ||
const mfa = makeMockState({ | ||
webauthnPublicKey: { challenge: new ArrayBuffer(0) }, | ||
onWebauthnAuthenticate: jest.fn(), | ||
}); | ||
render(<AuthnDialog mfa={mfa} onCancel={mockOnCancel} />); | ||
const webauthn = screen.getByText('Passkey/Hardware Key'); | ||
fireEvent.click(webauthn); | ||
expect(mfa.onWebauthnAuthenticate).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.