Skip to content

Commit

Permalink
Fix matrix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
richardhjtan committed Nov 19, 2024
1 parent 1d3e105 commit 2adec30
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/host/app/components/operator-mode/container.gts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export default class OperatorModeContainer extends Component<Signature> {
return await this.realmServer.fetchUser();
});

private get isUserInfoLoadings() {
private get isUserInfoLoading() {
return this.fetchUserInfo.isLoading;
}

Expand Down
34 changes: 20 additions & 14 deletions packages/matrix/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions packages/matrix/tests/card-chooser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
login,
registerRealmUsers,
showAllCards,
setupUserSubscribed,
} from '../helpers';

test.describe('Card Chooser', () => {
Expand All @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions packages/matrix/tests/create-realm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
createRealm,
login,
registerRealmUsers,
setupUserSubscribed,
} from '../helpers';

test.describe('Create Realm via Dashboard', () => {
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 13 additions & 2 deletions packages/matrix/tests/forgot-password.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -14,6 +18,7 @@ import {
validateEmailForResetPassword,
login,
registerRealmUsers,
setupUserSubscribed,
} from '../helpers';
import { registerUser, createRegistrationToken } from '../docker/synapse';

Expand All @@ -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);
Expand All @@ -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(
Expand Down Expand Up @@ -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);
});

Expand Down
2 changes: 2 additions & 0 deletions packages/matrix/tests/live-cards.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
login,
registerRealmUsers,
showAllCards,
setupUserSubscribed,
} from '../helpers';

test.describe('Live Cards', () => {
Expand All @@ -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 () => {
Expand Down
10 changes: 10 additions & 0 deletions packages/matrix/tests/login-using-email.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,29 @@ 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,
clearLocalStorage,
gotoRegistration,
assertLoggedIn,
registerRealmUsers,
setupUserSubscribed,
} from '../helpers';
import { registerUser, createRegistrationToken } from '../docker/synapse';

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',
});
Expand All @@ -29,18 +36,21 @@ 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!');
await updateUser(admin.accessToken, '@user1:localhost', {
emailAddresses: ['[email protected]'],
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 }) => {
Expand Down
12 changes: 11 additions & 1 deletion packages/matrix/tests/login.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -14,23 +18,29 @@ import {
openRoot,
registerRealmUsers,
testHost,
setupUserSubscribed,
} from '../helpers';
import jwt from 'jsonwebtoken';

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);
});

Expand Down
10 changes: 10 additions & 0 deletions packages/matrix/tests/messages.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand Down
10 changes: 10 additions & 0 deletions packages/matrix/tests/profile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});
Expand All @@ -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) {
Expand Down
10 changes: 9 additions & 1 deletion packages/matrix/tests/realm-urls.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 }) => {
Expand Down
Loading

0 comments on commit 2adec30

Please sign in to comment.