From 46c52a3bf2c90c0d7be90ce8528b9635925406a0 Mon Sep 17 00:00:00 2001 From: Timo Huber Date: Mon, 16 Oct 2023 08:43:35 +0200 Subject: [PATCH] use mrmime to validate email address --- pool/app/pool_user/entity.ml | 15 ++++-------- pool/test/command.ml | 6 +++++ pool/test/user_test.ml | 44 ++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 pool/test/user_test.ml diff --git a/pool/app/pool_user/entity.ml b/pool/app/pool_user/entity.ml index bc5a4ef9a..00df0e804 100644 --- a/pool/app/pool_user/entity.ml +++ b/pool/app/pool_user/entity.ml @@ -148,17 +148,10 @@ module EmailAddress = struct type t = string [@@deriving eq, show, yojson] let validate_characters email = - let open Re in - (* Checks for more than 1 character before and more than 2 characters after - the @ sign *) - let regex = - seq [ repn any 1 None; char '@'; repn any 2 None ] - |> whole_string - |> compile - in - if Re.execp regex email - then Ok email - else Error PoolError.(Invalid Field.EmailAddress) + let open Mrmime in + match Mailbox.of_string email with + | Ok _ -> Ok email + | Error _ -> Error PoolError.(Invalid Field.EmailAddress) ;; let strip_email_suffix email = diff --git a/pool/test/command.ml b/pool/test/command.ml index 8088a4575..3d67058cc 100644 --- a/pool/test/command.ml +++ b/pool/test/command.ml @@ -246,6 +246,12 @@ let () = Waiting_list_test.create_with_direct_registration_enabled ; test_case "update comment" `Quick Waiting_list_test.update ] ) + ; ( "user" + , [ test_case + "validate email addresses" + `Quick + User_test.validate_email_adress + ] ) ; "location", [ test_case "create location" `Quick Location_test.create ] ; ( "mailing" , [ test_case "create mailing" `Quick Mailing_test.create diff --git a/pool/test/user_test.ml b/pool/test/user_test.ml new file mode 100644 index 000000000..6b504f7b9 --- /dev/null +++ b/pool/test/user_test.ml @@ -0,0 +1,44 @@ +let validate_email_adress () = + let open Pool_user in + let check_result expected generated = + let open Alcotest in + let email = testable EmailAddress.pp EmailAddress.equal in + check (result email Test_utils.error) "succeeds" expected generated + in + let valid_addresses = + [ "it@econ.uzh.ch" + ; "very.common@example.com" + ; "very-common@example.com" + ; "very_common@example.com" + ; "very_123_common@example.com" + ; "verycommon123@example.com" + ; "_______@example.com" + ; "email@example.name" + ] + in + let invalid_addresses = + [ "plainaddress" + ; "email.example.com" + ; "email..email@example.com" + ; "verycommon;@example.com" + ; "verycommon.@example.com" + ; "very common;@example.com" + ; "ke.vi.nkee.ne.r.le.g.a.l888.@gmail.com" + ; "p;@hotmail.com" + ] + in + let () = + CCList.iter + (fun email -> + let expected = Ok (EmailAddress.of_string email) in + let result = EmailAddress.create email in + check_result expected result) + valid_addresses + in + CCList.iter + (fun email -> + let expected = Error Pool_common.Message.(Invalid Field.EmailAddress) in + let result = EmailAddress.create email in + check_result expected result) + invalid_addresses +;;