|
1 | | -import {expect, test} from '@jest/globals' |
| 1 | +import {expect, jest, test} from '@jest/globals' |
2 | 2 | import {Change, Changes} from '../src/schemas' |
3 | 3 | import {getDeniedLicenseChanges} from '../src/licenses' |
4 | 4 |
|
@@ -48,6 +48,28 @@ let rubyChange: Change = { |
48 | 48 | ] |
49 | 49 | } |
50 | 50 |
|
| 51 | +jest.mock('@actions/core') |
| 52 | + |
| 53 | +const mockOctokit = { |
| 54 | + rest: { |
| 55 | + licenses: { |
| 56 | + getForRepo: jest |
| 57 | + .fn() |
| 58 | + .mockReturnValue({data: {license: {spdx_id: 'AGPL'}}}) |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +jest.mock('octokit', () => { |
| 64 | + return { |
| 65 | + Octokit: class { |
| 66 | + constructor() { |
| 67 | + return mockOctokit |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +}) |
| 72 | + |
51 | 73 | test('it fails if a license outside the allow list is found', async () => { |
52 | 74 | const changes: Changes = [npmChange, rubyChange] |
53 | 75 | 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 |
108 | 130 | }) |
109 | 131 | expect(invalidChanges).toStrictEqual([npmChange]) |
110 | 132 | }) |
| 133 | + |
| 134 | +describe('GH License API fallback', () => { |
| 135 | + beforeEach(() => { |
| 136 | + jest.clearAllMocks() |
| 137 | + }) |
| 138 | + |
| 139 | + test('it calls licenses endpoint if atleast one of the changes has null license and valid source_repository_url', async () => { |
| 140 | + const nullLicenseChange = { |
| 141 | + ...npmChange, |
| 142 | + license: null, |
| 143 | + source_repository_url: 'http://github.com/some-owner/some-repo' |
| 144 | + } |
| 145 | + const [_, unknownChanges] = await getDeniedLicenseChanges( |
| 146 | + [nullLicenseChange, rubyChange], |
| 147 | + {} |
| 148 | + ) |
| 149 | + |
| 150 | + expect(mockOctokit.rest.licenses.getForRepo).toHaveBeenNthCalledWith(1, { |
| 151 | + owner: 'some-owner', |
| 152 | + repo: 'some-repo' |
| 153 | + }) |
| 154 | + expect(unknownChanges.length).toEqual(0) |
| 155 | + }) |
| 156 | + |
| 157 | + test('it does not call licenses API endpoint for change with null license and invalid source_repository_url ', async () => { |
| 158 | + const [_, unknownChanges] = await getDeniedLicenseChanges( |
| 159 | + [{...npmChange, license: null}], |
| 160 | + {} |
| 161 | + ) |
| 162 | + expect(mockOctokit.rest.licenses.getForRepo).not.toHaveBeenCalled() |
| 163 | + expect(unknownChanges.length).toEqual(1) |
| 164 | + }) |
| 165 | + |
| 166 | + test('it does not call licenses API endpoint if licenses for all changes are present', async () => { |
| 167 | + const [_, unknownChanges] = await getDeniedLicenseChanges( |
| 168 | + [npmChange, rubyChange], |
| 169 | + {} |
| 170 | + ) |
| 171 | + |
| 172 | + expect(mockOctokit.rest.licenses.getForRepo).not.toHaveBeenCalled() |
| 173 | + expect(unknownChanges.length).toEqual(0) |
| 174 | + }) |
| 175 | +}) |
0 commit comments