Skip to content

Commit eab1d5a

Browse files
Refactor test file paths to use relative paths for config.json and improve error handling in auth-handler tests
1 parent 20eb71d commit eab1d5a

6 files changed

Lines changed: 24 additions & 13 deletions

File tree

packages/contentstack-auth/test/unit/auth-handler.test.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { User } from '../../src/interfaces';
66
import { readFileSync } from 'fs';
77
import { join } from 'path';
88

9-
const config = JSON.parse(readFileSync(join(__dirname, '../config.json'), "utf-8"));
9+
const config = JSON.parse(readFileSync(join(__dirname, './config.json'), 'utf-8'));
1010

1111
const user: User = { email: '***REMOVED***', authtoken: 'testtoken' };
1212
const credentials = { email: '***REMOVED***', password: config.password };
@@ -32,8 +32,9 @@ describe('Auth Handler', function () {
3232
return Promise.reject(new Error('Invalid 2FA code'));
3333
}
3434
} else {
35-
// Handler expects 2FA required as a rejection (catch path checks error.errorCode === 294)
36-
return Promise.reject({ errorCode: 294 });
35+
const error: any = new Error('2FA required');
36+
error.errorCode = 294;
37+
return Promise.reject(error);
3738
}
3839
}
3940
return Promise.resolve({ user });
@@ -117,13 +118,23 @@ describe('Auth Handler', function () {
117118
it('Login with 2FA enabled invalid otp, failed to login', async function () {
118119
this.timeout(10000);
119120
TFAEnabled = true;
120-
let result;
121+
askOTPStub.restore();
122+
askOTPStub = sinon.stub(interactive, 'askOTP').callsFake(function () {
123+
return Promise.resolve(InvalidTFATestToken);
124+
});
121125
try {
122-
result = await authHandler.login(credentials.email, credentials.password);
126+
await authHandler.login(credentials.email, credentials.password);
127+
expect.fail('Should have thrown an error');
123128
} catch (error) {
124-
result = error;
129+
expect(error).to.be.instanceOf(Error);
130+
expect((error as Error).message).to.include('Invalid 2FA code');
131+
} finally {
132+
TFAEnabled = false;
133+
askOTPStub.restore();
134+
askOTPStub = sinon.stub(interactive, 'askOTP').callsFake(function () {
135+
return Promise.resolve(TFATestToken);
136+
});
125137
}
126-
TFAEnabled = false;
127138
});
128139

129140
it('Login with 2FA enabled with sms channel, should be logged in successfully', async function () {
@@ -166,4 +177,4 @@ describe('Auth Handler', function () {
166177
}
167178
});
168179
});
169-
});
180+
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import * as managementSDK from '@contentstack/cli-utilities';
1212
import { readFileSync } from 'fs';
1313
import { join } from 'path';
1414

15-
const conf = JSON.parse(readFileSync(join(__dirname, '../../config.json'), "utf-8"));
15+
const conf = JSON.parse(readFileSync(join(__dirname, '../config.json'), "utf-8"));
1616

1717
const config = configHandler;
1818

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import * as managementSDK from '@contentstack/cli-utilities';
1212
import { readFileSync } from 'fs';
1313
import { join } from 'path';
1414

15-
const conf = JSON.parse(readFileSync(join(__dirname, '../../config.json'), "utf-8"));
15+
const conf = JSON.parse(readFileSync(join(__dirname, '../config.json'), "utf-8"));
1616

1717
const config = configHandler;
1818

packages/contentstack-auth/test/unit/commands/tokens-add.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import nock from 'nock';
88
import { readFileSync } from 'fs';
99
import { join } from 'path';
1010

11-
const conf = JSON.parse(readFileSync(join(__dirname, '../../config.json'), "utf-8"));
11+
const conf = JSON.parse(readFileSync(join(__dirname, '../config.json'), "utf-8"));
1212

1313
dotenvConfig();
1414

packages/contentstack-auth/test/unit/interactive.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { cliux } from '@contentstack/cli-utilities';
55
import { readFileSync } from 'fs';
66
import { join } from 'path';
77

8-
const config = JSON.parse(readFileSync(join(__dirname, '../config.json'), "utf-8"));
8+
const config = JSON.parse(readFileSync(join(__dirname, './config.json'), "utf-8"));
99

1010
describe('Interactive', () => {
1111
let inquireStub: sinon.SinonStub;

packages/contentstack-auth/test/utils/auth-handler.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { User } from '../../src/interfaces';
66
import { readFileSync } from 'fs';
77
import { join } from 'path';
88

9-
const config = JSON.parse(readFileSync(join(__dirname, '../config.json'), "utf-8"));
9+
const config = JSON.parse(readFileSync(join(__dirname, '../unit/config.json'), "utf-8"));
1010

1111
const user: User = { email: '***REMOVED***', authtoken: 'testtoken' };
1212
const credentials = { email: '***REMOVED***', password: config.password };

0 commit comments

Comments
 (0)