-
Notifications
You must be signed in to change notification settings - Fork 655
/
Copy pathclient.test.ts
202 lines (163 loc) · 6.65 KB
/
client.test.ts
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { createAppAuth } from '@octokit/auth-app';
import { StrategyOptions } from '@octokit/auth-app/dist-types/types';
import { request } from '@octokit/request';
import { RequestInterface } from '@octokit/types';
import { getParameter } from '@aws-github-runner/aws-ssm-util';
import { mocked } from 'jest-mock';
import { MockProxy, mock } from 'jest-mock-extended';
import nock from 'nock';
import { createGithubAppAuth, createOctokitClient } from './client';
jest.mock('@aws-github-runner/aws-ssm-util');
jest.mock('@octokit/auth-app');
const cleanEnv = process.env;
const ENVIRONMENT = 'dev';
const GITHUB_APP_ID = '1';
const PARAMETER_GITHUB_APP_ID_NAME = `/actions-runner/${ENVIRONMENT}/github_app_id`;
const PARAMETER_GITHUB_APP_KEY_BASE64_NAME = `/actions-runner/${ENVIRONMENT}/github_app_key_base64`;
const mockedGet = mocked(getParameter);
beforeEach(() => {
jest.resetModules();
jest.clearAllMocks();
process.env = { ...cleanEnv };
process.env.PARAMETER_GITHUB_APP_ID_NAME = PARAMETER_GITHUB_APP_ID_NAME;
process.env.PARAMETER_GITHUB_APP_KEY_BASE64_NAME = PARAMETER_GITHUB_APP_KEY_BASE64_NAME;
nock.disableNetConnect();
});
describe('Test createOctoClient', () => {
test('Creates app client to GitHub public', async () => {
// Arrange
const token = '123456';
// Act
const result = await createOctokitClient(token);
// Assert
expect(result.request.endpoint.DEFAULTS.baseUrl).toBe('https://api.github.com');
});
test('Creates app client to GitHub ES', async () => {
// Arrange
const enterpriseServer = 'https://github.enterprise.notgoingtowork';
const token = '123456';
// Act
const result = await createOctokitClient(token, enterpriseServer);
// Assert
expect(result.request.endpoint.DEFAULTS.baseUrl).toBe(enterpriseServer);
expect(result.request.endpoint.DEFAULTS.mediaType.previews).toStrictEqual(['antiope']);
});
});
describe('Test createGithubAppAuth', () => {
const mockedCreatAppAuth = createAppAuth as unknown as jest.Mock;
const mockedDefaults = jest.spyOn(request, 'defaults');
let mockedRequestInterface: MockProxy<RequestInterface>;
const installationId = 1;
const authType = 'app';
const token = '123456';
const decryptedValue = 'decryptedValue';
const b64 = Buffer.from(decryptedValue, 'binary').toString('base64');
beforeEach(() => {
process.env.ENVIRONMENT = ENVIRONMENT;
});
test('Creates auth object with line breaks in SSH key.', async () => {
// Arrange
const authOptions = {
appId: parseInt(GITHUB_APP_ID),
privateKey: `${decryptedValue}
${decryptedValue}`,
installationId,
};
const b64PrivateKeyWithLineBreaks = Buffer.from(decryptedValue + '\n' + decryptedValue, 'binary').toString(
'base64',
);
mockedGet.mockResolvedValueOnce(GITHUB_APP_ID).mockResolvedValueOnce(b64PrivateKeyWithLineBreaks);
const mockedAuth = jest.fn();
mockedAuth.mockResolvedValue({ token });
mockedCreatAppAuth.mockImplementation(() => {
return mockedAuth;
});
// Act
await createGithubAppAuth(installationId);
// Assert
expect(mockedCreatAppAuth).toBeCalledTimes(1);
expect(mockedCreatAppAuth).toBeCalledWith({ ...authOptions });
});
test('Creates auth object for public GitHub', async () => {
// Arrange
const authOptions = {
appId: parseInt(GITHUB_APP_ID),
privateKey: decryptedValue,
installationId,
};
mockedGet.mockResolvedValueOnce(GITHUB_APP_ID).mockResolvedValueOnce(b64);
const mockedAuth = jest.fn();
mockedAuth.mockResolvedValue({ token });
mockedCreatAppAuth.mockImplementation(() => {
return mockedAuth;
});
// Act
const result = await createGithubAppAuth(installationId);
// Assert
expect(getParameter).toBeCalledWith(PARAMETER_GITHUB_APP_ID_NAME);
expect(getParameter).toBeCalledWith(PARAMETER_GITHUB_APP_KEY_BASE64_NAME);
expect(mockedCreatAppAuth).toBeCalledTimes(1);
expect(mockedCreatAppAuth).toBeCalledWith({ ...authOptions });
expect(mockedAuth).toBeCalledWith({ type: authType });
expect(result.token).toBe(token);
});
test('Creates auth object for Enterprise Server', async () => {
// Arrange
const githubServerUrl = 'https://github.enterprise.notgoingtowork';
mockedRequestInterface = mock<RequestInterface>();
mockedDefaults.mockImplementation(() => {
return mockedRequestInterface.defaults({ baseUrl: githubServerUrl });
});
const authOptions = {
appId: parseInt(GITHUB_APP_ID),
privateKey: decryptedValue,
installationId,
request: mockedRequestInterface.defaults({ baseUrl: githubServerUrl }),
};
mockedGet.mockResolvedValueOnce(GITHUB_APP_ID).mockResolvedValueOnce(b64);
const mockedAuth = jest.fn();
mockedAuth.mockResolvedValue({ token });
// eslint-disable-next-line @typescript-eslint/no-unused-vars
mockedCreatAppAuth.mockImplementation((authOptions: StrategyOptions) => {
return mockedAuth;
});
// Act
const result = await createGithubAppAuth(installationId, githubServerUrl);
// Assert
expect(getParameter).toBeCalledWith(PARAMETER_GITHUB_APP_ID_NAME);
expect(getParameter).toBeCalledWith(PARAMETER_GITHUB_APP_KEY_BASE64_NAME);
expect(mockedCreatAppAuth).toBeCalledTimes(1);
expect(mockedCreatAppAuth).toBeCalledWith(authOptions);
expect(mockedAuth).toBeCalledWith({ type: authType });
expect(result.token).toBe(token);
});
test('Creates auth object for Enterprise Server with no ID', async () => {
// Arrange
const githubServerUrl = 'https://github.enterprise.notgoingtowork';
mockedRequestInterface = mock<RequestInterface>();
mockedDefaults.mockImplementation(() => {
return mockedRequestInterface.defaults({ baseUrl: githubServerUrl });
});
const installationId = undefined;
const authOptions = {
appId: parseInt(GITHUB_APP_ID),
privateKey: decryptedValue,
request: mockedRequestInterface.defaults({ baseUrl: githubServerUrl }),
};
mockedGet.mockResolvedValueOnce(GITHUB_APP_ID).mockResolvedValueOnce(b64);
const mockedAuth = jest.fn();
mockedAuth.mockResolvedValue({ token });
mockedCreatAppAuth.mockImplementation(() => {
return mockedAuth;
});
// Act
const result = await createGithubAppAuth(installationId, githubServerUrl);
// Assert
expect(getParameter).toBeCalledWith(PARAMETER_GITHUB_APP_ID_NAME);
expect(getParameter).toBeCalledWith(PARAMETER_GITHUB_APP_KEY_BASE64_NAME);
expect(mockedCreatAppAuth).toBeCalledTimes(1);
expect(mockedCreatAppAuth).toBeCalledWith(authOptions);
expect(mockedAuth).toBeCalledWith({ type: authType });
expect(result.token).toBe(token);
});
});