|
| 1 | +/* |
| 2 | +Copyright 2023 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { API, Messages } from "mailhog"; |
| 18 | +import { Page } from "@playwright/test"; |
| 19 | + |
| 20 | +import { test as base, expect } from "../../element-web-test"; |
| 21 | +import { MatrixAuthenticationService } from "../../plugins/matrix-authentication-service"; |
| 22 | +import { StartHomeserverOpts } from "../../plugins/homeserver"; |
| 23 | + |
| 24 | +export const test = base.extend<{ |
| 25 | + masPrepare: MatrixAuthenticationService; |
| 26 | + mas: MatrixAuthenticationService; |
| 27 | +}>({ |
| 28 | + // There's a bit of a chicken and egg problem between MAS & Synapse where they each need to know how to reach each other |
| 29 | + // so spinning up a MAS is split into the prepare & start stage: prepare mas -> homeserver -> start mas to disentangle this. |
| 30 | + masPrepare: async ({ context }, use) => { |
| 31 | + const mas = new MatrixAuthenticationService(context); |
| 32 | + await mas.prepare(); |
| 33 | + await use(mas); |
| 34 | + }, |
| 35 | + mas: [ |
| 36 | + async ({ masPrepare: mas, homeserver, mailhog }, use, testInfo) => { |
| 37 | + await mas.start(homeserver, mailhog.instance); |
| 38 | + await use(mas); |
| 39 | + await mas.stop(testInfo); |
| 40 | + }, |
| 41 | + { auto: true }, |
| 42 | + ], |
| 43 | + startHomeserverOpts: async ({ masPrepare }, use) => { |
| 44 | + await use({ |
| 45 | + template: "mas-oidc", |
| 46 | + variables: { |
| 47 | + MAS_PORT: masPrepare.port, |
| 48 | + }, |
| 49 | + }); |
| 50 | + }, |
| 51 | + config: async ({ homeserver, startHomeserverOpts, context }, use) => { |
| 52 | + const issuer = `http://localhost:${(startHomeserverOpts as StartHomeserverOpts).variables["MAS_PORT"]}/`; |
| 53 | + const wellKnown = { |
| 54 | + "m.homeserver": { |
| 55 | + base_url: homeserver.config.baseUrl, |
| 56 | + }, |
| 57 | + "org.matrix.msc2965.authentication": { |
| 58 | + issuer, |
| 59 | + account: `${issuer}account`, |
| 60 | + }, |
| 61 | + }; |
| 62 | + |
| 63 | + // Ensure org.matrix.msc2965.authentication is in well-known |
| 64 | + await context.route("https://localhost/.well-known/matrix/client", async (route) => { |
| 65 | + await route.fulfill({ json: wellKnown }); |
| 66 | + }); |
| 67 | + |
| 68 | + await use({ |
| 69 | + default_server_config: wellKnown, |
| 70 | + }); |
| 71 | + }, |
| 72 | +}); |
| 73 | + |
| 74 | +export { expect }; |
| 75 | + |
| 76 | +export async function registerAccountMas( |
| 77 | + page: Page, |
| 78 | + mailhog: API, |
| 79 | + username: string, |
| 80 | + email: string, |
| 81 | + password: string, |
| 82 | +): Promise<void> { |
| 83 | + await expect(page.getByText("Please sign in to continue:")).toBeVisible(); |
| 84 | + |
| 85 | + await page.getByRole("link", { name: "Create Account" }).click(); |
| 86 | + await page.getByRole("textbox", { name: "Username" }).fill(username); |
| 87 | + await page.getByRole("textbox", { name: "Email address" }).fill(email); |
| 88 | + await page.getByRole("textbox", { name: "Password", exact: true }).fill(password); |
| 89 | + await page.getByRole("textbox", { name: "Confirm Password" }).fill(password); |
| 90 | + await page.getByRole("button", { name: "Continue" }).click(); |
| 91 | + |
| 92 | + let messages: Messages; |
| 93 | + await expect(async () => { |
| 94 | + messages = await mailhog.messages(); |
| 95 | + expect(messages.items).toHaveLength(1); |
| 96 | + }).toPass(); |
| 97 | + expect(messages.items[0].to).toEqual(`${username} <${email}>`); |
| 98 | + const [code] = messages.items[0].text.match(/(\d{6})/); |
| 99 | + |
| 100 | + await page.getByRole("textbox", { name: "6-digit code" }).fill(code); |
| 101 | + await page.getByRole("button", { name: "Continue" }).click(); |
| 102 | + await expect(page.getByText("Allow access to your account?")).toBeVisible(); |
| 103 | + await page.getByRole("button", { name: "Continue" }).click(); |
| 104 | +} |
0 commit comments