diff --git a/lib/lob/check.ex b/lib/lob/check.ex new file mode 100644 index 0000000..d46e709 --- /dev/null +++ b/lib/lob/check.ex @@ -0,0 +1,8 @@ +defmodule Lob.Check do + @moduledoc """ + Module implementing the Lob checks API. + """ + + use Lob.ResourceBase, endpoint: "checks", methods: [:create, :retrieve, :list, :delete] + +end diff --git a/lib/lob/letter.ex b/lib/lob/letter.ex new file mode 100644 index 0000000..bc76063 --- /dev/null +++ b/lib/lob/letter.ex @@ -0,0 +1,8 @@ +defmodule Lob.Letter do + @moduledoc """ + Module implementing the Lob letters API. + """ + + use Lob.ResourceBase, endpoint: "letters", methods: [:create, :retrieve, :list, :delete] + +end diff --git a/test/assets/8.5x11.pdf b/test/assets/8.5x11.pdf new file mode 100644 index 0000000..ea4902b Binary files /dev/null and b/test/assets/8.5x11.pdf differ diff --git a/test/assets/logo.png b/test/assets/logo.png new file mode 100644 index 0000000..e7ecfe2 Binary files /dev/null and b/test/assets/logo.png differ diff --git a/test/lob/check_test.exs b/test/lob/check_test.exs new file mode 100644 index 0000000..d613c27 --- /dev/null +++ b/test/lob/check_test.exs @@ -0,0 +1,188 @@ +defmodule Lob.CheckTest do + use ExUnit.Case + + alias Lob.Check + + setup do + sample_address = %{ + name: "TestAddress", + email: "test@test.com", + address_line1: "185 Berry Street", + address_line2: "Suite 6100", + address_city: "San Francisco", + address_state: "CA", + address_country: "US", + address_zip: "94107" + } + + sample_check = %{ + description: "Library Test Check #{DateTime.utc_now |> DateTime.to_string}", + amount: 100 + } + + # TODO(anthony): Once address API is added to wrapper, replace this ID with a created address + # TODO(anthony): Once bank account API is added to wrapper, replace this ID with a created bank account + %{ + test_address_id: "adr_a7d78be7f746a0a7", + test_bank_account_id: "bank_ffbb58dbc5a51d8", + sample_address: sample_address, + sample_check: sample_check + } + end + + describe "list/2" do + + test "lists checks" do + {:ok, checks, _headers} = Check.list() + assert checks.object == "list" + end + + test "includes total count" do + {:ok, checks, _headers} = Check.list(%{include: ["total_count"]}) + assert Map.get(checks, :total_count) != nil + end + + test "lists by limit" do + {:ok, checks, _headers} = Check.list(%{limit: 2}) + assert checks.count == 2 + end + + test "filters by metadata" do + {:ok, checks, _headers} = Check.list(%{metadata: %{foo: "bar"}}) + assert checks.count == 1 + end + + end + + describe "retrieve/2" do + + test "retrieves a check", %{test_address_id: test_address_id, test_bank_account_id: test_bank_account_id, sample_check: sample_check} do + {:ok, created_check, _headers} = + Check.create(%{ + description: sample_check.description, + to: test_address_id, + from: test_address_id, + bank_account: test_bank_account_id, + amount: 42 + }) + + {:ok, retrieved_check, _headers} = Check.retrieve(created_check.id) + assert retrieved_check.description == created_check.description + end + + end + + describe "create/2" do + + test "creates a check with address_id", %{test_address_id: test_address_id, test_bank_account_id: test_bank_account_id, sample_check: sample_check} do + {:ok, created_check, headers} = + Check.create(%{ + description: sample_check.description, + to: test_address_id, + from: test_address_id, + bank_account: test_bank_account_id, + amount: 42 + }) + + assert created_check.description == sample_check.description + assert Enum.member?(headers, {"X-Rate-Limit-Limit", "150"}) + end + + test "creates a check with address params", %{sample_check: sample_check, test_bank_account_id: test_bank_account_id, sample_address: sample_address} do + {:ok, created_check, headers} = + Check.create(%{ + description: sample_check.description, + to: sample_address, + from: sample_address, + bank_account: test_bank_account_id, + amount: 42 + }) + + assert created_check.description == sample_check.description + assert Enum.member?(headers, {"X-Rate-Limit-Limit", "150"}) + end + + test "creates a check with logo, attachment and check bottom as URL", %{test_address_id: test_address_id, test_bank_account_id: test_bank_account_id, sample_check: sample_check} do + {:ok, created_check, headers} = + Check.create(%{ + description: sample_check.description, + to: test_address_id, + from: test_address_id, + bank_account: test_bank_account_id, + amount: 42, + logo: "http://via.placeholder.com/100x100", + check_bottom: "https://s3-us-west-2.amazonaws.com/lob-assets/letter-goblue.pdf", + attachment: "https://s3-us-west-2.amazonaws.com/lob-assets/letter-goblue.pdf" + }) + + assert created_check.description == sample_check.description + assert Enum.member?(headers, {"X-Rate-Limit-Limit", "150"}) + end + + test "creates a check with logo as PNG and attachment and check bottom as PDF", %{test_address_id: test_address_id, test_bank_account_id: test_bank_account_id, sample_check: sample_check} do + {:ok, created_check, headers} = + Check.create(%{ + description: sample_check.description, + to: test_address_id, + from: test_address_id, + bank_account: test_bank_account_id, + amount: 42, + logo: %{local_path: "test/assets/logo.png"}, + check_bottom: %{local_path: "test/assets/8.5x11.pdf"}, + attachment: %{local_path: "test/assets/8.5x11.pdf"} + }) + + assert created_check.description == sample_check.description + assert Enum.member?(headers, {"X-Rate-Limit-Limit", "150"}) + end + + test "creates a check with an idempotency key", %{test_address_id: test_address_id, test_bank_account_id: test_bank_account_id, sample_check: sample_check} do + idempotency_key = UUID.uuid4() + + {:ok, created_check, _headers} = + Check.create(%{ + description: sample_check.description, + to: test_address_id, + from: test_address_id, + bank_account: test_bank_account_id, + amount: 42 + }, %{ + "Idempotency-Key" => idempotency_key + }) + + {:ok, duplicated_postcard, _headers} = + Check.create(%{ + description: "Duplicated Check", + to: test_address_id, + from: test_address_id, + bank_account: test_bank_account_id, + amount: 42 + }, %{ + "Idempotency-Key" => idempotency_key + }) + + assert created_check.description == duplicated_postcard.description + end + + end + + describe "delete/2" do + + test "deletes a check", %{test_address_id: test_address_id, test_bank_account_id: test_bank_account_id, sample_check: sample_check} do + {:ok, created_check, _headers} = + Check.create(%{ + description: sample_check.description, + to: test_address_id, + from: test_address_id, + bank_account: test_bank_account_id, + amount: 42 + }) + + {:ok, deleted_check, _headers} = Check.delete(created_check.id) + assert deleted_check.id == created_check.id + assert deleted_check.deleted == true + end + + end + +end diff --git a/test/lob/letter_test.exs b/test/lob/letter_test.exs new file mode 100644 index 0000000..e168511 --- /dev/null +++ b/test/lob/letter_test.exs @@ -0,0 +1,165 @@ +defmodule Lob.LetterTest do + use ExUnit.Case + + alias Lob.Letter + + setup do + sample_address = %{ + name: "TestAddress", + email: "test@test.com", + address_line1: "185 Berry Street", + address_line2: "Suite 6100", + address_city: "San Francisco", + address_state: "CA", + address_country: "US", + address_zip: "94107" + } + + sample_letter = %{ + description: "Library Test Letter #{DateTime.utc_now |> DateTime.to_string}" + } + + # TODO(anthony): Once address API is added to wrapper, replace this ID with a created address + %{ + test_address_id: "adr_a7d78be7f746a0a7", + sample_address: sample_address, + sample_letter: sample_letter + } + end + + describe "list/2" do + + test "lists letters" do + {:ok, letters, _headers} = Letter.list() + assert letters.object == "list" + end + + test "includes total count" do + {:ok, letters, _headers} = Letter.list(%{include: ["total_count"]}) + assert Map.get(letters, :total_count) != nil + end + + test "lists by limit" do + {:ok, letters, _headers} = Letter.list(%{limit: 2}) + assert letters.count == 2 + end + + test "filters by metadata" do + {:ok, letters, _headers} = Letter.list(%{metadata: %{foo: "bar"}}) + assert letters.count == 1 + end + + end + + describe "retrieve/2" do + + test "retrieves a letter", %{test_address_id: test_address_id, sample_letter: sample_letter} do + {:ok, created_letter, _headers} = + Letter.create(%{ + description: sample_letter.description, + to: test_address_id, + from: test_address_id, + color: true, + file: "https://s3-us-west-2.amazonaws.com/lob-assets/letter-goblue.pdf" + }) + + {:ok, retrieved_letter, _headers} = Letter.retrieve(created_letter.id) + assert retrieved_letter.description == created_letter.description + end + + end + + describe "create/2" do + + test "creates a letter with address_id", %{test_address_id: test_address_id, sample_letter: sample_letter} do + {:ok, created_letter, headers} = + Letter.create(%{ + description: sample_letter.description, + to: test_address_id, + from: test_address_id, + color: true, + file: "https://s3-us-west-2.amazonaws.com/lob-assets/letter-goblue.pdf" + }) + + assert created_letter.description == sample_letter.description + assert Enum.member?(headers, {"X-Rate-Limit-Limit", "150"}) + end + + test "creates a letter with address params", %{sample_letter: sample_letter, sample_address: sample_address} do + {:ok, created_letter, headers} = + Letter.create(%{ + description: sample_letter.description, + to: sample_address, + from: sample_address, + color: true, + file: "https://s3-us-west-2.amazonaws.com/lob-assets/letter-goblue.pdf" + }) + + assert created_letter.description == sample_letter.description + assert Enum.member?(headers, {"X-Rate-Limit-Limit", "150"}) + end + + test "creates a letter with file as PDF", %{test_address_id: test_address_id, sample_letter: sample_letter} do + {:ok, created_letter, headers} = + Letter.create(%{ + description: sample_letter.description, + to: test_address_id, + from: test_address_id, + color: true, + file: %{local_path: "test/assets/8.5x11.pdf"} + }) + + assert created_letter.description == sample_letter.description + assert Enum.member?(headers, {"X-Rate-Limit-Limit", "150"}) + end + + test "creates a letter with an idempotency key", %{test_address_id: test_address_id, sample_letter: sample_letter} do + idempotency_key = UUID.uuid4() + + {:ok, created_letter, _headers} = + Letter.create(%{ + description: sample_letter.description, + to: test_address_id, + from: test_address_id, + color: true, + file: %{local_path: "test/assets/8.5x11.pdf"} + }, %{ + "Idempotency-Key" => idempotency_key + }) + + {:ok, duplicated_letter, _headers} = + Letter.create(%{ + description: "Duplicated Letter", + to: test_address_id, + from: test_address_id, + color: true, + file: %{local_path: "test/assets/8.5x11.pdf"} + }, %{ + "Idempotency-Key" => idempotency_key + }) + + assert created_letter.description == duplicated_letter.description + end + + end + + describe "delete/2" do + + test "deletes a letter", %{test_address_id: test_address_id, sample_letter: sample_letter} do + {:ok, created_letter, _headers} = + Letter.create(%{ + description: sample_letter.description, + to: test_address_id, + from: test_address_id, + color: true, + file: %{local_path: "test/assets/8.5x11.pdf"} + }) + + {:ok, deleted_letter, _headers} = Letter.delete(created_letter.id) + assert deleted_letter.id == created_letter.id + assert deleted_letter.deleted == true + end + + end + +end diff --git a/test/lob/postcard_test.exs b/test/lob/postcard_test.exs index ef6536a..43d142d 100644 --- a/test/lob/postcard_test.exs +++ b/test/lob/postcard_test.exs @@ -30,22 +30,22 @@ defmodule Lob.PostcardTest do describe "list/2" do - test "should list postcards" do + test "lists postcards" do {:ok, postcards, _headers} = Postcard.list() assert postcards.object == "list" end - test "should include total count" do + test "includes total count" do {:ok, postcards, _headers} = Postcard.list(%{include: ["total_count"]}) assert Map.get(postcards, :total_count) != nil end - test "should list by limit" do + test "lists by limit" do {:ok, postcards, _headers} = Postcard.list(%{limit: 2}) assert postcards.count == 2 end - test "should filter by metadata" do + test "filters by metadata" do {:ok, postcards, _headers} = Postcard.list(%{metadata: %{foo: "bar"}}) assert postcards.count == 1 end @@ -54,7 +54,7 @@ defmodule Lob.PostcardTest do describe "retrieve/2" do - test "should retrieve a postcard", %{test_address_id: test_address_id, sample_postcard: sample_postcard} do + test "retrieves a postcard", %{test_address_id: test_address_id, sample_postcard: sample_postcard} do {:ok, created_postcard, _headers} = Postcard.create(%{ description: sample_postcard.description, @@ -63,15 +63,15 @@ defmodule Lob.PostcardTest do back: "https://lob.com/postcardback.pdf" }) - {:ok, retrieved_postcard, _headers} = Postcard.retrieve(created_postcard.id) - assert retrieved_postcard.description == created_postcard.description + {:ok, retrieved_postcard, _headers} = Postcard.retrieve(created_postcard.id) + assert retrieved_postcard.description == created_postcard.description end end describe "create/2" do - test "should create a postcard with address_id", %{test_address_id: test_address_id, sample_postcard: sample_postcard} do + test "creates a postcard with address_id", %{test_address_id: test_address_id, sample_postcard: sample_postcard} do {:ok, created_postcard, headers} = Postcard.create(%{ description: sample_postcard.description, @@ -84,7 +84,7 @@ defmodule Lob.PostcardTest do assert Enum.member?(headers, {"X-Rate-Limit-Limit", "150"}) end - test "should create a postcard with to address params", %{sample_postcard: sample_postcard, sample_address: sample_address} do + test "creates a postcard with to address params", %{sample_postcard: sample_postcard, sample_address: sample_address} do {:ok, created_postcard, headers} = Postcard.create(%{ description: sample_postcard.description, @@ -97,7 +97,7 @@ defmodule Lob.PostcardTest do assert Enum.member?(headers, {"X-Rate-Limit-Limit", "150"}) end - test "should create a postcard with from address params", %{test_address_id: test_address_id, sample_postcard: sample_postcard, sample_address: sample_address} do + test "creates a postcard with from address params", %{test_address_id: test_address_id, sample_postcard: sample_postcard, sample_address: sample_address} do {:ok, created_postcard, headers} = Postcard.create(%{ description: sample_postcard.description, @@ -111,7 +111,7 @@ defmodule Lob.PostcardTest do assert Enum.member?(headers, {"X-Rate-Limit-Limit", "150"}) end - test "should create a postcard with front and back as urls", %{test_address_id: test_address_id, sample_postcard: sample_postcard} do + test "creates a postcard with front and back as urls", %{test_address_id: test_address_id, sample_postcard: sample_postcard} do {:ok, created_postcard, headers} = Postcard.create(%{ description: sample_postcard.description, @@ -124,7 +124,7 @@ defmodule Lob.PostcardTest do assert Enum.member?(headers, {"X-Rate-Limit-Limit", "150"}) end - test "should create a postcard with front and back as PDFs", %{test_address_id: test_address_id, sample_postcard: sample_postcard} do + test "creates a postcard with front and back as PDFs", %{test_address_id: test_address_id, sample_postcard: sample_postcard} do {:ok, created_postcard, headers} = Postcard.create(%{ description: sample_postcard.description, @@ -137,7 +137,7 @@ defmodule Lob.PostcardTest do assert Enum.member?(headers, {"X-Rate-Limit-Limit", "150"}) end - test "should create a postcard with an idempotency key", %{test_address_id: test_address_id, sample_postcard: sample_postcard} do + test "creates a postcard with an idempotency key", %{test_address_id: test_address_id, sample_postcard: sample_postcard} do idempotency_key = UUID.uuid4() {:ok, created_postcard, _headers} = @@ -167,7 +167,7 @@ defmodule Lob.PostcardTest do describe "delete/2" do - test "should destroy a postcard", %{test_address_id: test_address_id, sample_postcard: sample_postcard} do + test "deletes a postcard", %{test_address_id: test_address_id, sample_postcard: sample_postcard} do {:ok, created_postcard, _headers} = Postcard.create(%{ description: sample_postcard.description,