-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathlogout.test.ts
More file actions
98 lines (81 loc) · 3.41 KB
/
logout.test.ts
File metadata and controls
98 lines (81 loc) · 3.41 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 { expect } from 'chai';
import * as sinon from 'sinon';
import LoginCommand from '../../../src/commands/auth/login';
import { authHandler, interactive, mfaHandler } from '../../../src/utils';
import {
configHandler,
cliux,
messageHandler,
authHandler as oauthHandler
} from '@contentstack/cli-utilities';
import * as managementSDK from '@contentstack/cli-utilities';
<<<<<<< HEAD
import { readFileSync } from 'fs';
import { join } from 'path';
const conf = JSON.parse(readFileSync(join(__dirname, '../config.json'), "utf-8"));
=======
// @ts-ignore
import * as conf from '../config.json';
>>>>>>> main
const config = configHandler;
const user = { email: '***REMOVED***', authtoken: 'testtoken' };
const credentials = { email: '***REMOVED***', password: conf.password };
const invalidCredentials = { email: '***REMOVED***', password: conf.invalidPassowrd };
const TFATestToken = '24563992';
describe('Login Command', () => {
let sandbox: sinon.SinonSandbox;
beforeEach(function () {
// Restore any existing stubs
sinon.restore();
sandbox = sinon.createSandbox();
// Setup config handler stubs
sandbox.stub(config, 'get').returns(credentials.email);
sandbox.stub(config, 'set').resolves();
// Setup CLI stubs
sandbox.stub(cliux, 'success').returns();
sandbox.stub(cliux, 'error').returns();
sandbox.stub(cliux, 'print').returns();
// Setup host property
sandbox.stub(LoginCommand.prototype, 'cmaHost').value('https://api.contentstack.io');
// Setup auth handler stub
sandbox.stub(authHandler, 'login').callsFake(async function (email, password, tfaToken): Promise<any> {
if (password === credentials.password) {
return user;
}
throw new Error('invalid credentials');
});
// Setup management SDK client stub
const mockClient = {
login: sandbox.stub().resolves({ user: { email: credentials.email, authtoken: 'test-token' } }),
logout: sandbox.stub().resolves({}),
getUser: sandbox.stub().resolves({ email: credentials.email })
};
sandbox.stub(managementSDK, 'managementSDKClient').resolves(mockClient);
authHandler.client = mockClient;
// Setup MFA handler stub
sandbox.stub(mfaHandler, 'getMFACode').resolves(TFATestToken);
// Setup OAuth handler stub
sandbox.stub(oauthHandler, 'setConfigData').resolves();
// Setup message handler stub
sandbox.stub(messageHandler, 'parse').returns('Successfully logged in!!');
});
afterEach(() => {
sandbox.restore();
});
it('Login with valid credentials, should be successful', async function () {
await LoginCommand.run(['-u', credentials.email, '-p', credentials.password]);
expect(config.get('email')).to.be.equal(credentials.email);
});
it('Login with with only email, should prompt for password', async function () {
const askPasswordStub = sandbox.stub(interactive, 'askPassword').resolves(credentials.password);
await LoginCommand.run(['-u', credentials.email]);
expect(askPasswordStub.calledOnce).to.be.true;
});
it('Login with no flags, should prompt for credentials', async function () {
const askPasswordStub = sandbox.stub(interactive, 'askPassword').resolves(credentials.password);
const askEmailStub = sandbox.stub(cliux, 'inquire').resolves(credentials.email);
await LoginCommand.run([]);
expect(askPasswordStub.calledOnce).to.be.true;
expect(askEmailStub.calledOnce).to.be.true;
});
});