-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathauth.spec.ts
More file actions
184 lines (177 loc) · 7.6 KB
/
Copy pathauth.spec.ts
File metadata and controls
184 lines (177 loc) · 7.6 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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 organizationData = require('./fixtures/organizationData.json');
const request = require('supertest');
import { startServer } 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';
import {
getRandomFreePort
} from '../helper/extension_utils';
jest.mock('inquirer');
let program;
jest.mock('../helper/extension_utils', () => ({
getRandomFreePort: jest.fn().mockResolvedValue(43123),
}));
jest.mock('configstore', () => {
const Store =
jest.requireActual('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();
}
};
});
jest.mock('open', () => {
return () => {}
})
export async function login(domain?: string, region?: string) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; // Disable SSL verification
const port = await getRandomFreePort([]);
const app = await startServer(port);
const req = request(app);
const args = ['ts-node', './src/fdk.ts', 'login', '--host', domain || 'api.fyndx1.de'];
if (region) args.push('--region', region);
await program.parseAsync(args);
return await req.post('/token').send(tokenData);
}
describe('Auth Commands', () => {
beforeAll(async () => {
setEnv();
program = await init('fdk');
const mock = new MockAdapter(axios);
configStore.set(CONFIG_KEYS.ORGANIZATION, organizationData._id);
mock.onGet('https://api.fyndx1.de/service/application/content/_healthz').reply(200);
mock.onGet('https://api.fynd.com/service/application/content/_healthz').reply(200);
mock.onGet('https://api.jiomartpartners.com/service/application/content/_healthz').reply(200);
mock.onGet('https://api-invoice.sandbox3.fynd.engineering/service/application/content/_healthz').reply(200);
mock.onGet(`${URLS.GET_ORGANIZATION_DETAILS()}`).reply(
200,
organizationData,
);
mock.onGet('https://api.jiomartpartners.com/service/partner/partners/v1.0/organization/60afe92972b7a964de57a1d4').reply(
200,
organizationData,
);
mock.onGet('https://api-invoice.sandbox3.fynd.engineering/service/partner/partners/v1.0/organization/60afe92972b7a964de57a1d4').reply(
200,
organizationData,
);
setEnv('api.fynd.com');
configStore.set(CONFIG_KEYS.ORGANIZATION, organizationData._id);
mock.onGet(`${URLS.GET_ORGANIZATION_DETAILS()}`).reply(
200,
organizationData,
);
setEnv();
configStore.set(CONFIG_KEYS.ORGANIZATION, organizationData._id);
configStore.delete(CONFIG_KEYS.ORGANIZATION);
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('Passing partners URL should set api host in env', async () => {
configStore.delete(CONFIG_KEYS.AUTH_TOKEN);
await login('partners.fynd.com');
expect(configStore.get(CONFIG_KEYS.CURRENT_ENV_VALUE)).toBe('api.fynd.com');
});
it('Passing partners URL with partners in domain should set proper api host in env', async () => {
configStore.delete(CONFIG_KEYS.AUTH_TOKEN);
await login('partners.jiomartpartners.com');
expect(configStore.get(CONFIG_KEYS.CURRENT_ENV_VALUE)).toBe('api.jiomartpartners.com');
});
it('Passing partners sandbox URL should set partners api host in env', async () => {
configStore.delete(CONFIG_KEYS.AUTH_TOKEN);
await login('partners-invoice.sandbox3.fynd.engineering');
expect(configStore.get(CONFIG_KEYS.CURRENT_ENV_VALUE)).toBe('api-invoice.sandbox3.fynd.engineering');
});
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 successfully login with and env should updated', async () => {
configStore.delete(CONFIG_KEYS.AUTH_TOKEN);
configStore.set(CONFIG_KEYS.REGION, 'asia-south1');
await login('api.fynd.com');
expect(configStore.get(CONFIG_KEYS.CURRENT_ENV_VALUE)).toBe('api.fynd.com');
expect(configStore.get(CONFIG_KEYS.AUTH_TOKEN).access_token).toBe(
'pr-4fb094006ed3a6d749b69875be0418b83238d078',
);
expect(configStore.get(CONFIG_KEYS.REGION)).toBeUndefined();
});
it('Should store region after regional partner panel login', async () => {
configStore.delete(CONFIG_KEYS.AUTH_TOKEN);
configStore.delete(CONFIG_KEYS.REGION);
await login('api.fyndx1.de', 'asia-south1');
expect(configStore.get(CONFIG_KEYS.REGION)).toBe('asia-south1');
});
it('Should pass command region while validating host before login', async () => {
configStore.delete(CONFIG_KEYS.AUTH_TOKEN);
configStore.set(CONFIG_KEYS.REGION, 'chinmay');
const getSpy = jest.spyOn(axios, 'get');
await login('api.fyndx1.de', 'asia-south2');
expect(getSpy).toHaveBeenCalledWith(
'https://api.fyndx1.de/service/application/content/_healthz',
{
headers: {
'x-region': 'asia-south2',
},
},
);
expect(configStore.get(CONFIG_KEYS.REGION)).toBe('asia-south2');
});
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.mock.lastCall[0]).toContain('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);
});
});