Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 2c7dfb5

Browse files
authored
Factor out basic email check (#10244)
1 parent f40d153 commit 2c7dfb5

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

src/components/views/dialogs/InviteDialog.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ export default class InviteDialog extends React.PureComponent<Props, IInviteDial
657657
this.setState({ tryingIdentityServer: true });
658658
return;
659659
}
660-
if (term.indexOf("@") > 0 && Email.looksValid(term) && SettingsStore.getValue(UIFeature.IdentityServer)) {
660+
if (Email.looksValid(term) && SettingsStore.getValue(UIFeature.IdentityServer)) {
661661
// Start off by suggesting the plain email while we try and resolve it
662662
// to a real account.
663663
this.setState({
@@ -796,7 +796,7 @@ export default class InviteDialog extends React.PureComponent<Props, IInviteDial
796796
continue;
797797
}
798798

799-
if (address.indexOf("@") > 0 && Email.looksValid(address)) {
799+
if (Email.looksValid(address)) {
800800
toAdd.push(new ThreepidMember(address));
801801
continue;
802802
}

src/email.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
Copyright 2016 OpenMarket Ltd
3+
Copyright 2023 The Matrix.org Foundation C.I.C.
34
45
Licensed under the Apache License, Version 2.0 (the "License");
56
you may not use this file except in compliance with the License.
@@ -22,5 +23,8 @@ const EMAIL_ADDRESS_REGEX = new RegExp(
2223
);
2324

2425
export function looksValid(email: string): boolean {
26+
// short circuit regex with this basic check
27+
if (email.indexOf("@") < 1) return false;
28+
2529
return EMAIL_ADDRESS_REGEX.test(email);
2630
}

test/components/views/dialogs/InviteDialog-test.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616

1717
import React from "react";
1818
import { render, screen } from "@testing-library/react";
19+
import userEvent from "@testing-library/user-event";
1920
import { RoomType } from "matrix-js-sdk/src/@types/event";
2021
import { Room } from "matrix-js-sdk/src/matrix";
2122

@@ -187,4 +188,19 @@ describe("InviteDialog", () => {
187188
await screen.findByText("[email protected]");
188189
await screen.findByText("Invite by email");
189190
});
191+
192+
it("should add pasted values", async () => {
193+
mockClient.getIdentityServerUrl.mockReturnValue("https://identity-server");
194+
mockClient.lookupThreePid.mockResolvedValue({});
195+
196+
render(<InviteDialog kind={KIND_INVITE} roomId={roomId} onFinished={jest.fn()} />);
197+
198+
const input = screen.getByTestId("invite-dialog-input");
199+
input.focus();
200+
await userEvent.paste(`${bobId} ${aliceEmail}`);
201+
202+
await screen.findByText(bobId);
203+
await screen.findByText(aliceEmail);
204+
expect(input).toHaveValue("");
205+
});
190206
});

test/email-test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright 2023 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { looksValid } from "../src/email";
18+
19+
describe("looksValid", () => {
20+
it.each([
21+
["", false],
22+
["alice", false],
23+
["@", false],
24+
["@alice:example.com", false],
25+
["@b.org", false],
26+
["alice@example", false],
27+
["[email protected]", true],
28+
["[email protected]", true],
29+
])("for »%s« should return %s", (value: string, expected: boolean) => {
30+
expect(looksValid(value)).toBe(expected);
31+
});
32+
});

0 commit comments

Comments
 (0)