-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathgithub.test.ts
More file actions
265 lines (234 loc) · 6.99 KB
/
github.test.ts
File metadata and controls
265 lines (234 loc) · 6.99 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import { expect, test, describe } from 'vitest';
import {
OctokitRepository,
shouldExcludeRepo,
detectGitHubTokenType,
supportsOAuthScopeIntrospection,
} from './github';
describe('detectGitHubTokenType', () => {
test('detects classic PAT (ghp_)', () => {
expect(detectGitHubTokenType('ghp_abc123def456')).toBe('classic_pat');
});
test('detects OAuth app user token (gho_)', () => {
expect(detectGitHubTokenType('gho_abc123def456')).toBe('oauth_user');
});
test('detects GitHub App user token (ghu_)', () => {
expect(detectGitHubTokenType('ghu_abc123def456')).toBe('app_user');
});
test('detects GitHub App installation token (ghs_)', () => {
expect(detectGitHubTokenType('ghs_abc123def456')).toBe('app_installation');
});
test('detects fine-grained PAT (github_pat_)', () => {
expect(detectGitHubTokenType('github_pat_abc123def456')).toBe('fine_grained_pat');
});
test('returns unknown for unrecognized token format', () => {
expect(detectGitHubTokenType('some_random_token')).toBe('unknown');
expect(detectGitHubTokenType('')).toBe('unknown');
expect(detectGitHubTokenType('v1.abc123')).toBe('unknown');
});
});
describe('supportsOAuthScopeIntrospection', () => {
test('returns true for classic PAT', () => {
expect(supportsOAuthScopeIntrospection('classic_pat')).toBe(true);
});
test('returns true for OAuth app user token', () => {
expect(supportsOAuthScopeIntrospection('oauth_user')).toBe(true);
});
test('returns false for GitHub App user token', () => {
expect(supportsOAuthScopeIntrospection('app_user')).toBe(false);
});
test('returns false for GitHub App installation token', () => {
expect(supportsOAuthScopeIntrospection('app_installation')).toBe(false);
});
test('returns false for fine-grained PAT', () => {
expect(supportsOAuthScopeIntrospection('fine_grained_pat')).toBe(false);
});
test('returns false for unknown token type', () => {
expect(supportsOAuthScopeIntrospection('unknown')).toBe(false);
});
});
test('shouldExcludeRepo returns true when clone_url is undefined', () => {
const repo = { full_name: 'test/repo' } as OctokitRepository;
expect(shouldExcludeRepo({
repo,
})).toBe(true);
});
test('shouldExcludeRepo returns false when the repo is not excluded.', () => {
const repo = {
full_name: 'test/repo',
clone_url: 'https://github.com/test/repo.git',
} as OctokitRepository;
expect(shouldExcludeRepo({
repo,
})).toBe(false);
});
test('shouldExcludeRepo handles forked repos correctly', () => {
const repo = {
full_name: 'test/forked-repo',
clone_url: 'https://github.com/test/forked-repo.git',
fork: true,
} as OctokitRepository;
expect(shouldExcludeRepo({ repo })).toBe(false);
expect(shouldExcludeRepo({ repo, exclude: { forks: true } })).toBe(true);
expect(shouldExcludeRepo({ repo, exclude: { forks: false } })).toBe(false);
});;
test('shouldExcludeRepo handles archived repos correctly', () => {
const repo = {
full_name: 'test/archived-repo',
clone_url: 'https://github.com/test/archived-repo.git',
archived: true,
} as OctokitRepository;
expect(shouldExcludeRepo({ repo })).toBe(false);
expect(shouldExcludeRepo({ repo, exclude: { archived: true } })).toBe(true);
expect(shouldExcludeRepo({ repo, exclude: { archived: false } })).toBe(false);
});
test('shouldExcludeRepo handles include.topics correctly', () => {
const repo = {
full_name: 'test/repo',
clone_url: 'https://github.com/test/repo.git',
topics: [
'test-topic',
'another-topic'
] as string[],
} as OctokitRepository;
expect(shouldExcludeRepo({
repo,
include: {}
})).toBe(false);
expect(shouldExcludeRepo({
repo,
include: {
topics: [],
}
})).toBe(true);
expect(shouldExcludeRepo({
repo,
include: {
topics: ['a-topic-that-does-not-exist'],
}
})).toBe(true);
expect(shouldExcludeRepo({
repo,
include: {
topics: ['test-topic'],
}
})).toBe(false);
expect(shouldExcludeRepo({
repo,
include: {
topics: ['test-*'],
}
})).toBe(false);
expect(shouldExcludeRepo({
repo,
include: {
topics: ['TEST-tOpIC'],
}
})).toBe(false);
});
test('shouldExcludeRepo handles exclude.topics correctly', () => {
const repo = {
full_name: 'test/repo',
clone_url: 'https://github.com/test/repo.git',
topics: [
'test-topic',
'another-topic'
],
} as OctokitRepository;
expect(shouldExcludeRepo({
repo,
exclude: {}
})).toBe(false);
expect(shouldExcludeRepo({
repo,
exclude: {
topics: [],
}
})).toBe(false);
expect(shouldExcludeRepo({
repo,
exclude: {
topics: ['a-topic-that-does-not-exist'],
}
})).toBe(false);
expect(shouldExcludeRepo({
repo,
exclude: {
topics: ['test-topic'],
}
})).toBe(true);
expect(shouldExcludeRepo({
repo,
exclude: {
topics: ['test-*'],
}
})).toBe(true);
expect(shouldExcludeRepo({
repo,
exclude: {
topics: ['TEST-tOpIC'],
}
})).toBe(true);
});
test('shouldExcludeRepo handles exclude.size correctly', () => {
const repo = {
full_name: 'test/repo',
clone_url: 'https://github.com/test/repo.git',
size: 6, // 6KB
} as OctokitRepository;
expect(shouldExcludeRepo({
repo,
exclude: {
size: {
min: 10 * 1000, // 10KB
}
}
})).toBe(true);
expect(shouldExcludeRepo({
repo,
exclude: {
size: {
max: 2 * 1000, // 2KB
}
}
})).toBe(true);
expect(shouldExcludeRepo({
repo,
exclude: {
size: {
min: 5 * 1000, // 5KB
max: 10 * 1000, // 10KB
}
}
})).toBe(false);
});
test('shouldExcludeRepo handles exclude.repos correctly', () => {
const repo = {
full_name: 'test/example-repo',
clone_url: 'https://github.com/test/example-repo.git',
} as OctokitRepository;
expect(shouldExcludeRepo({
repo,
exclude: {
repos: []
}
})).toBe(false);
expect(shouldExcludeRepo({
repo,
exclude: {
repos: ['test/example-repo']
}
})).toBe(true);
expect(shouldExcludeRepo({
repo,
exclude: {
repos: ['test/*']
}
})).toBe(true);
expect(shouldExcludeRepo({
repo,
exclude: {
repos: ['repo-does-not-exist']
}
})).toBe(false);
});