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

Commit

Permalink
Merge branch 'develop' into weeman1337/voice-broadcast-redaction
Browse files Browse the repository at this point in the history
  • Loading branch information
weeman1337 authored Dec 21, 2022
2 parents d05a3fa + bef8e07 commit 38332d6
Show file tree
Hide file tree
Showing 21 changed files with 1,359 additions and 331 deletions.
89 changes: 85 additions & 4 deletions cypress/e2e/crypto/crypto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { MatrixClient, Room } from "matrix-js-sdk/src/matrix";

import type { ISendEventResponse, MatrixClient, Room } from "matrix-js-sdk/src/matrix";
import type { VerificationRequest } from "matrix-js-sdk/src/crypto/verification/request/VerificationRequest";
import type { ISasEvent } from "matrix-js-sdk/src/crypto/verification/SAS";
import type { CypressBot } from "../../support/bot";
import { SynapseInstance } from "../../plugins/synapsedocker";
import Chainable = Cypress.Chainable;

type EmojiMapping = [emoji: string, name: string];
interface CryptoTestContext extends Mocha.Context {
synapse: SynapseInstance;
bob: MatrixClient;
bob: CypressBot;
}

const waitForVerificationRequest = (cli: MatrixClient): Promise<VerificationRequest> => {
Expand Down Expand Up @@ -197,7 +197,7 @@ describe("Cryptography", function () {
cy.bootstrapCrossSigning();
autoJoin(this.bob);

/* we need to have a room with the other user present, so we can open the verification panel */
// we need to have a room with the other user present, so we can open the verification panel
let roomId: string;
cy.createRoom({ name: "TestRoom", invite: [this.bob.getUserId()] }).then((_room1Id) => {
roomId = _room1Id;
Expand All @@ -210,4 +210,85 @@ describe("Cryptography", function () {

verify.call(this);
});

it("should show the correct shield on edited e2e events", function (this: CryptoTestContext) {
cy.bootstrapCrossSigning();

// bob has a second, not cross-signed, device
cy.loginBot(this.synapse, this.bob.getUserId(), this.bob.__cypress_password, {}).as("bobSecondDevice");

autoJoin(this.bob);

// first create the room, so that we can open the verification panel
cy.createRoom({ name: "TestRoom", invite: [this.bob.getUserId()] })
.as("testRoomId")
.then((roomId) => {
cy.log(`Created test room ${roomId}`);
cy.visit(`/#/room/${roomId}`);

// enable encryption
cy.getClient().then((cli) => {
cli.sendStateEvent(roomId, "m.room.encryption", { algorithm: "m.megolm.v1.aes-sha2" });
});

// wait for Bob to join the room, otherwise our attempt to open his user details may race
// with his join.
cy.contains(".mx_TextualEvent", "Bob joined the room").should("exist");
});

verify.call(this);

cy.get<string>("@testRoomId").then((roomId) => {
// bob sends a valid event
cy.wrap(this.bob.sendTextMessage(roomId, "Hoo!")).as("testEvent");

// the message should appear, decrypted, with no warning
cy.contains(".mx_EventTile_body", "Hoo!")
.closest(".mx_EventTile")
.should("have.class", "mx_EventTile_verified")
.should("not.have.descendants", ".mx_EventTile_e2eIcon_warning");

// bob sends an edit to the first message with his unverified device
cy.get<MatrixClient>("@bobSecondDevice").then((bobSecondDevice) => {
cy.get<ISendEventResponse>("@testEvent").then((testEvent) => {
bobSecondDevice.sendMessage(roomId, {
"m.new_content": {
msgtype: "m.text",
body: "Haa!",
},
"m.relates_to": {
rel_type: "m.replace",
event_id: testEvent.event_id,
},
});
});
});

// the edit should have a warning
cy.contains(".mx_EventTile_body", "Haa!")
.closest(".mx_EventTile")
.within(() => {
cy.get(".mx_EventTile_e2eIcon_warning").should("exist");
});

// a second edit from the verified device should be ok
cy.get<ISendEventResponse>("@testEvent").then((testEvent) => {
this.bob.sendMessage(roomId, {
"m.new_content": {
msgtype: "m.text",
body: "Hee!",
},
"m.relates_to": {
rel_type: "m.replace",
event_id: testEvent.event_id,
},
});
});

cy.contains(".mx_EventTile_body", "Hee!")
.closest(".mx_EventTile")
.should("have.class", "mx_EventTile_verified")
.should("not.have.descendants", ".mx_EventTile_e2eIcon_warning");
});
});
});
276 changes: 276 additions & 0 deletions cypress/e2e/integration-manager/read_events.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
/*
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";
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>
<input type="text" id="target-room-id"/>
<input type="text" id="event-type"/>
<input type="text" id="state-key"/>
<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: "read_events",
room_id: document.getElementById("target-room-id").value,
type: document.getElementById("event-type").value,
state_key: JSON.parse(document.getElementById("state-key").value),
},
'*',
);
};
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() {
cy.get(".mx_RightPanel_roomSummaryButton").click();
cy.get(".mx_RoomSummaryCard_appsGroup").within(() => {
cy.contains("Add widgets, bridges & bots").click();
});
}

function sendActionFromIntegrationManager(
integrationManagerUrl: string,
targetRoomId: string,
eventType: string,
stateKey: string | boolean,
) {
cy.accessIframe(`iframe[src*="${integrationManagerUrl}"]`).within(() => {
cy.get("#target-room-id").should("exist").type(targetRoomId);
cy.get("#event-type").should("exist").type(eventType);
cy.get("#state-key").should("exist").type(JSON.stringify(stateKey));
cy.get("#send-action").should("exist").click();
});
}

describe("Integration Manager: Read Events", () => {
let testUser: UserCredentials;
let synapse: SynapseInstance;
let integrationManagerUrl: string;

beforeEach(() => {
cy.serveHtmlFile(INTEGRATION_MANAGER_HTML).then((url) => {
integrationManagerUrl = url;
});
cy.startSynapse("default").then((data) => {
synapse = data;

cy.initTestUser(synapse, USER_DISPLAY_NAME, () => {
cy.window().then((win) => {
win.localStorage.setItem("mx_scalar_token", INTEGRATION_MANAGER_TOKEN);
win.localStorage.setItem(`mx_scalar_token_at_${integrationManagerUrl}`, INTEGRATION_MANAGER_TOKEN);
});
}).then((user) => {
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
cy.intercept(`${integrationManagerUrl}/account?scalar_token=${INTEGRATION_MANAGER_TOKEN}*`, (req) => {
req.continue((res) => {
return res.send(200, {
user_id: testUser.userId,
});
});
});

cy.createRoom({
name: ROOM_NAME,
}).as("roomId");
});
});

afterEach(() => {
cy.stopSynapse(synapse);
cy.stopWebServers();
});

it("should read a state event by state key", () => {
cy.all([cy.get<string>("@roomId"), cy.get<{}>("@integrationManager")]).then(([roomId]) => {
cy.viewRoomByName(ROOM_NAME);

const eventType = "io.element.integrations.installations";
const eventContent = {
foo: "bar",
};
const stateKey = "state-key-123";

// Send a state event
cy.getClient()
.then(async (client) => {
return await client.sendStateEvent(roomId, eventType, eventContent, stateKey);
})
.then((event) => {
openIntegrationManager();

// Read state events
sendActionFromIntegrationManager(integrationManagerUrl, roomId, eventType, stateKey);

// Check the response
cy.accessIframe(`iframe[src*="${integrationManagerUrl}"]`).within(() => {
cy.get("#message-response")
.should("include.text", event.event_id)
.should("include.text", `"content":${JSON.stringify(eventContent)}`);
});
});
});
});

it("should read a state event with empty state key", () => {
cy.all([cy.get<string>("@roomId"), cy.get<{}>("@integrationManager")]).then(([roomId]) => {
cy.viewRoomByName(ROOM_NAME);

const eventType = "io.element.integrations.installations";
const eventContent = {
foo: "bar",
};
const stateKey = "";

// Send a state event
cy.getClient()
.then(async (client) => {
return await client.sendStateEvent(roomId, eventType, eventContent, stateKey);
})
.then((event) => {
openIntegrationManager();

// Read state events
sendActionFromIntegrationManager(integrationManagerUrl, roomId, eventType, stateKey);

// Check the response
cy.accessIframe(`iframe[src*="${integrationManagerUrl}"]`).within(() => {
cy.get("#message-response")
.should("include.text", event.event_id)
.should("include.text", `"content":${JSON.stringify(eventContent)}`);
});
});
});
});

it("should read state events with any state key", () => {
cy.all([cy.get<string>("@roomId"), cy.get<{}>("@integrationManager")]).then(([roomId]) => {
cy.viewRoomByName(ROOM_NAME);

const eventType = "io.element.integrations.installations";

const stateKey1 = "state-key-123";
const eventContent1 = {
foo1: "bar1",
};
const stateKey2 = "state-key-456";
const eventContent2 = {
foo2: "bar2",
};
const stateKey3 = "state-key-789";
const eventContent3 = {
foo3: "bar3",
};

// Send state events
cy.getClient()
.then(async (client) => {
return Promise.all([
client.sendStateEvent(roomId, eventType, eventContent1, stateKey1),
client.sendStateEvent(roomId, eventType, eventContent2, stateKey2),
client.sendStateEvent(roomId, eventType, eventContent3, stateKey3),
]);
})
.then((events) => {
openIntegrationManager();

// Read state events
sendActionFromIntegrationManager(
integrationManagerUrl,
roomId,
eventType,
true, // Any state key
);

// Check the response
cy.accessIframe(`iframe[src*="${integrationManagerUrl}"]`).within(() => {
cy.get("#message-response")
.should("include.text", events[0].event_id)
.should("include.text", `"content":${JSON.stringify(eventContent1)}`)
.should("include.text", events[1].event_id)
.should("include.text", `"content":${JSON.stringify(eventContent2)}`)
.should("include.text", events[2].event_id)
.should("include.text", `"content":${JSON.stringify(eventContent3)}`);
});
});
});
});

it("should fail to read an event type which is not allowed", () => {
cy.all([cy.get<string>("@roomId"), cy.get<{}>("@integrationManager")]).then(([roomId]) => {
cy.viewRoomByName(ROOM_NAME);

const eventType = "com.example.event";
const stateKey = "";

openIntegrationManager();

// Read state events
sendActionFromIntegrationManager(integrationManagerUrl, roomId, eventType, stateKey);

// Check the response
cy.accessIframe(`iframe[src*="${integrationManagerUrl}"]`).within(() => {
cy.get("#message-response").should("include.text", "Failed to read events");
});
});
});
});
Loading

0 comments on commit 38332d6

Please sign in to comment.