2022-11-15 18:03:47 +01:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/// <reference types="cypress" />
|
|
|
|
|
2023-01-11 00:29:56 +01:00
|
|
|
import { HomeserverInstance } from "../../plugins/utils/homeserver";
|
2022-11-15 18:03:47 +01:00
|
|
|
import { UserCredentials } from "../../support/login";
|
|
|
|
|
|
|
|
const ROOM_NAME = "Integration Manager Test";
|
|
|
|
const USER_DISPLAY_NAME = "Alice";
|
|
|
|
|
|
|
|
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>
|
|
|
|
`;
|
|
|
|
|
|
|
|
function openIntegrationManager() {
|
2023-04-13 08:27:39 +02:00
|
|
|
cy.findByRole("tab", { name: "Room info" }).click();
|
|
|
|
cy.findByRole("button", { name: "Add widgets, bridges & bots" }).click();
|
2022-11-15 18:03:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function sendActionFromIntegrationManager(integrationManagerUrl: string) {
|
|
|
|
cy.accessIframe(`iframe[src*="${integrationManagerUrl}"]`).within(() => {
|
2023-04-13 08:27:39 +02:00
|
|
|
cy.findByRole("button", { name: "Press to send action" }).should("exist").click();
|
2022-11-15 18:03:47 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
describe("Integration Manager: Get OpenID Token", () => {
|
|
|
|
let testUser: UserCredentials;
|
2023-01-11 00:29:56 +01:00
|
|
|
let homeserver: HomeserverInstance;
|
2022-11-15 18:03:47 +01:00
|
|
|
let integrationManagerUrl: string;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-12-12 12:24:14 +01:00
|
|
|
cy.serveHtmlFile(INTEGRATION_MANAGER_HTML).then((url) => {
|
2022-11-15 18:03:47 +01:00
|
|
|
integrationManagerUrl = url;
|
|
|
|
});
|
2023-01-11 00:29:56 +01:00
|
|
|
cy.startHomeserver("default").then((data) => {
|
|
|
|
homeserver = data;
|
2022-11-15 18:03:47 +01:00
|
|
|
|
2023-01-11 00:29:56 +01:00
|
|
|
cy.initTestUser(homeserver, USER_DISPLAY_NAME, () => {
|
2022-12-12 12:24:14 +01:00
|
|
|
cy.window().then((win) => {
|
2022-11-15 18:03:47 +01:00
|
|
|
win.localStorage.setItem("mx_scalar_token", INTEGRATION_MANAGER_TOKEN);
|
|
|
|
win.localStorage.setItem(`mx_scalar_token_at_${integrationManagerUrl}`, INTEGRATION_MANAGER_TOKEN);
|
|
|
|
});
|
2022-12-12 12:24:14 +01:00
|
|
|
}).then((user) => {
|
2022-11-15 18:03:47 +01:00
|
|
|
testUser = user;
|
|
|
|
});
|
|
|
|
|
|
|
|
cy.setAccountData("m.widgets", {
|
|
|
|
"m.integration_manager": {
|
|
|
|
content: {
|
|
|
|
type: "m.integration_manager",
|
|
|
|
name: "Integration Manager",
|
|
|
|
url: integrationManagerUrl,
|
|
|
|
data: {
|
|
|
|
api_url: integrationManagerUrl,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
id: "integration-manager",
|
|
|
|
},
|
|
|
|
}).as("integrationManager");
|
|
|
|
|
|
|
|
// Succeed when checking the token is valid
|
2022-12-12 12:24:14 +01:00
|
|
|
cy.intercept(`${integrationManagerUrl}/account?scalar_token=${INTEGRATION_MANAGER_TOKEN}*`, (req) => {
|
|
|
|
req.continue((res) => {
|
2022-11-15 18:03:47 +01:00
|
|
|
return res.send(200, {
|
|
|
|
user_id: testUser.userId,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
cy.createRoom({
|
|
|
|
name: ROOM_NAME,
|
|
|
|
}).as("roomId");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2023-01-11 00:29:56 +01:00
|
|
|
cy.stopHomeserver(homeserver);
|
2022-11-15 18:03:47 +01:00
|
|
|
cy.stopWebServers();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should successfully obtain an openID token", () => {
|
2022-12-12 12:24:14 +01:00
|
|
|
cy.all([cy.get<{}>("@integrationManager")]).then(() => {
|
2022-11-15 18:03:47 +01:00
|
|
|
cy.viewRoomByName(ROOM_NAME);
|
|
|
|
|
|
|
|
openIntegrationManager();
|
|
|
|
sendActionFromIntegrationManager(integrationManagerUrl);
|
|
|
|
|
|
|
|
cy.accessIframe(`iframe[src*="${integrationManagerUrl}"]`).within(() => {
|
2023-04-13 08:27:39 +02:00
|
|
|
cy.get("#message-response").within(() => {
|
|
|
|
cy.findByText(/access_token/);
|
|
|
|
});
|
2022-11-15 18:03:47 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|