Skip to content

Commit

Permalink
Merge pull request #50 from mabiede/feature/backwards-compatible-config
Browse files Browse the repository at this point in the history
refactor backward compatibility
  • Loading branch information
Josef Erben authored Feb 6, 2023
2 parents cec2a10 + da3d775 commit e41e1a7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
6 changes: 6 additions & 0 deletions dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(env
(dev
(flags
(:standard -w +A-42-44-45-48-66 -warn-error +A-3))))

(data_only_dirs .git)
12 changes: 9 additions & 3 deletions lib/letters.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ module Config = struct
; mechanism : Sendmail.mechanism
}

let make ?(mechanism = Sendmail.PLAIN) ~username ~password ~hostname ~with_starttls =
let create ?(mechanism = Sendmail.PLAIN) ~username ~password ~hostname ~with_starttls ()
=
let username =
if String.equal username "" then Option.none else Option.some username
in
Expand All @@ -31,6 +32,10 @@ module Config = struct
}
;;

let make ~username ~password ~hostname ~with_starttls =
create ~username ~password ~hostname ~with_starttls ()
;;

let set_port port config = { config with port }
let set_ca_cert path config = { config with ca_certs = Ca_cert path }
let set_ca_path path config = { config with ca_certs = Ca_path path }
Expand Down Expand Up @@ -162,7 +167,8 @@ let build_email ~from ~recipients ~subject ~body =
let html = Mt.part ~header:html_headers (stream_of_string html) in
let header =
Header.of_list
[ Field (Field_name.content_type, Content, multipart_content_alternative) ]
Field.
[ Field (Field_name.content_type, Content, multipart_content_alternative) ]
in
(match boundary with
| None -> MtMultipart (Mt.multipart ~rng:Mt.rng ~header [ plain; html ])
Expand Down Expand Up @@ -200,7 +206,7 @@ let send =
let authentication : Sendmail.authentication option =
match c.username, c.password with
| Some username, Some password ->
Some { username; password; mechanism = c.mechanism }
Some { Sendmail.username; password; mechanism = c.mechanism }
| _ -> None
in
let port =
Expand Down
11 changes: 10 additions & 1 deletion lib/letters.mli
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@ module Config : sig
[with_starttls] True if start unencrypted connection and then "promote"
[?mechanism] login mechanism used by sendmail (default: PLAIN) *)
val make
val create
: ?mechanism:Sendmail.mechanism
-> username:string
-> password:string
-> hostname:string
-> with_starttls:bool
-> unit
-> t

(** Same as [create] with default mechanism - backwards compatibility *)
val make
: username:string
-> password:string
-> hostname:string
-> with_starttls:bool
-> t

(** Add a port to configuration record
Expand Down
23 changes: 23 additions & 0 deletions test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,30 @@ let test_create_mixed_body_email _ () =
Lwt.return (print_string message)
;;

let test_create_config () =
(* check for config compatibility *)
let username, password, hostname, with_starttls =
"SomeUser", "password", "localhost", true
in
let (_ : Config.t) = Config.make ~username ~password ~hostname ~with_starttls in
let (_ : Config.t) = Config.create ~username ~password ~hostname ~with_starttls () in
let (_ : Config.t) =
Config.create
~mechanism:Sendmail.LOGIN
~username
~password
~hostname
~with_starttls
()
in
()
;;

let () =
let () =
Alcotest.(
run "model" [ "config", [ test_case "create config" `Quick test_create_config ] ])
in
Lwt_main.run
(Alcotest_lwt.run
"Email creation"
Expand Down

0 comments on commit e41e1a7

Please sign in to comment.