|
| 1 | +/* |
| 2 | +Copyright 2026 Element Creations Ltd. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial |
| 5 | +Please see LICENSE files in the repository root for full details. |
| 6 | +*/ |
| 7 | + |
| 8 | +import React from "react"; |
| 9 | +import { fireEvent, render, type RenderResult } from "jest-matrix-react"; |
| 10 | + |
| 11 | +import InteractiveAuthComponent from "../../../../../src/components/structures/InteractiveAuth"; |
| 12 | +import { getMockClientWithEventEmitter, unmockClientPeg } from "../../../../test-utils"; |
| 13 | + |
| 14 | +describe("InteractiveAuthComponent", function () { |
| 15 | + const mockClient = getMockClientWithEventEmitter({ |
| 16 | + generateClientSecret: jest.fn().mockReturnValue("t35tcl1Ent5ECr3T"), |
| 17 | + getDomain: jest.fn().mockReturnValue("test.local"), |
| 18 | + }); |
| 19 | + const authUrl = "https://test.local/oauth?action=foo"; |
| 20 | + const onAuthFinished = jest.fn(); |
| 21 | + const makeRequest = jest.fn().mockResolvedValue({ a: 1 }); |
| 22 | + |
| 23 | + const defaultProps = { |
| 24 | + matrixClient: mockClient, |
| 25 | + makeRequest: jest.fn().mockResolvedValue(undefined), |
| 26 | + onAuthFinished: jest.fn(), |
| 27 | + }; |
| 28 | + const getComponent = (props = {}) => render(<InteractiveAuthComponent {...defaultProps} {...props} />); |
| 29 | + |
| 30 | + beforeEach(function () { |
| 31 | + jest.clearAllMocks(); |
| 32 | + jest.spyOn(global.window, "open").mockImplementation(); |
| 33 | + }); |
| 34 | + |
| 35 | + afterAll(() => { |
| 36 | + unmockClientPeg(); |
| 37 | + }); |
| 38 | + |
| 39 | + const getSubmitButton = ({ container }: RenderResult) => container.querySelector(".mx_Dialog_nonDialogButton"); |
| 40 | + |
| 41 | + it("should use an m.oauth stage", async () => { |
| 42 | + const authData = { |
| 43 | + session: "sess", |
| 44 | + flows: [{ stages: ["m.oauth"] }], |
| 45 | + params: { |
| 46 | + "m.oauth": { |
| 47 | + url: authUrl, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }; |
| 51 | + |
| 52 | + const wrapper = getComponent({ makeRequest, onAuthFinished, authData }); |
| 53 | + |
| 54 | + const submitNode = getSubmitButton(wrapper); |
| 55 | + expect(submitNode).toBeTruthy(); |
| 56 | + |
| 57 | + // click button; should trigger the auth URL to be opened |
| 58 | + fireEvent.click(submitNode!); |
| 59 | + |
| 60 | + expect(global.window.open).toHaveBeenCalledWith(authUrl, "_blank"); |
| 61 | + }); |
| 62 | + |
| 63 | + it("should use an unstable org.matrix.cross_signing_reset stage", async () => { |
| 64 | + const authData = { |
| 65 | + session: "sess", |
| 66 | + flows: [{ stages: ["org.matrix.cross_signing_reset"] }], |
| 67 | + params: { |
| 68 | + "org.matrix.cross_signing_reset": { |
| 69 | + url: authUrl, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }; |
| 73 | + |
| 74 | + const wrapper = getComponent({ makeRequest, onAuthFinished, authData }); |
| 75 | + |
| 76 | + const submitNode = getSubmitButton(wrapper); |
| 77 | + expect(submitNode).toBeTruthy(); |
| 78 | + |
| 79 | + // click button; should trigger the auth URL to be opened |
| 80 | + fireEvent.click(submitNode!); |
| 81 | + |
| 82 | + expect(global.window.open).toHaveBeenCalledWith(authUrl, "_blank"); |
| 83 | + }); |
| 84 | + |
| 85 | + it("should use the first flow when both stable and unstable are present", async () => { |
| 86 | + const authData = { |
| 87 | + session: "sess", |
| 88 | + flows: [{ stages: ["org.matrix.cross_signing_reset"] }, { stages: ["m.oauth"] }], |
| 89 | + params: { |
| 90 | + "org.matrix.cross_signing_reset": { |
| 91 | + url: authUrl, |
| 92 | + }, |
| 93 | + "m.oauth": { |
| 94 | + url: "https://should.not.be/opened", |
| 95 | + }, |
| 96 | + }, |
| 97 | + }; |
| 98 | + |
| 99 | + const wrapper = getComponent({ makeRequest, onAuthFinished, authData }); |
| 100 | + |
| 101 | + const submitNode = getSubmitButton(wrapper); |
| 102 | + expect(submitNode).toBeTruthy(); |
| 103 | + |
| 104 | + // click button; should trigger the auth URL to be opened |
| 105 | + fireEvent.click(submitNode!); |
| 106 | + |
| 107 | + expect(global.window.open).toHaveBeenCalledWith(authUrl, "_blank"); |
| 108 | + }); |
| 109 | +}); |
0 commit comments