From 2adec303e13d2ceff6d5291ebf6d4b9791840b4b Mon Sep 17 00:00:00 2001 From: Richard Tan Date: Tue, 19 Nov 2024 18:41:15 +0800 Subject: [PATCH] Fix matrix tests --- .../components/operator-mode/container.gts | 2 +- packages/matrix/helpers/index.ts | 34 +++++++++++-------- packages/matrix/tests/card-chooser.spec.ts | 2 ++ packages/matrix/tests/create-realm.spec.ts | 5 +++ packages/matrix/tests/forgot-password.spec.ts | 15 ++++++-- packages/matrix/tests/live-cards.spec.ts | 2 ++ .../matrix/tests/login-using-email.spec.ts | 10 ++++++ packages/matrix/tests/login.spec.ts | 12 ++++++- packages/matrix/tests/messages.spec.ts | 10 ++++++ packages/matrix/tests/profile.spec.ts | 10 ++++++ packages/matrix/tests/realm-urls.spec.ts | 10 +++++- .../tests/registration-with-token.spec.ts | 4 +-- .../tests/registration-without-token.spec.ts | 2 +- packages/matrix/tests/room-creation.spec.ts | 12 +++++++ packages/matrix/tests/skills.spec.ts | 10 ++++++ packages/realm-server/main.ts | 3 +- 16 files changed, 119 insertions(+), 24 deletions(-) diff --git a/packages/host/app/components/operator-mode/container.gts b/packages/host/app/components/operator-mode/container.gts index 4411d3101d..391456dfe6 100644 --- a/packages/host/app/components/operator-mode/container.gts +++ b/packages/host/app/components/operator-mode/container.gts @@ -129,7 +129,7 @@ export default class OperatorModeContainer extends Component { return await this.realmServer.fetchUser(); }); - private get isUserInfoLoadings() { + private get isUserInfoLoading() { return this.fetchUserInfo.isLoading; } diff --git a/packages/matrix/helpers/index.ts b/packages/matrix/helpers/index.ts index 946d452a69..e1db9e00b0 100644 --- a/packages/matrix/helpers/index.ts +++ b/packages/matrix/helpers/index.ts @@ -588,19 +588,15 @@ export async function setupUser( username: string, realmServer: IsolatedRealmServer, ) { - let findUser = await realmServer.executeSQL(`SELECT * FROM users`); - await realmServer.executeSQL( `INSERT INTO users (matrix_user_id) VALUES ('${username}')`, ); - - findUser = await realmServer.executeSQL(`SELECT * FROM users`); } export async function setupPayment( - page: Page, username: string, realmServer: IsolatedRealmServer, + page?: Page, ) { // decode the username from base64 const decodedUsername = Buffer.from(username, 'base64').toString('utf8'); @@ -675,17 +671,27 @@ export async function setupPayment( ); // Return url example: https://realms-staging.stack.cards/?from-free-plan-payment-link=true - // extract return url from page.url() // assert return url contains ?from-free-plan-payment-link=true - const currentUrl = new URL(page.url()); - const currentParams = currentUrl.searchParams; - await currentParams.append('from-free-plan-payment-link', 'true'); - const returnUrl = `${currentUrl.origin}${ - currentUrl.pathname - }?${currentParams.toString()}`; - - await page.goto(returnUrl); + if (page) { + const currentUrl = new URL(page.url()); + const currentParams = currentUrl.searchParams; + await currentParams.append('from-free-plan-payment-link', 'true'); + const returnUrl = `${currentUrl.origin}${ + currentUrl.pathname + }?${currentParams.toString()}`; + + await page.goto(returnUrl); + } +} + +export async function setupUserSubscribed( + username: string, + realmServer: IsolatedRealmServer, +) { + const matrixUserId = Buffer.from(username).toString('base64'); + await setupUser(username, realmServer); + await setupPayment(matrixUserId, realmServer); } export async function assertLoggedOut(page: Page) { diff --git a/packages/matrix/tests/card-chooser.spec.ts b/packages/matrix/tests/card-chooser.spec.ts index 75959a7000..2a9d364422 100644 --- a/packages/matrix/tests/card-chooser.spec.ts +++ b/packages/matrix/tests/card-chooser.spec.ts @@ -18,6 +18,7 @@ import { login, registerRealmUsers, showAllCards, + setupUserSubscribed, } from '../helpers'; test.describe('Card Chooser', () => { @@ -31,6 +32,7 @@ test.describe('Card Chooser', () => { async function setupRealms(page: Page) { await clearLocalStorage(page, serverIndexUrl); + await setupUserSubscribed('@user1:localhost', realmServer); await login(page, 'user1', 'pass', { url: serverIndexUrl, skipOpeningAssistant: true, diff --git a/packages/matrix/tests/create-realm.spec.ts b/packages/matrix/tests/create-realm.spec.ts index 368a930cba..28c8960e0e 100644 --- a/packages/matrix/tests/create-realm.spec.ts +++ b/packages/matrix/tests/create-realm.spec.ts @@ -15,6 +15,7 @@ import { createRealm, login, registerRealmUsers, + setupUserSubscribed, } from '../helpers'; test.describe('Create Realm via Dashboard', () => { @@ -43,10 +44,14 @@ test.describe('Create Realm via Dashboard', () => { }) => { let serverIndexUrl = new URL(appURL).origin; await clearLocalStorage(page, serverIndexUrl); + + await setupUserSubscribed('@user1:localhost', realmServer); + await login(page, 'user1', 'pass', { url: serverIndexUrl, skipOpeningAssistant: true, }); + await createRealm(page, 'new-workspace', '1New Workspace'); await page.locator('[data-test-workspace="1New Workspace"]').click(); let newRealmURL = new URL('user1/new-workspace/', serverIndexUrl).href; diff --git a/packages/matrix/tests/forgot-password.spec.ts b/packages/matrix/tests/forgot-password.spec.ts index 8c2bebb5e8..fe2f93a99b 100644 --- a/packages/matrix/tests/forgot-password.spec.ts +++ b/packages/matrix/tests/forgot-password.spec.ts @@ -5,6 +5,10 @@ import { updateUser, type SynapseInstance, } from '../docker/synapse'; +import { + startServer as startRealmServer, + type IsolatedRealmServer, +} from '../helpers/isolated-realm-server'; import { smtpStart, smtpStop } from '../docker/smtp4dev'; import { clearLocalStorage, @@ -14,6 +18,7 @@ import { validateEmailForResetPassword, login, registerRealmUsers, + setupUserSubscribed, } from '../helpers'; import { registerUser, createRegistrationToken } from '../docker/synapse'; @@ -25,19 +30,21 @@ const password = 'mypassword1!'; test.describe('Forgot password', () => { let synapse: SynapseInstance; - + let realmServer: IsolatedRealmServer; test.beforeEach(async ({ page }, testInfo) => { // These tests specifically are pretty slow as there's lots of reloading // Add 30s to the overall test timeout - testInfo.setTimeout(testInfo.timeout + 30000); + test.setTimeout(120_000); synapse = await synapseStart({ template: 'test', }); + await smtpStart(); let admin = await registerUser(synapse, 'admin', 'adminpass', true); await createRegistrationToken(admin.accessToken, REGISTRATION_TOKEN); await registerRealmUsers(synapse); + realmServer = await startRealmServer(); await clearLocalStorage(page); await gotoRegistration(page); await registerUser(synapse, username, password); @@ -50,9 +57,12 @@ test.describe('Forgot password', () => { test.afterEach(async () => { await synapseStop(synapse.synapseId); await smtpStop(); + await realmServer.stop(); }); test('It can reset password', async ({ page }) => { + await setupUserSubscribed('@user1:localhost', realmServer); + await gotoForgotPassword(page); await expect( @@ -108,6 +118,7 @@ test.describe('Forgot password', () => { await resetPasswordPage.locator('[data-test-back-to-login-btn]').click(); await login(resetPasswordPage, 'user1', 'mypassword2!'); + await assertLoggedIn(resetPasswordPage); }); diff --git a/packages/matrix/tests/live-cards.spec.ts b/packages/matrix/tests/live-cards.spec.ts index d435426718..3c8b3507bf 100644 --- a/packages/matrix/tests/live-cards.spec.ts +++ b/packages/matrix/tests/live-cards.spec.ts @@ -18,6 +18,7 @@ import { login, registerRealmUsers, showAllCards, + setupUserSubscribed, } from '../helpers'; test.describe('Live Cards', () => { @@ -37,6 +38,7 @@ test.describe('Live Cards', () => { await registerRealmUsers(synapse); realmServer = await startRealmServer(); await registerUser(synapse, 'user1', 'pass'); + await setupUserSubscribed('@user1:localhost', realmServer); }); test.afterEach(async () => { diff --git a/packages/matrix/tests/login-using-email.spec.ts b/packages/matrix/tests/login-using-email.spec.ts index c689032571..f1cad03714 100644 --- a/packages/matrix/tests/login-using-email.spec.ts +++ b/packages/matrix/tests/login-using-email.spec.ts @@ -5,6 +5,10 @@ import { updateUser, type SynapseInstance, } from '../docker/synapse'; +import { + startServer as startRealmServer, + type IsolatedRealmServer, +} from '../helpers/isolated-realm-server'; import { smtpStart, smtpStop } from '../docker/smtp4dev'; import { openRoot, @@ -12,6 +16,7 @@ import { gotoRegistration, assertLoggedIn, registerRealmUsers, + setupUserSubscribed, } from '../helpers'; import { registerUser, createRegistrationToken } from '../docker/synapse'; @@ -19,8 +24,10 @@ const REGISTRATION_TOKEN = 'abc123'; test.describe('Login using email', () => { let synapse: SynapseInstance; + let realmServer: IsolatedRealmServer; test.beforeEach(async ({ page }) => { + test.setTimeout(120_000); synapse = await synapseStart({ template: 'test', }); @@ -29,6 +36,7 @@ test.describe('Login using email', () => { let admin = await registerUser(synapse, 'admin', 'adminpass', true); await createRegistrationToken(admin.accessToken, REGISTRATION_TOKEN); await registerRealmUsers(synapse); + realmServer = await startRealmServer(); await clearLocalStorage(page); await gotoRegistration(page); await registerUser(synapse, 'user1', 'mypassword1!'); @@ -36,11 +44,13 @@ test.describe('Login using email', () => { emailAddresses: ['user1@example.com'], displayname: 'Test User', }); + await setupUserSubscribed('@user1:localhost', realmServer); }); test.afterEach(async () => { await synapseStop(synapse.synapseId); await smtpStop(); + await realmServer.stop(); }); test('Login using email', async ({ page }) => { diff --git a/packages/matrix/tests/login.spec.ts b/packages/matrix/tests/login.spec.ts index 2f6f66492d..2b2203bbbc 100644 --- a/packages/matrix/tests/login.spec.ts +++ b/packages/matrix/tests/login.spec.ts @@ -5,6 +5,10 @@ import { synapseStop, type SynapseInstance, } from '../docker/synapse'; +import { + startServer as startRealmServer, + type IsolatedRealmServer, +} from '../helpers/isolated-realm-server'; import { clearLocalStorage, assertLoggedIn, @@ -14,6 +18,7 @@ import { openRoot, registerRealmUsers, testHost, + setupUserSubscribed, } from '../helpers'; import jwt from 'jsonwebtoken'; @@ -21,16 +26,21 @@ const REALM_SECRET_SEED = "shhh! it's a secret"; test.describe('Login', () => { let synapse: SynapseInstance; + let realmServer: IsolatedRealmServer; + test.beforeEach(async ({ page }, testInfo) => { // These tests specifically are pretty slow as there's lots of reloading // Add 30s to the overall test timeout - testInfo.setTimeout(testInfo.timeout + 30000); + test.setTimeout(120_000); synapse = await synapseStart(); await registerRealmUsers(synapse); + realmServer = await startRealmServer(); await registerUser(synapse, 'user1', 'pass'); await clearLocalStorage(page); + await setupUserSubscribed('@user1:localhost', realmServer); }); test.afterEach(async () => { + await realmServer.stop(); await synapseStop(synapse.synapseId); }); diff --git a/packages/matrix/tests/messages.spec.ts b/packages/matrix/tests/messages.spec.ts index dce67d07a9..c3fc1fd562 100644 --- a/packages/matrix/tests/messages.spec.ts +++ b/packages/matrix/tests/messages.spec.ts @@ -17,23 +17,33 @@ import { getRoomEvents, setupTwoStackItems, showAllCards, + setupUserSubscribed, } from '../helpers'; import { synapseStart, synapseStop, type SynapseInstance, } from '../docker/synapse'; +import { + startServer as startRealmServer, + type IsolatedRealmServer, +} from '../helpers/isolated-realm-server'; test.describe('Room messages', () => { let synapse: SynapseInstance; + let realmServer: IsolatedRealmServer; let userCred: Credentials; test.beforeEach(async () => { + test.setTimeout(120_000); synapse = await synapseStart(); await registerRealmUsers(synapse); + realmServer = await startRealmServer(); userCred = await registerUser(synapse, 'user1', 'pass'); + await setupUserSubscribed('@user1:localhost', realmServer); }); test.afterEach(async () => { await synapseStop(synapse.synapseId); + await realmServer.stop(); }); test(`it can send a message in a room`, async ({ page }) => { diff --git a/packages/matrix/tests/profile.spec.ts b/packages/matrix/tests/profile.spec.ts index 3cc5ed432f..baabd698ca 100644 --- a/packages/matrix/tests/profile.spec.ts +++ b/packages/matrix/tests/profile.spec.ts @@ -13,11 +13,18 @@ import { assertLoggedOut, assertLoggedIn, registerRealmUsers, + setupUserSubscribed, } from '../helpers'; +import { + startServer as startRealmServer, + type IsolatedRealmServer, +} from '../helpers/isolated-realm-server'; test.describe('Profile', () => { let synapse: SynapseInstance; + let realmServer: IsolatedRealmServer; test.beforeEach(async () => { + test.setTimeout(120_000); synapse = await synapseStart({ template: 'test', }); @@ -27,17 +34,20 @@ test.describe('Profile', () => { await registerRealmUsers(synapse); await registerUser(synapse, 'user1', 'pass'); await registerUser(synapse, 'user0', 'pass'); + realmServer = await startRealmServer(); await updateUser(admin.accessToken, '@user1:localhost', { emailAddresses: ['user1@localhost'], }); await updateUser(admin.accessToken, '@user0:localhost', { emailAddresses: ['user0@localhost'], }); + await setupUserSubscribed('@user1:localhost', realmServer); }); test.afterEach(async () => { await synapseStop(synapse.synapseId); await smtpStop(); + await realmServer.stop(); }); async function gotoProfileSettings(page: Page) { diff --git a/packages/matrix/tests/realm-urls.spec.ts b/packages/matrix/tests/realm-urls.spec.ts index 8087142c72..6f74aac70a 100644 --- a/packages/matrix/tests/realm-urls.spec.ts +++ b/packages/matrix/tests/realm-urls.spec.ts @@ -8,16 +8,22 @@ import { updateUser, } from '../docker/synapse'; import { smtpStart, smtpStop } from '../docker/smtp4dev'; -import { login, registerRealmUsers } from '../helpers'; +import { login, registerRealmUsers, setupUserSubscribed } from '../helpers'; +import { + startServer as startRealmServer, + type IsolatedRealmServer, +} from '../helpers/isolated-realm-server'; test.describe('Realm URLs in Matrix account data', () => { let synapse: SynapseInstance; + let realmServer: IsolatedRealmServer; let user: { accessToken: string }; test.beforeEach(async () => { synapse = await synapseStart({ template: 'test', }); + realmServer = await startRealmServer(); await smtpStart(); let admin = await registerUser(synapse, 'admin', 'adminpass', true); @@ -33,11 +39,13 @@ test.describe('Realm URLs in Matrix account data', () => { 'com.cardstack.boxel.realms', JSON.stringify({ realms: [] }), ); + await setupUserSubscribed('@user1:localhost', realmServer); }); test.afterEach(async () => { await synapseStop(synapse.synapseId); await smtpStop(); + await realmServer.stop(); }); test('active realms are determined by account data', async ({ page }) => { diff --git a/packages/matrix/tests/registration-with-token.spec.ts b/packages/matrix/tests/registration-with-token.spec.ts index 14acbc1ff8..3a0adb0c0b 100644 --- a/packages/matrix/tests/registration-with-token.spec.ts +++ b/packages/matrix/tests/registration-with-token.spec.ts @@ -112,7 +112,7 @@ test.describe('User Registration w/ Token - isolated realm server', () => { const matrixUserId = Buffer.from('@user1:localhost').toString('base64'); await assertPaymentSetup(page, matrixUserId); - await setupPayment(page, matrixUserId, realmServer); + await setupPayment(matrixUserId, realmServer, page); await assertLoggedIn(page, { email: 'user1@example.com', displayName: 'Test User', @@ -205,7 +205,7 @@ test.describe('User Registration w/ Token - isolated realm server', () => { Buffer.from('@user2:localhost').toString('base64'); await assertPaymentSetup(page, user2MatrixUserId); - await setupPayment(page, user2MatrixUserId, realmServer); + await setupPayment(user2MatrixUserId, realmServer, page); await assertLoggedIn(page, { userId: '@user2:localhost', diff --git a/packages/matrix/tests/registration-without-token.spec.ts b/packages/matrix/tests/registration-without-token.spec.ts index a9c20eb6ac..c0c48aa6a6 100644 --- a/packages/matrix/tests/registration-without-token.spec.ts +++ b/packages/matrix/tests/registration-without-token.spec.ts @@ -72,7 +72,7 @@ test.describe('User Registration w/o Token', () => { const matrixUserId = Buffer.from('@user1:localhost').toString('base64'); await assertPaymentSetup(page, matrixUserId); - await setupPayment(page, matrixUserId, realmServer); + await setupPayment(matrixUserId, realmServer, page); await assertLoggedIn(page); }); }); diff --git a/packages/matrix/tests/room-creation.spec.ts b/packages/matrix/tests/room-creation.spec.ts index 3e4d98c23b..c42f41b25b 100644 --- a/packages/matrix/tests/room-creation.spec.ts +++ b/packages/matrix/tests/room-creation.spec.ts @@ -5,6 +5,10 @@ import { synapseStop, type SynapseInstance, } from '../docker/synapse'; +import { + startServer as startRealmServer, + type IsolatedRealmServer, +} from '../helpers/isolated-realm-server'; import { login, logout, @@ -22,23 +26,31 @@ import { isInRoom, getRoomsFromSync, initialRoomName, + setupUserSubscribed, } from '../helpers'; test.describe('Room creation', () => { let synapse: SynapseInstance; + let realmServer: IsolatedRealmServer; test.beforeEach(async ({ page }) => { + test.setTimeout(120_000); synapse = await synapseStart(); await registerRealmUsers(synapse); + realmServer = await startRealmServer(); await registerUser(synapse, 'user1', 'pass'); await registerUser(synapse, 'user2', 'pass'); await registerUser(synapse, 'xuser', 'pass'); await clearLocalStorage(page); + await setupUserSubscribed('@user1:localhost', realmServer); + await setupUserSubscribed('@user2:localhost', realmServer); + await setupUserSubscribed('@xuser:localhost', realmServer); }); test.afterEach(async ({ page }) => { await page.pause(); await clearLocalStorage(page); await synapseStop(synapse.synapseId); + await realmServer.stop(); }); test('it can create a room', async ({ page }) => { diff --git a/packages/matrix/tests/skills.spec.ts b/packages/matrix/tests/skills.spec.ts index 0c69df2cc3..42f46a0245 100644 --- a/packages/matrix/tests/skills.spec.ts +++ b/packages/matrix/tests/skills.spec.ts @@ -13,20 +13,30 @@ import { isInRoom, registerRealmUsers, getRoomEvents, + setupUserSubscribed, } from '../helpers'; import { synapseStart, synapseStop, type SynapseInstance, } from '../docker/synapse'; +import { + startServer as startRealmServer, + type IsolatedRealmServer, +} from '../helpers/isolated-realm-server'; test.describe('Skills', () => { let synapse: SynapseInstance; + let realmServer: IsolatedRealmServer; test.beforeEach(async () => { + test.setTimeout(120_000); synapse = await synapseStart(); await registerRealmUsers(synapse); + realmServer = await startRealmServer(); await registerUser(synapse, 'user1', 'pass'); await registerUser(synapse, 'user2', 'pass'); + await setupUserSubscribed('@user1:localhost', realmServer); + await setupUserSubscribed('@user2:localhost', realmServer); }); test.afterEach(async () => { await synapseStop(synapse.synapseId); diff --git a/packages/realm-server/main.ts b/packages/realm-server/main.ts index 1f3e3c46dd..aa7745d41c 100644 --- a/packages/realm-server/main.ts +++ b/packages/realm-server/main.ts @@ -278,8 +278,7 @@ let autoMigrate = migrateDB || undefined; ); } else if ( typeof message === 'string' && - message.startsWith('execute-sql:') && - registrationSecretDeferred + message.startsWith('execute-sql:') ) { let sql = message.substring('execute-sql:'.length); dbAdapter