Use native Chrome for Reddit login handoff
This commit is contained in:
1 parent
2c533fa52a
commit
dbb1842ef4
13 files changed
+324
-60
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import express from 'express';
|
||||
import type { AddressInfo } from 'node:net';
|
||||
import { afterEach, describe, expect, it } from 'vitest';
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { classifyRedditSessionError } from '../src/browser/client.js';
|
||||
import { createLoginRouter } from '../src/web/routes/login.js';
|
||||
|
||||
@@ -14,6 +14,14 @@ async function request(app: express.Express, path: string): Promise<Response> {
|
||||
return fetch(`http://127.0.0.1:${port}${path}`);
|
||||
}
|
||||
|
||||
async function post(app: express.Express, path: string): Promise<Response> {
|
||||
const server = app.listen(0);
|
||||
servers.push(server);
|
||||
await new Promise<void>((resolve) => server.once('listening', resolve));
|
||||
const { port } = server.address() as AddressInfo;
|
||||
return fetch(`http://127.0.0.1:${port}${path}`, { method: 'POST' });
|
||||
}
|
||||
|
||||
afterEach(async () => {
|
||||
await Promise.all(
|
||||
servers.splice(0).map(
|
||||
@@ -105,4 +113,27 @@ describe('Reddit session status', () => {
|
||||
).toBe('unreachable');
|
||||
expect(classifyRedditSessionError(new Error('login form was not found'))).toBe('login-required');
|
||||
});
|
||||
|
||||
it('starts a native Chrome login handoff without controlling the attached page', async () => {
|
||||
const requestNativeLogin = vi.fn();
|
||||
const getPage = vi.fn();
|
||||
const invalidate = vi.fn();
|
||||
const app = express();
|
||||
app.use(createLoginRouter({
|
||||
services: {
|
||||
accountAuth: { invalidate },
|
||||
browser: { getPage },
|
||||
nativeLogin: { request: requestNativeLogin },
|
||||
},
|
||||
logger: { info: vi.fn(), error: vi.fn() },
|
||||
} as never));
|
||||
|
||||
const response = await post(app, '/login/start');
|
||||
|
||||
expect(response.status).toBe(202);
|
||||
expect(await response.json()).toEqual({ ok: true, restarting: true });
|
||||
expect(invalidate).toHaveBeenCalledWith('Native Reddit login requested');
|
||||
expect(requestNativeLogin).toHaveBeenCalledOnce();
|
||||
expect(getPage).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user