This repository has been archived by the owner on Apr 14, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from solidusio/transaction-controller-spec
Raise error for invalid transactions and add specs
- Loading branch information
Showing
3 changed files
with
247 additions
and
1 deletion.
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
102 changes: 102 additions & 0 deletions
102
spec/controllers/solidus_paypal_braintree/transactions_controller_spec.rb
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,102 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe SolidusPaypalBraintree::TransactionsController, type: :controller do | ||
include_context "order ready for payment" | ||
|
||
let(:payment_method) { create_gateway } | ||
|
||
before do | ||
allow(controller).to receive(:spree_current_user) { user } | ||
allow(controller).to receive(:current_order) { order } | ||
end | ||
|
||
cassette_options = { cassette_name: "transactions_controller/create" } | ||
describe "POST create", vcr: cassette_options do | ||
subject(:post_create) { post :create, params } | ||
|
||
let(:params) do | ||
{ | ||
transaction: { | ||
nonce: "ABC123", | ||
payment_type: "MonopolyMoney", | ||
phone: "1112223333", | ||
email: "[email protected]", | ||
address_attributes: { | ||
first_name: "Wade", | ||
last_name: "Wilson", | ||
address_line_1: "123 Fake Street", | ||
city: "Seattle", | ||
zip: "98101", | ||
state_code: "WA", | ||
country_code: "US" | ||
} | ||
}, | ||
payment_method_id: payment_method.id | ||
} | ||
end | ||
|
||
context "when the transaction is valid" do | ||
it "imports the payment" do | ||
expect { post_create }.to change { order.payments.count }.by(1) | ||
expect(order.payments.first.amount).to eq 55 | ||
end | ||
|
||
context "and a valid address is provided" do | ||
it "creates a new address" do | ||
# Creating the order also creates 3 addresses, we want to make sure | ||
# the transaction import only creates 1 new one | ||
order | ||
expect { post_create }.to change { Spree::Address.count }.by(1) | ||
expect(Spree::Address.last.full_name).to eq "Wade Wilson" | ||
end | ||
end | ||
|
||
context "and an invalid address is provided" do | ||
before { params[:transaction][:address_attributes][:city] = nil } | ||
|
||
it "raises a validation error" do | ||
expect { post_create }.to raise_error( | ||
ActiveRecord::RecordInvalid, | ||
"Validation failed: " \ | ||
"Billing address city can't be blank, " \ | ||
"Shipping address city can't be blank" | ||
) | ||
end | ||
end | ||
|
||
context "and the transaction does not have an address" do | ||
before { params[:transaction].delete(:address_attributes) } | ||
|
||
it "does not create a new address" do | ||
order | ||
expect { post_create }.to_not change { Spree::Address.count } | ||
end | ||
end | ||
|
||
context "when import! leaves the order in confirm" do | ||
it "redirects the user to the confirm page" do | ||
expect(post_create).to redirect_to spree.checkout_state_path("confirm") | ||
end | ||
end | ||
|
||
context "when import! completes the order" do | ||
before { allow(order).to receive(:complete?).and_return(true) } | ||
|
||
it "displays the order to the user" do | ||
expect(post_create).to redirect_to spree.order_path(order) | ||
end | ||
end | ||
end | ||
|
||
context "when the transaction is invalid" do | ||
before { params[:transaction].delete(:phone) } | ||
|
||
it "raises an error" do | ||
expect { post_create }.to raise_error( | ||
SolidusPaypalBraintree::TransactionsController::InvalidTransactionError, | ||
"Transaction invalid: Phone can't be blank" | ||
) | ||
end | ||
end | ||
end | ||
end |
141 changes: 141 additions & 0 deletions
141
spec/fixtures/cassettes/transactions_controller/create.yml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.