Merge 4d4736f042 into 424b1302cb
This commit is contained in:
commit
211410b6f0
33 changed files with 621 additions and 91 deletions
0
spec/models/conference_spec.rb
Executable file → Normal file
0
spec/models/conference_spec.rb
Executable file → Normal file
148
spec/models/payment_spec.rb
Normal file
148
spec/models/payment_spec.rb
Normal file
|
|
@ -0,0 +1,148 @@
|
|||
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
|
||||
end
|
||||
|
||||
it { is_expected.to validate_presence_of(:full_name) }
|
||||
|
||||
it { is_expected.to validate_presence_of(:credit_card_number) }
|
||||
|
||||
it { is_expected.to validate_presence_of(:card_verification_value) }
|
||||
|
||||
it { is_expected.to validate_presence_of(:expiration_month) }
|
||||
|
||||
it { is_expected.to validate_presence_of(:expiration_year) }
|
||||
|
||||
it { is_expected.to validate_presence_of(:amount) }
|
||||
|
||||
it { is_expected.to validate_presence_of(:user_id) }
|
||||
|
||||
it { is_expected.to validate_presence_of(:conference_id) }
|
||||
|
||||
it 'is not valid with a amount equals zero' do
|
||||
should_not allow_value(0).for(:amount)
|
||||
end
|
||||
|
||||
it 'is not valid with a amount smaller than zero' do
|
||||
should_not allow_value(-1).for(:amount)
|
||||
end
|
||||
|
||||
it 'is valid with a amount greater than zero' do
|
||||
should allow_value(1).for(:amount)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe '#credit_card' do
|
||||
let(:payment) { create(:payment) }
|
||||
|
||||
it 'assigns correct "month"' do
|
||||
expect(payment.credit_card.month).to eq(6)
|
||||
end
|
||||
|
||||
it 'assigns correct "year"' do
|
||||
expect(payment.credit_card.year).to eq(Date.current.year + 2)
|
||||
end
|
||||
|
||||
it 'assigns correct "verification_value"' do
|
||||
expect(payment.credit_card.verification_value).to eq('123')
|
||||
end
|
||||
|
||||
it 'assigns correct "card_number"' do
|
||||
expect(payment.credit_card.display_number).to eq('XXXX-XXXX-XXXX-4111')
|
||||
end
|
||||
end
|
||||
|
||||
describe '#amount_to_pay' do
|
||||
let!(:user) { create(:user) }
|
||||
let!(:conference) { create(:conference) }
|
||||
let(:ticket_1) { create(:ticket, price: 10, price_currency: 'USD', conference: conference) }
|
||||
let(:payment) { create(:payment, user: user, conference: conference) }
|
||||
|
||||
it ' returns correct unpaid amount' do
|
||||
create(:ticket_purchase, ticket: ticket_1, user: user, quantity: 8)
|
||||
expect(payment.amount_to_pay).to eq(8000)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#purchase' do
|
||||
let!(:user) { create(:user) }
|
||||
let!(:ticket_1) { create(:ticket) }
|
||||
let!(:conference) { create(:conference, tickets: [ticket_1]) }
|
||||
let!(:payment) { create(:payment, user: user, conference: conference) }
|
||||
|
||||
let!(:tickets) { {ticket_1.id.to_s => '1'} }
|
||||
|
||||
before { TicketPurchase.purchase(conference, user, tickets) }
|
||||
|
||||
context 'when the payment is successful' do
|
||||
before { payment.purchase }
|
||||
|
||||
it 'returns true' do
|
||||
payment_result = payment.purchase
|
||||
expect(payment_result).to eq true
|
||||
end
|
||||
|
||||
it "assigns 'success' to payment.status" do
|
||||
expect(payment.status).to eq('success')
|
||||
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 }
|
||||
|
||||
let(:payment) { create(:payment, :invalid_credit_card) }
|
||||
|
||||
context 'when the card is invalid' do
|
||||
it 'returns false' do
|
||||
payment_result = payment.purchase
|
||||
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
|
||||
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
|
||||
|
|
@ -82,31 +82,72 @@ describe Ticket do
|
|||
end
|
||||
end
|
||||
|
||||
describe '#quantity_bought_by' do
|
||||
it 'returns the correct value if the user has bought this ticket' do
|
||||
create(:ticket_purchase,
|
||||
user: user,
|
||||
ticket: ticket,
|
||||
quantity: 20)
|
||||
expect(ticket.quantity_bought_by(user)).to eq(20)
|
||||
describe '#unpaid?' do
|
||||
let!(:ticket_purchase) { create(:ticket_purchase, user: user, ticket: ticket) }
|
||||
|
||||
context 'user has not paid' do
|
||||
|
||||
it 'returns true' do
|
||||
expect(ticket.unpaid?(user)).to eq(true)
|
||||
end
|
||||
end
|
||||
|
||||
it 'returns zero if the user has not bought this ticket' do
|
||||
expect(ticket.quantity_bought_by(user)).to eq(0)
|
||||
context 'user has paid' do
|
||||
before { ticket_purchase.update_attributes(paid: true) }
|
||||
|
||||
it 'returns false' do
|
||||
expect(ticket.unpaid?(user)).to eq(false)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#quantity_bought_by' do
|
||||
context 'user has not paid' do
|
||||
it 'returns the correct value if the user has bought this ticket' do
|
||||
create(:ticket_purchase,
|
||||
user: user,
|
||||
ticket: ticket,
|
||||
quantity: 20)
|
||||
expect(ticket.quantity_bought_by(user, paid: false)).to eq(20)
|
||||
end
|
||||
|
||||
it 'returns zero if the user has not bought this ticket' do
|
||||
expect(ticket.quantity_bought_by(user, paid: false)).to eq(0)
|
||||
end
|
||||
end
|
||||
|
||||
context 'user has paid' do
|
||||
let!(:ticket_purchase) { create(:ticket_purchase, user: user, ticket: ticket, quantity: 20) }
|
||||
before { ticket_purchase.update_attributes(paid: true) }
|
||||
|
||||
it 'returns the correct value if the user has bought and paid for this ticket' do
|
||||
expect(ticket.quantity_bought_by(user, paid: true)).to eq(20)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#total_price' do
|
||||
it 'returns the correct value if the user has bought this ticket' do
|
||||
create(:ticket_purchase,
|
||||
user: user,
|
||||
ticket: ticket,
|
||||
quantity: 20)
|
||||
expect(ticket.total_price(user)).to eq(Money.new(100000, 'USD'))
|
||||
context 'user has not paid' do
|
||||
it 'returns the correct value if the user has bought this ticket' do
|
||||
create(:ticket_purchase,
|
||||
user: user,
|
||||
ticket: ticket,
|
||||
quantity: 20)
|
||||
expect(ticket.total_price(user, paid: false)).to eq(Money.new(100000, 'USD'))
|
||||
end
|
||||
|
||||
it 'returns zero if the user has not bought this ticket' do
|
||||
expect(ticket.total_price(user, paid: false)).to eq(Money.new(0, 'USD'))
|
||||
end
|
||||
end
|
||||
|
||||
it 'returns zero if the user has not bought this ticket' do
|
||||
expect(ticket.total_price(user)).to eq(Money.new(0, 'USD'))
|
||||
context 'user has paid' do
|
||||
let!(:ticket_purchase) { create(:ticket_purchase, user: user, ticket: ticket, quantity: 20) }
|
||||
before { ticket_purchase.update_attributes(paid: true) }
|
||||
|
||||
it 'returns the correct value if the user has bought this ticket' do
|
||||
expect(ticket.total_price(user, paid: true)).to eq(Money.new(100000, 'USD'))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -116,7 +157,7 @@ describe Ticket do
|
|||
describe 'user has bought' do
|
||||
context 'no tickets' do
|
||||
it 'returns zero' do
|
||||
expect(Ticket.total_price(conference, user)).to eq(Money.new(0, 'USD'))
|
||||
expect(Ticket.total_price(conference, user, paid: false)).to eq(Money.new(0, 'USD'))
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -126,7 +167,7 @@ describe Ticket do
|
|||
end
|
||||
|
||||
it 'returns the correct total price' do
|
||||
expect(Ticket.total_price(conference, user)).to eq(Money.new(100000, 'USD'))
|
||||
expect(Ticket.total_price(conference, user, paid: false)).to eq(Money.new(100000, 'USD'))
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -138,7 +179,7 @@ describe Ticket do
|
|||
|
||||
it 'returns the correct total price' do
|
||||
total_price = Money.new(200000, 'USD')
|
||||
expect(Ticket.total_price(conference, user)).to eq(total_price)
|
||||
expect(Ticket.total_price(conference, user, paid: false)).to eq(total_price)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue