refactor payment model and its tests
This commit is contained in:
parent
25c0046290
commit
c4d934d711
4 changed files with 111 additions and 39 deletions
|
|
@ -2,10 +2,19 @@ FactoryGirl.define do
|
|||
factory :payment do
|
||||
first_name { "#{Faker::Hipster.word} abc" }
|
||||
last_name { "#{Faker::Hipster.word} xyz" }
|
||||
credit_card_number { '4242424242424242' }
|
||||
card_verification_value { '123' }
|
||||
expiration_month { '06' }
|
||||
credit_card_number '4242424242424111'
|
||||
card_verification_value '123'
|
||||
expiration_month 6
|
||||
expiration_year { Date.current.year + 2 }
|
||||
amount { '10' }
|
||||
amount 10
|
||||
end
|
||||
|
||||
trait :invalid_credit_card do
|
||||
credit_card_number '4242424242424222'
|
||||
end
|
||||
|
||||
trait :exception_credit_card do
|
||||
credit_card_number '4242424242424333'
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ feature Registration do
|
|||
fill_in 'last_name', with: 'bar'
|
||||
select Date.current.year + 2, from: 'expiration_year'
|
||||
fill_in 'card_verification_value', with: '123'
|
||||
fill_in 'credit_card_number', with: '3'
|
||||
fill_in 'credit_card_number', with: '4242424242423333'
|
||||
|
||||
click_button 'Charge Card'
|
||||
|
||||
|
|
@ -65,7 +65,7 @@ feature Registration do
|
|||
fill_in 'last_name', with: 'bar'
|
||||
select Date.current.year + 2, from: 'expiration_year'
|
||||
fill_in 'card_verification_value', with: '123'
|
||||
fill_in 'credit_card_number', with: '2'
|
||||
fill_in 'credit_card_number', with: '4242424242422222'
|
||||
|
||||
click_button 'Charge Card'
|
||||
|
||||
|
|
@ -93,7 +93,7 @@ feature Registration do
|
|||
fill_in 'last_name', with: 'bar'
|
||||
select Date.current.year + 2, from: 'expiration_year'
|
||||
fill_in 'card_verification_value', with: '123'
|
||||
fill_in 'credit_card_number', with: '4242424242424242'
|
||||
fill_in 'credit_card_number', with: '4242424242421111'
|
||||
|
||||
click_button 'Charge Card'
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,13 @@ require 'spec_helper'
|
|||
|
||||
describe Payment do
|
||||
|
||||
context 'new payment' do
|
||||
let(:payment) { create(:payment) }
|
||||
it 'sets status to "unpaid" by default' do
|
||||
expect(payment.status).to eq('unpaid')
|
||||
end
|
||||
end
|
||||
|
||||
describe 'validations' do
|
||||
it 'has a valid factory' do
|
||||
expect(build(:payment)).to be_valid
|
||||
|
|
@ -35,28 +42,85 @@ describe Payment do
|
|||
|
||||
end
|
||||
|
||||
describe 'purchase' do
|
||||
let!(:participant) { create(:user) }
|
||||
describe '#purchase' do
|
||||
let!(:user) { create(:user) }
|
||||
let!(:ticket_1) { create(:ticket) }
|
||||
let!(:conference) { create(:conference, tickets: [ticket_1]) }
|
||||
let(:payment) { create(:payment) }
|
||||
|
||||
it 'creates a purchase and payment for one ticket' do
|
||||
tickets = { ticket_1.id.to_s => '1' }
|
||||
message = TicketPurchase.purchase(conference, participant, tickets)
|
||||
purchase = TicketPurchase.where(conference_id: conference.id,
|
||||
user_id: participant.id,
|
||||
ticket_id: ticket_1.id).first
|
||||
it 'calls the payment gateway with the correct parameters' do
|
||||
expect(GATEWAY).to receive(:purchase).with(1000, payment.credit_card, currency: 'USD')
|
||||
.and_return(ActiveMerchant::Billing::Response.new(true, 'Success.'))
|
||||
|
||||
expect(TicketPurchase.count).to eq(1)
|
||||
expect(purchase.quantity).to eq(1)
|
||||
expect(message.blank?).to be true
|
||||
payment.purchase(user, conference, 1000)
|
||||
end
|
||||
|
||||
payment.purchase(participant, conference, 1000)
|
||||
context 'when the payment is successful' do
|
||||
before { payment.purchase(user, conference, 1000) }
|
||||
|
||||
purchase = Payment.first
|
||||
expect(Payment.count).to be(1)
|
||||
expect(purchase.amount).to eq(10)
|
||||
it 'returns true' do
|
||||
payment_result = payment.purchase(user, conference, 1000)
|
||||
expect(payment_result).to eq true
|
||||
end
|
||||
|
||||
it "assigns 'success' to payment.status" do
|
||||
expect(payment.status).to eq('success')
|
||||
end
|
||||
|
||||
it 'assigns user_id' do
|
||||
expect(payment.user_id).to eq(user.id)
|
||||
end
|
||||
|
||||
it 'assigns conference_id' do
|
||||
expect(payment.conference_id).to eq(conference.id)
|
||||
end
|
||||
|
||||
it 'assigns last4' do
|
||||
expect(payment.last4).to eq('XXXX-XXXX-XXXX-4111')
|
||||
end
|
||||
|
||||
it 'assigns authorization_code' do
|
||||
expect(payment.authorization_code).to eq("53433")
|
||||
end
|
||||
end
|
||||
|
||||
context 'if the payment is not successful' do
|
||||
before { payment.purchase(user, conference, 1000) }
|
||||
|
||||
let(:payment) { create(:payment, :invalid_credit_card) }
|
||||
|
||||
context 'when the card is invalid' do
|
||||
it 'returns false' do
|
||||
payment_result = payment.purchase(user, conference, 1000)
|
||||
expect(payment_result).to eq false
|
||||
end
|
||||
|
||||
it 'assigns "failure" to payment.status' do
|
||||
expect(payment.status).to eq('failure')
|
||||
end
|
||||
|
||||
it 'adds errors' do
|
||||
expect(payment.errors[:base].count).to eq(1)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when there is a connection problem with the gateway' do
|
||||
let(:payment) { create(:payment, :exception_credit_card) }
|
||||
|
||||
it 'returns false' do
|
||||
payment_result = payment.purchase(user, conference, 1000)
|
||||
expect(payment_result).to eq false
|
||||
end
|
||||
|
||||
it 'assigns "failure" to payment.status' do
|
||||
expect(payment.status).to eq('failure')
|
||||
end
|
||||
|
||||
it 'adds errors' do
|
||||
expect(payment.errors[:base].count).to eq(1)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue