-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
actions-test.ts
79 lines (70 loc) · 2.31 KB
/
actions-test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { ApiV2Error } from '../../api';
import { promptAsync } from '../../prompts';
import { showLoginPromptAsync } from '../actions';
import { retryUsernamePasswordAuthWithOTPAsync, UserSecondFactorDeviceMethod } from '../otp';
import { loginAsync } from '../user';
jest.mock('../../prompts');
jest.mock('../../api', () => {
const { ApiV2Error } = jest.requireActual('../../api');
return {
ApiV2Error,
};
});
jest.mock('../otp');
jest.mock('../user');
const asMock = (fn: any): jest.Mock => fn as jest.Mock;
beforeEach(() => {
asMock(promptAsync).mockReset();
asMock(promptAsync).mockImplementation(() => {
throw new Error('Should not be called');
});
asMock(loginAsync).mockReset();
});
describe(showLoginPromptAsync, () => {
it('prompts for OTP when 2FA is enabled', async () => {
asMock(promptAsync)
.mockImplementationOnce(() => ({ username: 'hello', password: 'world' }))
.mockImplementationOnce(() => ({ otp: '123456' }))
.mockImplementation(() => {
throw new Error("shouldn't happen");
});
asMock(loginAsync)
.mockImplementationOnce(async () => {
throw new ApiV2Error({ code: 'testcode', request: {} } as any, {
message: 'An OTP is required',
code: 'ONE_TIME_PASSWORD_REQUIRED',
metadata: {
secondFactorDevices: [
{
id: 'p0',
is_primary: true,
method: UserSecondFactorDeviceMethod.SMS,
sms_phone_number: 'testphone',
},
],
smsAutomaticallySent: true,
},
});
})
.mockImplementation(() => {});
await showLoginPromptAsync();
expect(retryUsernamePasswordAuthWithOTPAsync).toHaveBeenCalledWith('hello', 'world', {
secondFactorDevices: [
{
id: 'p0',
is_primary: true,
method: UserSecondFactorDeviceMethod.SMS,
sms_phone_number: 'testphone',
},
],
smsAutomaticallySent: true,
});
});
it('does not prompt if all required credentials are provided', async () => {
asMock(promptAsync).mockImplementation(() => {
throw new Error("shouldn't happen");
});
asMock(loginAsync).mockImplementation(() => {});
await showLoginPromptAsync({ username: 'hello', password: 'world' });
});
});