|
1 |
| -import fetchMock, { type MockMatcherFunction } from "fetch-mock"; |
| 1 | +import fetchMock from "fetch-mock"; |
2 | 2 |
|
3 | 3 | import { request } from "@octokit/request";
|
4 | 4 | import { vi, beforeEach, test, expect } from "vitest";
|
5 | 5 |
|
6 | 6 | import { createAppAuth, createOAuthUserAuth } from "../src/index.ts";
|
7 | 7 | import type { FactoryInstallation } from "../src/types.ts";
|
8 | 8 |
|
| 9 | +type MockMatcherFunction = fetchMock.MockMatcherFunction; |
| 10 | + |
9 | 11 | const APP_ID = 1;
|
10 | 12 | const PRIVATE_KEY = `-----BEGIN RSA PRIVATE KEY-----
|
11 | 13 | MIIEpAIBAAKCAQEA1c7+9z5Pad7OejecsQ0bu3aozN3tihPmljnnudb9G3HECdnH
|
@@ -397,6 +399,111 @@ test("repositoryIds auth option", async () => {
|
397 | 399 | });
|
398 | 400 | });
|
399 | 401 |
|
| 402 | +test("installationId strategy option coalesces concurrent calls with the same options", async () => { |
| 403 | + const sandbox = fetchMock |
| 404 | + .sandbox() |
| 405 | + .post("https://api.github.com/app/installations/123/access_tokens", { |
| 406 | + token: "secret123", |
| 407 | + expires_at: "1970-01-01T01:00:00.000Z", |
| 408 | + permissions: { |
| 409 | + metadata: "read", |
| 410 | + }, |
| 411 | + repository_selection: "all", |
| 412 | + }) |
| 413 | + .postOnce("https://api.github.com/app/installations/234/access_tokens", { |
| 414 | + token: "secret234", |
| 415 | + expires_at: "1970-01-01T01:00:00.000Z", |
| 416 | + permissions: { |
| 417 | + metadata: "read", |
| 418 | + }, |
| 419 | + repository_selection: "all", |
| 420 | + }); |
| 421 | + const requestMock = request.defaults({ |
| 422 | + headers: { |
| 423 | + "user-agent": "test", |
| 424 | + }, |
| 425 | + request: { |
| 426 | + fetch: sandbox, |
| 427 | + }, |
| 428 | + }); |
| 429 | + |
| 430 | + const auth = createAppAuth({ |
| 431 | + appId: APP_ID, |
| 432 | + privateKey: PRIVATE_KEY, |
| 433 | + installationId: 123, |
| 434 | + request: requestMock, |
| 435 | + }); |
| 436 | + const unrelatedAuth = createAppAuth({ |
| 437 | + appId: APP_ID, |
| 438 | + privateKey: PRIVATE_KEY, |
| 439 | + installationId: 234, |
| 440 | + request: requestMock, |
| 441 | + }); |
| 442 | + |
| 443 | + const authentications = await Promise.all([ |
| 444 | + auth({ type: "installation", refresh: true }), |
| 445 | + auth({ type: "installation", refresh: true }), |
| 446 | + unrelatedAuth({ type: "installation", refresh: true }), |
| 447 | + ]); |
| 448 | + expect(authentications[0]).toEqual({ |
| 449 | + type: "token", |
| 450 | + token: "secret123", |
| 451 | + tokenType: "installation", |
| 452 | + installationId: 123, |
| 453 | + permissions: { |
| 454 | + metadata: "read", |
| 455 | + }, |
| 456 | + createdAt: "1970-01-01T00:00:00.000Z", |
| 457 | + expiresAt: "1970-01-01T01:00:00.000Z", |
| 458 | + repositorySelection: "all", |
| 459 | + }); |
| 460 | + // same result as the first one |
| 461 | + expect(authentications[1]).toBe(authentications[0]); |
| 462 | + // and only request was made |
| 463 | + expect( |
| 464 | + sandbox.calls("https://api.github.com/app/installations/123/access_tokens") |
| 465 | + .length, |
| 466 | + ).toBe(1); |
| 467 | + |
| 468 | + // whereas the auth for the unrelated installation got a different token |
| 469 | + expect(authentications[2]).toEqual({ |
| 470 | + type: "token", |
| 471 | + token: "secret234", |
| 472 | + tokenType: "installation", |
| 473 | + installationId: 234, |
| 474 | + permissions: { |
| 475 | + metadata: "read", |
| 476 | + }, |
| 477 | + createdAt: "1970-01-01T00:00:00.000Z", |
| 478 | + expiresAt: "1970-01-01T01:00:00.000Z", |
| 479 | + repositorySelection: "all", |
| 480 | + }); |
| 481 | + |
| 482 | + // now that the original auth call has resolved, if we do it again it will actually run |
| 483 | + const laterAuthentication = await auth({ |
| 484 | + type: "installation", |
| 485 | + refresh: true, |
| 486 | + }); |
| 487 | + expect(laterAuthentication).toEqual({ |
| 488 | + type: "token", |
| 489 | + token: "secret123", |
| 490 | + tokenType: "installation", |
| 491 | + installationId: 123, |
| 492 | + permissions: { |
| 493 | + metadata: "read", |
| 494 | + }, |
| 495 | + createdAt: "1970-01-01T00:00:00.000Z", |
| 496 | + expiresAt: "1970-01-01T01:00:00.000Z", |
| 497 | + repositorySelection: "all", |
| 498 | + }); |
| 499 | + |
| 500 | + // a fresh request was made |
| 501 | + expect( |
| 502 | + sandbox.calls("https://api.github.com/app/installations/123/access_tokens") |
| 503 | + .length, |
| 504 | + ).toBe(2); |
| 505 | +}); |
| 506 | + |
400 | 507 | test("repositoryNames auth option", async () => {
|
401 | 508 | const matchCreateInstallationAccessToken: MockMatcherFunction = (
|
402 | 509 | _url,
|
|
0 commit comments