Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/O11y-Rerun' into O11y-Rerun-Test
Browse files Browse the repository at this point in the history
  • Loading branch information
darpanLalwani committed Sep 24, 2024
2 parents 6fa9794 + 7a8d7d8 commit 683ccaf
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 7 deletions.
1 change: 1 addition & 0 deletions setup-env/config/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = {
ACCESS_KEY: 'access-key',
BUILD_NAME: 'build-name',
PROJECT_NAME: 'project-name',
GITHUB_TOKEN: 'github-token',
GITHUB_APP: 'github-app',
GITHUB_TOKEN: 'github-token',

Check failure on line 9 in setup-env/config/constants.js

View workflow job for this annotation

GitHub Actions / unit-tests (macos-latest)

Duplicate key 'GITHUB_TOKEN'
},
Expand Down
29 changes: 28 additions & 1 deletion setup-env/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
ACCESS_KEY: 'access-key',
BUILD_NAME: 'build-name',
PROJECT_NAME: 'project-name',
GITHUB_TOKEN: 'github-token',
GITHUB_APP: 'github-app',
GITHUB_TOKEN: 'github-token',
},
Expand Down Expand Up @@ -9363,6 +9364,7 @@ class ActionInput {
this.buildName = core.getInput(INPUT.BUILD_NAME);
this.projectName = core.getInput(INPUT.PROJECT_NAME);
this.githubApp = core.getInput(INPUT.GITHUB_APP);
this.githubToken = core.getInput(INPUT.GITHUB_TOKEN);
this.rerunAttempt = process?.env?.GITHUB_RUN_ATTEMPT;
this.runId = process?.env?.GITHUB_RUN_ID;
this.repository = process?.env?.GITHUB_REPOSITORY;
Expand All @@ -9380,6 +9382,7 @@ class ActionInput {
this.buildName = InputValidator.validateBuildName(this.buildName);
this.projectName = InputValidator.validateProjectName(this.projectName);
this.githubApp = InputValidator.validateGithubAppName(this.githubApp);
this.githubToken = InputValidator.validateGithubToken(this.githubToken);
}

/**
Expand Down Expand Up @@ -9416,7 +9419,8 @@ class ActionInput {
}

// Ensure runId, repository, username, and accessKey are valid
if (!this.runId || !this.repository || this.repository === 'none' || !this.username || !this.accessKey) {
if (!this.runId || !this.repository || this.repository === 'none'
|| !this.githubToken || this.githubToken === 'none' || !this.username || !this.accessKey) {
return false;
}

Expand Down Expand Up @@ -9618,6 +9622,29 @@ class InputValidator {

throw new Error("Invalid input for 'github-app'. Must be a valid string.");
}

/**
* Validates the GitHub token input to ensure it is a valid non-empty string.
* If the input is 'none' or not provided, it returns 'none'.
* @param {string} githubToken Input for 'github-token'
* @returns {string} The validated GitHub token, or 'none' if input is 'none' or invalid
* @throws {Error} If the input is not a valid non-empty string
*/
static validateGithubToken(githubToken) {
if (typeof githubToken !== 'string') {
throw new Error("Invalid input for 'github-token'. Must be a valid non-empty string.");
}

if (githubToken.toLowerCase() === 'none') {
return 'none';
}

if (githubToken.trim().length > 0) {
return githubToken;
}

throw new Error("Invalid input for 'github-token'. Must be a valid non-empty string.");
}
}

module.exports = InputValidator;
Expand Down
5 changes: 4 additions & 1 deletion setup-env/src/actionInput/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ActionInput {
this.buildName = core.getInput(INPUT.BUILD_NAME);
this.projectName = core.getInput(INPUT.PROJECT_NAME);
this.githubApp = core.getInput(INPUT.GITHUB_APP);
this.githubToken = core.getInput(INPUT.GITHUB_TOKEN);
this.rerunAttempt = process?.env?.GITHUB_RUN_ATTEMPT;
this.runId = process?.env?.GITHUB_RUN_ID;
this.repository = process?.env?.GITHUB_REPOSITORY;
Expand All @@ -50,6 +51,7 @@ class ActionInput {
this.buildName = InputValidator.validateBuildName(this.buildName);
this.projectName = InputValidator.validateProjectName(this.projectName);
this.githubApp = InputValidator.validateGithubAppName(this.githubApp);
this.githubToken = InputValidator.validateGithubToken(this.githubToken);
}

/**
Expand Down Expand Up @@ -86,7 +88,8 @@ class ActionInput {
}

// Ensure runId, repository, username, and accessKey are valid
if (!this.runId || !this.repository || this.repository === 'none' || !this.username || !this.accessKey) {
if (!this.runId || !this.repository || this.repository === 'none'
|| !this.githubToken || this.githubToken === 'none' || !this.username || !this.accessKey) {
return false;
}

Expand Down
23 changes: 23 additions & 0 deletions setup-env/src/actionInput/inputValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,29 @@ class InputValidator {

throw new Error("Invalid input for 'github-app'. Must be a valid string.");
}

/**
* Validates the GitHub token input to ensure it is a valid non-empty string.
* If the input is 'none' or not provided, it returns 'none'.
* @param {string} githubToken Input for 'github-token'
* @returns {string} The validated GitHub token, or 'none' if input is 'none' or invalid
* @throws {Error} If the input is not a valid non-empty string
*/
static validateGithubToken(githubToken) {
if (typeof githubToken !== 'string') {
throw new Error("Invalid input for 'github-token'. Must be a valid non-empty string.");
}

if (githubToken.toLowerCase() === 'none') {
return 'none';
}

if (githubToken.trim().length > 0) {
return githubToken;
}

throw new Error("Invalid input for 'github-token'. Must be a valid non-empty string.");
}
}

module.exports = InputValidator;
37 changes: 32 additions & 5 deletions setup-env/test/actionInput/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('Action Input operations for fetching all inputs, triggering validation
sinon.stub(InputValidator, 'validateBuildName').returns('validatedBuildName');
sinon.stub(InputValidator, 'validateProjectName').returns('validatedProjectName');
sinon.stub(InputValidator, 'validateGithubAppName').returns('validatedAppName');
sinon.stub(InputValidator, 'validateGithubToken').returns('validatedToken');

// Provide required inputs
stubbedInput.withArgs(INPUT.USERNAME, { required: true }).returns('someUsername');
Expand Down Expand Up @@ -66,34 +67,57 @@ describe('Action Input operations for fetching all inputs, triggering validation
expect(e.message).to.eq('Action input failed for reason: Access Key Required');
}
});

it('Takes input and validates GitHub token and app name successfully', () => {
stubbedInput.withArgs(INPUT.GITHUB_TOKEN).returns('someToken');
stubbedInput.withArgs(INPUT.GITHUB_APP).returns('someApp');
const actionInput = new ActionInput();
expect(actionInput.githubToken).to.eq('validatedToken');
expect(actionInput.githubApp).to.eq('validatedAppName');
});
});

context('Set Environment Variables', () => {
let actionInput;

beforeEach(() => {
sinon.stub(core, 'exportVariable');
sinon.stub(core, 'info');
sinon.stub(core, 'startGroup');
sinon.stub(core, 'endGroup');
sinon.stub(ActionInput.prototype, '_fetchAllInput');
sinon.stub(ActionInput.prototype, '_validateInput');

// Mock required properties
actionInput = new ActionInput();
actionInput.username = 'someUsername';
actionInput.accessKey = 'someAccessKey';
actionInput.buildName = 'someBuildName';
actionInput.projectName = 'someProjectName';

// Stub checkIfBStackReRun to return true
sinon.stub(actionInput, 'checkIfBStackReRun').returns(Promise.resolve(true));
});

afterEach(() => {
sinon.restore();
});

it('Sets the environment variables required in test scripts for BrowserStack', () => {
const actionInput = new ActionInput();
actionInput.username = 'someUsername';
actionInput.accessKey = 'someAccessKey';
actionInput.buildName = 'someBuildName';
actionInput.projectName = 'someProjectName';
actionInput.setEnvVariables();
sinon.assert.calledWith(core.exportVariable, ENV_VARS.BROWSERSTACK_USERNAME, 'someUsername');
sinon.assert.calledWith(core.exportVariable, ENV_VARS.BROWSERSTACK_ACCESS_KEY, 'someAccessKey');
sinon.assert.calledWith(core.exportVariable, ENV_VARS.BROWSERSTACK_PROJECT_NAME, 'someProjectName');
sinon.assert.calledWith(core.exportVariable, ENV_VARS.BROWSERSTACK_BUILD_NAME, 'someBuildName');
});

it('Calls setBStackRerunEnvVars when checkIfBStackReRun returns true', async () => {
const setBStackRerunEnvVarsStub = sinon.stub(actionInput, 'setBStackRerunEnvVars').resolves();

await actionInput.setEnvVariables();

sinon.assert.calledOnce(setBStackRerunEnvVarsStub);
});
});

context('Check if BrowserStack Rerun', () => {
Expand All @@ -105,6 +129,7 @@ describe('Action Input operations for fetching all inputs, triggering validation
sinon.stub(InputValidator, 'validateBuildName').returns('validatedBuildName');
sinon.stub(InputValidator, 'validateProjectName').returns('validatedProjectName');
sinon.stub(InputValidator, 'validateGithubAppName').returns('validatedAppName');
sinon.stub(InputValidator, 'validateGithubToken').returns('validatedToken');

// Provide required inputs
stubbedInput.withArgs(INPUT.USERNAME, { required: true }).returns('someUsername');
Expand Down Expand Up @@ -162,6 +187,7 @@ describe('Action Input operations for fetching all inputs, triggering validation
sinon.stub(InputValidator, 'validateBuildName').returns('validatedBuildName');
sinon.stub(InputValidator, 'validateProjectName').returns('validatedProjectName');
sinon.stub(InputValidator, 'validateGithubAppName').returns('validatedAppName');
sinon.stub(InputValidator, 'validateGithubToken').returns('validatedToken');

// Provide required inputs
stubbedInput.withArgs(INPUT.USERNAME, { required: true }).returns('someUsername');
Expand Down Expand Up @@ -212,6 +238,7 @@ describe('Action Input operations for fetching all inputs, triggering validation
sinon.stub(InputValidator, 'validateBuildName').returns('validatedBuildName');
sinon.stub(InputValidator, 'validateProjectName').returns('validatedProjectName');
sinon.stub(InputValidator, 'validateGithubAppName').returns('validatedAppName');
sinon.stub(InputValidator, 'validateGithubToken').returns('validatedToken');

// Provide required inputs
stubbedInput.withArgs(INPUT.USERNAME, { required: true }).returns('someUsername');
Expand Down
23 changes: 23 additions & 0 deletions setup-env/test/actionInput/inputValidator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,29 @@ describe('InputValidator class to validate individual fields of the action input
expect(InputValidator.validateGithubAppName(validAppName)).to.eq(validAppName);
});
});

context('Validates GitHub Token', () => {
it("Returns 'none' if the token is not provided", () => {
expect(() => InputValidator.validateGithubToken()).to.throw("Invalid input for 'github-token'. Must be a valid non-empty string.");
});

it("Returns 'none' if the token is 'none' (case insensitive)", () => {
expect(InputValidator.validateGithubToken('None')).to.eq('none');
});

it('Throws an error if the token is an empty string', () => {
expect(() => InputValidator.validateGithubToken('')).to.throw("Invalid input for 'github-token'. Must be a valid non-empty string.");
});

it('Throws an error if the token is not a valid string', () => {
expect(() => InputValidator.validateGithubToken(123)).to.throw("Invalid input for 'github-token'. Must be a valid non-empty string.");
});

it('Returns the token if it is a valid non-empty string and not "none"', () => {
const validToken = 'someValidToken';
expect(InputValidator.validateGithubToken(validToken)).to.eq(validToken);
});
});
});
});
});

0 comments on commit 683ccaf

Please sign in to comment.