-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathauth.spec.ts
More file actions
98 lines (92 loc) · 3.51 KB
/
auth.spec.ts
File metadata and controls
98 lines (92 loc) · 3.51 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import rimraf from 'rimraf';
import inquirer from 'inquirer';
import configStore, { CONFIG_KEYS } from '../lib/Config';
import mockFunction from './helper';
import { setEnv } from './helper';
import { init } from '../fdk';
const tokenData = require('./fixtures/partnertoken.json');
const request = require('supertest');
import { startServer, getApp } from '../lib/Auth';
import { URLS } from '../lib/api/services/url';
import Logger from '../lib/Logger';
import MockAdapter from 'axios-mock-adapter';
import axios from 'axios';
jest.mock('inquirer');
let program;
jest.mock('configstore', () => {
const Store =
jest.requireActual<typeof import('configstore')>('configstore');
return class MockConfigstore {
store = new Store('test-cli', undefined, {
configPath: './auth-test-cli.json',
});
all = this.store.all;
size = this.store.size;
get(key: string) {
return this.store.get(key);
}
set(key: string, value) {
this.store.set(key, value);
}
delete(key) {
this.store.delete(key);
}
clear() {
this.store.clear();
}
};
});
export async function login() {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; // Disable SSL verification
const app = await startServer();
const req = request(app);
await program.parseAsync(['ts-node', './src/fdk.ts', 'login']);
return await req.post('/token').send(tokenData);
}
describe('Auth Commands', () => {
beforeAll(async () => {
setEnv();
program = await init('fdk');
const mock = new MockAdapter(axios);
await login();
});
afterAll(() => {
rimraf.sync('./auth-test-cli.json');
});
it('Should successfully login with partner panel', async () => {
expect(configStore.get(CONFIG_KEYS.AUTH_TOKEN).access_token).toBe(
'pr-4fb094006ed3a6d749b69875be0418b83238d078',
);
});
it('Should ask for change organization when user is already logged in', async () => {
const inquirerMock = mockFunction(inquirer.prompt);
inquirerMock.mockResolvedValue({ confirmChangeOrg: 'Yes' });
await login();
expect(configStore.get(CONFIG_KEYS.AUTH_TOKEN).access_token).toBe(
'pr-4fb094006ed3a6d749b69875be0418b83238d078',
);
});
it('Should exit when user selects no for organization change', async () => {
const inquirerMock = mockFunction(inquirer.prompt);
inquirerMock.mockResolvedValue({ confirmChangeOrg: 'No' });
await login();
expect(configStore.get(CONFIG_KEYS.AUTH_TOKEN).access_token).toBe(
'pr-4fb094006ed3a6d749b69875be0418b83238d078',
);
});
it('should console active user', async () => {
let consoleWarnSpy: jest.SpyInstance;
consoleWarnSpy = jest.spyOn(Logger, 'info').mockImplementation();
await program.parseAsync(['ts-node', './src/fdk.ts', 'user']);
const { current_user: user } = configStore.get(CONFIG_KEYS.AUTH_TOKEN);
expect(consoleWarnSpy).toHaveBeenCalledWith('Name: Jinal Virani');
expect(user.emails[0].email).toMatch('jinalvirani@gofynd.com');
});
it('should successfully logout user', async () => {
const inquirerMock = mockFunction(inquirer.prompt);
inquirerMock.mockResolvedValue({ confirmLogout: 'Yes' });
await program.parseAsync(['ts-node', './src/fdk.ts', 'logout']);
const storeSize = configStore.size;
expect(storeSize).toBe(0);
});
});