-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathauth-handler.test.ts
More file actions
177 lines (161 loc) · 6.01 KB
/
auth-handler.test.ts
File metadata and controls
177 lines (161 loc) · 6.01 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
import { expect } from 'chai';
import * as sinon from 'sinon';
import { authHandler, interactive } from '../../src/utils';
import { CLIError, cliux } from '@contentstack/cli-utilities';
import { User } from '../../src/interfaces';
// @ts-ignore
import * as config from '../config.json';
const user: User = { email: '***REMOVED***', authtoken: 'testtoken' };
const credentials = { email: '***REMOVED***', password: config.password };
const invalidCredentials = { email: '***REMOVED***', password: config.invalidPassowrd };
let TFAEnabled = false;
let TFAChannel = 'authy';
const TFATestToken = '24563992';
const InvalidTFATestToken = '24563965';
describe('Auth Handler', function () {
this.timeout(10000); // Increase timeout to 10s
let askOTPChannelStub: any;
let askOTPStub: any;
beforeEach(function () {
// Restore any existing stubs
sinon.restore();
const loginStub = sinon.stub().callsFake(function (param) {
if (param.password === credentials.password) {
if (TFAEnabled) {
if (TFAEnabled && param.tfa_token) {
if (param.tfa_token !== TFATestToken) {
return Promise.reject(new Error('Invalid 2FA code'));
}
} else {
const error: any = new Error('2FA required');
error.errorCode = 294;
return Promise.reject(error);
}
}
return Promise.resolve({ user });
} else {
return Promise.reject(new Error('Invalid credentials'));
}
});
const logoutStub = sinon.stub().callsFake(function (authtoken) {
if (authtoken === TFATestToken) {
return Promise.resolve({ user });
} else {
return Promise.reject(new Error('Invalid auth token'));
}
});
let contentStackClient: { login: Function; logout: Function; axiosInstance: any } = {
login: loginStub,
logout: logoutStub,
axiosInstance: {
post: sinon.stub().returns(Promise.resolve()),
},
};
authHandler.client = contentStackClient;
//Interactive stubs
askOTPChannelStub = sinon.stub(interactive, 'askOTPChannel').callsFake(function () {
return Promise.resolve(TFAChannel);
});
askOTPStub = sinon.stub(interactive, 'askOTP').callsFake(function () {
return Promise.resolve(TFATestToken);
});
});
afterEach(function () {
// Cleanup after each test
authHandler.client = null;
sinon.restore();
});
describe('#login', function () {
it('Login with credentials, should be logged in successfully', async function () {
const result = await authHandler.login(credentials.email, credentials.password);
expect(result).to.be.equal(user);
});
it.skip('Login with invalid credentials, failed to login', async function () {
sinon.restore();
sinon.stub(cliux, 'error').returns();
sinon.stub(cliux, 'print').returns();
sinon.stub(interactive, 'askOTPChannel').resolves('authenticator_app');
sinon.stub(interactive, 'askOTP').resolves('123456');
const loginStub = sinon.stub().rejects(new Error('Invalid credentials'));
const clientStub = {
login: loginStub,
axiosInstance: {
post: sinon.stub().resolves(),
},
};
authHandler.client = clientStub;
try {
await authHandler.login(invalidCredentials.email, invalidCredentials.password);
expect.fail('Should have thrown an error');
} catch (error) {
expect(error).to.be.instanceOf(CLIError);
expect(error.message).to.include('Invalid credentials');
} finally {
authHandler.client = null;
}
});
it('Login with 2FA enabled with authfy channel, should be logged in successfully', async function () {
TFAEnabled = true;
const result = await authHandler.login(credentials.email, credentials.password);
expect(result).to.be.equal(user);
TFAEnabled = false;
});
it('Login with 2FA enabled invalid otp, failed to login', async function () {
this.timeout(10000);
TFAEnabled = true;
askOTPStub.restore();
askOTPStub = sinon.stub(interactive, 'askOTP').callsFake(function () {
return Promise.resolve(InvalidTFATestToken);
});
try {
await authHandler.login(credentials.email, credentials.password);
expect.fail('Should have thrown an error');
} catch (error) {
expect(error).to.be.instanceOf(Error);
expect((error as Error).message).to.include('Invalid 2FA code');
} finally {
TFAEnabled = false;
askOTPStub.restore();
askOTPStub = sinon.stub(interactive, 'askOTP').callsFake(function () {
return Promise.resolve(TFATestToken);
});
}
});
it('Login with 2FA enabled with sms channel, should be logged in successfully', async function () {
TFAEnabled = true;
TFAChannel = 'sms';
const result = await authHandler.login(credentials.email, credentials.password);
expect(result).to.be.equal(user);
TFAEnabled = false;
});
});
describe('#logout', function () {
it('Logout, logout succesfully', async function () {
const result: { user: object } = (await authHandler.logout(TFATestToken)) as { user: object };
expect(result.user).to.be.equal(user);
});
it.skip('Logout with invalid authtoken, failed to logout', async function () {
sinon.restore();
sinon.stub(cliux, 'error').returns();
sinon.stub(cliux, 'print').returns();
const logoutStub = sinon.stub().rejects(new Error('Invalid auth token'));
const clientStub = {
login: sinon.stub(),
logout: logoutStub,
axiosInstance: {
post: sinon.stub().resolves(),
},
};
authHandler.client = clientStub;
try {
await authHandler.logout(InvalidTFATestToken);
expect.fail('Should have thrown an error');
} catch (error) {
expect(error).to.be.instanceOf(Error);
expect(error.message).to.equal('Invalid auth token');
} finally {
authHandler.client = null;
}
});
});
});