-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TCHAP: add message error for external users
- Loading branch information
marc.sirisak
committed
Dec 11, 2024
1 parent
b8ab31b
commit bfd1f37
Showing
10 changed files
with
439 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { MatrixClient, Room } from "matrix-js-sdk/src/matrix"; | ||
import TchapRoomUtils from "../util/TchapRoomUtils"; | ||
import { TchapRoomType } from "../@types/tchap"; | ||
import { _t, _td } from "~tchap-web/src/languageHandler"; | ||
import ErrorDialog from "~tchap-web/src/components/views/dialogs/ErrorDialog"; | ||
import Modal from "~tchap-web/src/Modal"; | ||
import SdkConfig from "~tchap-web/src/SdkConfig"; | ||
|
||
class ExpiredAccountHandler { | ||
|
||
public static isUserExternal(client: MatrixClient): boolean { | ||
console.log('client.getDomain()',client.getDomain()) | ||
const hsList = SdkConfig.get()["homeserver_list"] ?? []; | ||
const externesHs = hsList.find(hs => hs.server_name.includes("Externes")); | ||
console.log('externesHs', externesHs) | ||
console.log('externesHs includes', externesHs.base_url.includes(client.getDomain())) | ||
return !!externesHs.base_url.includes(client.getDomain()); | ||
} | ||
|
||
public static joinRoomError(room: Room): void{ | ||
if (!room) { | ||
this.displayModal(_t("room|error_join_title"), _t("room|error_join_generic_external")); | ||
return; | ||
} | ||
|
||
if (TchapRoomUtils.getTchapRoomType(room) == TchapRoomType.Forum) { | ||
this.displayModal(_t("room|error_join_title"), _t("room|error_join_public_external")) | ||
return; | ||
} | ||
|
||
this.displayModal(_t("room|error_join_title"), _t("room|error_join_private_external")) | ||
} | ||
|
||
public static createRoomError(): void { | ||
this.displayModal(_t("create_room|error_title"), _t("room|error_create_room_external")) | ||
} | ||
|
||
public static createSpaceError(): void { | ||
this.displayModal(_t("create_space|error_title"), _t("create_space|error_external")) | ||
} | ||
|
||
public static displayModal(title: string, description: string): void { | ||
Modal.createDialog(ErrorDialog, { | ||
title, | ||
description, | ||
}); | ||
} | ||
} | ||
|
||
export default ExpiredAccountHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
Copyright 2024 New Vector Ltd. | ||
Copyright 2022 The Matrix.org Foundation C.I.C. | ||
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only | ||
Please see LICENSE files in the repository root for full details. | ||
*/ | ||
|
||
import { mocked, Mocked } from "jest-mock"; | ||
import { MatrixClient } from "matrix-js-sdk/src/matrix"; | ||
|
||
import { | ||
mockPlatformPeg, | ||
getMockClientWithEventEmitter, | ||
mockClientMethodsUser, | ||
stubClient, | ||
} from "~tchap-web/test/test-utils"; | ||
import createRoom from "~tchap-web/src/createRoom"; | ||
import ExternalAccountHandler from "~tchap-web/src/tchap/lib/ExternalAccountHandler"; | ||
import SdkConfig, { ConfigOptions } from "~tchap-web/src/SdkConfig"; | ||
import Modal from "~tchap-web/src/Modal"; | ||
|
||
jest.mock("~tchap-web/src/tchap/lib/ExternalAccountHandler.ts"); | ||
|
||
describe("createRoom", () => { | ||
mockPlatformPeg(); | ||
|
||
let client: Mocked<MatrixClient>; | ||
|
||
const config: ConfigOptions = { | ||
homeserver_list: [ | ||
{ | ||
base_url: "https://matrix.dev01.tchap.incubateur.net", | ||
server_name: "Agents 1", | ||
}, | ||
{ | ||
base_url: "https://matrix.externes.com", | ||
server_name: "Externes", | ||
}, | ||
], | ||
}; | ||
SdkConfig.put(config); | ||
|
||
beforeEach(() => { | ||
Modal.createDialog = jest.fn(); | ||
// @ts-ignore mock (type error because empty return) | ||
mocked(Modal.createDialog).mockReturnValue({}); | ||
|
||
client = getMockClientWithEventEmitter({ | ||
...stubClient(), | ||
...mockClientMethodsUser("@bob:externes.com"), | ||
}); | ||
}); | ||
|
||
afterEach(() => jest.clearAllMocks()); | ||
|
||
it("doesn't create room when user is from external instance", async () => { | ||
// we mock tht something wrong happened | ||
jest.spyOn(client, "createRoom").mockRejectedValue(new Error("test") as never); | ||
|
||
// don't know why isUserExternal is not returning true despite the setup we did for being an external user | ||
// so we mock it, not the best... | ||
jest.spyOn(ExternalAccountHandler, "isUserExternal").mockReturnValue(true); | ||
|
||
await createRoom(client, {}); | ||
|
||
expect(ExternalAccountHandler.isUserExternal).toHaveBeenCalledTimes(1); | ||
|
||
expect(ExternalAccountHandler.createRoomError).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { mocked, MockedObject } from "jest-mock"; | ||
import { MatrixClient } from "matrix-js-sdk/src/matrix"; | ||
|
||
import ExternalAccountHandler from "~tchap-web/src/tchap/lib/ExternalAccountHandler"; | ||
import Modal from "~tchap-web/src/Modal"; | ||
import { getMockClientWithEventEmitter, mockClientMethodsUser } from "~tchap-web/test/test-utils"; | ||
import SdkConfig, { ConfigOptions } from "~tchap-web/src/SdkConfig"; | ||
import ErrorDialog from "~tchap-web/src/components/views/dialogs/ErrorDialog"; | ||
import { _t } from "~tchap-web/src/languageHandler"; | ||
|
||
describe("ExternalAccountHandler", () => { | ||
let mockClient: MockedObject<MatrixClient>; | ||
|
||
const userId = "@bob:externes.com"; | ||
const userIdNotExterne = "@bob:test.com"; | ||
|
||
beforeEach(() => { | ||
Modal.createDialog = jest.fn(); | ||
// @ts-ignore mock (type error because empty return) | ||
mocked(Modal.createDialog).mockReturnValue({}); | ||
|
||
const config: ConfigOptions = { | ||
homeserver_list: [ | ||
{ | ||
base_url: "https://matrix.dev01.tchap.incubateur.net", | ||
server_name: "Agents 1", | ||
}, | ||
{ | ||
base_url: "https://matrix.externes.com", | ||
server_name: "Externes", | ||
}, | ||
], | ||
}; | ||
SdkConfig.put(config); | ||
|
||
mockClient = getMockClientWithEventEmitter({ | ||
...mockClientMethodsUser(userId), | ||
stopClient: jest.fn(), | ||
removeAllListeners: jest.fn(), | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
SdkConfig.reset(); | ||
}); | ||
|
||
it("should indicate the user as external", () => { | ||
const isExternal = ExternalAccountHandler.isUserExternal(mockClient); | ||
expect(isExternal).toBeTruthy(); | ||
}); | ||
|
||
it("should call modal join room error with generic message", () => { | ||
ExternalAccountHandler.joinRoomError(null); | ||
expect(Modal.createDialog).toHaveBeenCalledTimes(1); | ||
expect(Modal.createDialog).toHaveBeenCalledWith(ErrorDialog, { | ||
title: _t("room|error_join_title"), | ||
description: _t("room|error_join_generic_external"), | ||
}); | ||
}); | ||
|
||
it("should call modal create room error with public message", () => { | ||
ExternalAccountHandler.createRoomError(); | ||
expect(Modal.createDialog).toHaveBeenCalledTimes(1); | ||
expect(Modal.createDialog).toHaveBeenCalledWith(ErrorDialog, { | ||
title: _t("create_room|error_title"), | ||
description: _t("room|error_create_room_external"), | ||
}); | ||
}); | ||
|
||
it("should call modal space error with private message", () => { | ||
ExternalAccountHandler.createSpaceError(); | ||
expect(Modal.createDialog).toHaveBeenCalledTimes(1); | ||
expect(Modal.createDialog).toHaveBeenCalledWith(ErrorDialog, { | ||
title: _t("create_space|error_title"), | ||
description: _t("create_space|error_external"), | ||
}); | ||
}); | ||
|
||
it("should not indicate the user as external", () => { | ||
mockClient = getMockClientWithEventEmitter({ | ||
...mockClientMethodsUser(userIdNotExterne), | ||
stopClient: jest.fn(), | ||
removeAllListeners: jest.fn(), | ||
}); | ||
|
||
const isExternal = ExternalAccountHandler.isUserExternal(mockClient); | ||
expect(isExternal).toBeFalsy(); | ||
}); | ||
}); |
Oops, something went wrong.