-
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.
test(sso): add test for email precheck, registration and welcome comp…
…onent
- Loading branch information
marc.sirisak
committed
Oct 1, 2024
1 parent
b574d43
commit 5920e14
Showing
8 changed files
with
281 additions
and
10 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
194 changes: 194 additions & 0 deletions
194
test/unit-tests/tchap/components/views/sso/EmailVerificationPage-test.tsx
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,194 @@ | ||
import React from "react"; | ||
import { render, cleanup, fireEvent, screen, act } from "@testing-library/react"; | ||
import { mocked, MockedObject } from "jest-mock"; | ||
import { MatrixClient } from "matrix-js-sdk/src/matrix"; | ||
|
||
import EmailVerificationPage from "~tchap-web/src/tchap/components/views/sso/EmailVerificationPage"; | ||
import TchapUtils from "~tchap-web/src/tchap/util/TchapUtils"; | ||
import { ValidatedServerConfig } from "~matrix-react-sdk/src/utils/ValidatedServerConfig"; | ||
import { mockPlatformPeg, stubClient } from "~matrix-react-sdk/test/test-utils"; | ||
import BasePlatform from "~matrix-react-sdk/src/BasePlatform"; | ||
import Login from "~matrix-react-sdk/src/Login"; | ||
|
||
jest.mock("~matrix-react-sdk/src/PlatformPeg") | ||
jest.mock("~tchap-web/src/tchap/util/TchapUtils"); | ||
jest.mock("~matrix-react-sdk/src/Login"); | ||
|
||
describe("<EmailVerificationPage />", () => { | ||
|
||
const userEmail = "marc@tchap.beta.gouv.fr"; | ||
const defaultHsUrl = "https://matrix.agent1.fr" | ||
const secondHsUrl = "https://matrix.agent2.fr" | ||
|
||
|
||
const PlatformPegMocked: MockedObject<BasePlatform> = mockPlatformPeg(); | ||
const mockedClient: MatrixClient = stubClient(); | ||
const mockedTchapUtils = mocked(TchapUtils); | ||
|
||
const mockLoginObject = (hs: string = defaultHsUrl) => { | ||
const mockLoginObject = mocked(new Login(hs, hs, null, {})); | ||
mockLoginObject.createTemporaryClient.mockImplementation(() => mockedClient); | ||
return mockLoginObject; | ||
} | ||
|
||
const mockedFetchHomeserverFromEmail = (hs: string = defaultHsUrl) => { | ||
mockedTchapUtils.fetchHomeserverForEmail.mockImplementation(() => (Promise.resolve({ base_url: hs, server_name: hs}))); | ||
} | ||
|
||
const mockedValidatedServerConfig = (withError: boolean = false, hsUrl: string = defaultHsUrl) => { | ||
if (withError) { | ||
mockedTchapUtils.makeValidatedServerConfig.mockImplementation(() => {throw new Error()}); | ||
} else { | ||
mockedTchapUtils.makeValidatedServerConfig.mockImplementation(() => (Promise.resolve({ | ||
hsUrl: defaultHsUrl, | ||
hsName: "hs", | ||
hsNameIsDifferent: false, | ||
isUrl: "", | ||
isDefault: true, | ||
isNameResolvable: true, | ||
warning: "" | ||
} as ValidatedServerConfig))) | ||
} | ||
} | ||
|
||
const mockedPlatformPegStartSSO = (withError: boolean) => { | ||
if (withError) { | ||
jest.spyOn(PlatformPegMocked, "startSingleSignOn").mockImplementation(() => {throw new Error()}) | ||
} else { | ||
jest.spyOn(PlatformPegMocked, "startSingleSignOn").mockImplementation(() => {}) | ||
} | ||
} | ||
|
||
const renderEmailVerificationPage = () => render(<EmailVerificationPage />); | ||
|
||
afterEach(() => { | ||
cleanup(); | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it("returns error when empty email", async () => { | ||
const { container } = renderEmailVerificationPage(); | ||
|
||
// Put text in email field | ||
const emailField = screen.getByRole("textbox"); | ||
fireEvent.focus(emailField); | ||
fireEvent.change(emailField, { target: { value: "" } }); | ||
|
||
// click on proconnect button | ||
const proconnectButton = screen.getByTestId("proconnect-submit"); | ||
await act(async () => { | ||
await fireEvent.click(proconnectButton); | ||
}) | ||
|
||
// Error classes should not appear | ||
expect(container.getElementsByClassName("mx_ErrorMessage").length).toBe(1); | ||
}); | ||
|
||
it("returns inccorrect email", async () => { | ||
const { container } = renderEmailVerificationPage(); | ||
|
||
// Put text in email field | ||
const emailField = screen.getByRole("textbox"); | ||
fireEvent.focus(emailField); | ||
fireEvent.change(emailField, { target: { value: "falseemail" } }); | ||
|
||
// click on proconnect button | ||
const proconnectButton = screen.getByTestId("proconnect-submit"); | ||
await act(async () => { | ||
await fireEvent.click(proconnectButton); | ||
}) | ||
|
||
// Error classes should not appear | ||
expect(container.getElementsByClassName("mx_ErrorMessage").length).toBe(1); | ||
}); | ||
|
||
|
||
it("should throw error when homeserver catch an error", async () => { | ||
const { container } = renderEmailVerificationPage(); | ||
|
||
// mock server returns an error | ||
mockedValidatedServerConfig(true); | ||
|
||
// Put text in email field | ||
const emailField = screen.getByRole("textbox"); | ||
fireEvent.focus(emailField); | ||
fireEvent.change(emailField, { target: { value: userEmail } }); | ||
|
||
// click on proconnect button | ||
const proconnectButton = screen.getByTestId("proconnect-submit"); | ||
await act(async () => { | ||
await fireEvent.click(proconnectButton); | ||
}) | ||
|
||
// Error classes should not appear | ||
expect(container.getElementsByClassName("mx_ErrorMessage").length).toBe(1); | ||
}) | ||
|
||
|
||
it("should throw and error when connecting to proconnect error", async () => { | ||
const { container } = renderEmailVerificationPage(); | ||
|
||
mockLoginObject(); | ||
mockedValidatedServerConfig(false); | ||
// mock platform page startsso error | ||
mockedPlatformPegStartSSO(true); | ||
|
||
// Put text in email field | ||
const emailField = screen.getByRole("textbox"); | ||
fireEvent.focus(emailField); | ||
fireEvent.change(emailField, { target: { value: userEmail } }); | ||
|
||
// click on proconnect button | ||
const proconnectButton = screen.getByTestId("proconnect-submit"); | ||
await act(async () => { | ||
await fireEvent.click(proconnectButton); | ||
}) | ||
|
||
// Error classes should not appear | ||
expect(container.getElementsByClassName("mx_ErrorMessage").length).toBe(1); | ||
}) | ||
|
||
|
||
it("should start sso with correct homeserver 1", async () => { | ||
renderEmailVerificationPage(); | ||
mockLoginObject(defaultHsUrl); | ||
mockedFetchHomeserverFromEmail(defaultHsUrl); | ||
|
||
mockedValidatedServerConfig(false, defaultHsUrl) | ||
mockedPlatformPegStartSSO(false); | ||
// Put text in email field | ||
const emailField = screen.getByRole("textbox"); | ||
fireEvent.focus(emailField); | ||
fireEvent.change(emailField, { target: { value: userEmail } }); | ||
|
||
// click on proconnect button | ||
const proconnectButton = screen.getByTestId("proconnect-submit"); | ||
await act(async () => { | ||
await fireEvent.click(proconnectButton); | ||
}) | ||
|
||
expect(mockedTchapUtils.makeValidatedServerConfig).toHaveBeenCalledWith({ base_url: defaultHsUrl, server_name: defaultHsUrl}); | ||
expect(PlatformPegMocked.startSingleSignOn).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("should start sso with correct homeserver 2", async () => { | ||
renderEmailVerificationPage(); | ||
mockLoginObject(secondHsUrl); | ||
mockedFetchHomeserverFromEmail(secondHsUrl); | ||
mockedValidatedServerConfig(false, secondHsUrl); | ||
mockedPlatformPegStartSSO(false); | ||
// Put text in email field | ||
const emailField = screen.getByRole("textbox"); | ||
fireEvent.focus(emailField); | ||
fireEvent.change(emailField, { target: { value: userEmail } }); | ||
|
||
// click on proconnect button | ||
const proconnectButton = screen.getByTestId("proconnect-submit"); | ||
await act(async () => { | ||
await fireEvent.click(proconnectButton); | ||
}) | ||
|
||
expect(mockedTchapUtils.makeValidatedServerConfig).toHaveBeenCalledWith({ base_url: secondHsUrl, server_name: secondHsUrl}); | ||
expect(PlatformPegMocked.startSingleSignOn).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
test/unit-tests/tchap/components/views/sso/Register-test.tsx
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,42 @@ | ||
import React from "react"; | ||
import { render, cleanup } from "@testing-library/react"; | ||
|
||
import SdkConfig, { ConfigOptions } from "~matrix-react-sdk/src/SdkConfig"; | ||
import Register from "~matrix-react-sdk/src/components/structures/auth/Registration"; | ||
import { IMatrixClientCreds } from "~tchap-web/linked-dependencies/matrix-react-sdk/src/MatrixClientPeg"; | ||
import { ValidatedServerConfig } from "~tchap-web/linked-dependencies/matrix-react-sdk/src/utils/ValidatedServerConfig"; | ||
|
||
|
||
describe("<Register />", () => { | ||
|
||
const addSSOFlowToMockConfig = (isActive: boolean = false) => { | ||
// mock SdkConfig.get("tchap_features") | ||
const config: ConfigOptions = { tchap_sso_flow: { isActive } }; | ||
SdkConfig.put(config); | ||
}; | ||
|
||
const renderRegister = () => render(<Register | ||
serverConfig={{} as ValidatedServerConfig} | ||
onLoggedIn={(params: IMatrixClientCreds, password: string): Promise<void> =>{ return Promise.resolve()}} | ||
onLoginClick={() => {}} | ||
onServerConfigChange={(config: ValidatedServerConfig) => {}} | ||
/>); | ||
|
||
afterEach(() => { | ||
cleanup(); | ||
}); | ||
|
||
it("returns proconnect button in register when the config include sso flow", () => { | ||
addSSOFlowToMockConfig(true); | ||
const { container } = renderRegister(); | ||
|
||
expect(container.getElementsByClassName("tc_pronnect").length).toBe(1); | ||
Check failure on line 33 in test/unit-tests/tchap/components/views/sso/Register-test.tsx GitHub Actions / Jest<Register /> › returns proconnect button in register when the config include sso flow
|
||
}); | ||
|
||
it("returns no proconnect button when the config does'nt include sso flow", () => { | ||
addSSOFlowToMockConfig(false); | ||
const { container } = renderRegister(); | ||
|
||
expect(container.getElementsByClassName("tc_pronnect").length).toBe(0); | ||
}); | ||
}); |
36 changes: 36 additions & 0 deletions
36
test/unit-tests/tchap/components/views/sso/Welcome-test.tsx
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,36 @@ | ||
import React from "react"; | ||
import { render, cleanup } from "@testing-library/react"; | ||
|
||
import SdkConfig, { ConfigOptions } from "~matrix-react-sdk/src/SdkConfig"; | ||
import Welcome from "~matrix-react-sdk/src/components/views/auth/Welcome"; | ||
|
||
|
||
describe("<Welcome />", () => { | ||
|
||
const addSSOFlowToMockConfig = (isActive: boolean = false) => { | ||
// mock SdkConfig.get("tchap_features") | ||
const config: ConfigOptions = { tchap_sso_flow: { isActive } }; | ||
SdkConfig.put(config); | ||
}; | ||
|
||
const renderWelcomePage = () => render(<Welcome />); | ||
|
||
afterEach(() => { | ||
cleanup(); | ||
}); | ||
|
||
it("returns welcome_sso html when sso_flow is active in config ", () => { | ||
addSSOFlowToMockConfig(true); | ||
const { container } = renderWelcomePage(); | ||
|
||
expect(container.getElementsByClassName("tc_proconnect").length).toBe(1); | ||
Check failure on line 26 in test/unit-tests/tchap/components/views/sso/Welcome-test.tsx GitHub Actions / Jest<Welcome /> › returns welcome_sso html when sso_flow is active in config
|
||
}); | ||
|
||
it("returns normal welcome html page without sso flow ", async () => { | ||
addSSOFlowToMockConfig(false); | ||
const { container } = renderWelcomePage(); | ||
|
||
expect(container.getElementsByClassName("tc_proconnect").length).toBe(0); | ||
expect(container.getElementsByClassName("mx_Button_iconSignIn").length).toBe(1); | ||
Check failure on line 34 in test/unit-tests/tchap/components/views/sso/Welcome-test.tsx GitHub Actions / Jest<Welcome /> › returns normal welcome html page without sso flow
|
||
}); | ||
}); |