Files
matrix-quotes-bot/node_modules/matrix-js-sdk/lib/testing.js
unfunny dfe4cc537f init
2026-09-13 00:29:11 -04:00

148 lines
4.3 KiB
JavaScript

/*
Copyright 2024 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.
*/
/**
* This file is a secondary entrypoint for the js-sdk library, exposing utilities which might be useful for writing tests.
*
* In general, it should not be included in runtime applications.
*
* @packageDocumentation
*/
import { MatrixEvent } from "./models/event.js";
import { EventType } from "./@types/event.js";
import { DecryptionError } from "./common-crypto/CryptoBackend.js";
import { OAuthGrantType } from "./oauth/index.js";
/**
* Create a {@link MatrixEvent}.
*
* @param opts - Values for the event.
*/
export function mkMatrixEvent(opts) {
const event = {
type: opts.type,
room_id: opts.roomId,
sender: opts.sender,
content: opts.content,
event_id: opts.eventId ?? "$" + Math.random() + "-" + Math.random(),
origin_server_ts: opts.ts ?? 0,
unsigned: opts.unsigned
};
if (opts.stateKey !== undefined) {
event.state_key = opts.stateKey;
}
const mxEvent = new MatrixEvent(event);
mxEvent.sender = {
userId: opts.sender,
membership: "join",
name: opts.sender,
rawDisplayName: opts.sender,
roomId: opts.sender,
getAvatarUrl: () => {},
getMxcAvatarUrl: () => {}
};
return mxEvent;
}
/**
* Create a `MatrixEvent` representing a successfully-decrypted `m.room.encrypted` event.
*
* @param opts - Values for the event.
*/
export async function mkEncryptedMatrixEvent(opts) {
const mxEvent = mkMatrixEvent({
type: EventType.RoomMessageEncrypted,
roomId: opts.roomId,
sender: opts.sender,
content: {
algorithm: "m.megolm.v1.aes-sha2"
},
eventId: opts.eventId
});
await decryptExistingEvent(mxEvent, {
plainType: opts.plainType,
plainContent: opts.plainContent
});
return mxEvent;
}
/**
* Create a `MatrixEvent` representing a `m.room.encrypted` event which could not be decrypted.
*
* @param opts - Values for the event.
*/
export async function mkDecryptionFailureMatrixEvent(opts) {
const mxEvent = mkMatrixEvent({
type: EventType.RoomMessageEncrypted,
roomId: opts.roomId,
sender: opts.sender,
content: {
algorithm: "m.megolm.v1.aes-sha2"
},
eventId: opts.eventId
});
const mockCrypto = {
decryptEvent: async _ev => {
throw new DecryptionError(opts.code, opts.msg);
}
};
await mxEvent.attemptDecryption(mockCrypto);
return mxEvent;
}
/**
* Given an event previously returned by {@link mkDecryptionFailureMatrixEvent}, simulate a successful re-decryption
* attempt.
*
* @param mxEvent - The event that will be decrypted.
* @param opts - New data for the successful decryption.
*/
export async function decryptExistingEvent(mxEvent, opts) {
const decryptionResult = {
claimedEd25519Key: "",
clearEvent: {
type: opts.plainType,
content: opts.plainContent
},
senderCurve25519Key: ""
};
const mockCrypto = {
decryptEvent: async _ev => decryptionResult
};
await mxEvent.attemptDecryption(mockCrypto);
}
/**
* Makes a valid OidcClientConfig with minimum valid values
* @param issuer used as the base for all other urls
* @param additionalGrantTypes to add to the default grant types
* @returns OidcClientConfig
* @experimental
*/
export const makeDelegatedAuthMetadata = (issuer = "https://auth.org/", additionalGrantTypes = []) => ({
issuer,
revocation_endpoint: issuer + "revoke",
token_endpoint: issuer + "token",
authorization_endpoint: issuer + "auth",
registration_endpoint: issuer + "registration",
device_authorization_endpoint: issuer + "device",
response_types_supported: ["code"],
grant_types_supported: [OAuthGrantType.AuthorizationCode, OAuthGrantType.RefreshToken, ...additionalGrantTypes],
code_challenge_methods_supported: ["S256"],
response_modes_supported: ["query", "fragment"]
});
//# sourceMappingURL=testing.js.map