Deflake more tests

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski
2025-01-13 08:22:20 +00:00
parent c1c98d37fc
commit 5e9066ec8c
6 changed files with 42 additions and 10 deletions

View File

@@ -92,6 +92,9 @@ test.use({
}, },
}, },
credentials: async ({ context, homeserver }, use) => { credentials: async ({ context, homeserver }, use) => {
// Restart the homeserver to wipe its in-memory db so we can reuse the same user ID without cross-signing prompts
await homeserver.restart();
const displayName = "Dave"; const displayName = "Dave";
const credentials = await homeserver.registerUser(username, password, displayName); const credentials = await homeserver.registerUser(username, password, displayName);
console.log(`Registered test user @user:localhost with displayname ${displayName}`); console.log(`Registered test user @user:localhost with displayname ${displayName}`);
@@ -100,9 +103,6 @@ test.use({
...credentials, ...credentials,
displayName, displayName,
}); });
// Restart the homeserver to wipe its in-memory db so we can reuse the same user ID without cross-signing prompts
await homeserver.restart();
}, },
}); });

View File

@@ -81,6 +81,11 @@ const test = base.extend<{
}); });
await use({ name, roomId }); await use({ name, roomId });
}, },
credentials: async ({ credentials, homeserver }, use) => {
// Restart the homeserver to wipe its in-memory db so we can purge the user_directory of users
await homeserver.restart();
await use(credentials);
},
}); });
test.describe("Spotlight", () => { test.describe("Spotlight", () => {

View File

@@ -9,11 +9,11 @@ Please see LICENSE files in the repository root for full details.
import { Fixtures } from "@playwright/test"; import { Fixtures } from "@playwright/test";
import { TestContainers } from "testcontainers"; import { TestContainers } from "testcontainers";
import { Services } from "../../../services.ts"; import { Services, TestFixtures } from "../../../services.ts";
import { OAuthServer } from "../../oauth_server"; import { OAuthServer } from "../../oauth_server";
export const legacyOAuthHomeserver: Fixtures<{}, Services> = { export const legacyOAuthHomeserver: Fixtures<TestFixtures, Services, TestFixtures> = {
_homeserver: [ _oAuthServer: [
async ({ _homeserver: container }, use) => { async ({ _homeserver: container }, use) => {
const server = new OAuthServer(); const server = new OAuthServer();
const port = server.start(); const port = server.start();
@@ -43,9 +43,13 @@ export const legacyOAuthHomeserver: Fixtures<{}, Services> = {
}, },
], ],
}); });
await use(container); await use(server);
server.stop(); server.stop();
}, },
{ scope: "worker" }, { scope: "worker" },
], ],
oAuthServer: async ({ _oAuthServer }, use, testInfo) => {
_oAuthServer.onTestStarted(testInfo);
await use(_oAuthServer);
},
}; };

View File

@@ -9,12 +9,21 @@ Please see LICENSE files in the repository root for full details.
import http from "http"; import http from "http";
import express from "express"; import express from "express";
import { AddressInfo } from "net"; import { AddressInfo } from "net";
import { TestInfo } from "@playwright/test";
import { randB64Bytes } from "../utils/rand.ts";
export class OAuthServer { export class OAuthServer {
private server?: http.Server; private server?: http.Server;
private sub?: string;
public onTestStarted(testInfo: TestInfo): void {
this.sub = testInfo.testId;
}
public start(): number { public start(): number {
if (this.server) this.stop(); if (this.server) this.stop();
const token = randB64Bytes(16);
const app = express(); const app = express();
@@ -28,7 +37,7 @@ export class OAuthServer {
const code = req.body.code; const code = req.body.code;
if (code === "valid_auth_code") { if (code === "valid_auth_code") {
res.send({ res.send({
access_token: "oauth_access_token", access_token: token,
token_type: "Bearer", token_type: "Bearer",
expires_in: "3600", expires_in: "3600",
}); });
@@ -43,7 +52,7 @@ export class OAuthServer {
// return an OAuth2 user info object // return an OAuth2 user info object
res.send({ res.send({
sub: "alice", sub: this.sub,
name: "Alice", name: "Alice",
}); });
}); });

View File

@@ -15,9 +15,12 @@ import { ContainerLogger } from "./testcontainers/utils.ts";
import { StartedMatrixAuthenticationServiceContainer } from "./testcontainers/mas.ts"; import { StartedMatrixAuthenticationServiceContainer } from "./testcontainers/mas.ts";
import { HomeserverContainer, StartedHomeserverContainer } from "./testcontainers/HomeserverContainer.ts"; import { HomeserverContainer, StartedHomeserverContainer } from "./testcontainers/HomeserverContainer.ts";
import { MailhogContainer, StartedMailhogContainer } from "./testcontainers/mailhog.ts"; import { MailhogContainer, StartedMailhogContainer } from "./testcontainers/mailhog.ts";
import { OAuthServer } from "./plugins/oauth_server";
interface TestFixtures { export interface TestFixtures {
mailhogClient: mailhog.API; mailhogClient: mailhog.API;
// Set in legacyOAuthHomeserver only
oAuthServer?: OAuthServer;
} }
export interface Services { export interface Services {
@@ -31,6 +34,9 @@ export interface Services {
_homeserver: HomeserverContainer<any>; _homeserver: HomeserverContainer<any>;
homeserver: StartedHomeserverContainer; homeserver: StartedHomeserverContainer;
mas?: StartedMatrixAuthenticationServiceContainer; mas?: StartedMatrixAuthenticationServiceContainer;
// Set in legacyOAuthHomeserver only
_oAuthServer?: OAuthServer;
} }
export const test = base.extend<TestFixtures, Services>({ export const test = base.extend<TestFixtures, Services>({

View File

@@ -162,6 +162,14 @@ const DEFAULT_CONFIG = {
access_token_ttl: 300, access_token_ttl: 300,
compat_token_ttl: 300, compat_token_ttl: 300,
}, },
rate_limiting: {
login: {
burst: 1000,
},
registration: {
burst: 1000,
},
},
}; };
export class MatrixAuthenticationServiceContainer extends GenericContainer { export class MatrixAuthenticationServiceContainer extends GenericContainer {