Skip to content

Commit

Permalink
Fix tests wip.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joerger committed Dec 3, 2024
1 parent 82590ae commit 518f3ef
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
4 changes: 2 additions & 2 deletions web/packages/teleport/src/Account/Account.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ test('adding an MFA device', async () => {
const user = userEvent.setup();
const ctx = createTeleportContext();
jest.spyOn(ctx.mfaService, 'fetchDevices').mockResolvedValue([testPasskey]);
jest.spyOn(auth, 'getChallenge').mockResolvedValue({
jest.spyOn(auth, 'getMfaChallenge').mockResolvedValue({
webauthnPublicKey: null,
totpChallenge: true,
ssoChallenge: null,
Expand Down Expand Up @@ -327,7 +327,7 @@ test('removing an MFA method', async () => {
const user = userEvent.setup();
const ctx = createTeleportContext();
jest.spyOn(ctx.mfaService, 'fetchDevices').mockResolvedValue([testMfaMethod]);
jest.spyOn(auth, 'getChallenge').mockResolvedValue({
jest.spyOn(auth, 'getMfaChallenge').mockResolvedValue({
webauthnPublicKey: null,
totpChallenge: false,
ssoChallenge: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ beforeEach(() => {
user = userEvent.setup();
onSuccess = jest.fn();

jest.spyOn(auth, 'getMfaChallenge');
jest
.spyOn(auth, 'getMfaChallengeResponse')
.mockResolvedValueOnce(dummyChallengeResponse);
Expand Down Expand Up @@ -194,7 +195,7 @@ describe('with WebAuthn MFA reauthentication', () => {
);
await user.click(reauthenticateStep.getByText('MFA Device'));
await user.click(reauthenticateStep.getByText('Next'));
expect(auth.getMfaChallengeResponse).toHaveBeenCalledWith({
expect(auth.getMfaChallenge).toHaveBeenCalledWith({
scope: MfaChallengeScope.CHANGE_PASSWORD,
userVerificationRequirement: 'discouraged',
});
Expand Down Expand Up @@ -296,7 +297,7 @@ describe('with OTP MFA reauthentication', () => {
);
await user.click(reauthenticateStep.getByText('Authenticator App'));
await user.click(reauthenticateStep.getByText('Next'));
expect(auth.getMfaChallengeResponse).not.toHaveBeenCalled();
expect(auth.getMfaChallenge).not.toHaveBeenCalled();
}

it('changes password', async () => {
Expand Down Expand Up @@ -420,7 +421,7 @@ describe('without reauthentication', () => {
'new-pass1234'
);
await user.click(changePasswordStep.getByText('Save Changes'));
expect(auth.getMfaChallengeResponse).not.toHaveBeenCalled();
expect(auth.getMfaChallenge).not.toHaveBeenCalled();
expect(auth.changePassword).toHaveBeenCalledWith({
oldPassword: 'current-pass',
newPassword: 'new-pass1234',
Expand Down
35 changes: 19 additions & 16 deletions web/packages/teleport/src/services/api/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { MfaChallengeResponse } from '../mfa';
import api, {
MFA_HEADER,
defaultRequestOptions,
Expand All @@ -26,18 +27,20 @@ import api, {
describe('api.fetch', () => {
const mockedFetch = jest.spyOn(global, 'fetch').mockResolvedValue({} as any); // we don't care about response

const webauthnResp = {
id: 'some-id',
type: 'some-type',
extensions: {
appid: false,
},
rawId: 'some-raw-id',
response: {
authenticatorData: 'authen-data',
clientDataJSON: 'client-data-json',
signature: 'signature',
userHandle: 'user-handle',
const mfaResp: MfaChallengeResponse = {
webauthn_response: {
id: 'some-id',
type: 'some-type',
extensions: {
appid: false,
},
rawId: 'some-raw-id',
response: {
authenticatorData: 'authen-data',
clientDataJSON: 'client-data-json',
signature: 'signature',
userHandle: 'user-handle',
},
},
};

Expand Down Expand Up @@ -88,7 +91,7 @@ describe('api.fetch', () => {
});

test('with webauthnResponse', async () => {
await api.fetch('/something', undefined, webauthnResp);
await api.fetch('/something', undefined, mfaResp);
expect(mockedFetch).toHaveBeenCalledTimes(1);

const firstCall = mockedFetch.mock.calls[0];
Expand All @@ -100,14 +103,14 @@ describe('api.fetch', () => {
...defaultRequestOptions.headers,
...getAuthHeaders(),
[MFA_HEADER]: JSON.stringify({
webauthnAssertionResponse: webauthnResp,
webauthnAssertionResponse: mfaResp.webauthn_response,
}),
},
});
});

test('with customOptions and webauthnResponse', async () => {
await api.fetch('/something', customOpts, webauthnResp);
await api.fetch('/something', customOpts, mfaResp);
expect(mockedFetch).toHaveBeenCalledTimes(1);

const firstCall = mockedFetch.mock.calls[0];
Expand All @@ -120,7 +123,7 @@ describe('api.fetch', () => {
...customOpts.headers,
...getAuthHeaders(),
[MFA_HEADER]: JSON.stringify({
webauthnAssertionResponse: webauthnResp,
webauthnAssertionResponse: mfaResp.webauthn_response,
}),
},
});
Expand Down

0 comments on commit 518f3ef

Please sign in to comment.