|
| 1 | +defmodule SparkPost.TemplateTest do |
| 2 | + use ExUnit.Case, async: false |
| 3 | + |
| 4 | + alias SparkPost.Content.TemplateRef |
| 5 | + alias SparkPost.{Endpoint, MockServer, Template} |
| 6 | + alias SparkPost.Template.ContentResponse |
| 7 | + |
| 8 | + import Mock |
| 9 | + |
| 10 | + defmodule TestStruct do |
| 11 | + def basic_template do |
| 12 | + %TemplateRef{template_id: "TEMPLATE_ID", use_draft_template: nil} |
| 13 | + end |
| 14 | + |
| 15 | + def template_with_draft do |
| 16 | + %TemplateRef{template_id: "TEMPLATE_ID", use_draft_template: true} |
| 17 | + end |
| 18 | + |
| 19 | + def substitution_data do |
| 20 | + %{ |
| 21 | + key1: "value1", |
| 22 | + key2: "value2" |
| 23 | + } |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + describe "preview tests" do |
| 28 | + test_with_mock "Template.preview succeeds with Template.ContentResponse", |
| 29 | + HTTPoison, [request: fn (method, url, body, headers, opts) -> |
| 30 | + assert method == :post |
| 31 | + # draft not set |
| 32 | + assert String.ends_with?(url, "preview") |
| 33 | + fun = MockServer.mk_http_resp(200, MockServer.get_json("previewtemplate")) |
| 34 | + fun.(method, url, body, headers, opts) |
| 35 | + end] do |
| 36 | + resp = Template.preview(TestStruct.basic_template(), TestStruct.substitution_data()) |
| 37 | + assert %ContentResponse{} = resp |
| 38 | + end |
| 39 | + |
| 40 | + test_with_mock "Template.preview succeeds with Template.ContentResponse and draft set", |
| 41 | + HTTPoison, [request: fn (method, url, body, headers, opts) -> |
| 42 | + assert method == :post |
| 43 | + assert String.ends_with?(url, "preview?draft=true") |
| 44 | + fun = MockServer.mk_http_resp(200, MockServer.get_json("previewtemplate")) |
| 45 | + fun.(method, url, body, headers, opts) |
| 46 | + end] do |
| 47 | + resp = Template.preview(TestStruct.template_with_draft(), TestStruct.substitution_data()) |
| 48 | + assert %ContentResponse{} = resp |
| 49 | + end |
| 50 | + |
| 51 | + test_with_mock "Template.preview fails with Endpoint.Error", HTTPoison, |
| 52 | + [request: MockServer.mk_fail] do |
| 53 | + resp = Template.preview(TestStruct.basic_template(), TestStruct.substitution_data()) |
| 54 | + assert %Endpoint.Error{} = resp |
| 55 | + end |
| 56 | + |
| 57 | + test_with_mock "Template.preview unmarshals complex from field correctly", HTTPoison, |
| 58 | + [request: fn (method, url, body, headers, opts) -> |
| 59 | + assert method == :post |
| 60 | + fun = MockServer.mk_http_resp(200, MockServer.get_json("previewtemplate")) |
| 61 | + fun.(method, url, body, headers, opts) |
| 62 | + end] do |
| 63 | + resp = Template.preview(TestStruct.basic_template(), TestStruct.substitution_data()) |
| 64 | + assert %SparkPost.Address{ |
| 65 | + name: "Example Company Marketing", |
| 66 | + "email": "marketing@bounces.company.example" |
| 67 | + } == resp.from |
| 68 | + end |
| 69 | + |
| 70 | + test_with_mock "Template.preview unmarshals simple from field correctly", HTTPoison, |
| 71 | + [request: fn (method, url, body, headers, opts) -> |
| 72 | + assert method == :post |
| 73 | + fun = MockServer.mk_http_resp(200, MockServer.get_json("previewtemplate_simpleemail")) |
| 74 | + fun.(method, url, body, headers, opts) |
| 75 | + end] do |
| 76 | + resp = Template.preview(TestStruct.basic_template(), TestStruct.substitution_data()) |
| 77 | + assert %SparkPost.Address{ |
| 78 | + name: nil, |
| 79 | + "email": "marketing@bounces.company.example" |
| 80 | + } == resp.from |
| 81 | + end |
| 82 | + end |
| 83 | +end |
0 commit comments