@zereight/mcp-gitlab 2.0.8 → 2.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,389 @@
1
+ #!/usr/bin/env tsx
2
+ /**
3
+ * OAuth Authentication Tests
4
+ * Tests for GitLab OAuth2 authentication flow
5
+ */
6
+ import * as fs from 'fs';
7
+ import * as path from 'path';
8
+ import * as http from 'http';
9
+ import * as net from 'net';
10
+ import { GitLabOAuth } from '../oauth.js';
11
+ // Test configuration
12
+ const TEST_CLIENT_ID = process.env.GITLAB_OAUTH_CLIENT_ID || 'test-client-id';
13
+ const TEST_REDIRECT_URI = process.env.GITLAB_OAUTH_REDIRECT_URI || 'http://127.0.0.1:8888/callback';
14
+ const TEST_GITLAB_URL = process.env.GITLAB_API_URL?.replace('/api/v4', '') || 'https://gitlab.com';
15
+ const TEST_TOKEN_PATH = path.join(process.cwd(), '.test-gitlab-token.json');
16
+ const testResults = [];
17
+ // Helper function to run a single test
18
+ async function runTest(name, testFn, skip = false) {
19
+ if (skip) {
20
+ console.log(`⏭️ SKIPPED: ${name}`);
21
+ testResults.push({ name, status: 'skipped', duration: 0 });
22
+ return;
23
+ }
24
+ const startTime = Date.now();
25
+ try {
26
+ console.log(`🧪 Testing: ${name}`);
27
+ await testFn();
28
+ const duration = Date.now() - startTime;
29
+ console.log(`✅ PASSED: ${name} (${duration}ms)`);
30
+ testResults.push({ name, status: 'passed', duration });
31
+ }
32
+ catch (error) {
33
+ const duration = Date.now() - startTime;
34
+ const errorMsg = error instanceof Error ? error.message : String(error);
35
+ console.log(`❌ FAILED: ${name} (${duration}ms)`);
36
+ console.log(` Error: ${errorMsg}`);
37
+ testResults.push({ name, status: 'failed', duration, error: errorMsg });
38
+ }
39
+ }
40
+ // Helper function to assert conditions
41
+ function assert(condition, message) {
42
+ if (!condition) {
43
+ throw new Error(`Assertion failed: ${message}`);
44
+ }
45
+ }
46
+ // Helper function to check if port is available
47
+ async function isPortAvailable(port) {
48
+ return new Promise((resolve) => {
49
+ const server = net.createServer();
50
+ server.once('error', (err) => {
51
+ if (err.code === 'EADDRINUSE') {
52
+ resolve(false);
53
+ }
54
+ else {
55
+ resolve(true);
56
+ }
57
+ });
58
+ server.once('listening', () => {
59
+ server.close();
60
+ resolve(true);
61
+ });
62
+ server.listen(port, '127.0.0.1');
63
+ });
64
+ }
65
+ // Clean up test token file
66
+ function cleanupTestToken() {
67
+ if (fs.existsSync(TEST_TOKEN_PATH)) {
68
+ fs.unlinkSync(TEST_TOKEN_PATH);
69
+ }
70
+ }
71
+ // Test 1: GitLabOAuth class instantiation
72
+ async function testOAuthInstantiation() {
73
+ const oauth = new GitLabOAuth({
74
+ clientId: TEST_CLIENT_ID,
75
+ redirectUri: TEST_REDIRECT_URI,
76
+ gitlabUrl: TEST_GITLAB_URL,
77
+ scopes: ['api'],
78
+ tokenStoragePath: TEST_TOKEN_PATH,
79
+ });
80
+ assert(oauth !== null, 'OAuth instance should be created');
81
+ assert(typeof oauth.getAccessToken === 'function', 'Should have getAccessToken method');
82
+ assert(typeof oauth.clearToken === 'function', 'Should have clearToken method');
83
+ assert(typeof oauth.hasValidToken === 'function', 'Should have hasValidToken method');
84
+ }
85
+ // Test 2: Token storage path configuration
86
+ async function testTokenStoragePath() {
87
+ const customPath = path.join(process.cwd(), '.custom-test-token.json');
88
+ const oauth = new GitLabOAuth({
89
+ clientId: TEST_CLIENT_ID,
90
+ redirectUri: TEST_REDIRECT_URI,
91
+ gitlabUrl: TEST_GITLAB_URL,
92
+ scopes: ['api'],
93
+ tokenStoragePath: customPath,
94
+ });
95
+ assert(oauth !== null, 'OAuth instance with custom path should be created');
96
+ // Clean up
97
+ if (fs.existsSync(customPath)) {
98
+ fs.unlinkSync(customPath);
99
+ }
100
+ }
101
+ // Test 3: Scope configuration
102
+ async function testScopeConfiguration() {
103
+ const oauth = new GitLabOAuth({
104
+ clientId: TEST_CLIENT_ID,
105
+ redirectUri: TEST_REDIRECT_URI,
106
+ gitlabUrl: TEST_GITLAB_URL,
107
+ scopes: ['api'],
108
+ tokenStoragePath: TEST_TOKEN_PATH,
109
+ });
110
+ assert(oauth !== null, 'OAuth instance with api scope should be created');
111
+ }
112
+ // Test 4: Multiple scopes (should still work but is redundant)
113
+ async function testMultipleScopesRedundant() {
114
+ const oauth = new GitLabOAuth({
115
+ clientId: TEST_CLIENT_ID,
116
+ redirectUri: TEST_REDIRECT_URI,
117
+ gitlabUrl: TEST_GITLAB_URL,
118
+ scopes: ['api', 'read_user', 'read_api', 'write_repository'],
119
+ tokenStoragePath: TEST_TOKEN_PATH,
120
+ });
121
+ assert(oauth !== null, 'OAuth instance with multiple scopes should be created');
122
+ }
123
+ // Test 5: hasValidToken returns false when no token exists
124
+ async function testHasValidTokenNoToken() {
125
+ cleanupTestToken();
126
+ const oauth = new GitLabOAuth({
127
+ clientId: TEST_CLIENT_ID,
128
+ redirectUri: TEST_REDIRECT_URI,
129
+ gitlabUrl: TEST_GITLAB_URL,
130
+ scopes: ['api'],
131
+ tokenStoragePath: TEST_TOKEN_PATH,
132
+ });
133
+ const hasToken = oauth.hasValidToken();
134
+ assert(hasToken === false, 'Should return false when no token exists');
135
+ }
136
+ // Test 6: hasValidToken returns true with valid token
137
+ async function testHasValidTokenWithToken() {
138
+ const tokenData = {
139
+ access_token: 'test-token',
140
+ token_type: 'Bearer',
141
+ created_at: Date.now(),
142
+ expires_in: 7200, // 2 hours
143
+ };
144
+ fs.writeFileSync(TEST_TOKEN_PATH, JSON.stringify(tokenData), { mode: 0o600 });
145
+ const oauth = new GitLabOAuth({
146
+ clientId: TEST_CLIENT_ID,
147
+ redirectUri: TEST_REDIRECT_URI,
148
+ gitlabUrl: TEST_GITLAB_URL,
149
+ scopes: ['api'],
150
+ tokenStoragePath: TEST_TOKEN_PATH,
151
+ });
152
+ const hasToken = oauth.hasValidToken();
153
+ assert(hasToken === true, 'Should return true with valid token');
154
+ cleanupTestToken();
155
+ }
156
+ // Test 7: hasValidToken returns false with expired token
157
+ async function testHasValidTokenExpired() {
158
+ const tokenData = {
159
+ access_token: 'test-token',
160
+ token_type: 'Bearer',
161
+ created_at: Date.now() - 10000000, // 2.7+ hours ago
162
+ expires_in: 7200, // 2 hours
163
+ };
164
+ fs.writeFileSync(TEST_TOKEN_PATH, JSON.stringify(tokenData), { mode: 0o600 });
165
+ const oauth = new GitLabOAuth({
166
+ clientId: TEST_CLIENT_ID,
167
+ redirectUri: TEST_REDIRECT_URI,
168
+ gitlabUrl: TEST_GITLAB_URL,
169
+ scopes: ['api'],
170
+ tokenStoragePath: TEST_TOKEN_PATH,
171
+ });
172
+ const hasToken = oauth.hasValidToken();
173
+ assert(hasToken === false, 'Should return false with expired token');
174
+ cleanupTestToken();
175
+ }
176
+ // Test 8: clearToken removes token file
177
+ async function testClearToken() {
178
+ const tokenData = {
179
+ access_token: 'test-token',
180
+ token_type: 'Bearer',
181
+ created_at: Date.now(),
182
+ };
183
+ fs.writeFileSync(TEST_TOKEN_PATH, JSON.stringify(tokenData), { mode: 0o600 });
184
+ const oauth = new GitLabOAuth({
185
+ clientId: TEST_CLIENT_ID,
186
+ redirectUri: TEST_REDIRECT_URI,
187
+ gitlabUrl: TEST_GITLAB_URL,
188
+ scopes: ['api'],
189
+ tokenStoragePath: TEST_TOKEN_PATH,
190
+ });
191
+ oauth.clearToken();
192
+ assert(!fs.existsSync(TEST_TOKEN_PATH), 'Token file should be deleted');
193
+ }
194
+ // Test 9: Token file has correct permissions (Unix only)
195
+ async function testTokenFilePermissions() {
196
+ if (process.platform === 'win32') {
197
+ throw new Error('Skipping permission test on Windows');
198
+ }
199
+ const tokenData = {
200
+ access_token: 'test-token',
201
+ token_type: 'Bearer',
202
+ created_at: Date.now(),
203
+ };
204
+ fs.writeFileSync(TEST_TOKEN_PATH, JSON.stringify(tokenData), { mode: 0o600 });
205
+ const stats = fs.statSync(TEST_TOKEN_PATH);
206
+ const mode = stats.mode & 0o777;
207
+ assert(mode === 0o600, `Token file should have 0600 permissions, got ${mode.toString(8)}`);
208
+ cleanupTestToken();
209
+ }
210
+ // Test 10: Port availability check
211
+ async function testPortAvailability() {
212
+ const port = 8888;
213
+ const available = await isPortAvailable(port);
214
+ // We just check that the function works, not the actual availability
215
+ assert(typeof available === 'boolean', 'Port availability check should return boolean');
216
+ }
217
+ // Test 11: OAuth redirect URI parsing
218
+ async function testRedirectUriParsing() {
219
+ const redirectUri = 'http://127.0.0.1:8888/callback';
220
+ const url = new URL(redirectUri);
221
+ assert(url.port === '8888', 'Should correctly parse port from redirect URI');
222
+ assert(url.pathname === '/callback', 'Should correctly parse path from redirect URI');
223
+ assert(url.hostname === '127.0.0.1', 'Should correctly parse hostname from redirect URI');
224
+ }
225
+ // Test 12: Token expiration calculation
226
+ async function testTokenExpirationCalculation() {
227
+ const now = Date.now();
228
+ const expiresIn = 7200; // 2 hours in seconds
229
+ const buffer = 5 * 60 * 1000; // 5 minutes in milliseconds
230
+ const expiryTime = now + (expiresIn * 1000);
231
+ const shouldRefreshAt = expiryTime - buffer;
232
+ assert(shouldRefreshAt < expiryTime, 'Refresh time should be before expiry');
233
+ assert(shouldRefreshAt > now, 'Refresh time should be in the future for new token');
234
+ }
235
+ // Test 13: Concurrent OAuth server handling (shared server concept)
236
+ async function testSharedServerConcept() {
237
+ // Test that multiple instances can theoretically share a port
238
+ const port = 9999;
239
+ // First instance: start server
240
+ const server = http.createServer((req, res) => {
241
+ res.writeHead(200);
242
+ res.end('OK');
243
+ });
244
+ await new Promise((resolve) => {
245
+ server.listen(port, '127.0.0.1', () => resolve());
246
+ });
247
+ // Check port is now in use
248
+ const inUse = !(await isPortAvailable(port));
249
+ assert(inUse === true, 'Port should be in use after server starts');
250
+ // Clean up
251
+ await new Promise((resolve) => {
252
+ server.close(() => resolve());
253
+ });
254
+ // Check port is available again
255
+ const available = await isPortAvailable(port);
256
+ assert(available === true, 'Port should be available after server closes');
257
+ }
258
+ // Test 14: Environment variable configuration
259
+ async function testEnvironmentVariableConfig() {
260
+ const clientId = process.env.GITLAB_OAUTH_CLIENT_ID;
261
+ const redirectUri = process.env.GITLAB_OAUTH_REDIRECT_URI || 'http://127.0.0.1:8888/callback';
262
+ assert(typeof clientId === 'string' || clientId === undefined, 'Client ID should be string or undefined');
263
+ assert(typeof redirectUri === 'string', 'Redirect URI should be string');
264
+ const url = new URL(redirectUri);
265
+ assert(url.protocol === 'http:', 'Redirect URI should use http protocol for localhost');
266
+ }
267
+ // Test 15: Token data structure validation
268
+ async function testTokenDataStructure() {
269
+ const tokenData = {
270
+ access_token: 'glpat-test123456789',
271
+ refresh_token: 'refresh-test123456789',
272
+ token_type: 'Bearer',
273
+ expires_in: 7200,
274
+ created_at: Date.now(),
275
+ };
276
+ assert(typeof tokenData.access_token === 'string', 'access_token should be string');
277
+ assert(typeof tokenData.token_type === 'string', 'token_type should be string');
278
+ assert(typeof tokenData.created_at === 'number', 'created_at should be number');
279
+ assert(tokenData.expires_in === undefined || typeof tokenData.expires_in === 'number', 'expires_in should be number or undefined');
280
+ }
281
+ // Test 16: Invalid token storage path handling
282
+ async function testInvalidTokenStoragePath() {
283
+ const invalidPath = '/root/nonexistent/directory/.token.json';
284
+ const oauth = new GitLabOAuth({
285
+ clientId: TEST_CLIENT_ID,
286
+ redirectUri: TEST_REDIRECT_URI,
287
+ gitlabUrl: TEST_GITLAB_URL,
288
+ scopes: ['api'],
289
+ tokenStoragePath: invalidPath,
290
+ });
291
+ // Should create instance even with invalid path (error occurs during save)
292
+ assert(oauth !== null, 'Should create instance with invalid path');
293
+ }
294
+ // Test 17: Self-hosted GitLab URL configuration
295
+ async function testSelfHostedGitLabUrl() {
296
+ const selfHostedUrl = 'https://gitlab.example.com';
297
+ const oauth = new GitLabOAuth({
298
+ clientId: TEST_CLIENT_ID,
299
+ redirectUri: TEST_REDIRECT_URI,
300
+ gitlabUrl: selfHostedUrl,
301
+ scopes: ['api'],
302
+ tokenStoragePath: TEST_TOKEN_PATH,
303
+ });
304
+ assert(oauth !== null, 'Should create instance with self-hosted URL');
305
+ }
306
+ // Test 18: Custom port in redirect URI
307
+ async function testCustomPortInRedirectUri() {
308
+ const customRedirectUri = 'http://127.0.0.1:9999/callback';
309
+ const oauth = new GitLabOAuth({
310
+ clientId: TEST_CLIENT_ID,
311
+ redirectUri: customRedirectUri,
312
+ gitlabUrl: TEST_GITLAB_URL,
313
+ scopes: ['api'],
314
+ tokenStoragePath: TEST_TOKEN_PATH,
315
+ });
316
+ assert(oauth !== null, 'Should create instance with custom port');
317
+ const url = new URL(customRedirectUri);
318
+ assert(url.port === '9999', 'Should correctly parse custom port');
319
+ }
320
+ // Main test runner
321
+ async function runOAuthTests() {
322
+ console.log('🚀 GitLab OAuth Authentication Tests\n');
323
+ console.log('='.repeat(50));
324
+ // Core functionality tests
325
+ await runTest('OAuth class instantiation', testOAuthInstantiation);
326
+ await runTest('Token storage path configuration', testTokenStoragePath);
327
+ await runTest('Scope configuration with api only', testScopeConfiguration);
328
+ await runTest('Multiple scopes configuration (redundant)', testMultipleScopesRedundant);
329
+ // Token management tests
330
+ await runTest('hasValidToken returns false without token', testHasValidTokenNoToken);
331
+ await runTest('hasValidToken returns true with valid token', testHasValidTokenWithToken);
332
+ await runTest('hasValidToken returns false with expired token', testHasValidTokenExpired);
333
+ await runTest('clearToken removes token file', testClearToken);
334
+ await runTest('Token file has correct permissions', testTokenFilePermissions, process.platform === 'win32');
335
+ // Network and configuration tests
336
+ await runTest('Port availability check', testPortAvailability);
337
+ await runTest('OAuth redirect URI parsing', testRedirectUriParsing);
338
+ await runTest('Token expiration calculation', testTokenExpirationCalculation);
339
+ await runTest('Shared server concept', testSharedServerConcept);
340
+ // Configuration tests
341
+ await runTest('Environment variable configuration', testEnvironmentVariableConfig);
342
+ await runTest('Token data structure validation', testTokenDataStructure);
343
+ await runTest('Invalid token storage path handling', testInvalidTokenStoragePath);
344
+ await runTest('Self-hosted GitLab URL configuration', testSelfHostedGitLabUrl);
345
+ await runTest('Custom port in redirect URI', testCustomPortInRedirectUri);
346
+ // Cleanup
347
+ cleanupTestToken();
348
+ // Print summary
349
+ console.log('\n' + '='.repeat(50));
350
+ console.log('📊 Test Results Summary\n');
351
+ const passed = testResults.filter(r => r.status === 'passed').length;
352
+ const failed = testResults.filter(r => r.status === 'failed').length;
353
+ const skipped = testResults.filter(r => r.status === 'skipped').length;
354
+ const total = testResults.length;
355
+ console.log(`Total tests: ${total}`);
356
+ console.log(`✅ Passed: ${passed}`);
357
+ console.log(`❌ Failed: ${failed}`);
358
+ console.log(`⏭️ Skipped: ${skipped}`);
359
+ if (total > 0) {
360
+ const successRate = ((passed / (total - skipped)) * 100).toFixed(1);
361
+ console.log(`Success rate: ${successRate}%`);
362
+ }
363
+ // Show failed tests
364
+ const failedTests = testResults.filter(r => r.status === 'failed');
365
+ if (failedTests.length > 0) {
366
+ console.log('\n❌ Failed Tests:');
367
+ failedTests.forEach(test => {
368
+ console.log(` - ${test.name}`);
369
+ console.log(` ${test.error}`);
370
+ });
371
+ }
372
+ // Save results to file
373
+ const reportPath = 'test-results-oauth.json';
374
+ fs.writeFileSync(reportPath, JSON.stringify(testResults, null, 2));
375
+ console.log(`\n📄 Detailed results saved to ${reportPath}`);
376
+ return failed === 0;
377
+ }
378
+ // Run tests if this is the main module
379
+ if (import.meta.url === `file://${process.argv[1]}`) {
380
+ runOAuthTests()
381
+ .then(success => {
382
+ process.exit(success ? 0 : 1);
383
+ })
384
+ .catch(error => {
385
+ console.error('Error running tests:', error);
386
+ process.exit(1);
387
+ });
388
+ }
389
+ export { runOAuthTests, testResults };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zereight/mcp-gitlab",
3
- "version": "2.0.8",
3
+ "version": "2.0.11",
4
4
  "description": "MCP server for using the GitLab API",
5
5
  "license": "MIT",
6
6
  "author": "zereight",
@@ -27,7 +27,8 @@
27
27
  "test:remote-auth": "npm run build && npx tsx --test test/remote-auth-simple-test.ts",
28
28
  "test:server": "npm run build && node build/test/test-all-transport-server.js",
29
29
  "test:mcp:readonly": "tsx test/readonly-mcp-tests.ts",
30
- "test:all": "npm run test && npm run test:mcp:readonly",
30
+ "test:oauth": "tsx test/oauth-tests.ts",
31
+ "test:all": "npm run test && npm run test:mcp:readonly && npm run test:oauth",
31
32
  "lint": "eslint . --ext .ts",
32
33
  "lint:fix": "eslint . --ext .ts --fix",
33
34
  "format": "prettier --write \"**/*.{js,ts,json,md}\"",
@@ -42,8 +43,10 @@
42
43
  "http-proxy-agent": "^7.0.2",
43
44
  "https-proxy-agent": "^7.0.6",
44
45
  "node-fetch": "^3.3.2",
46
+ "open": "^10.2.0",
45
47
  "pino": "^9.7.0",
46
48
  "pino-pretty": "^13.0.0",
49
+ "pkce-challenge": "^5.0.0",
47
50
  "socks-proxy-agent": "^8.0.5",
48
51
  "tough-cookie": "^5.1.2",
49
52
  "zod-to-json-schema": "^3.23.5"