Convert sticker end-to-end tests to Cypress (#8807)
* Convert sticker end-to-end tests to Cypress Reference materials: * https://www.cypress.io/blog/2020/02/12/working-with-iframes-in-cypress/ * https://github.com/cypress-io/cypress/issues/136 * https://docs.cypress.io/api/commands/origin#Other-limitations Ideally we'd be able to use `cy.origin()` to jump into the iframe, but it's explicitly not supported. Instead we disable web security as instructed by cypress because it's our only reasonable option here. Thankfully, disabling web security doesn't appear to remove the crypto libraries from the browser so we can still function in that respect. Rationale for why we can't just serve the sticker picker off the app domain is included in the code. * Appease the linter * More linter appeasement
This commit is contained in:
parent
5b149bcffd
commit
4171c008a4
11 changed files with 371 additions and 154 deletions
|
@ -7,5 +7,6 @@
|
||||||
"retries": {
|
"retries": {
|
||||||
"runMode": 2,
|
"runMode": 2,
|
||||||
"openMode": 0
|
"openMode": 0
|
||||||
}
|
},
|
||||||
|
"chromeWebSecurity": false
|
||||||
}
|
}
|
||||||
|
|
163
cypress/integration/9-widgets/stickers.spec.ts
Normal file
163
cypress/integration/9-widgets/stickers.spec.ts
Normal file
|
@ -0,0 +1,163 @@
|
||||||
|
/*
|
||||||
|
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" />
|
||||||
|
|
||||||
|
import { SynapseInstance } from "../../plugins/synapsedocker";
|
||||||
|
|
||||||
|
const STICKER_PICKER_WIDGET_ID = "fake-sticker-picker";
|
||||||
|
const STICKER_PICKER_WIDGET_NAME = "Fake Stickers";
|
||||||
|
const STICKER_NAME = "Test Sticker";
|
||||||
|
const ROOM_NAME_1 = "Sticker Test";
|
||||||
|
const ROOM_NAME_2 = "Sticker Test Two";
|
||||||
|
const STICKER_MESSAGE = JSON.stringify({
|
||||||
|
action: "m.sticker",
|
||||||
|
api: "fromWidget",
|
||||||
|
data: {
|
||||||
|
name: "teststicker",
|
||||||
|
description: STICKER_NAME,
|
||||||
|
file: "test.png",
|
||||||
|
content: {
|
||||||
|
body: STICKER_NAME,
|
||||||
|
msgtype: "m.sticker",
|
||||||
|
url: "mxc://somewhere",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
requestId: "1",
|
||||||
|
widgetId: STICKER_PICKER_WIDGET_ID,
|
||||||
|
});
|
||||||
|
const WIDGET_HTML = `
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<title>Fake Sticker Picker</title>
|
||||||
|
<script>
|
||||||
|
window.onmessage = ev => {
|
||||||
|
if (ev.data.action === 'capabilities') {
|
||||||
|
window.parent.postMessage(Object.assign({
|
||||||
|
response: {
|
||||||
|
capabilities: ["m.sticker"]
|
||||||
|
},
|
||||||
|
}, ev.data), '*');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<button name="Send" id="sendsticker">Press for sticker</button>
|
||||||
|
<script>
|
||||||
|
document.getElementById('sendsticker').onclick = () => {
|
||||||
|
window.parent.postMessage(${STICKER_MESSAGE}, '*')
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`;
|
||||||
|
|
||||||
|
function openStickerPicker() {
|
||||||
|
cy.get('.mx_MessageComposer_buttonMenu').click();
|
||||||
|
cy.get('#stickersButton').click();
|
||||||
|
}
|
||||||
|
|
||||||
|
function sendStickerFromPicker() {
|
||||||
|
// Note: Until https://github.com/cypress-io/cypress/issues/136 is fixed we will need
|
||||||
|
// to use `chromeWebSecurity: false` in our cypress config. Not even cy.origin() can
|
||||||
|
// break into the iframe for us :(
|
||||||
|
cy.accessIframe(`iframe[title="${STICKER_PICKER_WIDGET_NAME}"]`).within({}, () => {
|
||||||
|
cy.get("#sendsticker").should('exist').click();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Sticker picker should close itself after sending.
|
||||||
|
cy.get(".mx_AppTileFullWidth#stickers").should('not.exist');
|
||||||
|
}
|
||||||
|
|
||||||
|
function expectTimelineSticker(roomId: string) {
|
||||||
|
// Make sure it's in the right room
|
||||||
|
cy.get('.mx_EventTile_sticker > a')
|
||||||
|
.should("have.attr", "href")
|
||||||
|
.and("include", `/${roomId}/`);
|
||||||
|
|
||||||
|
// Make sure the image points at the sticker image
|
||||||
|
cy.get<HTMLImageElement>(`img[alt="${STICKER_NAME}"]`)
|
||||||
|
.should("have.attr", "src")
|
||||||
|
.and("match", /thumbnail\/somewhere\?/);
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("Stickers", () => {
|
||||||
|
// We spin up a web server for the sticker picker so that we're not testing to see if
|
||||||
|
// sysadmins can deploy sticker pickers on the same Element domain - we actually want
|
||||||
|
// to make sure that cross-origin postMessage works properly. This makes it difficult
|
||||||
|
// to write the test though, as we have to juggle iframe logistics.
|
||||||
|
//
|
||||||
|
// See sendStickerFromPicker() for more detail on iframe comms.
|
||||||
|
|
||||||
|
let stickerPickerUrl: string;
|
||||||
|
let synapse: SynapseInstance;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
cy.startSynapse("default").then(data => {
|
||||||
|
synapse = data;
|
||||||
|
|
||||||
|
cy.initTestUser(synapse, "Sally");
|
||||||
|
});
|
||||||
|
cy.serveHtmlFile(WIDGET_HTML).then(url => {
|
||||||
|
stickerPickerUrl = url;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
cy.stopSynapse(synapse);
|
||||||
|
cy.stopWebServers();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should send a sticker to multiple rooms', () => {
|
||||||
|
cy.createRoom({
|
||||||
|
name: ROOM_NAME_1,
|
||||||
|
}).as("roomId1");
|
||||||
|
cy.createRoom({
|
||||||
|
name: ROOM_NAME_2,
|
||||||
|
}).as("roomId2");
|
||||||
|
cy.setAccountData("m.widgets", {
|
||||||
|
[STICKER_PICKER_WIDGET_ID]: {
|
||||||
|
content: {
|
||||||
|
type: "m.stickerpicker",
|
||||||
|
name: STICKER_PICKER_WIDGET_NAME,
|
||||||
|
url: stickerPickerUrl,
|
||||||
|
},
|
||||||
|
id: STICKER_PICKER_WIDGET_ID,
|
||||||
|
},
|
||||||
|
}).as("stickers");
|
||||||
|
|
||||||
|
cy.all([
|
||||||
|
cy.get<string>("@roomId1"),
|
||||||
|
cy.get<string>("@roomId2"),
|
||||||
|
cy.get<{}>("@stickers"), // just want to wait for it to be set up
|
||||||
|
]).then(([roomId1, roomId2]) => {
|
||||||
|
cy.viewRoomByName(ROOM_NAME_1);
|
||||||
|
cy.url().should("contain", `/#/room/${roomId1}`);
|
||||||
|
openStickerPicker();
|
||||||
|
sendStickerFromPicker();
|
||||||
|
expectTimelineSticker(roomId1);
|
||||||
|
|
||||||
|
// Ensure that when we switch to a different room that the sticker
|
||||||
|
// goes to the right place
|
||||||
|
cy.viewRoomByName(ROOM_NAME_2);
|
||||||
|
cy.url().should("contain", `/#/room/${roomId2}`);
|
||||||
|
openStickerPicker();
|
||||||
|
sendStickerFromPicker();
|
||||||
|
expectTimelineSticker(roomId2);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
|
@ -20,6 +20,7 @@ import PluginEvents = Cypress.PluginEvents;
|
||||||
import PluginConfigOptions = Cypress.PluginConfigOptions;
|
import PluginConfigOptions = Cypress.PluginConfigOptions;
|
||||||
import { performance } from "./performance";
|
import { performance } from "./performance";
|
||||||
import { synapseDocker } from "./synapsedocker";
|
import { synapseDocker } from "./synapsedocker";
|
||||||
|
import { webserver } from "./webserver";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {Cypress.PluginConfig}
|
* @type {Cypress.PluginConfig}
|
||||||
|
@ -27,4 +28,5 @@ import { synapseDocker } from "./synapsedocker";
|
||||||
export default function(on: PluginEvents, config: PluginConfigOptions) {
|
export default function(on: PluginEvents, config: PluginConfigOptions) {
|
||||||
performance(on, config);
|
performance(on, config);
|
||||||
synapseDocker(on, config);
|
synapseDocker(on, config);
|
||||||
|
webserver(on, config);
|
||||||
}
|
}
|
||||||
|
|
52
cypress/plugins/webserver.ts
Normal file
52
cypress/plugins/webserver.ts
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
/*
|
||||||
|
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" />
|
||||||
|
|
||||||
|
import * as http from "http";
|
||||||
|
import { AddressInfo } from "net";
|
||||||
|
|
||||||
|
import PluginEvents = Cypress.PluginEvents;
|
||||||
|
import PluginConfigOptions = Cypress.PluginConfigOptions;
|
||||||
|
|
||||||
|
const servers: http.Server[] = [];
|
||||||
|
|
||||||
|
function serveHtmlFile(html: string): string {
|
||||||
|
const server = http.createServer((req, res) => {
|
||||||
|
res.writeHead(200, {
|
||||||
|
"Content-Type": "text/html",
|
||||||
|
});
|
||||||
|
res.end(html);
|
||||||
|
});
|
||||||
|
server.listen();
|
||||||
|
servers.push(server);
|
||||||
|
|
||||||
|
return `http://localhost:${(server.address() as AddressInfo).port}/`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function stopWebServers(): null {
|
||||||
|
for (const server of servers) {
|
||||||
|
server.close();
|
||||||
|
}
|
||||||
|
servers.splice(0, servers.length); // clear
|
||||||
|
|
||||||
|
return null; // tell cypress we did the task successfully (doesn't allow undefined)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function webserver(on: PluginEvents, config: PluginConfigOptions) {
|
||||||
|
on("task", { serveHtmlFile, stopWebServers });
|
||||||
|
on("after:run", stopWebServers);
|
||||||
|
}
|
|
@ -47,6 +47,12 @@ declare global {
|
||||||
* @param userId the id of the user to invite
|
* @param userId the id of the user to invite
|
||||||
*/
|
*/
|
||||||
inviteUser(roomId: string, userId: string): Chainable<{}>;
|
inviteUser(roomId: string, userId: string): Chainable<{}>;
|
||||||
|
/**
|
||||||
|
* Sets account data for the user.
|
||||||
|
* @param type The type of account data.
|
||||||
|
* @param data The data to store.
|
||||||
|
*/
|
||||||
|
setAccountData(type: string, data: object): Chainable<{}>;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -91,3 +97,9 @@ Cypress.Commands.add("inviteUser", (roomId: string, userId: string): Chainable<{
|
||||||
return cli.invite(roomId, userId);
|
return cli.invite(roomId, userId);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Cypress.Commands.add("setAccountData", (type: string, data: object): Chainable<{}> => {
|
||||||
|
return cy.getClient().then(async (cli: MatrixClient) => {
|
||||||
|
return cli.setAccountData(type, data);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
45
cypress/support/iframes.ts
Normal file
45
cypress/support/iframes.ts
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
/*
|
||||||
|
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" />
|
||||||
|
|
||||||
|
import Chainable = Cypress.Chainable;
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-namespace
|
||||||
|
namespace Cypress {
|
||||||
|
interface Chainable {
|
||||||
|
/**
|
||||||
|
* Gets you into the `body` of the selectable iframe. Best to call
|
||||||
|
* `within({}, () => { ... })` on the returned Chainable to access
|
||||||
|
* further elements.
|
||||||
|
* @param selector The jquery selector to find the frame with.
|
||||||
|
*/
|
||||||
|
accessIframe(selector: string): Chainable<JQuery<HTMLElement>>;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Inspired by https://www.cypress.io/blog/2020/02/12/working-with-iframes-in-cypress/
|
||||||
|
Cypress.Commands.add("accessIframe", (selector: string): Chainable<JQuery<HTMLElement>> => {
|
||||||
|
return cy.get(selector)
|
||||||
|
.its("0.contentDocument.body").should("not.be.empty")
|
||||||
|
// Cypress loses types in the mess of wrapping, so force cast
|
||||||
|
.then(cy.wrap) as Chainable<JQuery<HTMLElement>>;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Needed to make this file a module
|
||||||
|
export { };
|
|
@ -29,3 +29,6 @@ import "./clipboard";
|
||||||
import "./util";
|
import "./util";
|
||||||
import "./app";
|
import "./app";
|
||||||
import "./percy";
|
import "./percy";
|
||||||
|
import "./webserver";
|
||||||
|
import "./views";
|
||||||
|
import "./iframes";
|
||||||
|
|
40
cypress/support/views.ts
Normal file
40
cypress/support/views.ts
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
/*
|
||||||
|
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" />
|
||||||
|
|
||||||
|
import Chainable = Cypress.Chainable;
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-namespace
|
||||||
|
namespace Cypress {
|
||||||
|
interface Chainable {
|
||||||
|
/**
|
||||||
|
* Opens the given room by name. The room must be visible in the
|
||||||
|
* room list.
|
||||||
|
* @param name The room name to find and click on/open.
|
||||||
|
*/
|
||||||
|
viewRoomByName(name: string): Chainable<JQuery<HTMLElement>>;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Cypress.Commands.add("viewRoomByName", (name: string): Chainable<JQuery<HTMLElement>> => {
|
||||||
|
return cy.get(`.mx_RoomTile[aria-label="${name}"]`).click();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Needed to make this file a module
|
||||||
|
export { };
|
52
cypress/support/webserver.ts
Normal file
52
cypress/support/webserver.ts
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
/*
|
||||||
|
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" />
|
||||||
|
|
||||||
|
import Chainable = Cypress.Chainable;
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-namespace
|
||||||
|
namespace Cypress {
|
||||||
|
interface Chainable {
|
||||||
|
/**
|
||||||
|
* Starts a web server which serves the given HTML.
|
||||||
|
* @param html The HTML to serve
|
||||||
|
* @returns The URL at which the HTML can be accessed.
|
||||||
|
*/
|
||||||
|
serveHtmlFile(html: string): Chainable<string>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stops all running web servers.
|
||||||
|
*/
|
||||||
|
stopWebServers(): Chainable<void>;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function serveHtmlFile(html: string): Chainable<string> {
|
||||||
|
return cy.task<string>("serveHtmlFile", html);
|
||||||
|
}
|
||||||
|
|
||||||
|
function stopWebServers(): Chainable<void> {
|
||||||
|
return cy.task("stopWebServers");
|
||||||
|
}
|
||||||
|
|
||||||
|
Cypress.Commands.add("serveHtmlFile", serveHtmlFile);
|
||||||
|
Cypress.Commands.add("stopWebServers", stopWebServers);
|
||||||
|
|
||||||
|
// Needed to make this file a module
|
||||||
|
export { };
|
|
@ -25,7 +25,6 @@ import { ElementSession } from "./session";
|
||||||
import { RestSessionCreator } from "./rest/creator";
|
import { RestSessionCreator } from "./rest/creator";
|
||||||
import { RestMultiSession } from "./rest/multi";
|
import { RestMultiSession } from "./rest/multi";
|
||||||
import { RestSession } from "./rest/session";
|
import { RestSession } from "./rest/session";
|
||||||
import { stickerScenarios } from './scenarios/sticker';
|
|
||||||
|
|
||||||
export async function scenario(createSession: (s: string) => Promise<ElementSession>,
|
export async function scenario(createSession: (s: string) => Promise<ElementSession>,
|
||||||
restCreator: RestSessionCreator): Promise<void> {
|
restCreator: RestSessionCreator): Promise<void> {
|
||||||
|
@ -51,15 +50,6 @@ export async function scenario(createSession: (s: string) => Promise<ElementSess
|
||||||
console.log("create REST users:");
|
console.log("create REST users:");
|
||||||
const charlies = await createRestUsers(restCreator);
|
const charlies = await createRestUsers(restCreator);
|
||||||
await lazyLoadingScenarios(alice, bob, charlies);
|
await lazyLoadingScenarios(alice, bob, charlies);
|
||||||
|
|
||||||
// we spawn another session for stickers, partially because it involves injecting
|
|
||||||
// a custom sticker picker widget for the account, although mostly because for these
|
|
||||||
// tests to scale, they probably need to be split up more, which means running each
|
|
||||||
// scenario with it's own session (and will make it easier to find relevant logs),
|
|
||||||
// so lets move in this direction (although at some point we'll also need to start
|
|
||||||
// closing them as we go rather than leaving them all open until the end).
|
|
||||||
const stickerSession = await createSession("sally");
|
|
||||||
await stickerScenarios("sally", "ilikestickers", stickerSession, restCreator);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createRestUsers(restCreator: RestSessionCreator): Promise<RestMultiSession> {
|
async function createRestUsers(restCreator: RestSessionCreator): Promise<RestMultiSession> {
|
||||||
|
|
|
@ -1,143 +0,0 @@
|
||||||
/*
|
|
||||||
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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import * as http from "http";
|
|
||||||
import { AddressInfo } from "net";
|
|
||||||
|
|
||||||
import { RestSessionCreator } from "../rest/creator";
|
|
||||||
import { ElementSession } from "../session";
|
|
||||||
import { login } from "../usecases/login";
|
|
||||||
import { selectRoom } from "../usecases/select-room";
|
|
||||||
import { sendSticker } from "../usecases/send-sticker";
|
|
||||||
|
|
||||||
const STICKER_PICKER_WIDGET_ID = "fake-sticker-picker";
|
|
||||||
const ROOM_NAME_1 = "Sticker Test";
|
|
||||||
const ROOM_NAME_2 = "Sticker Test Two";
|
|
||||||
const STICKER_MESSAGE = JSON.stringify({
|
|
||||||
action: "m.sticker",
|
|
||||||
api: "fromWidget",
|
|
||||||
data: {
|
|
||||||
name: "teststicker",
|
|
||||||
description: "Test Sticker",
|
|
||||||
file: "test.png",
|
|
||||||
content: {
|
|
||||||
body: "Test Sticker",
|
|
||||||
msgtype: "m.sticker",
|
|
||||||
url: "mxc://somewhere",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
requestId: "1",
|
|
||||||
widgetId: STICKER_PICKER_WIDGET_ID,
|
|
||||||
});
|
|
||||||
const WIDGET_HTML = `
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<title>Fake Sticker Picker</title>
|
|
||||||
<script>
|
|
||||||
window.onmessage = ev => {
|
|
||||||
if (ev.data.action === 'capabilities') {
|
|
||||||
window.parent.postMessage(Object.assign({
|
|
||||||
response: {
|
|
||||||
capabilities: ["m.sticker"]
|
|
||||||
},
|
|
||||||
}, ev.data), '*');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
</script>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<button name="Send" id="sendsticker">Press for sticker</button>
|
|
||||||
<script>
|
|
||||||
document.getElementById('sendsticker').onclick = () => {
|
|
||||||
window.parent.postMessage(${STICKER_MESSAGE}, '*')
|
|
||||||
};
|
|
||||||
</script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
`;
|
|
||||||
|
|
||||||
class WidgetServer {
|
|
||||||
private server: http.Server = null;
|
|
||||||
|
|
||||||
start() {
|
|
||||||
this.server = http.createServer(this.onRequest);
|
|
||||||
this.server.listen();
|
|
||||||
}
|
|
||||||
|
|
||||||
stop() {
|
|
||||||
this.server.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
get port(): number {
|
|
||||||
return (this.server.address()as AddressInfo).port;
|
|
||||||
}
|
|
||||||
|
|
||||||
onRequest = (req: http.IncomingMessage, res: http.ServerResponse) => {
|
|
||||||
res.writeHead(200);
|
|
||||||
res.end(WIDGET_HTML);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function stickerScenarios(
|
|
||||||
username: string, password: string,
|
|
||||||
session: ElementSession, restCreator: RestSessionCreator,
|
|
||||||
): Promise<void> {
|
|
||||||
console.log(" making account to test stickers");
|
|
||||||
|
|
||||||
const creds = await restCreator.createSession(username, password);
|
|
||||||
|
|
||||||
// we make the room here which also approves the consent stuff
|
|
||||||
// (besides, we test creating rooms elsewhere: no need to do so again)
|
|
||||||
await creds.createRoom(ROOM_NAME_1, {});
|
|
||||||
await creds.createRoom(ROOM_NAME_2, {});
|
|
||||||
|
|
||||||
console.log(" injecting fake sticker picker");
|
|
||||||
|
|
||||||
const widgetServer = new WidgetServer();
|
|
||||||
widgetServer.start();
|
|
||||||
|
|
||||||
const stickerPickerUrl = `http://localhost:${widgetServer.port}/`;
|
|
||||||
|
|
||||||
await creds.put(`/user/${encodeURIComponent(creds.userId())}/account_data/m.widgets`, {
|
|
||||||
"fake_sticker_picker": {
|
|
||||||
content: {
|
|
||||||
type: "m.stickerpicker",
|
|
||||||
name: "Fake Stickers",
|
|
||||||
url: stickerPickerUrl,
|
|
||||||
},
|
|
||||||
id: STICKER_PICKER_WIDGET_ID,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
await login(session, username, password, session.hsUrl);
|
|
||||||
|
|
||||||
session.log.startGroup(`can send a sticker`);
|
|
||||||
await selectRoom(session, ROOM_NAME_1);
|
|
||||||
await sendSticker(session);
|
|
||||||
session.log.endGroup();
|
|
||||||
|
|
||||||
// switch to another room & send another one
|
|
||||||
session.log.startGroup(`can send a sticker to another room`);
|
|
||||||
|
|
||||||
const navPromise = session.page.waitForNavigation();
|
|
||||||
await selectRoom(session, ROOM_NAME_2);
|
|
||||||
await navPromise;
|
|
||||||
|
|
||||||
await sendSticker(session);
|
|
||||||
session.log.endGroup();
|
|
||||||
|
|
||||||
widgetServer.stop();
|
|
||||||
}
|
|
Loading…
Reference in a new issue