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

feat(letters-checks): adds letter and check resource ENG-4760 #3

Merged
merged 2 commits into from
Jan 18, 2018
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
8 changes: 8 additions & 0 deletions lib/lob/check.ex
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions lib/lob/letter.ex
Original file line number Diff line number Diff line change
@@ -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
Binary file added test/assets/8.5x11.pdf
Binary file not shown.
Binary file added test/assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
188 changes: 188 additions & 0 deletions test/lob/check_test.exs
Original file line number Diff line number Diff line change
@@ -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
165 changes: 165 additions & 0 deletions test/lob/letter_test.exs
Original file line number Diff line number Diff line change
@@ -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
Loading