Factor out basic email check (#10244)

pull/28217/head
Michael Weimann 2023-02-28 10:39:35 +01:00 committed by GitHub
parent f40d15388c
commit 2c7dfb5401
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 2 deletions

View File

@ -657,7 +657,7 @@ export default class InviteDialog extends React.PureComponent<Props, IInviteDial
this.setState({ tryingIdentityServer: true }); this.setState({ tryingIdentityServer: true });
return; return;
} }
if (term.indexOf("@") > 0 && Email.looksValid(term) && SettingsStore.getValue(UIFeature.IdentityServer)) { if (Email.looksValid(term) && SettingsStore.getValue(UIFeature.IdentityServer)) {
// Start off by suggesting the plain email while we try and resolve it // Start off by suggesting the plain email while we try and resolve it
// to a real account. // to a real account.
this.setState({ this.setState({
@ -796,7 +796,7 @@ export default class InviteDialog extends React.PureComponent<Props, IInviteDial
continue; continue;
} }
if (address.indexOf("@") > 0 && Email.looksValid(address)) { if (Email.looksValid(address)) {
toAdd.push(new ThreepidMember(address)); toAdd.push(new ThreepidMember(address));
continue; continue;
} }

View File

@ -1,5 +1,6 @@
/* /*
Copyright 2016 OpenMarket Ltd Copyright 2016 OpenMarket Ltd
Copyright 2023 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -22,5 +23,8 @@ const EMAIL_ADDRESS_REGEX = new RegExp(
); );
export function looksValid(email: string): boolean { export function looksValid(email: string): boolean {
// short circuit regex with this basic check
if (email.indexOf("@") < 1) return false;
return EMAIL_ADDRESS_REGEX.test(email); return EMAIL_ADDRESS_REGEX.test(email);
} }

View File

@ -16,6 +16,7 @@ limitations under the License.
import React from "react"; import React from "react";
import { render, screen } from "@testing-library/react"; import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { RoomType } from "matrix-js-sdk/src/@types/event"; import { RoomType } from "matrix-js-sdk/src/@types/event";
import { Room } from "matrix-js-sdk/src/matrix"; import { Room } from "matrix-js-sdk/src/matrix";
@ -187,4 +188,19 @@ describe("InviteDialog", () => {
await screen.findByText("foobar@email.com"); await screen.findByText("foobar@email.com");
await screen.findByText("Invite by email"); await screen.findByText("Invite by email");
}); });
it("should add pasted values", async () => {
mockClient.getIdentityServerUrl.mockReturnValue("https://identity-server");
mockClient.lookupThreePid.mockResolvedValue({});
render(<InviteDialog kind={KIND_INVITE} roomId={roomId} onFinished={jest.fn()} />);
const input = screen.getByTestId("invite-dialog-input");
input.focus();
await userEvent.paste(`${bobId} ${aliceEmail}`);
await screen.findByText(bobId);
await screen.findByText(aliceEmail);
expect(input).toHaveValue("");
});
}); });

32
test/email-test.ts Normal file
View File

@ -0,0 +1,32 @@
/*
Copyright 2023 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.
*/
import { looksValid } from "../src/email";
describe("looksValid", () => {
it.each([
["", false],
["alice", false],
["@", false],
["@alice:example.com", false],
["@b.org", false],
["alice@example", false],
["a@b.org", true],
["alice@example.com", true],
])("for »%s« should return %s", (value: string, expected: boolean) => {
expect(looksValid(value)).toBe(expected);
});
});