(input: {
readonly client: Client;
readonly emulator: EmulatorClient;
readonly emulatorBaseUrl: string;
readonly target: TargetShape;
readonly integration: IntegrationSlug;
readonly oauthClient: OAuthClientSlug;
})
| 141 | }); |
| 142 | |
| 143 | const connectGoogleAccount = (input: { |
| 144 | readonly client: Client; |
| 145 | readonly emulator: EmulatorClient; |
| 146 | readonly emulatorBaseUrl: string; |
| 147 | readonly target: TargetShape; |
| 148 | readonly integration: IntegrationSlug; |
| 149 | readonly oauthClient: OAuthClientSlug; |
| 150 | }) => |
| 151 | Effect.gen(function* () { |
| 152 | const redirectUri = new URL("/api/oauth/callback", input.target.baseUrl).toString(); |
| 153 | const credential = yield* Effect.promise(() => |
| 154 | input.emulator.credentials.mint({ |
| 155 | type: "oauth-authorization-code", |
| 156 | redirect_uris: [redirectUri], |
| 157 | }), |
| 158 | ); |
| 159 | if (!credential.client_id || !credential.client_secret) { |
| 160 | return yield* Effect.die(`Google emulator did not mint an OAuth client`); |
| 161 | } |
| 162 | |
| 163 | yield* input.client.openapi.configure({ |
| 164 | params: { slug: input.integration }, |
| 165 | payload: { baseUrl: input.emulatorBaseUrl }, |
| 166 | }); |
| 167 | yield* input.client.oauth.createClient({ |
| 168 | payload: { |
| 169 | owner: "org", |
| 170 | slug: input.oauthClient, |
| 171 | grant: "authorization_code", |
| 172 | authorizationUrl: `${input.emulatorBaseUrl}/o/oauth2/v2/auth`, |
| 173 | tokenUrl: `${input.emulatorBaseUrl}/oauth2/token`, |
| 174 | clientId: credential.client_id, |
| 175 | clientSecret: credential.client_secret, |
| 176 | originIntegration: input.integration, |
| 177 | }, |
| 178 | }); |
| 179 | |
| 180 | const started = yield* input.client.oauth.start({ |
| 181 | payload: { |
| 182 | client: input.oauthClient, |
| 183 | clientOwner: "org", |
| 184 | owner: "org", |
| 185 | name: CONNECTION, |
| 186 | integration: input.integration, |
| 187 | template: GOOGLE_AUTH_TEMPLATE, |
| 188 | redirectUri, |
| 189 | }, |
| 190 | }); |
| 191 | expect(started.status, "OAuth starts with an emulator redirect").toBe("redirect"); |
| 192 | if (started.status !== "redirect") return yield* Effect.die("OAuth unexpectedly connected"); |
| 193 | |
| 194 | const code = yield* completeGoogleConsent(started.authorizationUrl); |
| 195 | const completed = yield* input.client.oauth.complete({ |
| 196 | payload: { state: started.state, code }, |
| 197 | }); |
| 198 | expect(completed.integration, "OAuth completion creates the connection").toBe( |
| 199 | input.integration, |
| 200 | ); |
no test coverage detected