Skip to content

Commit

Permalink
use mrmime to validate email address
Browse files Browse the repository at this point in the history
  • Loading branch information
timohuber committed Oct 16, 2023
1 parent af9590d commit 46c52a3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
15 changes: 4 additions & 11 deletions pool/app/pool_user/entity.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
6 changes: 6 additions & 0 deletions pool/test/command.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions pool/test/user_test.ml
Original file line number Diff line number Diff line change
@@ -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 =
[ "[email protected]"
; "[email protected]"
; "[email protected]"
; "[email protected]"
; "[email protected]"
; "[email protected]"
; "[email protected]"
; "[email protected]"
]
in
let invalid_addresses =
[ "plainaddress"
; "email.example.com"
; "[email protected]"
; "verycommon;@example.com"
; "[email protected]"
; "very common;@example.com"
; "[email protected]"
; "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
;;

0 comments on commit 46c52a3

Please sign in to comment.