-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathimport-credentials.test.js
70 lines (57 loc) · 2.74 KB
/
import-credentials.test.js
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
const inquirer = require('inquirer');
const importCredentials = require('../src/create-twilio-function/import-credentials');
describe('importCredentials', () => {
describe('if credentials are present in the env', () => {
afterEach(() => {
delete process.env.TWILIO_ACCOUNT_SID;
delete process.env.TWILIO_AUTH_TOKEN;
});
test('it should prompt to ask if to use credentials and return them if affirmative', async () => {
process.env.TWILIO_ACCOUNT_SID = 'AC1234';
process.env.TWILIO_AUTH_TOKEN = 'auth-token';
inquirer.prompt = jest.fn(() => Promise.resolve({ importedCredentials: true }));
const credentials = await importCredentials({});
expect(inquirer.prompt).toHaveBeenCalledTimes(1);
expect(inquirer.prompt).toHaveBeenCalledWith(expect.any(Array));
expect(credentials.accountSid).toBe('AC1234');
expect(credentials.authToken).toBe('auth-token');
});
test('it should prompt to ask if to use credentials and return an empty object if negative', async () => {
process.env.TWILIO_ACCOUNT_SID = 'AC1234';
process.env.TWILIO_AUTH_TOKEN = 'auth-token';
inquirer.prompt = jest.fn(() => Promise.resolve({ importedCredentials: false }));
const credentials = await importCredentials({});
expect(inquirer.prompt).toHaveBeenCalledTimes(1);
expect(inquirer.prompt).toHaveBeenCalledWith(expect.any(Array));
expect(credentials.accountSid).toBe(undefined);
expect(credentials.authToken).toBe(undefined);
});
test('it should return credentials if the option importCredentials is true', async () => {
process.env.TWILIO_ACCOUNT_SID = 'AC1234';
process.env.TWILIO_AUTH_TOKEN = 'auth-token';
const credentials = await importCredentials({ importedCredentials: true });
expect(inquirer.prompt).not.toHaveBeenCalled();
expect(credentials.accountSid).toBe('AC1234');
expect(credentials.authToken).toBe('auth-token');
});
test('it should not return credentials if skipCredentials is true', async () => {
process.env.TWILIO_ACCOUNT_SID = 'AC1234';
process.env.TWILIO_AUTH_TOKEN = 'auth-token';
const credentials = await importCredentials({
skipCredentials: true,
importedCredentials: true,
});
expect(inquirer.prompt).not.toHaveBeenCalled();
expect(credentials.accountSid).toBe(undefined);
expect(credentials.authToken).toBe(undefined);
});
});
describe('if there are no credentials in the env', () => {
test('it should not ask about importing credentials', async () => {
delete process.env.TWILIO_ACCOUNT_SID;
delete process.env.TWILIO_AUTH_TOKEN;
await importCredentials({});
expect(inquirer.prompt).not.toHaveBeenCalled();
});
});
});