-
Notifications
You must be signed in to change notification settings - Fork 722
Expand file tree
/
Copy pathoctokit.test.ts
More file actions
81 lines (71 loc) · 2.83 KB
/
octokit.test.ts
File metadata and controls
81 lines (71 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { Octokit } from '@octokit/rest';
import { ActionRequestMessage } from '../scale-runners/scale-up';
import { getOctokit } from './octokit';
import { describe, it, expect, beforeEach, vi } from 'vitest';
const mockOctokit = {
apps: {
getOrgInstallation: vi.fn(),
getRepoInstallation: vi.fn(),
},
};
vi.mock('../github/auth', async () => ({
createGithubInstallationAuth: vi.fn().mockImplementation(async (installationId) => {
return { token: 'token', type: 'installation', installationId: installationId };
}),
createOctokitClient: vi.fn().mockImplementation(() => new Octokit()),
createGithubAppAuth: vi.fn().mockResolvedValue({ token: 'token', appIndex: 0 }),
getAppCount: vi.fn().mockResolvedValue(1),
getStoredInstallationId: vi.fn().mockResolvedValue(undefined),
}));
vi.mock('@octokit/rest', async () => ({
Octokit: vi.fn().mockImplementation(function () {
return mockOctokit;
}),
}));
// We've already mocked '../github/auth' above
describe('Test getOctokit', () => {
const data = [
{
description: 'Should look-up org installation if installationId is 0.',
input: { orgLevelRunner: false, installationId: 0 },
output: { callReposInstallation: true, callOrgInstallation: false },
},
{
description: 'Should look-up org installation if installationId is 0.',
input: { orgLevelRunner: true, installationId: 0 },
output: { callReposInstallation: false, callOrgInstallation: true },
},
{
description: 'Should not look-up org installation if provided in payload.',
input: { orgLevelRunner: true, installationId: 1 },
output: { callReposInstallation: false, callOrgInstallation: false },
},
];
beforeEach(() => {
vi.clearAllMocks();
});
it.each(data)(`$description`, async ({ input, output }) => {
const payload = {
eventType: 'workflow_job',
id: 0,
installationId: input.installationId,
repositoryOwner: 'owner',
repositoryName: 'repo',
} as ActionRequestMessage;
if (input.orgLevelRunner) {
mockOctokit.apps.getOrgInstallation.mockResolvedValue({ data: { id: 1 } });
mockOctokit.apps.getRepoInstallation.mockRejectedValue(new Error('Error'));
} else {
mockOctokit.apps.getRepoInstallation.mockResolvedValue({ data: { id: 2 } });
mockOctokit.apps.getOrgInstallation.mockRejectedValue(new Error('Error'));
}
await expect(getOctokit('', input.orgLevelRunner, payload)).resolves.toBeDefined();
if (output.callOrgInstallation) {
expect(mockOctokit.apps.getOrgInstallation).toHaveBeenCalled();
expect(mockOctokit.apps.getRepoInstallation).not.toHaveBeenCalled();
} else if (output.callReposInstallation) {
expect(mockOctokit.apps.getRepoInstallation).toHaveBeenCalled();
expect(mockOctokit.apps.getOrgInstallation).not.toHaveBeenCalled();
}
});
});