Skip to content

Commit

Permalink
Mock tests shouldn't use gringotts.ex
Browse files Browse the repository at this point in the history
  • Loading branch information
oyeb committed Mar 20, 2018
1 parent 7192d0b commit fea679e
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions test/gateways/cams_test.exs
Expand Up @@ -45,7 +45,8 @@ defmodule Gringotts.Gateways.CamsTest do
@options [
order_id: 1,
billing_address: @address,
description: "Store Purchase"
description: "Store Purchase",
config: @auth
]

@money Money.new(:USD, 100)
Expand All @@ -56,26 +57,19 @@ defmodule Gringotts.Gateways.CamsTest do
@id "some_transaction_id"
@bad_id "some_fake_transaction_id"

setup_all do
Application.put_env(:gringotts, Gateway,
username: "some_secret_user_name",
password: "some_secret_password"
)
end

describe "purchase" do
test "with correct params" do
with_mock HTTPoison,
post: fn _url, _body, _headers -> MockResponse.successful_purchase() end do
assert {:ok, %Response{}} = Gringotts.purchase(Gateway, @money, @card, @options)
assert {:ok, %Response{}} = Gateway.purchase(@money, @card, @options)
end
end

test "with bad card" do
with_mock HTTPoison,
post: fn _url, _body, _headers -> MockResponse.failed_purchase_with_bad_credit_card() end do
{:error, %Response{reason: reason}} =
Gringotts.purchase(Gateway, @money, @bad_card, @options)
Gateway.purchase(@money, @bad_card, @options)

assert String.contains?(reason, "Invalid Credit Card Number")
end
Expand All @@ -84,7 +78,7 @@ defmodule Gringotts.Gateways.CamsTest do
test "with invalid currency" do
with_mock HTTPoison,
post: fn _url, _body, _headers -> MockResponse.with_invalid_currency() end do
{:error, %Response{reason: reason}} = Gringotts.purchase(Gateway, @bad_currency, @card, @options)
{:error, %Response{reason: reason}} = Gateway.purchase(@bad_currency, @card, @options)
assert String.contains?(reason, "The cc payment type")
end
end
Expand All @@ -94,15 +88,15 @@ defmodule Gringotts.Gateways.CamsTest do
test "with correct params" do
with_mock HTTPoison,
post: fn _url, _body, _headers -> MockResponse.successful_authorize() end do
assert {:ok, %Response{}} = Gringotts.authorize(Gateway, @money, @card, @options)
assert {:ok, %Response{}} = Gateway.authorize(@money, @card, @options)
end
end

test "with bad card" do
with_mock HTTPoison,
post: fn _url, _body, _headers -> MockResponse.failed_authorized_with_bad_card() end do
{:error, %Response{reason: reason}} =
Gringotts.authorize(Gateway, @money, @bad_card, @options)
Gateway.authorize(@money, @bad_card, @options)

assert String.contains?(reason, "Invalid Credit Card Number")
end
Expand All @@ -113,22 +107,22 @@ defmodule Gringotts.Gateways.CamsTest do
test "with full amount" do
with_mock HTTPoison, post: fn _url, _body, _headers -> MockResponse.successful_capture() end do
assert {:ok, %Response{}} =
Gringotts.capture(Gateway, @money, @id , @options)
Gateway.capture(@money, @id , @options)
end
end

test "with partial amount" do
with_mock HTTPoison, post: fn _url, _body, _headers -> MockResponse.successful_capture() end do
assert {:ok, %Response{}} =
Gringotts.capture(Gateway, @money_less, @id , @options)
Gateway.capture(@money_less, @id , @options)
end
end

test "with invalid transaction_id" do
with_mock HTTPoison,
post: fn _url, _body, _headers -> MockResponse.invalid_transaction_id() end do
{:error, %Response{reason: reason}} =
Gringotts.capture(Gateway, @money, @bad_id, @options)
Gateway.capture(@money, @bad_id, @options)

assert String.contains?(reason, "Transaction not found")
end
Expand All @@ -138,7 +132,7 @@ defmodule Gringotts.Gateways.CamsTest do
with_mock HTTPoison,
post: fn _url, _body, _headers -> MockResponse.more_than_authorization_amount() end do
{:error, %Response{reason: reason}} =
Gringotts.capture(Gateway, @money_more, @id , @options)
Gateway.capture(@money_more, @id , @options)

assert String.contains?(reason, "exceeds the authorization amount")
end
Expand All @@ -148,7 +142,7 @@ defmodule Gringotts.Gateways.CamsTest do
with_mock HTTPoison,
post: fn _url, _body, _headers -> MockResponse.multiple_capture_on_same_transaction() end do
{:error, %Response{reason: reason}} =
Gringotts.capture(Gateway, @money, @id , @options)
Gateway.capture(@money, @id , @options)

assert String.contains?(reason, "A capture requires that")
end
Expand All @@ -159,15 +153,15 @@ defmodule Gringotts.Gateways.CamsTest do
test "with correct params" do
with_mock HTTPoison, post: fn _url, _body, _headers -> MockResponse.successful_refund() end do
assert {:ok, %Response{}} =
Gringotts.refund(Gateway, @money, @id , @options)
Gateway.refund(@money, @id , @options)
end
end

test "with more than purchased amount" do
with_mock HTTPoison,
post: fn _url, _body, _headers -> MockResponse.more_than_purchase_amount() end do
{:error, %Response{reason: reason}} =
Gringotts.refund(Gateway, @money_more, @id , @options)
Gateway.refund(@money_more, @id , @options)

assert String.contains?(reason, "Refund amount may not exceed")
end
Expand All @@ -177,15 +171,15 @@ defmodule Gringotts.Gateways.CamsTest do
describe "void" do
test "with correct params" do
with_mock HTTPoison, post: fn _url, _body, _headers -> MockResponse.successful_void() end do
{:ok, %Response{message: message}} = Gringotts.void(Gateway, @id , @options)
{:ok, %Response{message: message}} = Gateway.void(@id , @options)
assert String.contains?(message, "Void Successful")
end
end

test "with invalid transaction_id" do
with_mock HTTPoison,
post: fn _url, _body, _headers -> MockResponse.invalid_transaction_id() end do
{:error, %Response{reason: reason}} = Gringotts.void(Gateway, @bad_id, @options)
{:error, %Response{reason: reason}} = Gateway.void(@bad_id, @options)
assert String.contains?(reason, "Transaction not found")
end
end
Expand Down

0 comments on commit fea679e

Please sign in to comment.