|
| 1 | +// Base for all `main` tests. |
| 2 | +// @ts-check |
| 3 | +import { MockAgent, setGlobalDispatcher } from "undici"; |
| 4 | + |
| 5 | +export async function test(cb = (_mockPool) => {}) { |
| 6 | + // Set required environment variables and inputs |
| 7 | + process.env.GITHUB_REPOSITORY_OWNER = "actions"; |
| 8 | + process.env.GITHUB_REPOSITORY = "actions/create-github-app-token"; |
| 9 | + // inputs are set as environment variables with the prefix INPUT_ |
| 10 | + // https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#example-specifying-inputs |
| 11 | + process.env.INPUT_APP_ID = "123456"; |
| 12 | + process.env.INPUT_PRIVATE_KEY = `-----BEGIN RSA PRIVATE KEY----- |
| 13 | +MIIEowIBAAKCAQEA280nfuUM9w00Ib9E2rvZJ6Qu3Ua3IqR34ZlK53vn/Iobn2EL |
| 14 | +Z9puc5Q/nFBU15NKwHyQNb+OG2hTCkjd1Xi9XPzEOH1r42YQmTGq8YCkUSkk6KZA |
| 15 | +5dnhLwN9pFquT9fQgrf4r1D5GJj3rqvj8JDr1sBmunArqY5u4gziSrIohcjLIZV0 |
| 16 | +cIMz/RUIMe/EAsNeiwzEteHAtf/WpMs+OfF94SIUrDlkPr0H0H3DER8l1HZAvE0e |
| 17 | +eD3ZJ6njrF6UHQWDVrekSTB0clpVTTU9TMpe+gs2nnFww9G8As+WsW8xHVjVipJy |
| 18 | +AwqBhiR+s7wlcbh2i0NQqt8GL9/jIFTmleiwsQIDAQABAoIBAHNyP8pgl/yyzKzk |
| 19 | +/0871wUBMTQ7zji91dGCaFtJM0HrcDK4D/uOOPEv7nE1qDpKPLr5Me1pHUS7+NGw |
| 20 | +EAPtlNhgUtew2JfppdIwyi5qeOPADoi7ud6AH8xHsxg+IMwC+JuP8WhzyUHoJj9y |
| 21 | +PRi/pX94Mvy9qdE25HqKddjx1mLdaHhxqPkr16/em23uYZqm1lORsCPD3vTlthj7 |
| 22 | +WiEbBSqmpYvjj8iFP4yFk2N+LvuWgSilRzq1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nqsVp75kGS6OH61 |
| 38 | +-----END RSA PRIVATE KEY-----`; // This key is invalidated. It’s from https://github.com/octokit/auth-app.js/issues/465#issuecomment-1564998327. |
| 39 | + |
| 40 | + // Set up mocking |
| 41 | + const mockAgent = new MockAgent(); |
| 42 | + mockAgent.disableNetConnect(); |
| 43 | + setGlobalDispatcher(mockAgent); |
| 44 | + const mockPool = mockAgent.get("https://api.github.com"); |
| 45 | + |
| 46 | + // Calling `auth({ type: "app" })` to obtain a JWT doesn’t make network requests, so no need to intercept. |
| 47 | + |
| 48 | + // Mock installation id request |
| 49 | + const mockInstallationId = "123456"; |
| 50 | + const owner = process.env.INPUT_OWNER ?? process.env.GITHUB_REPOSITORY_OWNER; |
| 51 | + const repo = encodeURIComponent( |
| 52 | + (process.env.INPUT_REPOSITORIES ?? process.env.GITHUB_REPOSITORY).split( |
| 53 | + "," |
| 54 | + )[0] |
| 55 | + ); |
| 56 | + mockPool |
| 57 | + .intercept({ |
| 58 | + path: `/repos/${owner}/${repo}/installation`, |
| 59 | + method: "GET", |
| 60 | + headers: { |
| 61 | + accept: "application/vnd.github.v3+json", |
| 62 | + "user-agent": "actions/create-github-app-token", |
| 63 | + // Intentionally omitting the `authorization` header, since JWT creation is not idempotent. |
| 64 | + }, |
| 65 | + }) |
| 66 | + .reply( |
| 67 | + 200, |
| 68 | + { id: mockInstallationId }, |
| 69 | + { headers: { "content-type": "application/json" } } |
| 70 | + ); |
| 71 | + |
| 72 | + // Mock installation access token request |
| 73 | + const mockInstallationAccessToken = |
| 74 | + "ghs_16C7e42F292c6912E7710c838347Ae178B4a"; // This token is invalidated. It’s from https://docs.github.com/en/rest/apps/apps?apiVersion=2022-11-28#create-an-installation-access-token-for-an-app. |
| 75 | + mockPool |
| 76 | + .intercept({ |
| 77 | + path: `/app/installations/${mockInstallationId}/access_tokens`, |
| 78 | + method: "POST", |
| 79 | + headers: { |
| 80 | + accept: "application/vnd.github.v3+json", |
| 81 | + "user-agent": "actions/create-github-app-token", |
| 82 | + // Note: Intentionally omitting the `authorization` header, since JWT creation is not idempotent. |
| 83 | + }, |
| 84 | + }) |
| 85 | + .reply( |
| 86 | + 201, |
| 87 | + { token: mockInstallationAccessToken }, |
| 88 | + { headers: { "content-type": "application/json" } } |
| 89 | + ); |
| 90 | + |
| 91 | + // Run the callback |
| 92 | + cb(mockPool); |
| 93 | + |
| 94 | + // Run the main script |
| 95 | + await import("../main.js"); |
| 96 | +} |
0 commit comments