-
Notifications
You must be signed in to change notification settings - Fork 351
Expand file tree
/
Copy pathtest-geteffectiveprojectid.ts
More file actions
373 lines (324 loc) · 12.4 KB
/
Copy pathtest-geteffectiveprojectid.ts
File metadata and controls
373 lines (324 loc) · 12.4 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/**
* Test suite for getEffectiveProjectId function
* Tests the behavior of project ID resolution with different environment configurations
*/
import { describe, test, before, after } from 'node:test';
import assert from 'node:assert';
import {
launchServer,
findAvailablePort,
cleanupServers,
ServerInstance,
TransportMode,
HOST
} from './utils/server-launcher.js';
import { MockGitLabServer, findMockServerPort } from './utils/mock-gitlab-server.js';
import { StreamableHTTPTestClient } from './clients/streamable-http-client.js';
import { CustomHeaderClient } from './clients/custom-header-client.js';
// Use the same token that will be passed via GITLAB_TOKEN_TEST environment variable
const MOCK_TOKEN = process.env.GITLAB_TOKEN_TEST || 'glpat-mock-token-12345';
const DEFAULT_PROJECT_ID = '123';
const OTHER_PROJECT_ID = '456';
// Ensure GITLAB_TOKEN_TEST is set for launchServer() validation
if (!process.env.GITLAB_TOKEN_TEST && !process.env.GITLAB_TOKEN) {
process.env.GITLAB_TOKEN_TEST = MOCK_TOKEN;
}
if (!process.env.TEST_PROJECT_ID) {
process.env.TEST_PROJECT_ID = DEFAULT_PROJECT_ID;
}
console.log('🔍 Testing getEffectiveProjectId functionality');
console.log('');
describe('getEffectiveProjectId', { concurrency: 1 }, () => {
describe('getEffectiveProjectId - No GITLAB_ALLOWED_PROJECT_IDS', () => {
let mcpUrl: string;
let mockGitLab: MockGitLabServer;
let servers: ServerInstance[] = [];
let client: StreamableHTTPTestClient;
before(async () => {
// Start mock GitLab server
const mockPort = await findMockServerPort(9100);
mockGitLab = new MockGitLabServer({
port: mockPort,
validTokens: [MOCK_TOKEN]
});
await mockGitLab.start();
const mockGitLabUrl = mockGitLab.getUrl();
// Start MCP server WITHOUT GITLAB_ALLOWED_PROJECT_IDS
const mcpPort = await findAvailablePort(3100);
const server = await launchServer({
mode: TransportMode.STREAMABLE_HTTP,
port: mcpPort,
timeout: 5000,
env: {
STREAMABLE_HTTP: 'true',
REMOTE_AUTHORIZATION: 'true',
GITLAB_API_URL: `${mockGitLabUrl}/api/v4`,
GITLAB_PROJECT_ID: DEFAULT_PROJECT_ID,
GITLAB_READ_ONLY_MODE: 'true',
}
});
servers.push(server);
mcpUrl = `http://${HOST}:${mcpPort}/mcp`;
client = new StreamableHTTPTestClient();
await client.connect(mcpUrl);
console.log(`Mock GitLab: ${mockGitLabUrl}`);
console.log(`MCP Server: ${mcpUrl}`);
console.log(`Default Project: ${DEFAULT_PROJECT_ID}`);
});
after(async () => {
if (client) {
await client.disconnect();
}
cleanupServers(servers);
if (mockGitLab) {
await mockGitLab.stop();
}
});
test('should use GITLAB_PROJECT_ID when no project_id is provided', async () => {
// Call get_project without specifying project_id
const result = await client.callTool('get_project', {
project_id: ''
});
assert.ok(result.content, 'Should have content');
const content = result.content[0];
assert.ok('text' in content, 'Content should have text');
const project = JSON.parse(content.text);
// The mock server should receive a request for the default project
assert.strictEqual(project.id.toString(), DEFAULT_PROJECT_ID, 'Should use GITLAB_PROJECT_ID as default');
console.log(` ✓ Used default project ${DEFAULT_PROJECT_ID} when no project_id provided`);
});
test('should prioritize passed project_id over GITLAB_PROJECT_ID', async () => {
// Call get_project with a different project_id
const result = await client.callTool('get_project', {
project_id: OTHER_PROJECT_ID
});
assert.ok(result.content, 'Should have content');
const content = result.content[0];
assert.ok('text' in content, 'Content should have text');
const project = JSON.parse(content.text);
// Should use the passed project_id, not GITLAB_PROJECT_ID
assert.strictEqual(project.id.toString(), OTHER_PROJECT_ID, 'Should use passed project_id');
console.log(` ✓ Used passed project_id ${OTHER_PROJECT_ID} instead of default ${DEFAULT_PROJECT_ID}`);
});
});
describe('getEffectiveProjectId - With single GITLAB_ALLOWED_PROJECT_IDS', () => {
let mcpUrl: string;
let mockGitLab: MockGitLabServer;
let servers: ServerInstance[] = [];
let client: StreamableHTTPTestClient;
before(async () => {
// Start mock GitLab server
const mockPort = await findMockServerPort(9200);
mockGitLab = new MockGitLabServer({
port: mockPort,
validTokens: [MOCK_TOKEN]
});
await mockGitLab.start();
const mockGitLabUrl = mockGitLab.getUrl();
// Start MCP server WITH single GITLAB_ALLOWED_PROJECT_IDS
const mcpPort = await findAvailablePort(3200);
const server = await launchServer({
mode: TransportMode.STREAMABLE_HTTP,
port: mcpPort,
timeout: 5000,
env: {
REMOTE_AUTHORIZATION: 'true',
GITLAB_API_URL: `${mockGitLabUrl}/api/v4`,
GITLAB_PROJECT_ID: DEFAULT_PROJECT_ID,
GITLAB_ALLOWED_PROJECT_IDS: DEFAULT_PROJECT_ID,
GITLAB_READ_ONLY_MODE: 'true',
}
});
servers.push(server);
mcpUrl = `http://${HOST}:${mcpPort}/mcp`;
client = new StreamableHTTPTestClient();
await client.connect(mcpUrl);
console.log(`Mock GitLab: ${mockGitLabUrl}`);
console.log(`MCP Server: ${mcpUrl}`);
console.log(`Allowed Project: ${DEFAULT_PROJECT_ID}`);
});
after(async () => {
if (client) {
await client.disconnect();
}
cleanupServers(servers);
if (mockGitLab) {
await mockGitLab.stop();
}
});
test('should use single allowed project as default', async () => {
const result = await client.callTool('get_project', {
project_id: ''
});
assert.ok(result.content, 'Should have content');
const content = result.content[0];
assert.ok('text' in content, 'Content should have text');
const project = JSON.parse(content.text);
assert.strictEqual(project.id.toString(), DEFAULT_PROJECT_ID, 'Should use allowed project as default');
console.log(` ✓ Used allowed project ${DEFAULT_PROJECT_ID} as default`);
});
test('should reject access to non-allowed project', async () => {
try {
await client.callTool('get_project', {
project_id: OTHER_PROJECT_ID
});
assert.fail('Should have rejected access to non-allowed project');
} catch (error) {
assert.ok(error instanceof Error);
assert.ok(error.message.includes('Access denied'), 'Should indicate access denied');
console.log(' ✓ Correctly rejected access to non-allowed project');
}
});
});
describe('getEffectiveProjectId - With multiple GITLAB_ALLOWED_PROJECT_IDS', () => {
let mcpUrl: string;
let mockGitLab: MockGitLabServer;
let servers: ServerInstance[] = [];
let client: StreamableHTTPTestClient;
before(async () => {
// Start mock GitLab server
const mockPort = await findMockServerPort(9300);
mockGitLab = new MockGitLabServer({
port: mockPort,
validTokens: [MOCK_TOKEN]
});
await mockGitLab.start();
const mockGitLabUrl = mockGitLab.getUrl();
// Start MCP server WITH multiple GITLAB_ALLOWED_PROJECT_IDS
const mcpPort = await findAvailablePort(3300);
const server = await launchServer({
mode: TransportMode.STREAMABLE_HTTP,
port: mcpPort,
timeout: 5000,
env: {
REMOTE_AUTHORIZATION: 'true',
GITLAB_API_URL: `${mockGitLabUrl}/api/v4`,
GITLAB_PROJECT_ID: DEFAULT_PROJECT_ID,
}
});
servers.push(server);
mcpUrl = `http://${HOST}:${mcpPort}/mcp`;
client = new StreamableHTTPTestClient();
await client.connect(mcpUrl);
console.log(`Mock GitLab: ${mockGitLabUrl}`);
console.log(`MCP Server: ${mcpUrl}`);
console.log(`Allowed Projects: ${DEFAULT_PROJECT_ID},${OTHER_PROJECT_ID}`);
});
after(async () => {
if (client) {
await client.disconnect();
}
cleanupServers(servers);
if (mockGitLab) {
await mockGitLab.stop();
}
});
test('should require explicit project_id when multiple projects allowed', async () => {
try {
await client.callTool('get_project', {
project_id: ''
});
assert.fail('Should have required explicit project_id');
} catch (error) {
assert.ok(error instanceof Error);
assert.ok(error.message.includes('Please specify a project ID'), 'Should require project ID');
console.log(' ✓ Correctly required explicit project_id');
}
});
test('should allow access to first allowed project', async () => {
const result = await client.callTool('get_project', {
project_id: DEFAULT_PROJECT_ID
});
assert.ok(result.content, 'Should have content');
const content = result.content[0];
assert.ok('text' in content, 'Content should have text');
const project = JSON.parse(content.text);
assert.strictEqual(project.id.toString(), DEFAULT_PROJECT_ID, 'Should allow first project');
console.log(` ✓ Allowed access to first project ${DEFAULT_PROJECT_ID}`);
});
test('should allow access to second allowed project', async () => {
const result = await client.callTool('get_project', {
project_id: OTHER_PROJECT_ID
});
assert.ok(result.content, 'Should have content');
const content = result.content[0];
assert.ok('text' in content, 'Content should have text');
const project = JSON.parse(content.text);
assert.strictEqual(project.id.toString(), OTHER_PROJECT_ID, 'Should allow second project');
console.log(` ✓ Allowed access to second project ${OTHER_PROJECT_ID}`);
});
});
describe('GITLAB_PROJECT_ID guards repository and group mutators', () => {
let mcpUrl: string;
let mockGitLab: MockGitLabServer;
let servers: ServerInstance[] = [];
let client: CustomHeaderClient;
before(async () => {
const mockPort = await findMockServerPort(9400);
mockGitLab = new MockGitLabServer({
port: mockPort,
validTokens: [MOCK_TOKEN]
});
await mockGitLab.start();
const mockGitLabUrl = mockGitLab.getUrl();
const mcpPort = await findAvailablePort(3400);
const server = await launchServer({
mode: TransportMode.STREAMABLE_HTTP,
port: mcpPort,
timeout: 5000,
env: {
REMOTE_AUTHORIZATION: 'true',
GITLAB_API_URL: `${mockGitLabUrl}/api/v4`,
GITLAB_PROJECT_ID: DEFAULT_PROJECT_ID,
GITLAB_READ_ONLY_MODE: 'true',
}
});
servers.push(server);
mcpUrl = `http://${HOST}:${mcpPort}/mcp`;
client = new CustomHeaderClient({
authorization: `Bearer ${MOCK_TOKEN}`,
});
await client.connect(mcpUrl);
});
after(async () => {
if (client) await client.disconnect();
cleanupServers(servers);
if (mockGitLab) await mockGitLab.stop();
});
test('should reject create_repository when GITLAB_PROJECT_ID is set', async () => {
try {
await client.callTool('create_repository', { name: 'test-repo' });
assert.fail('Should have rejected create_repository');
} catch (error) {
assert.ok(error instanceof Error);
assert.ok(error.message.includes('create_repository is not allowed'), 'Should mention create_repository');
}
});
test('should reject fork_repository when GITLAB_PROJECT_ID is set', async () => {
try {
await client.callTool('fork_repository', { project_id: '999' });
assert.fail('Should have rejected fork_repository');
} catch (error) {
assert.ok(error instanceof Error);
assert.ok(error.message.includes('fork_repository is not allowed'), 'Should mention fork_repository');
}
});
test('should reject create_group when GITLAB_PROJECT_ID is set', async () => {
try {
await client.callTool('create_group', { name: 'test-group', path: 'test-group' });
assert.fail('Should have rejected create_group');
} catch (error) {
assert.ok(error instanceof Error);
assert.ok(error.message.includes('create_group is not allowed'), 'Should mention create_group');
}
});
test('should allow get_project (non-mutator) when GITLAB_PROJECT_ID is set', async () => {
const result = await client.callTool('get_project', { project_id: '' });
assert.ok(result.content, 'Should have content');
const content = result.content[0];
assert.ok('text' in content, 'Content should have text');
const project = JSON.parse(content.text);
assert.strictEqual(project.id.toString(), DEFAULT_PROJECT_ID, 'Should use default project');
});
});
}); // end wrapper describe