Skip to content

Commit 2b59ba6

Browse files
committed
Integration Test fixes and unit test fixes
1 parent e64af71 commit 2b59ba6

3 files changed

Lines changed: 100 additions & 35 deletions

File tree

.talismanrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,6 @@ fileignoreconfig:
4545
checksum: b560bf300a07b40d9c24534c8e3493b7569233de061cfcdd58eb615c96e83d75
4646
- filename: packages/contentstack-auth/test/integration/auth.test.ts
4747
checksum: 96a66c141cf8f83443f967f62be210c3a95e06cf3d6c7bcb25229a4de7f05c5f
48+
- filename: packages/contentstack-auth/test/unit/commands/login.test.ts
49+
checksum: c756628a3f814340035a97fbb0dbabdf5e20b9557e63e6a77138f661e3e234d5
4850
version: "1.0"

packages/contentstack-auth/test/integration/auth.test.ts

Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import { expect } from 'chai';
22
import * as sinon from 'sinon';
3-
import { authHandler, interactive } from '../../src/utils';
4-
import { configHandler, cliux } from '@contentstack/cli-utilities';
3+
import { authHandler, interactive, totpHandler } from '../../src/utils';
4+
import {
5+
configHandler,
6+
cliux,
7+
messageHandler,
8+
authHandler as oauthHandler,
9+
CLIError
10+
} from '@contentstack/cli-utilities';
11+
import * as managementSDK from '@contentstack/cli-utilities';
512
import { Helper } from './helper';
613

714
const config = configHandler;
@@ -11,24 +18,48 @@ const TFATestToken = '24563992';
1118

1219
describe('contentstack-auth plugin test', () => {
1320
let sandbox: sinon.SinonSandbox;
21+
let mockClient: {
22+
login: sinon.SinonStub;
23+
logout: sinon.SinonStub;
24+
getUser: sinon.SinonStub;
25+
};
1426

1527
beforeEach(() => {
1628
sandbox = sinon.createSandbox();
1729

18-
// Stub interactive prompts
30+
// Interactive prompts
1931
sandbox.stub(interactive, 'askUsername').resolves(credentials.email);
2032
sandbox.stub(interactive, 'askPassword').resolves(credentials.password);
21-
sandbox.stub(interactive, 'askOTPChannel').resolves('authy');
33+
sandbox.stub(interactive, 'askOTPChannel').resolves('authenticator_app');
2234
sandbox.stub(interactive, 'askOTP').resolves(TFATestToken);
2335

24-
// Stub cliux
36+
// CLI UI
2537
sandbox.stub(cliux, 'success');
2638
sandbox.stub(cliux, 'error');
2739
sandbox.stub(cliux, 'inquire').resolves(credentials.email);
2840

29-
// Stub config
41+
// Config
3042
sandbox.stub(config, 'set');
3143
sandbox.stub(config, 'get').returns(credentials.email);
44+
45+
// Management SDK Client
46+
mockClient = {
47+
login: sandbox.stub().resolves({ user: { email: credentials.email, authtoken: 'test-token' } }),
48+
logout: sandbox.stub().resolves({}),
49+
getUser: sandbox.stub().resolves({ email: credentials.email })
50+
};
51+
sandbox.stub(managementSDK, 'managementSDKClient').resolves(mockClient);
52+
authHandler.client = mockClient;
53+
54+
// TOTP Handler
55+
sandbox.stub(totpHandler, 'getTOTPCode').resolves(TFATestToken);
56+
57+
// OAuth Handler
58+
sandbox.stub(oauthHandler, 'setConfigData').resolves();
59+
sandbox.stub(oauthHandler, 'host').value('https://api.contentstack.io');
60+
61+
// Message Handler
62+
sandbox.stub(messageHandler, 'parse').returns('Successfully logged in!!');
3263
});
3364

3465
afterEach(() => {
@@ -64,33 +95,37 @@ describe('contentstack-auth plugin test', () => {
6495
});
6596

6697
describe('Check auth:login command with 2FA', function() {
67-
this.timeout(10000); // Increase timeout to 10s
98+
this.timeout(10000);
6899

69100
it('Login should succeed with 2FA', async () => {
70-
const loginStub = sandbox.stub(authHandler, 'login');
71-
loginStub.onFirstCall().rejects({ error_code: 294 });
72-
loginStub.onSecondCall().resolves({
73-
email: credentials.email,
74-
authtoken: 'test-token'
101+
mockClient.login.resetBehavior();
102+
mockClient.login.resetHistory();
103+
104+
mockClient.login
105+
.onFirstCall().resolves({ error_code: 294 })
106+
.onSecondCall().resolves({ user: { email: credentials.email, authtoken: 'test-token' } });
107+
108+
await authHandler.login(credentials.email, credentials.password);
109+
expect(mockClient.login.callCount).to.equal(2);
75110
});
76-
77-
await Helper.run(['auth:login', `--username=${credentials.email}`, `--password=${credentials.password}`]);
78-
expect(loginStub.calledTwice).to.be.true;
79-
});
80111

81-
it('Login should fail with invalid 2FA code', async () => {
82-
const loginStub = sandbox.stub(authHandler, 'login');
83-
loginStub.onFirstCall().rejects({ error_code: 294 });
84-
loginStub.onSecondCall().rejects(new Error('Invalid 2FA code'));
85-
86-
try {
87-
await Helper.run(['auth:login', `--username=${credentials.email}`, `--password=${credentials.password}`]);
88-
} catch (error) {
89-
expect((error as Error).message).to.include('Invalid 2FA code');
90-
}
91-
92-
expect(loginStub.calledTwice).to.be.true;
93-
});
112+
it('Login should fail with invalid 2FA code', async () => {
113+
mockClient.login.resetBehavior();
114+
mockClient.login.resetHistory();
115+
116+
mockClient.login
117+
.onFirstCall().resolves({ error_code: 294 })
118+
.onSecondCall().rejects(new Error('Invalid 2FA code'));
119+
120+
try {
121+
await authHandler.login(credentials.email, credentials.password);
122+
throw new Error('Should have failed');
123+
} catch (error) {
124+
expect(error).to.be.instanceOf(CLIError);
125+
}
126+
127+
expect(mockClient.login.callCount).to.equal(2);
128+
});
94129
});
95130

96131
describe('Check auth:login command with OAuth', function() {

packages/contentstack-auth/test/unit/commands/login.test.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import { expect } from 'chai';
22
import * as sinon from 'sinon';
33
import LoginCommand from '../../../src/commands/auth/login';
4-
import { authHandler, interactive } from '../../../src/utils';
5-
import { configHandler, cliux } from '@contentstack/cli-utilities';
4+
import { authHandler, interactive, totpHandler } from '../../../src/utils';
5+
import {
6+
configHandler,
7+
cliux,
8+
messageHandler,
9+
authHandler as oauthHandler
10+
} from '@contentstack/cli-utilities';
11+
import * as managementSDK from '@contentstack/cli-utilities';
612
// @ts-ignore
713
import * as conf from '../../config.json';
814

@@ -16,17 +22,39 @@ const TFATestToken = '24563992';
1622
describe('Login Command', () => {
1723
let loginStub: sinon.SinonStub;
1824

19-
before(function () {
20-
loginStub = sinon.stub(authHandler, 'login').callsFake(function (email, password, tfaToken): Promise<any> {
25+
let sandbox: sinon.SinonSandbox;
26+
27+
beforeEach(function () {
28+
sandbox = sinon.createSandbox();
29+
// Setup auth handler stub
30+
loginStub = sandbox.stub(authHandler, 'login').callsFake(function (email, password, tfaToken): Promise<any> {
2131
if (password === credentials.password) {
2232
return Promise.resolve(user);
2333
}
2434
return Promise.reject({ message: 'invalid credentials' });
2535
});
36+
37+
// Setup management SDK client stub
38+
const mockClient = {
39+
login: sandbox.stub().resolves({ user: { email: credentials.email, authtoken: 'test-token' } }),
40+
logout: sandbox.stub().resolves({}),
41+
getUser: sandbox.stub().resolves({ email: credentials.email })
42+
};
43+
sandbox.stub(managementSDK, 'managementSDKClient').resolves(mockClient);
44+
authHandler.client = mockClient;
45+
46+
// Setup TOTP handler stub
47+
sandbox.stub(totpHandler, 'getTOTPCode').resolves(TFATestToken);
48+
49+
// Setup OAuth handler stub
50+
sandbox.stub(oauthHandler, 'setConfigData').resolves();
51+
52+
// Setup message handler stub
53+
sandbox.stub(messageHandler, 'parse').returns('Successfully logged in!!');
2654
});
2755

28-
after(() => {
29-
loginStub.restore();
56+
afterEach(() => {
57+
sandbox.restore();
3058
});
3159

3260
it('Login with valid credentials, should be successful', async function () {

0 commit comments

Comments
 (0)