forked from worlddriven/documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-transfer-permissions.test.js
More file actions
213 lines (175 loc) · 6.43 KB
/
check-transfer-permissions.test.js
File metadata and controls
213 lines (175 loc) · 6.43 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
#!/usr/bin/env node
import { describe, test, mock, beforeEach, afterEach } from 'node:test';
import assert from 'node:assert';
import {
checkTransferPermission,
checkMultipleTransferPermissions,
} from './check-transfer-permissions.js';
describe('checkTransferPermission', () => {
describe('input validation', () => {
test('should throw error if token is missing', async () => {
await assert.rejects(
async () => await checkTransferPermission(null, 'owner/repo'),
{ message: 'GitHub token is required' }
);
});
test('should throw error if originRepo is missing', async () => {
await assert.rejects(
async () => await checkTransferPermission('token', ''),
{ message: 'Origin repository must be in format "owner/repo-name"' }
);
});
test('should throw error if originRepo format is invalid', async () => {
await assert.rejects(
async () => await checkTransferPermission('token', 'invalid-format'),
{ message: 'Origin repository must be in format "owner/repo-name"' }
);
});
test('should throw error if originRepo has empty owner or repo', async () => {
await assert.rejects(
async () => await checkTransferPermission('token', '/repo'),
{ message: 'Invalid origin repository format' }
);
await assert.rejects(
async () => await checkTransferPermission('token', 'owner/'),
{ message: 'Invalid origin repository format' }
);
});
});
describe('API response handling', () => {
let originalFetch;
beforeEach(() => {
originalFetch = globalThis.fetch;
});
afterEach(() => {
globalThis.fetch = originalFetch;
});
test('should return hasPermission true when user has admin access', async () => {
globalThis.fetch = mock.fn(() =>
Promise.resolve({
ok: true,
status: 200,
json: () =>
Promise.resolve({
permissions: { admin: true, push: true, pull: true },
}),
})
);
const result = await checkTransferPermission('test-token', 'owner/repo');
assert.strictEqual(result.hasPermission, true);
assert.strictEqual(result.permissionLevel, 'admin');
assert.ok(result.details.includes('admin access'));
});
test('should return hasPermission false when user has write access only', async () => {
globalThis.fetch = mock.fn(() =>
Promise.resolve({
ok: true,
status: 200,
json: () =>
Promise.resolve({
permissions: { admin: false, push: true, pull: true },
}),
})
);
const result = await checkTransferPermission('test-token', 'owner/repo');
assert.strictEqual(result.hasPermission, false);
assert.strictEqual(result.permissionLevel, 'write');
assert.ok(result.details.includes('admin required'));
});
test('should return hasPermission false when user has read access only', async () => {
globalThis.fetch = mock.fn(() =>
Promise.resolve({
ok: true,
status: 200,
json: () =>
Promise.resolve({
permissions: { admin: false, push: false, pull: true },
}),
})
);
const result = await checkTransferPermission('test-token', 'owner/repo');
assert.strictEqual(result.hasPermission, false);
assert.strictEqual(result.permissionLevel, 'read');
});
test('should handle 404 response for non-existent repository', async () => {
globalThis.fetch = mock.fn(() =>
Promise.resolve({
ok: false,
status: 404,
})
);
const result = await checkTransferPermission('test-token', 'owner/repo');
assert.strictEqual(result.hasPermission, false);
assert.strictEqual(result.permissionLevel, 'none');
assert.ok(result.details.includes('not found'));
});
test('should handle API errors gracefully', async () => {
globalThis.fetch = mock.fn(() =>
Promise.resolve({
ok: false,
status: 403,
text: () => Promise.resolve('Rate limit exceeded'),
})
);
const result = await checkTransferPermission('test-token', 'owner/repo');
assert.strictEqual(result.hasPermission, false);
assert.strictEqual(result.permissionLevel, 'unknown');
assert.ok(result.details.includes('403'));
});
test('should handle network errors gracefully', async () => {
globalThis.fetch = mock.fn(() =>
Promise.reject(new Error('Network error'))
);
const result = await checkTransferPermission('test-token', 'owner/repo');
assert.strictEqual(result.hasPermission, false);
assert.strictEqual(result.permissionLevel, 'error');
assert.ok(result.details.includes('Network error'));
});
test('should handle missing permissions object in response', async () => {
globalThis.fetch = mock.fn(() =>
Promise.resolve({
ok: true,
status: 200,
json: () => Promise.resolve({}),
})
);
const result = await checkTransferPermission('test-token', 'owner/repo');
assert.strictEqual(result.hasPermission, false);
assert.strictEqual(result.permissionLevel, 'none');
});
});
});
describe('checkMultipleTransferPermissions', () => {
let originalFetch;
beforeEach(() => {
originalFetch = globalThis.fetch;
});
afterEach(() => {
globalThis.fetch = originalFetch;
});
test('should return a Map with results for all repositories', async () => {
let callCount = 0;
globalThis.fetch = mock.fn(() => {
callCount++;
return Promise.resolve({
ok: true,
status: 200,
json: () =>
Promise.resolve({
permissions: { admin: callCount === 1, push: true, pull: true },
}),
});
});
const repos = ['owner/repo1', 'owner/repo2'];
const results = await checkMultipleTransferPermissions('test-token', repos);
assert.ok(results instanceof Map);
assert.strictEqual(results.size, 2);
assert.strictEqual(results.get('owner/repo1').hasPermission, true);
assert.strictEqual(results.get('owner/repo2').hasPermission, false);
});
test('should return empty Map for empty input array', async () => {
const results = await checkMultipleTransferPermissions('test-token', []);
assert.ok(results instanceof Map);
assert.strictEqual(results.size, 0);
});
});