57 lines
2.2 KiB
TypeScript
57 lines
2.2 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),
|
|
getAuthenticatedUsername: vi.fn(async () => 'bot-account'),
|
|
};
|
|
const matrix = { whoami: vi.fn(async () => credentials.userId) };
|
|
const chat = { setMatrixClient: vi.fn() };
|
|
const save = vi.fn();
|
|
const accountAuth = { markAuthenticated: 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: credentials.userId,
|
|
redditUsername: 'bot-account',
|
|
matrixUserId: credentials.userId,
|
|
});
|
|
});
|
|
|
|
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(), getAuthenticatedUsername: vi.fn() },
|
|
{ setMatrixClient: vi.fn() } as never,
|
|
createMatrix,
|
|
vi.fn(),
|
|
{ markAuthenticated: vi.fn() } as never,
|
|
);
|
|
|
|
await expect(importer.import(cookies, { ...credentials, homeserver: 'https://attacker.example' }))
|
|
.rejects.toThrow(/homeserver/i);
|
|
expect(createMatrix).not.toHaveBeenCalled();
|
|
});
|
|
});
|