@zereight/mcp-gitlab 2.1.69 → 2.2.0

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.
Files changed (155) hide show
  1. package/README.ko.md +10 -4
  2. package/README.md +238 -233
  3. package/README.pt-BR.md +4 -4
  4. package/README.zh-CN.md +6 -4
  5. package/build/config.js +12 -4
  6. package/build/deprecated-env.js +47 -0
  7. package/build/index.js +456 -237
  8. package/build/oauth-device-flow.js +80 -0
  9. package/build/oauth.js +135 -18
  10. package/build/schemas.js +1 -1
  11. package/build/server/remote-http-stack.js +39 -0
  12. package/build/tools/discover-activation.js +38 -0
  13. package/build/tools/exposed-tools.js +35 -0
  14. package/build/tools/listed-tools.js +76 -0
  15. package/build/tools/registry.js +226 -57
  16. package/build/tools/tool-profile.js +66 -0
  17. package/build/utils/compact-tool-allowlist.js +14 -0
  18. package/build/utils/logger.js +34 -12
  19. package/build/utils/url.js +15 -0
  20. package/package.json +3 -3
  21. package/build/scripts/check-skill-sync.js +0 -143
  22. package/build/scripts/generate-server-card.js +0 -39
  23. package/build/scripts/generate-tool-coverage.js +0 -58
  24. package/build/scripts/generate-tool-docs.js +0 -430
  25. package/build/scripts/tool-coverage/coverage.js +0 -341
  26. package/build/scripts/tool-coverage/coverage.test.js +0 -142
  27. package/build/test/callback-proxy-tests.js +0 -321
  28. package/build/test/cli/args.test.js +0 -175
  29. package/build/test/cli/boolean-flags.test.js +0 -106
  30. package/build/test/cli/compact-mcp-result.test.js +0 -93
  31. package/build/test/cli/exposure.test.js +0 -104
  32. package/build/test/cli/load-config-snapshot.js +0 -5
  33. package/build/test/cli/output.test.js +0 -74
  34. package/build/test/cli/replay-command.test.js +0 -37
  35. package/build/test/cli/router.test.js +0 -183
  36. package/build/test/client-pool-test.js +0 -109
  37. package/build/test/clients/client.js +0 -34
  38. package/build/test/clients/custom-header-client.js +0 -122
  39. package/build/test/clients/sse-client.js +0 -95
  40. package/build/test/clients/stdio-client.js +0 -113
  41. package/build/test/clients/streamable-http-client.js +0 -95
  42. package/build/test/config-allowed-groups.test.js +0 -97
  43. package/build/test/create-commit-actions.test.js +0 -102
  44. package/build/test/draft-note-schema.test.js +0 -44
  45. package/build/test/dynamic-api-url-allowlist.test.js +0 -121
  46. package/build/test/dynamic-api-url-test.js +0 -304
  47. package/build/test/dynamic-routing-tests.js +0 -495
  48. package/build/test/get-merge-request-discussion-schema.test.js +0 -30
  49. package/build/test/gitlab-artifact-entry-schema.test.js +0 -44
  50. package/build/test/gitlab-diff-schema.test.js +0 -36
  51. package/build/test/gitlab-pipeline-schedule-schema.test.js +0 -32
  52. package/build/test/gitlab-tag-schema.test.js +0 -51
  53. package/build/test/group-milestone-schema.test.js +0 -45
  54. package/build/test/mcp-oauth-tests.js +0 -905
  55. package/build/test/mcp-server-name.test.js +0 -87
  56. package/build/test/merge-merge-request-schema.test.js +0 -75
  57. package/build/test/multi-server-test.js +0 -190
  58. package/build/test/no-proxy-integration-test.js +0 -183
  59. package/build/test/no-proxy-test.js +0 -149
  60. package/build/test/nullable-gitlab-response-fields.test.js +0 -37
  61. package/build/test/nullish-tool-arguments-schema.test.js +0 -118
  62. package/build/test/oauth-device-flow-tests.js +0 -450
  63. package/build/test/oauth-startup.test.js +0 -204
  64. package/build/test/oauth-tests.js +0 -428
  65. package/build/test/path-segment-encoding.test.js +0 -98
  66. package/build/test/remote-auth-simple-test.js +0 -277
  67. package/build/test/remote-auth-tests.js +0 -314
  68. package/build/test/response-masking.test.js +0 -445
  69. package/build/test/schema-tests.js +0 -1679
  70. package/build/test/server/metrics.test.js +0 -47
  71. package/build/test/server/request-helpers.test.js +0 -48
  72. package/build/test/server/version.test.js +0 -13
  73. package/build/test/sse-auth-guard.test.js +0 -96
  74. package/build/test/sse-session-limits.test.js +0 -273
  75. package/build/test/stateless/callback-proxy.test.js +0 -510
  76. package/build/test/stateless/client-id.test.js +0 -177
  77. package/build/test/stateless/codec.test.js +0 -328
  78. package/build/test/stateless/config-ttl.test.js +0 -149
  79. package/build/test/stateless/consumed-proxy-code-cache.test.js +0 -150
  80. package/build/test/stateless/session-id-integration.test.js +0 -675
  81. package/build/test/stateless/session-id.test.js +0 -157
  82. package/build/test/streamable-http-concurrent-session.test.js +0 -171
  83. package/build/test/streamable-http-dns-rebinding.test.js +0 -185
  84. package/build/test/streamable-http-sse-stream.test.js +0 -350
  85. package/build/test/streamable-http-static-token-auth.test.js +0 -157
  86. package/build/test/streamable-http-unauthenticated-discovery.test.js +0 -207
  87. package/build/test/test-all-transport-server.js +0 -255
  88. package/build/test/test-auth-retry.js +0 -195
  89. package/build/test/test-blank-filters.js +0 -156
  90. package/build/test/test-ci-catalog.js +0 -177
  91. package/build/test/test-ci-lint.js +0 -191
  92. package/build/test/test-ci-variables.js +0 -309
  93. package/build/test/test-create-repository.js +0 -120
  94. package/build/test/test-dependency-proxy.js +0 -195
  95. package/build/test/test-deployment-tools.js +0 -463
  96. package/build/test/test-download-attachment.js +0 -190
  97. package/build/test/test-dynamic-project-scope.js +0 -524
  98. package/build/test/test-empty-toolsets.js +0 -535
  99. package/build/test/test-get-file-blame.js +0 -145
  100. package/build/test/test-get-file-contents.js +0 -128
  101. package/build/test/test-geteffectiveprojectid.js +0 -812
  102. package/build/test/test-health-check.js +0 -115
  103. package/build/test/test-issue-description-patch.js +0 -256
  104. package/build/test/test-job-artifacts.js +0 -275
  105. package/build/test/test-json-schema.js +0 -166
  106. package/build/test/test-list-group-members.js +0 -150
  107. package/build/test/test-list-group-merge-requests.js +0 -165
  108. package/build/test/test-list-issues.js +0 -170
  109. package/build/test/test-list-merge-requests.js +0 -201
  110. package/build/test/test-list-milestones.js +0 -145
  111. package/build/test/test-list-project-members.js +0 -132
  112. package/build/test/test-merge-request-approval-state-tools.js +0 -213
  113. package/build/test/test-merge-request-approvals.js +0 -187
  114. package/build/test/test-merge-request-pipelines.js +0 -106
  115. package/build/test/test-mr-diffs-filter.js +0 -132
  116. package/build/test/test-mr-file-diffs.js +0 -251
  117. package/build/test/test-oauth-proxy-rate-limit.js +0 -133
  118. package/build/test/test-orbit.js +0 -155
  119. package/build/test/test-permission-mode.js +0 -424
  120. package/build/test/test-pipeline-schedules.js +0 -482
  121. package/build/test/test-protected-branches.js +0 -174
  122. package/build/test/test-remote-downloads.js +0 -510
  123. package/build/test/test-search-code.js +0 -275
  124. package/build/test/test-snippets.js +0 -735
  125. package/build/test/test-tags.js +0 -220
  126. package/build/test/test-todos.js +0 -195
  127. package/build/test/test-token-optimizations.js +0 -691
  128. package/build/test/test-toolset-filtering.js +0 -499
  129. package/build/test/test-update-issue-slim.js +0 -141
  130. package/build/test/test-update-project.js +0 -112
  131. package/build/test/test-upload-markdown.js +0 -206
  132. package/build/test/test-vulnerabilities.js +0 -399
  133. package/build/test/test-webhooks.js +0 -218
  134. package/build/test/tool-description-quality.test.js +0 -50
  135. package/build/test/utils/bulk-publish-options.test.js +0 -32
  136. package/build/test/utils/download-token.test.js +0 -35
  137. package/build/test/utils/forwarded-public-base-url.test.js +0 -46
  138. package/build/test/utils/gitlab-instance-version.test.js +0 -32
  139. package/build/test/utils/gitlab-version-gate.test.js +0 -74
  140. package/build/test/utils/graphql-query.test.js +0 -168
  141. package/build/test/utils/helpers-path.test.js +0 -104
  142. package/build/test/utils/jmespath-tool-result.test.js +0 -91
  143. package/build/test/utils/masking.test.js +0 -384
  144. package/build/test/utils/merge-request-position.test.js +0 -57
  145. package/build/test/utils/mock-gitlab-server.js +0 -614
  146. package/build/test/utils/proxy-client-ip.test.js +0 -28
  147. package/build/test/utils/redact-sensitive.test.js +0 -32
  148. package/build/test/utils/safe-redirect-fetch.test.js +0 -463
  149. package/build/test/utils/server-launcher.js +0 -231
  150. package/build/test/utils/tool-args.test.js +0 -210
  151. package/build/test/utils/url.test.js +0 -128
  152. package/build/test/utils/version-check.test.js +0 -239
  153. package/build/test/utils/wiki-title.test.js +0 -21
  154. package/build/test-note.js +0 -54
  155. package/build/test-resolve-issue-note.js +0 -127
@@ -1,450 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import { describe, it } from "node:test";
3
- import * as fs from "node:fs";
4
- import * as os from "node:os";
5
- import * as path from "node:path";
6
- import { getPositionalCliCommand } from "../cli-command.js";
7
- import { gitlabOriginFromApiUrl, runAuthCommandAsync } from "../auth-cli.js";
8
- import { runDeviceAuthorizationGrantAsync } from "../oauth-device-flow.js";
9
- import { GitLabOAuth } from "../oauth.js";
10
- const GITLAB_URL = "https://gitlab.example";
11
- const CLIENT_ID = "test-client-id";
12
- const DEVICE_CODE = "device-code-not-for-stdout";
13
- const USER_CODE = "0A44L90H";
14
- const ACCESS_TOKEN = "oauth-device-flow-access-token";
15
- const REFRESH_TOKEN = "refresh-token-device-flow";
16
- function requestUrl(input) {
17
- if (typeof input === "string") {
18
- return input;
19
- }
20
- if (input instanceof URL) {
21
- return input.href;
22
- }
23
- return input.url;
24
- }
25
- function jsonResponse(status, body) {
26
- return new Response(JSON.stringify(body), {
27
- status,
28
- headers: { "Content-Type": "application/json" },
29
- });
30
- }
31
- function createFetchStub(plan, capture) {
32
- const remainingTokens = [...plan.tokens];
33
- return async (input, init) => {
34
- const url = requestUrl(input);
35
- if (url.includes("/oauth/authorize_device")) {
36
- if (typeof init?.body === "string") {
37
- capture?.authorizeBodies.push(init.body);
38
- }
39
- return jsonResponse(plan.authorize.status, plan.authorize.body);
40
- }
41
- if (url.includes("/oauth/token")) {
42
- const next = remainingTokens.shift();
43
- if (!next) {
44
- throw new Error(`Unexpected token poll: ${url}`);
45
- }
46
- return jsonResponse(next.status, next.body);
47
- }
48
- throw new Error(`Unexpected fetch URL: ${url}`);
49
- };
50
- }
51
- function deviceAuthorizationBody(interval = 5) {
52
- return {
53
- device_code: DEVICE_CODE,
54
- user_code: USER_CODE,
55
- verification_uri: `${GITLAB_URL}/oauth/device`,
56
- verification_uri_complete: `${GITLAB_URL}/oauth/device?user_code=${USER_CODE}`,
57
- expires_in: 300,
58
- interval,
59
- };
60
- }
61
- function successTokenBody() {
62
- return {
63
- access_token: ACCESS_TOKEN,
64
- refresh_token: REFRESH_TOKEN,
65
- expires_in: 7200,
66
- token_type: "Bearer",
67
- };
68
- }
69
- function pendingBody() {
70
- return { error: "authorization_pending" };
71
- }
72
- function createTempTokenPath() {
73
- return path.join(os.tmpdir(), `gitlab-mcp-device-flow-${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}.json`);
74
- }
75
- function createOauthClient(tokenStoragePath) {
76
- return new GitLabOAuth({
77
- clientId: CLIENT_ID,
78
- redirectUri: "http://127.0.0.1:8888/callback",
79
- gitlabUrl: GITLAB_URL,
80
- scopes: ["api"],
81
- tokenStoragePath,
82
- });
83
- }
84
- function createAuthorizeCapture() {
85
- return { authorizeBodies: [] };
86
- }
87
- function capturedAuthorizeScope(bodies) {
88
- if (bodies.length !== 1) {
89
- return null;
90
- }
91
- const [body] = bodies;
92
- if (body === undefined) {
93
- return null;
94
- }
95
- return new URLSearchParams(body).get("scope");
96
- }
97
- async function runAuthCapturingScopeAsync(input) {
98
- const tokenPath = createTempTokenPath();
99
- const capture = createAuthorizeCapture();
100
- const fetchImpl = createFetchStub({
101
- authorize: { status: 200, body: deviceAuthorizationBody() },
102
- tokens: [{ status: 200, body: successTokenBody() }],
103
- }, capture);
104
- try {
105
- await runAuthCommandAsync({
106
- argv: [
107
- "node",
108
- "index.js",
109
- "auth",
110
- `--client-id=${CLIENT_ID}`,
111
- `--api-url=${GITLAB_URL}`,
112
- `--token-path=${tokenPath}`,
113
- ...(input.extraArgv ?? []),
114
- ],
115
- env: input.env ?? {},
116
- stdout: createStdoutBuffer().stdout,
117
- fetchImpl,
118
- sleepAsync: async () => { },
119
- });
120
- return capturedAuthorizeScope(capture.authorizeBodies);
121
- }
122
- finally {
123
- if (fs.existsSync(tokenPath)) {
124
- fs.unlinkSync(tokenPath);
125
- }
126
- }
127
- }
128
- function createStdoutBuffer() {
129
- const chunks = [];
130
- return {
131
- stdout: {
132
- write(chunk) {
133
- chunks.push(chunk);
134
- return true;
135
- },
136
- },
137
- text: () => chunks.join(""),
138
- };
139
- }
140
- function createSleepRecorder() {
141
- const delays = [];
142
- return {
143
- delays,
144
- async sleepAsync(ms) {
145
- delays.push(ms);
146
- },
147
- };
148
- }
149
- function readStoredToken(value) {
150
- if (typeof value !== "object" || value === null) {
151
- throw new Error("token file is not an object");
152
- }
153
- const accessToken = Reflect.get(value, "access_token");
154
- const refreshToken = Reflect.get(value, "refresh_token");
155
- const expiresIn = Reflect.get(value, "expires_in");
156
- const createdAt = Reflect.get(value, "created_at");
157
- const tokenType = Reflect.get(value, "token_type");
158
- if (typeof accessToken !== "string") {
159
- throw new Error("access_token missing");
160
- }
161
- if (typeof createdAt !== "number") {
162
- throw new Error("created_at missing");
163
- }
164
- if (typeof tokenType !== "string") {
165
- throw new Error("token_type missing");
166
- }
167
- return {
168
- access_token: accessToken,
169
- refresh_token: typeof refreshToken === "string" ? refreshToken : undefined,
170
- expires_in: typeof expiresIn === "number" ? expiresIn : undefined,
171
- created_at: createdAt,
172
- token_type: tokenType,
173
- };
174
- }
175
- describe("When reading the CLI command", () => {
176
- describe("with auth as the first positional argument", () => {
177
- it("should return auth", () => {
178
- assert.equal(getPositionalCliCommand(["node", "index.js", "auth"]), "auth");
179
- });
180
- });
181
- describe("with only --token", () => {
182
- it("should return undefined so MCP startup stays unchanged", () => {
183
- assert.equal(getPositionalCliCommand(["node", "index.js", "--token=glpat-xxxxxxxx"]), undefined);
184
- });
185
- });
186
- describe("with flags before auth", () => {
187
- it("should still return auth", () => {
188
- assert.equal(getPositionalCliCommand(["node", "index.js", "--api-url=https://gitlab.com", "auth"]), "auth");
189
- });
190
- });
191
- describe("with tsx index.ts auth", () => {
192
- it("should skip the script path and return auth", () => {
193
- assert.equal(getPositionalCliCommand(["node", "/usr/local/bin/tsx", "index.ts", "auth", "--help"]), "auth");
194
- });
195
- });
196
- describe("with --use-oauth before a curated command", () => {
197
- it("should still return mr instead of consuming it as the flag value", () => {
198
- assert.equal(getPositionalCliCommand(["node", "index.js", "--use-oauth", "mr", "list"]), "mr");
199
- });
200
- });
201
- describe("with --read-only false before a curated command", () => {
202
- it("should skip the boolean literal and return mr", () => {
203
- assert.equal(getPositionalCliCommand(["node", "index.js", "--read-only", "false", "mr", "list"]), "mr");
204
- });
205
- });
206
- describe("with space-separated --api-url before auth", () => {
207
- it("should skip the URL value and return auth", () => {
208
- assert.equal(getPositionalCliCommand([
209
- "node",
210
- "index.js",
211
- "--api-url",
212
- "https://gitlab.example/api/v4",
213
- "auth",
214
- ]), "auth");
215
- });
216
- });
217
- describe("with space-separated --client-id before auth", () => {
218
- it("should skip the client id value and return auth", () => {
219
- assert.equal(getPositionalCliCommand(["node", "index.js", "--client-id", "app-id", "auth"]), "auth");
220
- });
221
- });
222
- describe("with -h before auth", () => {
223
- it("should not consume auth as a help value", () => {
224
- assert.equal(getPositionalCliCommand(["node", "index.js", "-h", "auth"]), "auth");
225
- });
226
- });
227
- });
228
- describe("When converting a GitLab API URL", () => {
229
- describe("with an /api/v4 suffix", () => {
230
- it("should strip it the same way initializeOAuthClient does", () => {
231
- assert.equal(gitlabOriginFromApiUrl("https://gitlab.com/api/v4"), "https://gitlab.com");
232
- });
233
- });
234
- describe("with a trailing slash after /api/v4", () => {
235
- it("should strip the suffix and trailing slash", () => {
236
- assert.equal(gitlabOriginFromApiUrl("https://gitlab.example/api/v4/"), "https://gitlab.example");
237
- });
238
- });
239
- });
240
- describe("When running device authorization grant", () => {
241
- describe("with authorization_pending then a token", () => {
242
- it("should return TokenData compatible with the existing OAuth store", async () => {
243
- const fetchImpl = createFetchStub({
244
- authorize: { status: 200, body: deviceAuthorizationBody() },
245
- tokens: [
246
- { status: 400, body: pendingBody() },
247
- { status: 200, body: successTokenBody() },
248
- ],
249
- });
250
- const sleep = createSleepRecorder();
251
- const token = await runDeviceAuthorizationGrantAsync({
252
- gitlabUrl: GITLAB_URL,
253
- clientId: CLIENT_ID,
254
- scopes: ["api"],
255
- fetchImpl,
256
- sleepAsync: sleep.sleepAsync,
257
- });
258
- assert.equal(token.access_token, ACCESS_TOKEN);
259
- assert.equal(token.refresh_token, REFRESH_TOKEN);
260
- assert.equal(token.expires_in, 7200);
261
- assert.equal(token.token_type, "Bearer");
262
- assert.equal(typeof token.created_at, "number");
263
- assert.deepEqual(sleep.delays, [5000]);
264
- });
265
- });
266
- describe("with slow_down then a token", () => {
267
- it("should wait the increased interval before the next poll", async () => {
268
- const fetchImpl = createFetchStub({
269
- authorize: { status: 200, body: deviceAuthorizationBody(5) },
270
- tokens: [
271
- { status: 400, body: { error: "slow_down" } },
272
- { status: 200, body: successTokenBody() },
273
- ],
274
- });
275
- const sleep = createSleepRecorder();
276
- await runDeviceAuthorizationGrantAsync({
277
- gitlabUrl: GITLAB_URL,
278
- clientId: CLIENT_ID,
279
- scopes: ["api"],
280
- fetchImpl,
281
- sleepAsync: sleep.sleepAsync,
282
- });
283
- assert.deepEqual(sleep.delays, [10000]);
284
- });
285
- });
286
- });
287
- describe("When the token endpoint returns expired_token", () => {
288
- describe("with no token file yet", () => {
289
- it("should throw and not write a token file", async () => {
290
- const tokenPath = createTempTokenPath();
291
- const fetchImpl = createFetchStub({
292
- authorize: { status: 200, body: deviceAuthorizationBody() },
293
- tokens: [{ status: 400, body: { error: "expired_token" } }],
294
- });
295
- const oauth = createOauthClient(tokenPath);
296
- await assert.rejects(() => oauth.runDeviceFlowAsync({ fetchImpl, sleepAsync: async () => { } }), /Device code expired/);
297
- assert.equal(fs.existsSync(tokenPath), false);
298
- });
299
- });
300
- });
301
- describe("When the token endpoint returns access_denied", () => {
302
- describe("with no token file yet", () => {
303
- it("should throw and not write a token file", async () => {
304
- const tokenPath = createTempTokenPath();
305
- const fetchImpl = createFetchStub({
306
- authorize: { status: 200, body: deviceAuthorizationBody() },
307
- tokens: [{ status: 400, body: { error: "access_denied" } }],
308
- });
309
- const oauth = createOauthClient(tokenPath);
310
- await assert.rejects(() => oauth.runDeviceFlowAsync({ fetchImpl, sleepAsync: async () => { } }), /Authorization was denied/);
311
- assert.equal(fs.existsSync(tokenPath), false);
312
- });
313
- });
314
- });
315
- describe("When GitLab returns 404 for authorize_device", () => {
316
- describe("with a self-managed instance", () => {
317
- it("should tell the user to use a PAT", async () => {
318
- const fetchImpl = createFetchStub({
319
- authorize: { status: 404, body: { error: "not_found" } },
320
- tokens: [],
321
- });
322
- await assert.rejects(() => runDeviceAuthorizationGrantAsync({
323
- gitlabUrl: GITLAB_URL,
324
- clientId: CLIENT_ID,
325
- scopes: ["api"],
326
- fetchImpl,
327
- sleepAsync: async () => { },
328
- }), /GITLAB_PERSONAL_ACCESS_TOKEN/);
329
- });
330
- });
331
- });
332
- describe("When device flow succeeds", () => {
333
- describe("with GitLabOAuth.saveToken", () => {
334
- it("should persist the existing token file shape", async () => {
335
- const tokenPath = createTempTokenPath();
336
- const fetchImpl = createFetchStub({
337
- authorize: { status: 200, body: deviceAuthorizationBody() },
338
- tokens: [{ status: 200, body: successTokenBody() }],
339
- });
340
- try {
341
- await createOauthClient(tokenPath).runDeviceFlowAsync({
342
- fetchImpl,
343
- sleepAsync: async () => { },
344
- });
345
- const raw = JSON.parse(fs.readFileSync(tokenPath, "utf8"));
346
- const stored = readStoredToken(raw);
347
- assert.equal(stored.access_token, ACCESS_TOKEN);
348
- assert.equal(stored.refresh_token, REFRESH_TOKEN);
349
- assert.equal(stored.expires_in, 7200);
350
- assert.equal(stored.token_type, "Bearer");
351
- assert.equal(typeof stored.created_at, "number");
352
- assert.equal(fs.statSync(tokenPath).mode & 0o777, 0o600);
353
- }
354
- finally {
355
- if (fs.existsSync(tokenPath)) {
356
- fs.unlinkSync(tokenPath);
357
- }
358
- }
359
- });
360
- });
361
- });
362
- describe("When running the auth command", () => {
363
- describe("with --help", () => {
364
- it("should print usage and not fetch", async () => {
365
- const out = createStdoutBuffer();
366
- const fetchImpl = async () => {
367
- throw new Error("fetch should not run for --help");
368
- };
369
- await runAuthCommandAsync({
370
- argv: ["node", "index.js", "auth", "--help"],
371
- env: {},
372
- stdout: out.stdout,
373
- fetchImpl,
374
- });
375
- assert.match(out.text(), /zereight-mcp-gitlab auth/);
376
- assert.equal(out.text().includes(DEVICE_CODE), false);
377
- });
378
- });
379
- describe("without a client id", () => {
380
- it("should fail before calling GitLab", async () => {
381
- await assert.rejects(() => runAuthCommandAsync({
382
- argv: ["node", "index.js", "auth"],
383
- env: {},
384
- stdout: createStdoutBuffer().stdout,
385
- fetchImpl: async () => {
386
- throw new Error("fetch should not run without client id");
387
- },
388
- }), /GITLAB_OAUTH_CLIENT_ID/);
389
- });
390
- });
391
- describe("with a successful device flow", () => {
392
- it("should print the user code and not the device code or access token", async () => {
393
- const tokenPath = createTempTokenPath();
394
- const out = createStdoutBuffer();
395
- const fetchImpl = createFetchStub({
396
- authorize: { status: 200, body: deviceAuthorizationBody() },
397
- tokens: [{ status: 200, body: successTokenBody() }],
398
- });
399
- try {
400
- await runAuthCommandAsync({
401
- argv: [
402
- "node",
403
- "index.js",
404
- "auth",
405
- `--client-id=${CLIENT_ID}`,
406
- `--api-url=${GITLAB_URL}`,
407
- `--token-path=${tokenPath}`,
408
- ],
409
- env: {},
410
- stdout: out.stdout,
411
- fetchImpl,
412
- sleepAsync: async () => { },
413
- });
414
- const printed = out.text();
415
- assert.equal(printed.includes(USER_CODE), true);
416
- assert.match(printed, /Token saved to/);
417
- assert.match(printed, /GITLAB_USE_OAUTH=true/);
418
- assert.match(printed, /GITLAB_OAUTH_TOKEN_PATH/);
419
- assert.equal(printed.includes(DEVICE_CODE), false);
420
- assert.equal(printed.includes(ACCESS_TOKEN), false);
421
- assert.equal(fs.existsSync(tokenPath), true);
422
- }
423
- finally {
424
- if (fs.existsSync(tokenPath)) {
425
- fs.unlinkSync(tokenPath);
426
- }
427
- }
428
- });
429
- });
430
- describe("without a read-only flag", () => {
431
- it("should request the api scope", async () => {
432
- assert.equal(await runAuthCapturingScopeAsync({}), "api");
433
- });
434
- });
435
- describe("with --permission-mode=readonly", () => {
436
- it("should request the read_api scope", async () => {
437
- assert.equal(await runAuthCapturingScopeAsync({ extraArgv: ["--permission-mode=readonly"] }), "read_api");
438
- });
439
- });
440
- describe("with GITLAB_PERMISSION_MODE=readonly", () => {
441
- it("should request the read_api scope", async () => {
442
- assert.equal(await runAuthCapturingScopeAsync({ env: { GITLAB_PERMISSION_MODE: "readonly" } }), "read_api");
443
- });
444
- });
445
- describe("with --read-only=true", () => {
446
- it("should request the read_api scope", async () => {
447
- assert.equal(await runAuthCapturingScopeAsync({ extraArgv: ["--read-only=true"] }), "read_api");
448
- });
449
- });
450
- });
@@ -1,204 +0,0 @@
1
- /**
2
- * OAuth startup tests.
3
- *
4
- * - isAuthInvalidTokenResponse: pure classifier — refresh failures that mean
5
- * "re-login would fix it" vs transient/config errors that must not open a browser.
6
- * - ensureOAuthToken: lazy first-use delivery — the caller must receive the
7
- * token even when the on-disk token is still valid (regression: returning
8
- * early on hasValidToken() alone leaves requests unauthenticated).
9
- *
10
- * Temp token files + a loopback mock for /oauth/token only; no MCP server.
11
- * The missing-token and invalid_grant paths start the interactive browser
12
- * flow and are deliberately not covered here.
13
- */
14
- import { describe, test, afterEach } from "node:test";
15
- import assert from "node:assert";
16
- import * as fs from "node:fs";
17
- import * as os from "node:os";
18
- import * as path from "node:path";
19
- import * as http from "node:http";
20
- import { GitLabOAuth, ensureOAuthToken, isAuthInvalidTokenResponse } from "../oauth.js";
21
- const validTokenFile = {
22
- access_token: "cached-access-token",
23
- refresh_token: "cached-refresh-token",
24
- expires_in: 7200,
25
- created_at: Date.now(),
26
- token_type: "Bearer",
27
- };
28
- const expiredTokenFile = {
29
- ...validTokenFile,
30
- created_at: Date.now() - 10 * 3600 * 1000,
31
- };
32
- const tmpDirs = [];
33
- const servers = [];
34
- afterEach(async () => {
35
- for (const server of servers.splice(0)) {
36
- await new Promise(resolve => server.close(() => resolve()));
37
- }
38
- for (const dir of tmpDirs.splice(0)) {
39
- fs.rmSync(dir, { recursive: true, force: true });
40
- }
41
- });
42
- function makeTokenFile(data) {
43
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), "oauth-startup-"));
44
- tmpDirs.push(dir);
45
- const file = path.join(dir, "token.json");
46
- fs.writeFileSync(file, JSON.stringify(data));
47
- return file;
48
- }
49
- function makeClient(tokenStoragePath, gitlabUrl, extra) {
50
- return new GitLabOAuth({
51
- clientId: "test-client",
52
- redirectUri: "http://127.0.0.1:8888/callback",
53
- gitlabUrl,
54
- scopes: ["api"],
55
- tokenStoragePath,
56
- ...extra,
57
- });
58
- }
59
- async function startTokenServer(handler) {
60
- const bodies = [];
61
- const server = http.createServer((req, res) => {
62
- let body = "";
63
- req.on("data", (chunk) => {
64
- body += chunk.toString();
65
- });
66
- req.on("end", () => {
67
- bodies.push(body);
68
- const { status, json } = handler();
69
- res.writeHead(status, { "Content-Type": "application/json" });
70
- res.end(JSON.stringify(json));
71
- });
72
- });
73
- servers.push(server);
74
- await new Promise(resolve => server.listen(0, "127.0.0.1", () => resolve()));
75
- const addr = server.address();
76
- assert(addr && typeof addr !== "string");
77
- return { url: `http://127.0.0.1:${addr.port}`, bodies };
78
- }
79
- describe("isAuthInvalidTokenResponse", () => {
80
- test("400 invalid_grant is auth-invalid", () => {
81
- assert.strictEqual(isAuthInvalidTokenResponse(400, '{"error":"invalid_grant"}'), true);
82
- });
83
- test("400 invalid_token is auth-invalid", () => {
84
- assert.strictEqual(isAuthInvalidTokenResponse(400, '{"error":"invalid_token"}'), true);
85
- });
86
- test("401 invalid_grant is auth-invalid", () => {
87
- assert.strictEqual(isAuthInvalidTokenResponse(401, '{"error":"invalid_grant"}'), true);
88
- });
89
- test("400 invalid_client is not auth-invalid (config error)", () => {
90
- assert.strictEqual(isAuthInvalidTokenResponse(400, '{"error":"invalid_client"}'), false);
91
- });
92
- test("400 invalid_request is not auth-invalid (config error)", () => {
93
- assert.strictEqual(isAuthInvalidTokenResponse(400, '{"error":"invalid_request"}'), false);
94
- });
95
- test("500 server_error is not auth-invalid (transient)", () => {
96
- assert.strictEqual(isAuthInvalidTokenResponse(500, '{"error":"server_error"}'), false);
97
- });
98
- test("429 is not auth-invalid (rate limited)", () => {
99
- assert.strictEqual(isAuthInvalidTokenResponse(429, '{"error":"invalid_grant"}'), false);
100
- });
101
- test("non-JSON body is not auth-invalid", () => {
102
- assert.strictEqual(isAuthInvalidTokenResponse(400, "gateway timeout"), false);
103
- });
104
- });
105
- describe("ensureOAuthToken", () => {
106
- test("null client is a no-op", async () => {
107
- const delivered = [];
108
- await ensureOAuthToken(null, null, t => {
109
- delivered.push(t);
110
- });
111
- await ensureOAuthToken(undefined, undefined, t => {
112
- delivered.push(t);
113
- });
114
- assert.deepStrictEqual(delivered, []);
115
- });
116
- test("first call with valid cached token delivers it without network", async () => {
117
- // Closed port: any fetch attempt fails, proving the cached path is network-free.
118
- const client = makeClient(makeTokenFile(validTokenFile), "http://127.0.0.1:9");
119
- const delivered = [];
120
- await ensureOAuthToken(client, null, t => {
121
- delivered.push(t);
122
- });
123
- assert.deepStrictEqual(delivered, ["cached-access-token"]);
124
- });
125
- test("already-held valid token is not re-delivered", async () => {
126
- const client = makeClient(makeTokenFile(validTokenFile), "http://127.0.0.1:9");
127
- const delivered = [];
128
- await ensureOAuthToken(client, "cached-access-token", t => {
129
- delivered.push(t);
130
- });
131
- assert.deepStrictEqual(delivered, []);
132
- });
133
- test("expired token refreshes via /oauth/token and persists", async () => {
134
- const { url, bodies } = await startTokenServer(() => ({
135
- status: 200,
136
- json: {
137
- access_token: "fresh-token",
138
- refresh_token: "fresh-refresh",
139
- expires_in: 7200,
140
- token_type: "Bearer",
141
- },
142
- }));
143
- const file = makeTokenFile(expiredTokenFile);
144
- const client = makeClient(file, url);
145
- const delivered = [];
146
- await ensureOAuthToken(client, null, t => {
147
- delivered.push(t);
148
- });
149
- assert.deepStrictEqual(delivered, ["fresh-token"]);
150
- assert.strictEqual(bodies.length, 1);
151
- assert.match(bodies[0], /grant_type=refresh_token/);
152
- assert.match(bodies[0], /refresh_token=cached-refresh-token/);
153
- const saved = JSON.parse(fs.readFileSync(file, "utf8"));
154
- assert.strictEqual(saved.access_token, "fresh-token");
155
- });
156
- test("transient refresh failure propagates without opening a browser", async () => {
157
- const { url } = await startTokenServer(() => ({ status: 500, json: { error: "server_error" } }));
158
- const client = makeClient(makeTokenFile(expiredTokenFile), url);
159
- const delivered = [];
160
- // Rejection (not a browser popup) proves the no-browser path.
161
- await assert.rejects(() => ensureOAuthToken(client, null, t => {
162
- delivered.push(t);
163
- }), /Token refresh failed: 500/);
164
- assert.deepStrictEqual(delivered, []);
165
- });
166
- test("token script resolves once, then fast path holds", async () => {
167
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), "oauth-startup-"));
168
- tmpDirs.push(dir);
169
- const client = makeClient(path.join(dir, "token.json"), "http://127.0.0.1:9", {
170
- tokenScript: "echo script-token-1",
171
- });
172
- const delivered = [];
173
- await ensureOAuthToken(client, null, t => {
174
- delivered.push(t);
175
- });
176
- assert.deepStrictEqual(delivered, ["script-token-1"]);
177
- await ensureOAuthToken(client, delivered[0], t => {
178
- delivered.push(t);
179
- });
180
- assert.deepStrictEqual(delivered, ["script-token-1"]);
181
- });
182
- test("concurrent calls coalesce into a single refresh request", async () => {
183
- const { url, bodies } = await startTokenServer(() => ({
184
- status: 200,
185
- json: {
186
- access_token: "coalesced-token",
187
- refresh_token: "new-refresh",
188
- expires_in: 7200,
189
- token_type: "Bearer",
190
- },
191
- }));
192
- const file = makeTokenFile(expiredTokenFile);
193
- const client = makeClient(file, url);
194
- const deliveredA = [];
195
- const deliveredB = [];
196
- await Promise.all([
197
- ensureOAuthToken(client, null, t => deliveredA.push(t)),
198
- ensureOAuthToken(client, null, t => deliveredB.push(t)),
199
- ]);
200
- assert.deepStrictEqual(deliveredA, ["coalesced-token"]);
201
- assert.deepStrictEqual(deliveredB, ["coalesced-token"]);
202
- assert.strictEqual(bodies.length, 1);
203
- });
204
- });