-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write policy and tests for Stripe Customer
Add StripeCustomerPolicy.create? Add StripeCustomerController.create and .show Moved slugged routes to bottom of router, to allow for authenticated :show endpoints Call stripe service to create customer (requires test API key in dev) Add attributes to stripe_customer, including email and id which hide when not current user
- Loading branch information
1 parent
7ebaf31
commit f06c57e
Showing
27 changed files
with
398 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,29 @@ | ||
defmodule CodeCorps.Stripe.Adapters.StripeCustomer do | ||
import CodeCorps.MapUtils, only: [rename: 3] | ||
import CodeCorps.MapUtils, only: [rename: 3, keys_to_string: 1] | ||
|
||
def params_from_stripe(%{} = stripe_map) do | ||
stripe_map |> rename("id", "id_from_stripe") | ||
def to_params(%Stripe.Customer{} = customer) do | ||
customer | ||
|> Map.from_struct | ||
|> Map.take([:created, :currency, :delinquent, :id, :email]) | ||
|> rename(:id, :id_from_stripe) | ||
|> keys_to_string | ||
end | ||
|
||
@non_stripe_attribute_keys ["user_id"] | ||
|
||
def add_non_stripe_attributes(%{} = params, %{} = attributes) do | ||
attributes | ||
|> get_non_stripe_attributes | ||
|> add_to(params) | ||
end | ||
|
||
defp get_non_stripe_attributes(%{} = attributes) do | ||
attributes | ||
|> Map.take(@non_stripe_attribute_keys) | ||
end | ||
|
||
defp add_to(%{} = attributes, %{} = params) do | ||
params | ||
|> Map.merge(attributes) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
defmodule CodeCorps.Stripe.InMemory do | ||
defmodule Customer do | ||
def create(map) do | ||
{:ok, do_create(map)} | ||
end | ||
|
||
defp do_create(_) do | ||
{:ok, created} = DateTime.from_unix(1479472835) | ||
|
||
%Stripe.Customer{ | ||
id: "cus_9aMOFmqy1esIRE", | ||
account_balance: 0, | ||
created: created, | ||
currency: "usd", | ||
default_source: nil, | ||
delinquent: false, | ||
description: nil, | ||
email: "mail@test.com", | ||
livemode: false, | ||
metadata: %{} | ||
} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
defmodule CodeCorps.StripeService do | ||
@stripe Application.get_env(:code_corps, :stripe) | ||
|
||
def create_customer(map) do | ||
@stripe.Customer.create(map) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
defmodule CodeCorps.StripeCustomerControllerTest do | ||
use CodeCorps.ApiCase, resource_name: :stripe_customer | ||
|
||
describe "show" do | ||
@tag :authenticated | ||
test "shows chosen resource when user is authenticated and authorized", %{conn: conn, current_user: current_user} do | ||
stripe_customer = insert(:stripe_customer, user: current_user) | ||
|
||
conn | ||
|> request_show(stripe_customer) | ||
|> json_response(200) | ||
|> Map.get("data") | ||
|> assert_result_id(stripe_customer.id) | ||
end | ||
|
||
test "renders 401 when unauthenticated", %{conn: conn} do | ||
stripe_customer = insert(:stripe_customer) | ||
assert conn |> request_show(stripe_customer) |> json_response(401) | ||
end | ||
|
||
@tag :authenticated | ||
test "renders 403 when not authorized", %{conn: conn} do | ||
stripe_customer = insert(:stripe_customer) | ||
assert conn |> request_show(stripe_customer) |> json_response(403) | ||
end | ||
|
||
@tag :authenticated | ||
test "renders 404 when id is nonexistent", %{conn: conn} do | ||
assert conn |> request_show(:not_found) |> json_response(404) | ||
end | ||
end | ||
|
||
describe "create" do | ||
@tag :authenticated | ||
test "creates and renders resource user is authenticated and authorized", %{conn: conn, current_user: current_user} do | ||
assert conn |> request_create(%{user: current_user}) |> json_response(201) | ||
end | ||
|
||
test "does not create resource and renders 401 when unauthenticated", %{conn: conn} do | ||
assert conn |> request_create |> json_response(401) | ||
end | ||
|
||
@tag :authenticated | ||
test "does not create resource and renders 403 when not authorized", %{conn: conn} do | ||
assert conn |> request_create |> json_response(403) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 37 additions & 5 deletions
42
test/lib/code_corps/stripe/adapters/stripe_customer_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,46 @@ | ||
defmodule CodeCorps.Stripe.Adapters.StripeCustomerTest do | ||
use ExUnit.Case, async: true | ||
|
||
import CodeCorps.Stripe.Adapters.StripeCustomer, only: [params_from_stripe: 1] | ||
import CodeCorps.Stripe.Adapters.StripeCustomer, only: [to_params: 1, add_non_stripe_attributes: 2] | ||
|
||
@stripe_map %{"id" => "str_123", "foo" => "bar"} | ||
@local_map %{"id_from_stripe" => "str_123", "foo" => "bar"} | ||
{:ok, timestamp} = DateTime.from_unix(1479472835) | ||
|
||
describe "params_from_stripe/1" do | ||
@stripe_customer %Stripe.Customer{ | ||
id: "cus_9aMOFmqy1esIRE", | ||
account_balance: 0, | ||
created: timestamp, | ||
currency: "usd", | ||
default_source: nil, | ||
delinquent: false, | ||
description: nil, | ||
email: "mail@stripe.com", | ||
livemode: false, | ||
metadata: %{} | ||
} | ||
|
||
@local_map %{ | ||
"id_from_stripe" => "cus_9aMOFmqy1esIRE", | ||
"created" => timestamp, | ||
"currency" => "usd", | ||
"delinquent" => false, | ||
"email" => "mail@stripe.com" | ||
} | ||
|
||
describe "to_params/1" do | ||
test "converts from stripe map to local properly" do | ||
assert @stripe_map |> params_from_stripe == @local_map | ||
assert @stripe_customer |> to_params == @local_map | ||
end | ||
end | ||
|
||
describe "add_non_stripe_attributes/2" do | ||
test "adds 'user_id' from second hash into first hash" do | ||
params = %{"id_from_stripe" => "cus_123"} | ||
attributes = %{"user_id" =>123, "foo" => "bar"} | ||
|
||
actual_output = params |> add_non_stripe_attributes(attributes) | ||
expected_output = %{"id_from_stripe" => "cus_123", "user_id" => 123} | ||
|
||
assert actual_output == expected_output | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
defmodule CodeCorps.StripeCustomerPolicyTest do | ||
use CodeCorps.PolicyCase | ||
|
||
import CodeCorps.StripeCustomerPolicy, only: [show?: 2] | ||
|
||
describe "show?" do | ||
test "returns true when user is an admin" do | ||
user = build(:user, admin: true) | ||
stripe_customer = build(:stripe_customer) | ||
|
||
assert show?(user, stripe_customer) | ||
end | ||
|
||
test "returns true when user is viewing their own information" do | ||
user = insert(:user) | ||
stripe_customer = insert(:stripe_customer, user: user) | ||
|
||
assert show?(user, stripe_customer) | ||
end | ||
|
||
test "returns false when user id is not the StripeCustomer's user_id" do | ||
[user, another_user] = insert_pair(:user) | ||
stripe_customer = insert(:stripe_customer, user: user) | ||
|
||
refute show?(another_user, stripe_customer) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.