| @@ -10,6 +10,9 @@ import { deriveGoogleDiscoveryIdentity, googleDiscoveryAdapter } from "./spec-fo · 9 unmodified lines |
| 10 | 10 | import { googleCatalog } from "./presets"; |
| 11 | 11 | |
| 12 | 12 | const TASKS_URL = "https://www.googleapis.com/discovery/v1/apis/tasks/v1/rest"; |
| 13 | +const GMAIL_URL = "https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest"; |
| 14 | +const GMAIL_MODIFY_SCOPE = "https://www.googleapis.com/auth/gmail.modify"; |
| 15 | +const GMAIL_FULL_SCOPE = "https://mail.google.com/"; |
| 13 | 16 | |
| 14 | 17 | const tasksDiscoveryDoc = { |
| 15 | 18 | name: "tasks", |
| @@ -70,6 +73,66 @@ const discoveryHttpClientLayer = Layer.succeed(HttpClient.HttpClient)( · 54 unmodified lines |
| 70 | 73 | ), |
| 71 | 74 | ); |
| 72 | 75 | |
| 76 | +const gmailDiscoveryDoc = { |
| 77 | + name: "gmail", |
| 78 | + version: "v1", |
| 79 | + title: "Gmail API", |
| 80 | + rootUrl: "https://gmail.googleapis.com/", |
| 81 | + servicePath: "", |
| 82 | + auth: { |
| 83 | + oauth2: { |
| 84 | + scopes: { |
| 85 | + [GMAIL_MODIFY_SCOPE]: { description: "Read and modify Gmail" }, |
| 86 | + [GMAIL_FULL_SCOPE]: { description: "Full Gmail access" }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + resources: { |
| 91 | + users: { |
| 92 | + resources: { |
| 93 | + messages: { |
| 94 | + methods: { |
| 95 | + list: { |
| 96 | + id: "gmail.users.messages.list", |
| 97 | + httpMethod: "GET", |
| 98 | + path: "gmail/v1/users/{userId}/messages", |
| 99 | + scopes: [GMAIL_MODIFY_SCOPE, GMAIL_FULL_SCOPE], |
| 100 | + parameters: { |
| 101 | + userId: { location: "path", required: true, type: "string" }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + delete: { |
| 105 | + id: "gmail.users.messages.delete", |
| 106 | + httpMethod: "DELETE", |
| 107 | + path: "gmail/v1/users/{userId}/messages/{id}", |
| 108 | + scopes: [GMAIL_FULL_SCOPE], |
| 109 | + parameters: { |
| 110 | + userId: { location: "path", required: true, type: "string" }, |
| 111 | + id: { location: "path", required: true, type: "string" }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + }, |
| 119 | + schemas: {}, |
| 120 | +}; |
| 121 | + |
| 122 | +const gmailDiscoveryHttpClientLayer = Layer.succeed(HttpClient.HttpClient)( |
| 123 | + HttpClient.make((request: HttpClientRequest.HttpClientRequest) => |
| 124 | + Effect.succeed( |
| 125 | + HttpClientResponse.fromWeb( |
| 126 | + request, |
| 127 | + new Response(JSON.stringify(gmailDiscoveryDoc), { |
| 128 | + status: request.url === GMAIL_URL ? 200 : 404, |
| 129 | + headers: { "content-type": "application/json" }, |
| 130 | + }), |
| 131 | + ), |
| 132 | + ), |
| 133 | + ), |
| 134 | +); |
| 135 | + |
| 73 | 136 | it.effect("fetches and converts a Google Discovery document", () => |
| 74 | 137 | Effect.gen(function* () { |
| 75 | 138 | const converted = yield* googleDiscoveryAdapter.fetch({ |
| @@ -165,3 +228,45 @@ it.effect( · 89 unmodified lines |
| 165 | 228 | ); |
| 166 | 229 | }), |
| 167 | 230 | ); |
| 231 | + |
| 232 | +it.effect("preserves a Google preset's consent scope boundary when refreshing", () => |
| 233 | + Effect.gen(function* () { |
| 234 | + const gmailPreset = googleCatalog.find((preset) => preset.id === "google-gmail")!; |
| 235 | + const authTemplate: readonly AuthenticationInput[] = (gmailPreset.authTemplate ?? []).flatMap( |
| 236 | + (template) => (template.kind === "oauth2" ? [template] : []), |
| 237 | + ); |
| 238 | + const executor = yield* createExecutor( |
| 239 | + makeTestConfig({ |
| 240 | + plugins: [ |
| 241 | + openApiPlugin({ |
| 242 | + httpClientLayer: gmailDiscoveryHttpClientLayer, |
| 243 | + presets: [gmailPreset], |
| 244 | + specFormats: [googleDiscoveryAdapter], |
| 245 | + }), |
| 246 | + memoryCredentialsPlugin(), |
| 247 | + ], |
| 248 | + }), |
| 249 | + ); |
| 250 | + |
| 251 | + const added = yield* executor.openapi.addSpec({ |
| 252 | + spec: { kind: "url", url: GMAIL_URL }, |
| 253 | + slug: gmailPreset.defaultSlug, |
| 254 | + specFormat: gmailPreset.specFormat, |
| 255 | + family: gmailPreset.family, |
| 256 | + authenticationTemplate: authTemplate, |
| 257 | + }); |
| 258 | + expect(added.toolCount).toBe(1); |
| 259 | + |
| 260 | + const updated = yield* executor.openapi.updateSpec("google_gmail"); |
| 261 | + |
| 262 | + const config = yield* executor.openapi.getConfig("google_gmail"); |
| 263 | + const oauthTemplate = config?.authenticationTemplate?.find( |
| 264 | + (template) => template.kind === "oauth2", |
| 265 | + ); |
| 266 | + expect(updated.toolCount).toBe(1); |
| 267 | + expect(updated.addedTools).not.toContain("gmail.users.messages.delete"); |
| 268 | + expect(oauthTemplate?.kind === "oauth2" ? oauthTemplate.scopes : undefined).toContain( |
| 269 | + GMAIL_MODIFY_SCOPE, |
| 270 | + ); |
| 271 | + }), |
| 272 | +); |