Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 8e42497

Browse files
justinbotrichvdht3chguy
authored
Fix integration manager get_open_id_token action and add E2E tests (#9520)
* Fix missing await * Fix get openID token action requiring room ID and user ID * Add e2e test for integration manager get openID token * Remove outdated comment * Update test description Co-authored-by: Richard van der Hoff <[email protected]> * Fix type * Fix types again Co-authored-by: Richard van der Hoff <[email protected]> Co-authored-by: Michael Telatynski <[email protected]>
1 parent 663c7e0 commit 8e42497

File tree

2 files changed

+149
-8
lines changed

2 files changed

+149
-8
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
Copyright 2022 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+
/// <reference types="cypress" />
18+
19+
import { SynapseInstance } from "../../plugins/synapsedocker";
20+
import { UserCredentials } from "../../support/login";
21+
22+
const ROOM_NAME = "Integration Manager Test";
23+
const USER_DISPLAY_NAME = "Alice";
24+
25+
const INTEGRATION_MANAGER_TOKEN = "DefinitelySecret_DoNotUseThisForReal";
26+
const INTEGRATION_MANAGER_HTML = `
27+
<html lang="en">
28+
<head>
29+
<title>Fake Integration Manager</title>
30+
</head>
31+
<body>
32+
<button name="Send" id="send-action">Press to send action</button>
33+
<button name="Close" id="close">Press to close</button>
34+
<p id="message-response">No response</p>
35+
<script>
36+
document.getElementById("send-action").onclick = () => {
37+
window.parent.postMessage(
38+
{
39+
action: "get_open_id_token",
40+
},
41+
'*',
42+
);
43+
};
44+
document.getElementById("close").onclick = () => {
45+
window.parent.postMessage(
46+
{
47+
action: "close_scalar",
48+
},
49+
'*',
50+
);
51+
};
52+
// Listen for a postmessage response
53+
window.addEventListener("message", (event) => {
54+
document.getElementById("message-response").innerText = JSON.stringify(event.data);
55+
});
56+
</script>
57+
</body>
58+
</html>
59+
`;
60+
61+
function openIntegrationManager() {
62+
cy.get(".mx_RightPanel_roomSummaryButton").click();
63+
cy.get(".mx_RoomSummaryCard_appsGroup").within(() => {
64+
cy.contains("Add widgets, bridges & bots").click();
65+
});
66+
}
67+
68+
function sendActionFromIntegrationManager(integrationManagerUrl: string) {
69+
cy.accessIframe(`iframe[src*="${integrationManagerUrl}"]`).within(() => {
70+
cy.get("#send-action").should("exist").click();
71+
});
72+
}
73+
74+
describe("Integration Manager: Get OpenID Token", () => {
75+
let testUser: UserCredentials;
76+
let synapse: SynapseInstance;
77+
let integrationManagerUrl: string;
78+
79+
beforeEach(() => {
80+
cy.serveHtmlFile(INTEGRATION_MANAGER_HTML).then(url => {
81+
integrationManagerUrl = url;
82+
});
83+
cy.startSynapse("default").then(data => {
84+
synapse = data;
85+
86+
cy.initTestUser(synapse, USER_DISPLAY_NAME, () => {
87+
cy.window().then(win => {
88+
win.localStorage.setItem("mx_scalar_token", INTEGRATION_MANAGER_TOKEN);
89+
win.localStorage.setItem(`mx_scalar_token_at_${integrationManagerUrl}`, INTEGRATION_MANAGER_TOKEN);
90+
});
91+
}).then(user => {
92+
testUser = user;
93+
});
94+
95+
cy.setAccountData("m.widgets", {
96+
"m.integration_manager": {
97+
content: {
98+
type: "m.integration_manager",
99+
name: "Integration Manager",
100+
url: integrationManagerUrl,
101+
data: {
102+
api_url: integrationManagerUrl,
103+
},
104+
},
105+
id: "integration-manager",
106+
},
107+
}).as("integrationManager");
108+
109+
// Succeed when checking the token is valid
110+
cy.intercept(`${integrationManagerUrl}/account?scalar_token=${INTEGRATION_MANAGER_TOKEN}*`, req => {
111+
req.continue(res => {
112+
return res.send(200, {
113+
user_id: testUser.userId,
114+
});
115+
});
116+
});
117+
118+
cy.createRoom({
119+
name: ROOM_NAME,
120+
}).as("roomId");
121+
});
122+
});
123+
124+
afterEach(() => {
125+
cy.stopSynapse(synapse);
126+
cy.stopWebServers();
127+
});
128+
129+
it("should successfully obtain an openID token", () => {
130+
cy.all([
131+
cy.get<{}>("@integrationManager"),
132+
]).then(() => {
133+
cy.viewRoomByName(ROOM_NAME);
134+
135+
openIntegrationManager();
136+
sendActionFromIntegrationManager(integrationManagerUrl);
137+
138+
cy.accessIframe(`iframe[src*="${integrationManagerUrl}"]`).within(() => {
139+
cy.get("#message-response").should('include.text', 'access_token');
140+
});
141+
});
142+
});
143+
});

src/ScalarMessaging.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ function kickUser(event: MessageEvent<any>, roomId: string, userId: string): voi
376376
});
377377
}
378378

379-
function setWidget(event: MessageEvent<any>, roomId: string): void {
379+
function setWidget(event: MessageEvent<any>, roomId: string | null): void {
380380
const widgetId = event.data.widget_id;
381381
let widgetType = event.data.type;
382382
const widgetUrl = event.data.url;
@@ -435,6 +435,7 @@ function setWidget(event: MessageEvent<any>, roomId: string): void {
435435
} else { // Room widget
436436
if (!roomId) {
437437
sendError(event, _t('Missing roomId.'), null);
438+
return;
438439
}
439440
WidgetUtils.setRoomWidget(roomId, widgetId, widgetType, widgetUrl, widgetName, widgetData, widgetAvatarUrl)
440441
.then(() => {
@@ -651,7 +652,7 @@ function returnStateEvent(event: MessageEvent<any>, roomId: string, eventType: s
651652

652653
async function getOpenIdToken(event: MessageEvent<any>) {
653654
try {
654-
const tokenObject = MatrixClientPeg.get().getOpenIdToken();
655+
const tokenObject = await MatrixClientPeg.get().getOpenIdToken();
655656
sendResponse(event, tokenObject);
656657
} catch (ex) {
657658
logger.warn("Unable to fetch openId token.", ex);
@@ -706,15 +707,15 @@ const onMessage = function(event: MessageEvent<any>): void {
706707

707708
if (!roomId) {
708709
// These APIs don't require roomId
709-
// Get and set user widgets (not associated with a specific room)
710-
// If roomId is specified, it must be validated, so room-based widgets agreed
711-
// handled further down.
712710
if (event.data.action === Action.GetWidgets) {
713711
getWidgets(event, null);
714712
return;
715713
} else if (event.data.action === Action.SetWidget) {
716714
setWidget(event, null);
717715
return;
716+
} else if (event.data.action === Action.GetOpenIdToken) {
717+
getOpenIdToken(event);
718+
return;
718719
} else {
719720
sendError(event, _t('Missing room_id in request'));
720721
return;
@@ -776,9 +777,6 @@ const onMessage = function(event: MessageEvent<any>): void {
776777
case Action.SetBotPower:
777778
setBotPower(event, roomId, userId, event.data.level, event.data.ignoreIfGreater);
778779
break;
779-
case Action.GetOpenIdToken:
780-
getOpenIdToken(event);
781-
break;
782780
default:
783781
logger.warn("Unhandled postMessage event with action '" + event.data.action +"'");
784782
break;

0 commit comments

Comments
 (0)