-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathauth.test.ts
More file actions
155 lines (125 loc) · 5.12 KB
/
auth.test.ts
File metadata and controls
155 lines (125 loc) · 5.12 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
import { expect } from 'chai';
import * as sinon from 'sinon';
import { authHandler, interactive } from '../../src/utils';
import {
configHandler,
cliux,
messageHandler,
authHandler as oauthHandler,
} from '@contentstack/cli-utilities';
import * as managementSDK from '@contentstack/cli-utilities';
import { Helper } from './helper';
const config = configHandler;
const credentials = { email: 'test@example.com', password: 'testpassword' };
const invalidCredentials = { email: 'test@example.com', password: 'invalidpassword' };
const TFATestToken = '24563992';
describe('contentstack-auth plugin test', () => {
let sandbox: sinon.SinonSandbox;
let mockClient: {
login: sinon.SinonStub;
logout: sinon.SinonStub;
getUser: sinon.SinonStub;
};
beforeEach(() => {
sinon.restore();
sandbox = sinon.createSandbox();
// Interactive prompts
sandbox.stub(interactive, 'askUsername').resolves(credentials.email);
sandbox.stub(interactive, 'askPassword').resolves(credentials.password);
sandbox.stub(interactive, 'askOTPChannel').resolves('authenticator_app');
sandbox.stub(interactive, 'askOTP').resolves(TFATestToken);
// CLI UI
sandbox.stub(cliux, 'success');
sandbox.stub(cliux, 'error');
sandbox.stub(cliux, 'inquire').resolves(credentials.email);
// Config
sandbox.stub(config, 'set');
sandbox.stub(config, 'get').returns(credentials.email);
// Management SDK Client
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;
// OAuth Handler
sandbox.stub(oauthHandler, 'setConfigData').resolves();
sandbox.stub(oauthHandler, 'host').value('https://api.contentstack.io');
// Message Handler
sandbox.stub(messageHandler, 'parse').returns('Successfully logged in!!');
});
afterEach(() => {
sandbox.restore();
});
describe('Check auth:login command with --username, --password flags and wrong credentials', function() {
it.skip('Login should fail due to wrong credentials (flags)', async () => {
sandbox.stub(authHandler, 'login').rejects(new Error('Invalid credentials'));
try {
await Helper.run(['auth:login', `--username=${credentials.email}`, `--password=${invalidCredentials.password}`]);
} catch (error) {
expect((error as Error).message).to.include('Invalid credentials');
}
});
});
describe('Check auth:login command with --username, --password flags', function() {
it.skip('Login should succeed (flags)', async () => {
sandbox.stub(authHandler, 'login').resolves({
email: credentials.email,
authtoken: 'test-token'
});
await Helper.run(['auth:login', `--username=${credentials.email}`, `--password=${credentials.password}`]);
expect(config.get('email')).to.equal(credentials.email);
});
});
describe('Check auth:login command with 2FA', function() {
it('Login should succeed with 2FA', async () => {
mockClient.login.resetBehavior();
mockClient.login.resetHistory();
mockClient.login
.onFirstCall().rejects({ errorCode: 294 })
.onSecondCall().resolves({ user: { email: credentials.email, authtoken: 'test-token' } });
await authHandler.login(credentials.email, credentials.password);
expect(mockClient.login.callCount).to.equal(2);
});
it.skip('Login should fail with invalid 2FA code', async function() {
// Reset and restore all stubs
sandbox.restore();
sandbox = sinon.createSandbox();
// Setup client stubs
const mockClient = {
login: sandbox.stub(),
axiosInstance: {
post: sandbox.stub().resolves()
}
};
mockClient.login
.onFirstCall().resolves({ error_code: 294 })
.onSecondCall().rejects(new Error('Invalid 2FA code'));
// Setup interactive stubs
sandbox.stub(interactive, 'askOTPChannel').resolves('authenticator_app');
sandbox.stub(interactive, 'askOTP').resolves('123456');
sandbox.stub(cliux, 'print').returns();
sandbox.stub(cliux, 'error').returns();
// Set client
authHandler.client = mockClient;
try {
await authHandler.login(credentials.email, credentials.password);
throw new Error('Should have failed');
} catch (error) {
expect((error as Error).message).to.include('Invalid 2FA code');
} finally {
authHandler.client = null;
}
});
});
describe('Check auth:login command with OAuth', function() {
it.skip('Login should succeed with OAuth', async () => {
Object.defineProperty(authHandler, 'oauth', {
value: sandbox.stub().resolves(),
configurable: true
});
await Helper.run(['auth:login', '--oauth']);
});
});
});