Files
RedditBot/tests/reddit-session-importer.test.ts
T

77 lines
3.3 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest';
import type { Cookie } from 'playwright';
import { RedditSessionImporter } from '../src/services/reddit-session-importer.js';
const cookies: Cookie[] = [{
name: 'reddit_session', value: 'secret', domain: '.reddit.com', path: '/', expires: -1,
httpOnly: true, secure: true, sameSite: 'Lax',
}];
const credentials = {
homeserver: 'https://matrix.redditspace.com',
accessToken: 'matrix-secret',
userId: '@t2_bot:reddit.com',
capturedAt: 1_700_000_000_000,
};
describe('RedditSessionImporter', () => {
it('verifies both parts of the dump before activating the account', async () => {
const browser = {
importRedditCookies: vi.fn(),
isSessionValid: vi.fn(async () => true),
getAuthenticatedIdentity: vi.fn(async () => ({ username: 'bot-account', accountId: 't2_bot' })),
};
const matrix = { whoami: vi.fn(async () => credentials.userId) };
const chat = { setMatrixClient: vi.fn() };
const save = vi.fn();
const accountAuth = { markAuthenticated: vi.fn(), invalidate: vi.fn() };
const importer = new RedditSessionImporter(browser, chat as never, vi.fn(() => matrix as never), save, accountAuth as never);
await importer.import(cookies, credentials);
expect(matrix.whoami).toHaveBeenCalledOnce();
expect(browser.importRedditCookies).toHaveBeenCalledWith(cookies);
expect(chat.setMatrixClient).toHaveBeenCalledWith(matrix);
expect(save).toHaveBeenCalledWith(credentials);
expect(accountAuth.markAuthenticated).toHaveBeenCalledWith({
accountId: 't2_bot',
redditUsername: 'bot-account',
matrixUserId: credentials.userId,
});
});
it('rejects a dump whose Reddit cookies and Matrix token belong to different accounts', async () => {
const browser = {
importRedditCookies: vi.fn(),
isSessionValid: vi.fn(async () => true),
getAuthenticatedIdentity: vi.fn(async () => ({ username: 'other-account', accountId: 't2_other' })),
};
const matrix = { whoami: vi.fn(async () => credentials.userId) };
const chat = { setMatrixClient: vi.fn() };
const save = vi.fn();
const accountAuth = { markAuthenticated: vi.fn(), invalidate: vi.fn() };
const importer = new RedditSessionImporter(browser, chat as never, vi.fn(() => matrix as never), save, accountAuth as never);
await expect(importer.import(cookies, credentials)).rejects.toThrow(/does not match Matrix account/);
expect(accountAuth.invalidate).toHaveBeenCalledWith(expect.stringMatching(/does not match Matrix account/));
expect(save).not.toHaveBeenCalled();
expect(chat.setMatrixClient).not.toHaveBeenCalled();
expect(accountAuth.markAuthenticated).not.toHaveBeenCalled();
});
it('rejects non-Reddit Matrix homeservers without making a request', async () => {
const createMatrix = vi.fn();
const importer = new RedditSessionImporter(
{ importRedditCookies: vi.fn(), isSessionValid: vi.fn(), getAuthenticatedIdentity: vi.fn() },
{ setMatrixClient: vi.fn() } as never,
createMatrix,
vi.fn(),
{ markAuthenticated: vi.fn(), invalidate: vi.fn() } as never,
);
await expect(importer.import(cookies, { ...credentials, homeserver: 'https://attacker.example' }))
.rejects.toThrow(/homeserver/i);
expect(createMatrix).not.toHaveBeenCalled();
});
});