-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathoctokit.test.ts
More file actions
202 lines (164 loc) · 6.03 KB
/
octokit.test.ts
File metadata and controls
202 lines (164 loc) · 6.03 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
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
// @vitest-environment happy-dom
import {
mockGitHubAppAccount,
mockGitHubCloudAccount,
mockGitHubEnterpriseServerAccount,
} from '../../__mocks__/account-mocks';
import * as comms from '../system/comms';
import {
clearOctokitClientCache,
createOctokitClient,
createOctokitClientUncached,
} from './octokit';
import * as utils from './utils';
describe('renderer/utils/api/octokit.ts', () => {
const mockDecryptValue = vi.spyOn(comms, 'decryptValue');
beforeEach(() => {
mockDecryptValue.mockResolvedValue('decrypted-token');
clearOctokitClientCache();
});
afterEach(() => {
clearOctokitClientCache();
});
describe('createOctokitClient', () => {
it('should create octokit rest client for GitHub Cloud', async () => {
const getGitHubAPIBaseUrlSpy = vi.spyOn(utils, 'getGitHubAPIBaseUrl');
getGitHubAPIBaseUrlSpy.mockReturnValue(
new URL('https://api.github.com/'),
);
const octokit = await createOctokitClient(mockGitHubCloudAccount, 'rest');
expect(getGitHubAPIBaseUrlSpy).toHaveBeenCalledWith('github.com', 'rest');
expect(octokit).toBeDefined();
expect(mockDecryptValue).toHaveBeenCalledWith(
mockGitHubCloudAccount.token,
);
});
it('should create octokit graphql client for GitHub Cloud', async () => {
const getGitHubAPIBaseUrlSpy = vi.spyOn(utils, 'getGitHubAPIBaseUrl');
getGitHubAPIBaseUrlSpy.mockReturnValue(
new URL('https://api.github.com/'),
);
const octokit = await createOctokitClient(
mockGitHubCloudAccount,
'graphql',
);
expect(getGitHubAPIBaseUrlSpy).toHaveBeenCalledWith(
'github.com',
'graphql',
);
expect(octokit).toBeDefined();
expect(mockDecryptValue).toHaveBeenCalledWith(
mockGitHubCloudAccount.token,
);
});
it('should create octokit rest client for GitHub Enterprise Server', async () => {
const getGitHubAPIBaseUrlSpy = vi.spyOn(utils, 'getGitHubAPIBaseUrl');
getGitHubAPIBaseUrlSpy.mockReturnValue(
new URL('https://github.gitify.io/api/v3/'),
);
const octokit = await createOctokitClient(
mockGitHubEnterpriseServerAccount,
'rest',
);
expect(getGitHubAPIBaseUrlSpy).toHaveBeenCalledWith(
'github.gitify.io',
'rest',
);
expect(octokit).toBeDefined();
expect(mockDecryptValue).toHaveBeenCalledWith(
mockGitHubEnterpriseServerAccount.token,
);
});
it('should create octokit graphql client for GitHub Enterprise Server', async () => {
const getGitHubAPIBaseUrlSpy = vi.spyOn(utils, 'getGitHubAPIBaseUrl');
getGitHubAPIBaseUrlSpy.mockReturnValue(
new URL('https://github.gitify.io/api/graphql/'),
);
const octokit = await createOctokitClient(
mockGitHubEnterpriseServerAccount,
'graphql',
);
expect(getGitHubAPIBaseUrlSpy).toHaveBeenCalledWith(
'github.gitify.io',
'graphql',
);
expect(octokit).toBeDefined();
expect(mockDecryptValue).toHaveBeenCalledWith(
mockGitHubEnterpriseServerAccount.token,
);
});
it('should cache and reuse octokit clients for the same account and api type', async () => {
const getGitHubAPIBaseUrlSpy = vi.spyOn(utils, 'getGitHubAPIBaseUrl');
getGitHubAPIBaseUrlSpy.mockReturnValue(
new URL('https://api.github.com/'),
);
const octokit1 = await createOctokitClient(
mockGitHubCloudAccount,
'rest',
);
const octokit2 = await createOctokitClient(
mockGitHubCloudAccount,
'rest',
);
// Should return the same instance
expect(octokit1).toBe(octokit2);
// Should only decrypt token once (on first call)
expect(mockDecryptValue).toHaveBeenCalledTimes(1);
// Should only get base URL once (on first call)
expect(getGitHubAPIBaseUrlSpy).toHaveBeenCalledTimes(1);
});
it('should create separate uncached clients each time', async () => {
const getGitHubAPIBaseUrlSpy = vi.spyOn(utils, 'getGitHubAPIBaseUrl');
getGitHubAPIBaseUrlSpy.mockReturnValue(
new URL('https://api.github.com/'),
);
const client1 = await createOctokitClientUncached(
mockGitHubCloudAccount,
'rest',
);
const client2 = await createOctokitClientUncached(
mockGitHubCloudAccount,
'rest',
);
// Should create different instances each time
expect(client1).not.toBe(client2);
// Should decrypt token each time
expect(mockDecryptValue).toHaveBeenCalledTimes(2);
// Should get base URL each time
expect(getGitHubAPIBaseUrlSpy).toHaveBeenCalledTimes(2);
});
it('should create different clients for different accounts with same api type', async () => {
const getGitHubAPIBaseUrlSpy = vi.spyOn(utils, 'getGitHubAPIBaseUrl');
getGitHubAPIBaseUrlSpy.mockReturnValue(
new URL('https://api.github.com/'),
);
const octokit1 = await createOctokitClient(mockGitHubAppAccount, 'rest');
const octokit2 = await createOctokitClient(
mockGitHubCloudAccount,
'rest',
);
// Should be different instances for different tokens
expect(octokit1).not.toBe(octokit2);
// Should decrypt both tokens
expect(mockDecryptValue).toHaveBeenCalledTimes(2);
});
it('should create different clients for same accounts with different api type', async () => {
const getGitHubAPIBaseUrlSpy = vi.spyOn(utils, 'getGitHubAPIBaseUrl');
getGitHubAPIBaseUrlSpy.mockReturnValue(
new URL('https://api.github.com/'),
);
const octokit1 = await createOctokitClient(
mockGitHubCloudAccount,
'rest',
);
const octokit2 = await createOctokitClient(
mockGitHubCloudAccount,
'graphql',
);
// Should be different instances for different tokens
expect(octokit1).not.toBe(octokit2);
// Should decrypt both tokens
expect(mockDecryptValue).toHaveBeenCalledTimes(2);
});
});
});