Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use mrmime to validate email address #229

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
;;