Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
sukamat authored Mar 27, 2024
2 parents 5105b54 + 17425b4 commit fa1a317
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 23 deletions.
1 change: 1 addition & 0 deletions .github/workflows/deploy_stage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jobs:
AIO_RUNTIME_AUTH: ${{ secrets.AIO_RUNTIME_AUTH_STAGE }}
GROUP_CHECK_URL: ${{ vars.GROUP_CHECK_URL }}
GRAYBOX_USER_GROUPS: ${{ vars.GRAYBOX_USER_GROUPS }}
SPLUNK_HEC__HEC_TOKEN: ${{ secrets.SPLUNK_HEC_TOKEN_STAGE }}
uses: adobe/[email protected]
with:
os: ${{ matrix.os }}
Expand Down
10 changes: 6 additions & 4 deletions actions/graybox/promote.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const appConfig = require('../appConfig');
async function main(params) {
const logger = getAioLogger();
const ow = openwhisk();
let responsePayload;
logger.info('Graybox Promote action invoked');
let responsePayload = 'Graybox Promote action invoked';
logger.info(responsePayload);
try {
appConfig.setAppConfig(params);
const grpIds = appConfig.getConfig().grayboxUserGroups;
Expand All @@ -47,14 +47,16 @@ async function main(params) {
payload: responsePayload
};
}).catch(async (err) => {
logger.error('Failed to invoke graybox promote action', err);
responsePayload = 'Failed to invoke graybox promote action';
logger.error(`${responsePayload}: ${err}`);
return {
code: 500,
payload: responsePayload
};
}));
} catch (err) {
logger.error('Unknown error occurred', err);
responsePayload = 'Unknown error occurred';
logger.error(`${responsePayload}: ${err}`);
responsePayload = err;
}

Expand Down
109 changes: 90 additions & 19 deletions test/graybox/promote.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,100 @@
* from Adobe.
************************************************************************* */

jest.mock('@adobe/aio-sdk', () => ({
Core: {
Logger: jest.fn()
}
}));
const openwhisk = require('openwhisk');
const action = require('../../actions/graybox/promote');
const { getAioLogger } = require('../../actions/utils');
const { validateAction } = require('../../actions/graybox/validateAction');
const appConfig = require('../../actions/appConfig');

const { Core } = require('@adobe/aio-sdk');
jest.mock('openwhisk');
jest.mock('../../actions/utils');
jest.mock('../../actions/graybox/validateAction');
jest.mock('../../actions/appConfig');

const mockLoggerInstance = { info: jest.fn(), debug: jest.fn(), error: jest.fn() };
Core.Logger.mockReturnValue(mockLoggerInstance);
describe('main function', () => {
let loggerMock;
let owMock;
let params;

jest.mock('node-fetch');
const action = require('../../actions/graybox/promote');
beforeEach(() => {
loggerMock = {
info: jest.fn(),
error: jest.fn()
};
getAioLogger.mockReturnValue(loggerMock);

beforeEach(() => {
Core.Logger.mockClear();
mockLoggerInstance.info.mockReset();
mockLoggerInstance.debug.mockReset();
mockLoggerInstance.error.mockReset();
});
owMock = {
actions: {
invoke: jest.fn()
}
};
openwhisk.mockReturnValue(owMock);
validateAction.mockReturnValue({ code: 200 });

appConfig.setAppConfig.mockImplementation(() => {
appConfig.getConfig.mockReturnValue({
grayboxUserGroups: ['group1', 'group2']
});
});

params = {
// mock params
};
});

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

test('invokes graybox promote action successfully', async () => {
appConfig.ignoreUserCheck.mockReturnValue(false);
owMock.actions.invoke.mockResolvedValue({
result: {
code: 200,

}
});

const result = await action.main(params);
const msg = 'Graybox Promote action invoked';
expect(loggerMock.info).toHaveBeenCalledWith(msg);
expect(validateAction).toHaveBeenCalledWith(params, ['group1', 'group2'], false);
expect(owMock.actions.invoke).toHaveBeenCalledWith({
name: 'graybox/promote-worker',
blocking: false,
result: false,
params
});
expect(result).toEqual({ code: 200, payload: msg });
});

test('handles validation failure', async () => {
validateAction.mockReturnValue({ code: 400 });

const result = await action.main(params);

expect(loggerMock.info).toHaveBeenCalledWith(expect.stringContaining('Validation failed'));
expect(result).toEqual({ code: 400 });
});

test('handles graybox promote action invocation failure', async () => {
const errMsg = 'Failed to invoke graybox promote action';
owMock.actions.invoke.mockRejectedValue(new Error(errMsg));

const result = await action.main(params);

expect(loggerMock.error).toHaveBeenCalledWith(expect.stringContaining(errMsg));
expect(result).toEqual({ code: 500, payload: errMsg });
});

test('handles unknown error', async () => {
const errMsg = 'Unknown error occurred';
validateAction.mockRejectedValue(new Error(errMsg));

const result = await action.main(params);

describe('promote', () => {
test('main should be defined', () => {
expect(action.main).toBeInstanceOf(Function);
expect(loggerMock.error).toHaveBeenCalledWith(expect.stringContaining(errMsg));
expect(result).toEqual({ code: 500, payload: new Error(errMsg) });
});
});
83 changes: 83 additions & 0 deletions test/graybox/validateAction.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* ************************************************************************
* ADOBE CONFIDENTIAL
* ___________________
*
* Copyright 2024 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe.
************************************************************************* */

const { validateAction } = require('../../actions/graybox/validateAction'); // Update the path accordingly
const GrayboxUser = require('../../actions/grayboxUser');

const mockValidParams = {
rootFolder: '/app',
gbRootFolder: '/app-graybox',
projectExcelPath: '/path/to/excel.xlsx',
experienceName: '/max',
spToken: 'abcde',
adminPageUri: 'https://a.com/path?ref=branch&repo=app&owner=org',
draftsOnly: true,
promoteIgnorePaths: '/path1'
};

// Mock GrayboxUser class and its methods
jest.mock('../../actions/grayboxUser', () => jest.fn().mockImplementation(() => ({
isInGroups: jest.fn().mockResolvedValue(true)
})));

describe('validateAction', () => {
beforeEach(() => {
jest.clearAllMocks();
});

test('should return 400 if required params are missing', async () => {
const params = {
// Missing some required parameters
};
const grpIds = [];
const result = await validateAction(params, grpIds);
expect(result.code).toBe(400);
});

test('should return 401 if user is not authorized', async () => {
const params = mockValidParams;
const grpIds = [];

// Mocking user not authorized
GrayboxUser.mockImplementation(() => ({
isInGroups: jest.fn().mockResolvedValue(false)
}));
const result = await validateAction(params, grpIds);
expect(result.code).toBe(401);
});

test('should return 200 if user is authorized and all required params are present', async () => {
const params = mockValidParams;
const grpIds = [];

// Mocking user authorized
GrayboxUser.mockImplementation(() => ({
isInGroups: jest.fn().mockResolvedValue(true)
}));
const result = await validateAction(params, grpIds);
expect(result.code).toBe(200);
});

test('should return 200 if ignoreUserCheck is true', async () => {
const params = mockValidParams;
const grpIds = [];
const result = await validateAction(params, grpIds, true);
expect(result.code).toBe(200);
// GrayboxUser constructor should not get called
expect(GrayboxUser).not.toHaveBeenCalled();
});
});

0 comments on commit fa1a317

Please sign in to comment.