Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ jobs:
- name: Run tests for Contentstack Branches
working-directory: ./packages/contentstack-branches
run: npm run test:unit

- name: Run tests for Contentstack Clone
working-directory: ./packages/contentstack-clone
run: npm run test:unit

# - name: Fetch latest references
# run: |
Expand Down
23 changes: 12 additions & 11 deletions packages/contentstack-auth/src/utils/auth-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,25 +118,26 @@ class AuthHandler {
if (result.user) {
log.debug('Login successful, user found', { module: 'auth-handler', userEmail: result.user.email });
resolve(result.user as User);
} else if (result.error_code === 294) {
const tfToken = await this.handleOTPFlow(tfaToken, loginPayload);

} else {
log.debug('Login failed: no user found.', { module: 'auth-handler', result });
reject(new Error(messageHandler.parse('CLI_AUTH_LOGIN_NO_USER')));
}
})
.catch(async (error: any) => {
if (error.errorCode === 294) {
try {
const tfToken = await this.handleOTPFlow(tfaToken, loginPayload);
resolve(await this.login(email, password, tfToken));
} catch (error) {
log.debug('Login with TFA token failed.', { module: 'auth-handler', error });
cliux.print('CLI_AUTH_2FA_FAILED', { color: 'red' });
reject(error);
}
} else {
log.debug('Login failed: no user found.', { module: 'auth-handler', result });
reject(new Error(messageHandler.parse('CLI_AUTH_LOGIN_NO_USER')));
log.debug('Login API call failed.', { module: 'auth-handler', error: error?.errorMessage || error });
cliux.print('CLI_AUTH_LOGIN_FAILED', { color: 'yellow' });
reject(error);
}
})
.catch((error: any) => {
log.debug('Login API call failed.', { module: 'auth-handler', error: error?.errorMessage || error });
cliux.print('CLI_AUTH_LOGIN_FAILED', { color: 'yellow' });
handleAndLogError(error, { module: 'auth-handler' });
});
} else {
const hasEmail = !!email;
Expand Down Expand Up @@ -203,7 +204,7 @@ class AuthHandler {
.catch((error: Error) => {
log.debug('Token validation failed.', { module: 'auth-handler', error: error.message });
cliux.print('CLI_AUTH_TOKEN_VALIDATION_FAILED', { color: 'yellow' });
handleAndLogError(error, { module: 'auth-handler' });
reject(error);
});
} else {
log.debug('Token validation failed: no auth token provided.', { module: 'auth-handler' });
Expand Down
14 changes: 0 additions & 14 deletions packages/contentstack-auth/src/utils/mfa-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,6 @@ class MFAHandler {
}
}

if (!secret) {
log.debug('Checking stored MFA secret', { module: 'mfa-handler' });
const mfaConfig = configHandler.get('mfa');
if (mfaConfig?.secret) {
try {
secret = this.encrypter.decrypt(mfaConfig.secret);
source = 'stored configuration';
} catch (error) {
log.debug('Failed to decrypt stored MFA secret', { module: 'mfa-handler', error });
handleAndLogError(error, { module: 'mfa-handler' }, messageHandler.parse('CLI_AUTH_MFA_DECRYPT_FAILED'));
}
}
}

if (secret) {
try {
const code = this.generateMFACode(secret);
Expand Down
22 changes: 17 additions & 5 deletions packages/contentstack-auth/test/unit/auth-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ describe('Auth Handler', function () {
return Promise.reject(new Error('Invalid 2FA code'));
}
} else {
return Promise.resolve({ error_code: 294 });
const error: any = new Error('2FA required');
error.errorCode = 294;
return Promise.reject(error);
}
}
return Promise.resolve({ user });
Expand Down Expand Up @@ -115,13 +117,23 @@ describe('Auth Handler', function () {
it('Login with 2FA enabled invalid otp, failed to login', async function () {
this.timeout(10000);
TFAEnabled = true;
let result;
askOTPStub.restore();
askOTPStub = sinon.stub(interactive, 'askOTP').callsFake(function () {
return Promise.resolve(InvalidTFATestToken);
});
try {
result = await authHandler.login(credentials.email, credentials.password);
await authHandler.login(credentials.email, credentials.password);
expect.fail('Should have thrown an error');
} catch (error) {
result = 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);
});
}
TFAEnabled = false;
});

it('Login with 2FA enabled with sms channel, should be logged in successfully', async function () {
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-branches/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"mocha": "10.8.2",
"nyc": "^15.1.0",
"oclif": "^4.17.46",
"sinon": "^19.0.5",
"sinon": "^21.0.1",
"ts-node": "^10.9.2",
"typescript": "^4.9.5"
},
Expand Down
53 changes: 52 additions & 1 deletion packages/contentstack-clone/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,54 @@
{
"extends": "oclif"
"env": {
"node": true,
"es2021": true
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "tsconfig.json",
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"extends": [
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking"
],
"ignorePatterns": [
"lib/**/*",
"test/**/*",
"node_modules/**/*",
"*.js"
],
"rules": {
"@typescript-eslint/no-unused-vars": [
"error",
{
"args": "none",
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_"
}
],
"@typescript-eslint/prefer-namespace-keyword": "error",
"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/no-misused-promises": "error",
"@typescript-eslint/await-thenable": "error",
"quotes": ["error", "single", { "avoidEscape": true, "allowTemplateLiterals": true }],
"semi": "off",
"@typescript-eslint/no-redeclare": "off",
"eqeqeq": ["error", "smart"],
"id-match": "error",
"no-eval": "error",
"no-var": "error",
"@typescript-eslint/no-explicit-any": "warn",
"@typescript-eslint/no-require-imports": "off",
"prefer-const": "error",
"@typescript-eslint/no-unsafe-call": "off",
"@typescript-eslint/no-unsafe-member-access": "off",
"@typescript-eslint/no-unsafe-assignment": "off",
"@typescript-eslint/no-unsafe-return": "off",
"@typescript-eslint/no-unsafe-argument": "off",
"@typescript-eslint/require-await": "off"
}
}
1 change: 1 addition & 0 deletions packages/contentstack-clone/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/yarn.lock
node_modules
coverage
/lib
8 changes: 8 additions & 0 deletions packages/contentstack-clone/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"require": ["test/helpers/init.js", "ts-node/register", "source-map-support/register"],
"watch-extensions": [
"ts"
],
"recursive": true,
"timeout": 5000
}
28 changes: 28 additions & 0 deletions packages/contentstack-clone/.nycrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"include": [
"lib/**/*.js",
"src/**/*.ts"
],
"exclude": [
"**/*.test.ts",
"**/test/**",
"**/node_modules/**"
],
"reporter": [
"text",
"text-summary",
"lcov",
"html"
],
"check-coverage": false,
"statements": 90,
"branches": 90,
"functions": 90,
"lines": 90,
"extension": [
".ts",
".js"
],
"sourceMap": true,
"instrument": true
}
Loading
Loading