2023-12-12 18:26:08 +01:00
|
|
|
/*
|
2024-09-09 15:57:16 +02:00
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
Copyright 2022, 2023 The Matrix.org Foundation C.I.C.
|
2023-12-12 18:26:08 +01:00
|
|
|
|
2024-09-09 15:57:16 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2023-12-12 18:26:08 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
import type { Page } from "@playwright/test";
|
|
|
|
import { test, expect } from "../../element-web-test";
|
|
|
|
import { openIntegrationManager } from "./utils";
|
|
|
|
|
|
|
|
const ROOM_NAME = "Integration Manager Test";
|
|
|
|
|
|
|
|
const INTEGRATION_MANAGER_TOKEN = "DefinitelySecret_DoNotUseThisForReal";
|
|
|
|
const INTEGRATION_MANAGER_HTML = `
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<title>Fake Integration Manager</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<button name="Send" id="send-action">Press to send action</button>
|
|
|
|
<button name="Close" id="close">Press to close</button>
|
|
|
|
<p id="message-response">No response</p>
|
|
|
|
<script>
|
|
|
|
document.getElementById("send-action").onclick = () => {
|
|
|
|
window.parent.postMessage(
|
|
|
|
{
|
|
|
|
action: "get_open_id_token",
|
|
|
|
},
|
|
|
|
'*',
|
|
|
|
);
|
|
|
|
};
|
|
|
|
document.getElementById("close").onclick = () => {
|
|
|
|
window.parent.postMessage(
|
|
|
|
{
|
|
|
|
action: "close_scalar",
|
|
|
|
},
|
|
|
|
'*',
|
|
|
|
);
|
|
|
|
};
|
|
|
|
// Listen for a postmessage response
|
|
|
|
window.addEventListener("message", (event) => {
|
|
|
|
document.getElementById("message-response").innerText = JSON.stringify(event.data);
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`;
|
|
|
|
|
|
|
|
async function sendActionFromIntegrationManager(page: Page, integrationManagerUrl: string) {
|
|
|
|
const iframe = page.frameLocator(`iframe[src*="${integrationManagerUrl}"]`);
|
|
|
|
await iframe.getByRole("button", { name: "Press to send action" }).click();
|
|
|
|
}
|
|
|
|
|
|
|
|
test.describe("Integration Manager: Get OpenID Token", () => {
|
|
|
|
test.use({
|
|
|
|
displayName: "Alice",
|
|
|
|
room: async ({ user, app }, use) => {
|
|
|
|
const roomId = await app.client.createRoom({
|
|
|
|
name: ROOM_NAME,
|
|
|
|
});
|
|
|
|
await use({ roomId });
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
let integrationManagerUrl: string;
|
|
|
|
test.beforeEach(async ({ page, webserver }) => {
|
|
|
|
integrationManagerUrl = webserver.start(INTEGRATION_MANAGER_HTML);
|
|
|
|
|
|
|
|
await page.addInitScript(
|
|
|
|
({ token, integrationManagerUrl }) => {
|
|
|
|
window.localStorage.setItem("mx_scalar_token", token);
|
|
|
|
window.localStorage.setItem(`mx_scalar_token_at_${integrationManagerUrl}`, token);
|
|
|
|
},
|
|
|
|
{
|
|
|
|
token: INTEGRATION_MANAGER_TOKEN,
|
|
|
|
integrationManagerUrl,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test.beforeEach(async ({ page, user, app, room }) => {
|
|
|
|
await app.client.setAccountData("m.widgets", {
|
|
|
|
"m.integration_manager": {
|
|
|
|
content: {
|
|
|
|
type: "m.integration_manager",
|
|
|
|
name: "Integration Manager",
|
|
|
|
url: integrationManagerUrl,
|
|
|
|
data: {
|
|
|
|
api_url: integrationManagerUrl,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
id: "integration-manager",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// Succeed when checking the token is valid
|
|
|
|
await page.route(
|
|
|
|
`${integrationManagerUrl}/account?scalar_token=${INTEGRATION_MANAGER_TOKEN}*`,
|
|
|
|
async (route) => {
|
|
|
|
await route.fulfill({
|
|
|
|
json: {
|
|
|
|
user_id: user.userId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
await app.viewRoomByName(ROOM_NAME);
|
|
|
|
});
|
|
|
|
|
2024-07-23 12:56:25 +02:00
|
|
|
test("should successfully obtain an openID token", async ({ page, app }) => {
|
|
|
|
await openIntegrationManager(app);
|
2023-12-12 18:26:08 +01:00
|
|
|
await sendActionFromIntegrationManager(page, integrationManagerUrl);
|
|
|
|
|
|
|
|
const iframe = page.frameLocator(`iframe[src*="${integrationManagerUrl}"]`);
|
|
|
|
await expect(iframe.locator("#message-response").getByText(/access_token/)).toBeVisible();
|
|
|
|
});
|
|
|
|
});
|