Skip to content
Merged
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
10 changes: 9 additions & 1 deletion .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
* @contentstack/security-admin
* @contentstack/launch-pr-reviewers

.github/workflows/sca-scan.yml @contentstack/security-admin

**/.snyk @contentstack/security-admin

.github/workflows/policy-scan.yml @contentstack/security-admin

.github/workflows/issues-jira.yml @contentstack/security-admin
29 changes: 0 additions & 29 deletions .github/workflows/secrets-scan.yml

This file was deleted.

10 changes: 10 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## Testing instructions

- Follow the Arrange, Act, Assert structure when writing unit tests.
- When writing unit tests, create individual unit tests that cover each logical branching in the code.
- For the happy path, can we have a single unit test where all the top level if conditions are executed? This might help with reducing the number of total unit tests created and still give same test coverage.
- For the tests for edge cases do not create separate describe blocks, keep the hierarchy flat.
- For the tests for edge cases, do not skip assertions, its still worth adding all assertions similar to the happy paths tests.
- Use only jest for writing test cases and refer existing unit test under the /src folder.
- Do not create code comments for any changes.

8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"/oclif.manifest.json"
],
"dependencies": {
"@apollo/client": "^3.11.8",
"@apollo/client": "^3.14.0",
"@contentstack/cli-command": "^1.4.0",
"@contentstack/cli-utilities": "^1.12.0",
"@oclif/core": "^4.2.7",
Expand Down
256 changes: 252 additions & 4 deletions src/adapters/base-class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,213 @@ describe('BaseClass', () => {
config: config.variablePreparationTypeOptions,
} as any);
});

it('should handle string variableType by converting to array - Import variables from a stack', async () => {
baseClass = new BaseClass({
log: logMock,
exit: exitMock,
config: {
variableType: 'Import variables from a stack',
variablePreparationTypeOptions: config.variablePreparationTypeOptions,
},
} as any);

const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce();

await baseClass.handleEnvImportFlow();

expect(importEnvFromStackMock).toHaveBeenCalled();
expect(exitMock).not.toHaveBeenCalled();
});

it('should handle string variableType by converting to array - Manually add custom variables to the list', async () => {
baseClass = new BaseClass({
log: logMock,
exit: exitMock,
config: {
variableType: 'Manually add custom variables to the list',
variablePreparationTypeOptions: config.variablePreparationTypeOptions,
},
} as any);

const promptForEnvValuesMock = jest.spyOn(baseClass, 'promptForEnvValues').mockResolvedValueOnce();

await baseClass.handleEnvImportFlow();

expect(promptForEnvValuesMock).toHaveBeenCalled();
expect(exitMock).not.toHaveBeenCalled();
});

it('should handle string variableType by converting to array - Import variables from the .env.local file', async () => {
baseClass = new BaseClass({
log: logMock,
exit: exitMock,
config: {
variableType: 'Import variables from the .env.local file',
variablePreparationTypeOptions: config.variablePreparationTypeOptions,
},
} as any);

const importVariableFromLocalConfigMock = jest
.spyOn(baseClass, 'importVariableFromLocalConfig')
.mockResolvedValueOnce();

await baseClass.handleEnvImportFlow();

expect(importVariableFromLocalConfigMock).toHaveBeenCalled();
expect(exitMock).not.toHaveBeenCalled();
});

it('should handle string variableType by converting to array - Skip adding environment variables', async () => {
baseClass = new BaseClass({
log: logMock,
exit: exitMock,
config: {
variableType: 'Skip adding environment variables',
variablePreparationTypeOptions: config.variablePreparationTypeOptions,
},
} as any);

await baseClass.handleEnvImportFlow();

expect(baseClass.envVariables).toEqual([]);
expect(logMock).toHaveBeenCalledWith('Skipped adding environment variables.', 'info');
expect(exitMock).not.toHaveBeenCalled();
});

it('should fail if string to array conversion is removed', async () => {
baseClass = new BaseClass({
log: logMock,
exit: exitMock,
config: {
variableType: 'Skip adding environment variables',
variablePreparationTypeOptions: config.variablePreparationTypeOptions,
},
} as any);

await baseClass.handleEnvImportFlow();

expect(exitMock).not.toHaveBeenCalled();
expect(logMock).not.toHaveBeenCalledWith(
"The 'Skip adding environment variables' option cannot be combined with other environment variable options. Please choose either 'Skip adding environment variables' or one or more of the other available options.",
'error',
);
});

it('should handle two options: Import from stack and Manually add variables', async () => {
baseClass = new BaseClass({
log: logMock,
exit: exitMock,
config: {
variableType: ['Import variables from a stack', 'Manually add custom variables to the list'],
variablePreparationTypeOptions: config.variablePreparationTypeOptions,
},
} as any);

const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce();
const promptForEnvValuesMock = jest.spyOn(baseClass, 'promptForEnvValues').mockResolvedValueOnce();

await baseClass.handleEnvImportFlow();

expect(importEnvFromStackMock).toHaveBeenCalled();
expect(promptForEnvValuesMock).toHaveBeenCalled();
expect(exitMock).not.toHaveBeenCalled();
});

it('should handle two options: Import from stack and Import from .env.local', async () => {
baseClass = new BaseClass({
log: logMock,
exit: exitMock,
config: {
variableType: ['Import variables from a stack', 'Import variables from the .env.local file'],
variablePreparationTypeOptions: config.variablePreparationTypeOptions,
},
} as any);

const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce();
const importVariableFromLocalConfigMock = jest
.spyOn(baseClass, 'importVariableFromLocalConfig')
.mockResolvedValueOnce();

await baseClass.handleEnvImportFlow();

expect(importEnvFromStackMock).toHaveBeenCalled();
expect(importVariableFromLocalConfigMock).toHaveBeenCalled();
expect(exitMock).not.toHaveBeenCalled();
});

it('should handle two options: Manually add and Import from .env.local', async () => {
baseClass = new BaseClass({
log: logMock,
exit: exitMock,
config: {
variableType: ['Manually add custom variables to the list', 'Import variables from the .env.local file'],
variablePreparationTypeOptions: config.variablePreparationTypeOptions,
},
} as any);

const promptForEnvValuesMock = jest.spyOn(baseClass, 'promptForEnvValues').mockResolvedValueOnce();
const importVariableFromLocalConfigMock = jest
.spyOn(baseClass, 'importVariableFromLocalConfig')
.mockResolvedValueOnce();

await baseClass.handleEnvImportFlow();

expect(promptForEnvValuesMock).toHaveBeenCalled();
expect(importVariableFromLocalConfigMock).toHaveBeenCalled();
expect(exitMock).not.toHaveBeenCalled();
});

it('should handle all three options: Import from stack, Manually add, and Import from .env.local', async () => {
baseClass = new BaseClass({
log: logMock,
exit: exitMock,
config: {
variableType: [
'Import variables from a stack',
'Manually add custom variables to the list',
'Import variables from the .env.local file',
],
variablePreparationTypeOptions: config.variablePreparationTypeOptions,
},
} as any);

const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce();
const promptForEnvValuesMock = jest.spyOn(baseClass, 'promptForEnvValues').mockResolvedValueOnce();
const importVariableFromLocalConfigMock = jest
.spyOn(baseClass, 'importVariableFromLocalConfig')
.mockResolvedValueOnce();

await baseClass.handleEnvImportFlow();

expect(importEnvFromStackMock).toHaveBeenCalled();
expect(promptForEnvValuesMock).toHaveBeenCalled();
expect(importVariableFromLocalConfigMock).toHaveBeenCalled();
expect(exitMock).not.toHaveBeenCalled();
});

it('should fail when Skip is combined with other options', async () => {
baseClass = new BaseClass({
log: logMock,
exit: exitMock,
config: {
variableType: ['Skip adding environment variables', 'Import variables from a stack'],
variablePreparationTypeOptions: config.variablePreparationTypeOptions,
},
} as any);

const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce(undefined);

await baseClass.handleEnvImportFlow();

expect(logMock).toHaveBeenCalledWith(
"The 'Skip adding environment variables' option cannot be combined with other environment variable options. Please choose either 'Skip adding environment variables' or one or more of the other available options.",
'error',
);
expect(exitMock).toHaveBeenCalledWith(1);
expect(importEnvFromStackMock).toHaveBeenCalled();
});

it('should exit if no options are selected', async () => {
(ux.inquire as jest.Mock).mockResolvedValueOnce([]);

Expand All @@ -45,21 +252,30 @@ describe('BaseClass', () => {
});

it('should exit if "Skip adding environment variables" is selected with other options', async () => {
const exitMockWithThrow = jest.fn().mockImplementation(() => {
throw new Error('Exit called');
});
baseClass = new BaseClass({
log: logMock,
exit: exitMockWithThrow,
config: config.variablePreparationTypeOptions,
} as any);

const importEnvFromStackMock = jest.spyOn(baseClass, 'importEnvFromStack').mockResolvedValueOnce();
(ux.inquire as jest.Mock).mockResolvedValueOnce([
'Skip adding environment variables',
'Import variables from a stack',
]);

await baseClass.handleEnvImportFlow();
await expect(baseClass.handleEnvImportFlow()).rejects.toThrow('Exit called');

expect(logMock).toHaveBeenCalledWith(
"The 'Skip adding environment variables' option cannot be combined with other environment variable options. Please choose either 'Skip adding environment variables' or one or more of the other available options.",
'error',
);

expect(exitMock).toHaveBeenCalledWith(1);
expect(importEnvFromStackMock).toHaveBeenCalled();
expect(exitMockWithThrow).toHaveBeenCalledWith(1);
expect(importEnvFromStackMock).not.toHaveBeenCalled();
});

it('should call importEnvFromStack if "Import variables from a stack" is selected', async () => {
Expand Down Expand Up @@ -162,7 +378,7 @@ describe('BaseClass', () => {
'Import variables from the .env.local file',
]);

await baseClass.handleEnvImportFlow();
await baseClass.handleEnvImportFlow();

expect(importEnvFromStackMock).toHaveBeenCalled();
expect(promptForEnvValuesMock).toHaveBeenCalled();
Expand Down Expand Up @@ -298,4 +514,36 @@ describe('BaseClass', () => {
expect(exitMock).toHaveBeenCalledWith(1);
});
});

describe('handleEnvVariables', () => {
beforeEach(() => {
baseClass = new BaseClass({
log: logMock,
exit: exitMock,
config: {},
} as any);
});

afterEach(() => {
jest.clearAllMocks();
});

it('should parse environment variables from config string with various value formats including URLs', async () => {
baseClass = new BaseClass({
log: logMock,
exit: exitMock,
config: {
envVariables: 'APP_ENV:prod, API_URL:https://api.example.com/v1, DB_URL:postgresql://localhost:5432/dbname',
},
} as any);

await baseClass.promptForEnvValues();

expect(baseClass.envVariables).toEqual([
{ key: 'APP_ENV', value: 'prod' },
{ key: 'API_URL', value: 'https://api.example.com/v1' },
{ key: 'DB_URL', value: 'postgresql://localhost:5432/dbname' },
]);
});
});
});
Loading