Add tests for GitHub License API fallback

This commit is contained in:
cnagadya
2022-10-13 11:57:38 +00:00
parent d1e9a12830
commit 4c0961eff6
6 changed files with 98 additions and 20 deletions
+66 -1
View File
@@ -1,4 +1,4 @@
import {expect, test} from '@jest/globals'
import {expect, jest, test} from '@jest/globals'
import {Change, Changes} from '../src/schemas'
import {getDeniedLicenseChanges} from '../src/licenses'
@@ -48,6 +48,28 @@ let rubyChange: Change = {
]
}
jest.mock('@actions/core')
const mockOctokit = {
rest: {
licenses: {
getForRepo: jest
.fn()
.mockReturnValue({data: {license: {spdx_id: 'AGPL'}}})
}
}
}
jest.mock('octokit', () => {
return {
Octokit: class {
constructor() {
return mockOctokit
}
}
}
})
test('it fails if a license outside the allow list is found', async () => {
const changes: Changes = [npmChange, rubyChange]
const [invalidChanges, _] = await getDeniedLicenseChanges(changes, {
@@ -108,3 +130,46 @@ test('it fails if a license outside the allow list is found in both of added and
})
expect(invalidChanges).toStrictEqual([npmChange])
})
describe('GH License API fallback', () => {
beforeEach(() => {
jest.clearAllMocks()
})
test('it calls licenses endpoint if atleast one of the changes has null license and valid source_repository_url', async () => {
const nullLicenseChange = {
...npmChange,
license: null,
source_repository_url: 'http://github.com/some-owner/some-repo'
}
const [_, unknownChanges] = await getDeniedLicenseChanges(
[nullLicenseChange, rubyChange],
{}
)
expect(mockOctokit.rest.licenses.getForRepo).toHaveBeenNthCalledWith(1, {
owner: 'some-owner',
repo: 'some-repo'
})
expect(unknownChanges.length).toEqual(0)
})
test('it does not call licenses API endpoint for change with null license and invalid source_repository_url ', async () => {
const [_, unknownChanges] = await getDeniedLicenseChanges(
[{...npmChange, license: null}],
{}
)
expect(mockOctokit.rest.licenses.getForRepo).not.toHaveBeenCalled()
expect(unknownChanges.length).toEqual(1)
})
test('it does not call licenses API endpoint if licenses for all changes are present', async () => {
const [_, unknownChanges] = await getDeniedLicenseChanges(
[npmChange, rubyChange],
{}
)
expect(mockOctokit.rest.licenses.getForRepo).not.toHaveBeenCalled()
expect(unknownChanges.length).toEqual(0)
})
})
Generated Vendored
+8 -11
View File
@@ -193,10 +193,7 @@ const fetchGHLicense = (owner, repo) => __awaiter(void 0, void 0, void 0, functi
auth: core.getInput('repo-token', { required: true })
});
try {
const response = yield octokit.request('GET /repos/{owner}/{repo}/license', {
owner,
repo
});
const response = yield octokit.rest.licenses.getForRepo({ owner, repo });
return (_b = (_a = response.data.license) === null || _a === void 0 ? void 0 : _a.spdx_id) !== null && _b !== void 0 ? _b : null;
}
catch (_) {
@@ -40229,11 +40226,11 @@ exports.visitAsync = visitAsync;
"use strict";
__nccwpck_require__.r(__webpack_exports__);
/* harmony export */ __nccwpck_require__.d(__webpack_exports__, {
/* harmony export */ "default": () => (__WEBPACK_DEFAULT_EXPORT__),
/* harmony export */ "modifierNames": () => (/* binding */ modifierNames),
/* harmony export */ "foregroundColorNames": () => (/* binding */ foregroundColorNames),
/* harmony export */ "backgroundColorNames": () => (/* binding */ backgroundColorNames),
/* harmony export */ "colorNames": () => (/* binding */ colorNames)
/* harmony export */ "colorNames": () => (/* binding */ colorNames),
/* harmony export */ "default": () => (__WEBPACK_DEFAULT_EXPORT__)
/* harmony export */ });
const ANSI_BACKGROUND_OFFSET = 10;
@@ -40302,6 +40299,11 @@ const styles = {
},
};
const modifierNames = Object.keys(styles.modifier);
const foregroundColorNames = Object.keys(styles.color);
const backgroundColorNames = Object.keys(styles.bgColor);
const colorNames = [...foregroundColorNames, ...backgroundColorNames];
function assembleStyles() {
const codes = new Map();
@@ -40454,11 +40456,6 @@ const ansiStyles = assembleStyles();
/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (ansiStyles);
const modifierNames = Object.keys(styles.modifier);
const foregroundColorNames = Object.keys(styles.color);
const backgroundColorNames = Object.keys(styles.bgColor);
const colorNames = [...foregroundColorNames, ...backgroundColorNames];
/***/ }),
Generated Vendored
+1 -1
View File
File diff suppressed because one or more lines are too long
+21
View File
@@ -21,6 +21,7 @@
"zod": "^3.19.1"
},
"devDependencies": {
"@types/jest": "^27.5.2",
"@types/node": "^16.11.65",
"@typescript-eslint/eslint-plugin": "^5.40.0",
"@typescript-eslint/parser": "^5.40.0",
@@ -1997,6 +1998,16 @@
"@types/istanbul-lib-report": "*"
}
},
"node_modules/@types/jest": {
"version": "27.5.2",
"resolved": "https://registry.npmjs.org/@types/jest/-/jest-27.5.2.tgz",
"integrity": "sha512-mpT8LJJ4CMeeahobofYWIjFo0xonRS/HfxnVEPMPFSQdGUt1uHCnoPT7Zhb+sjDU2wz0oKV0OLUR0WzrHNgfeA==",
"dev": true,
"dependencies": {
"jest-matcher-utils": "^27.0.0",
"pretty-format": "^27.0.0"
}
},
"node_modules/@types/json-schema": {
"version": "7.0.9",
"resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.9.tgz",
@@ -9743,6 +9754,16 @@
"@types/istanbul-lib-report": "*"
}
},
"@types/jest": {
"version": "27.5.2",
"resolved": "https://registry.npmjs.org/@types/jest/-/jest-27.5.2.tgz",
"integrity": "sha512-mpT8LJJ4CMeeahobofYWIjFo0xonRS/HfxnVEPMPFSQdGUt1uHCnoPT7Zhb+sjDU2wz0oKV0OLUR0WzrHNgfeA==",
"dev": true,
"requires": {
"jest-matcher-utils": "^27.0.0",
"pretty-format": "^27.0.0"
}
},
"@types/json-schema": {
"version": "7.0.9",
"resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.9.tgz",
+1
View File
@@ -37,6 +37,7 @@
"zod": "^3.19.1"
},
"devDependencies": {
"@types/jest": "^27.5.2",
"@types/node": "^16.11.65",
"@typescript-eslint/eslint-plugin": "^5.40.0",
"@typescript-eslint/parser": "^5.40.0",
+1 -7
View File
@@ -65,13 +65,7 @@ const fetchGHLicense = async (
})
try {
const response = await octokit.request(
'GET /repos/{owner}/{repo}/license',
{
owner,
repo
}
)
const response = await octokit.rest.licenses.getForRepo({owner, repo})
return response.data.license?.spdx_id ?? null
} catch (_) {
return null