(env)
| 90 | } |
| 91 | |
| 92 | async function githubSession(env) { |
| 93 | const clientNonce = "test-browser-nonce-that-is-at-least-32-chars"; |
| 94 | const start = await handleAuthVoteRoute( |
| 95 | new Request(`${BASE}/auth/github?return_to=%2Floop-library%2Floops%2Fovernight-docs-sweep%2F&client_nonce=${clientNonce}`), |
| 96 | env, |
| 97 | ); |
| 98 | assert.equal(start.status, 200); |
| 99 | const authorization = new URL((await start.json()).authorizationUrl); |
| 100 | assert.equal(authorization.origin, "https://github.com"); |
| 101 | assert.equal(authorization.searchParams.get("scope"), "read:user"); |
| 102 | const state = authorization.searchParams.get("state"); |
| 103 | const calls = []; |
| 104 | const callback = await handleAuthVoteRoute( |
| 105 | new Request(`${BASE}/auth/callback/github?code=test-code&state=${encodeURIComponent(state)}`), |
| 106 | env, |
| 107 | { |
| 108 | fetch: async (input, init = {}) => { |
| 109 | calls.push({ input, init }); |
| 110 | if (input === "https://github.com/login/oauth/access_token") { |
| 111 | return Response.json({ access_token: "github-access-token" }); |
| 112 | } |
| 113 | if (input === "https://api.github.com/user") { |
| 114 | return Response.json({ id: 42, login: "octoloop", name: "Octo Loop" }); |
| 115 | } |
| 116 | throw new Error(`Unexpected request: ${input}`); |
| 117 | }, |
| 118 | }, |
| 119 | ); |
| 120 | assert.equal(callback.status, 200); |
| 121 | const callbackBody = await callback.text(); |
| 122 | assert.match(callbackBody, /sessionStorage\.setItem\("ll_session"/); |
| 123 | assert.match(callbackBody, /loop-library\/loops\/overnight-docs-sweep/); |
| 124 | assert.match(callbackBody, new RegExp(clientNonce)); |
| 125 | assert.equal(calls.length, 2); |
| 126 | assert.equal( |
| 127 | new Headers(calls[1].init.headers).get("Authorization"), |
| 128 | "Bearer github-access-token", |
| 129 | ); |
| 130 | const sessionMatch = callbackBody.match( |
| 131 | /"sessionToken":"([A-Za-z0-9_.-]+)"/, |
| 132 | ); |
| 133 | assert(sessionMatch, "session token missing from OAuth bridge"); |
| 134 | return sessionMatch[1]; |
| 135 | } |
| 136 | |
| 137 | test("GitHub OAuth creates a signed session that can cast, switch, and remove a vote", async () => { |
| 138 | const env = makeEnv(); |
no test coverage detected