diff --git a/app/models/payment.rb b/app/models/payment.rb index 5694303d..6d5a769f 100644 --- a/app/models/payment.rb +++ b/app/models/payment.rb @@ -47,7 +47,7 @@ class Payment < ActiveRecord::Base end unless response.success? errors.add(:base, response.message) - self.status = 2 + self.status = 'failure' return false end self.user_id = user.id diff --git a/spec/controllers/conference_registration_controller_spec.rb b/spec/controllers/conference_registration_controller_spec.rb index 3c5655f0..d47d9271 100644 --- a/spec/controllers/conference_registration_controller_spec.rb +++ b/spec/controllers/conference_registration_controller_spec.rb @@ -42,7 +42,7 @@ describe ConferenceRegistrationsController, type: :controller do it 'does not assign price of purchased tickets to total_price and purchased tickets to tickets without payment' do expect(assigns(:total_price)).to eq Money.new(0, 'USD') - expect(assigns(:tickets)).not_to match_array [@purchased_ticket] + expect(assigns(:tickets)).not_to exist end end diff --git a/spec/factories/payments.rb b/spec/factories/payments.rb new file mode 100644 index 00000000..e4ccaee4 --- /dev/null +++ b/spec/factories/payments.rb @@ -0,0 +1,11 @@ +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' } + expiration_year { Date.current.year + 2 } + amount { '10' } + end +end diff --git a/spec/models/conference_spec.rb b/spec/models/conference_spec.rb old mode 100755 new mode 100644 diff --git a/spec/models/payment_spec.rb b/spec/models/payment_spec.rb new file mode 100644 index 00000000..cd60b03f --- /dev/null +++ b/spec/models/payment_spec.rb @@ -0,0 +1,117 @@ +require 'spec_helper' + +describe Payment do + + describe 'validations' do + it 'has a valid factory' do + expect(build(:payment)).to be_valid + end + + it 'is not valid without a first_name' do + should validate_presence_of(:first_name) + end + + it 'is not valid without a last_name' do + should validate_presence_of(:last_name) + end + + it 'is not valid without a credit_card_number' do + should validate_presence_of(:credit_card_number) + end + + it 'is not valid without a card_verification_value' do + should validate_presence_of(:card_verification_value) + end + + it 'is not valid without a expiration_month' do + should validate_presence_of(:expiration_month) + end + + it 'is not valid without a expiration_year' do + should validate_presence_of(:expiration_year) + end + + it 'is not valid without a amount' do + should validate_presence_of(:amount) + end + + 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 'self#purchase' do + let!(:participant) { create(:user) } + let!(:ticket_1) { create(:ticket) } + let!(:ticket_2) { create(:ticket) } + let!(:conference) { create(:conference, tickets: [ticket_1, ticket_2]) } + + 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 + + expect(TicketPurchase.count).to eq(1) + expect(purchase.quantity).to eq(1) + expect(message.blank?).to be true + + response = Payment.purchase(participant, conference, 1000) + payment = Payment.where(conference_id: conference.id, + user_id: participant.id) + + expect(Payment.count).to eq(1) + expect(payment.amount).to eq(10) + expect(response.blank?).to be true + end + + it 'creates several purchases for more than one ticket' do + tickets = { ticket_1.id.to_s => '1', ticket_2.id.to_s => '1' } + message = TicketPurchase.purchase(conference, participant, tickets) + purchase_1 = TicketPurchase.where(conference_id: conference.id, + user_id: participant.id, + ticket_id: ticket_1.id).first + + purchase_2 = TicketPurchase.where(conference_id: conference.id, + user_id: participant.id, + ticket_id: ticket_2.id).first + + expect(TicketPurchase.count).to eq(2) + expect(purchase_1.quantity).to eq(1) + expect(purchase_2.quantity).to eq(1) + expect(message.blank?).to be true + + response = Payment.purchase(participant, conference, 2000) + payment = Payment.where(conference_id: conference.id, + user_id: participant.id) + + expect(Payment.count).to eq(1) + expect(payment.amount).to eq(20) + expect(response.blank?).to be true + end + + it 'creates no ticket purchase or payment if amount is less than 1' do + tickets = { ticket_1.id.to_s => '-1' } + TicketPurchase.purchase(conference, participant, tickets) + + expect(TicketPurchase.count).to eq(0) + end + + it 'creates no ticket purchase or payment if amount is 0' do + tickets = { ticket_1.id.to_s => '0' } + TicketPurchase.purchase(conference, participant, tickets) + + expect(TicketPurchase.count).to eq(0) + end + end +end diff --git a/spec/models/ticket_spec.rb b/spec/models/ticket_spec.rb index 496037ec..f6b5cd7f 100644 --- a/spec/models/ticket_spec.rb +++ b/spec/models/ticket_spec.rb @@ -82,17 +82,37 @@ describe Ticket do end end + describe '#unpaid?' do + let!(:ticket_purchase) { create(:ticket_purchase, user: user, ticket: ticket) } + + context 'user has not paid' do + before { ticket_purchase.update_attributes(paid: false) } + + it 'returns true' do + expect(ticket.unpaid?(user)).to eq(true) + end + end + + 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 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, 'f')).to eq(20) + expect(ticket.quantity_bought_by(user, false)).to eq(20) end it 'returns zero if the user has not bought this ticket' do - expect(ticket.quantity_bought_by(user, 'f')).to eq(0) + expect(ticket.quantity_bought_by(user, false)).to eq(0) end end @@ -102,11 +122,11 @@ describe Ticket do user: user, ticket: ticket, quantity: 20) - expect(ticket.total_price(user, 'f')).to eq(Money.new(100000, 'USD')) + expect(ticket.total_price(user, 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, 'f')).to eq(Money.new(0, 'USD')) + expect(ticket.total_price(user, false)).to eq(Money.new(0, 'USD')) end end @@ -116,7 +136,7 @@ describe Ticket do describe 'user has bought' do context 'no tickets' do it 'returns zero' do - expect(Ticket.total_price(conference, user, 'f')).to eq(Money.new(0, 'USD')) + expect(Ticket.total_price(conference, user, false)).to eq(Money.new(0, 'USD')) end end @@ -126,7 +146,7 @@ describe Ticket do end it 'returns the correct total price' do - expect(Ticket.total_price(conference, user, 'f')).to eq(Money.new(100000, 'USD')) + expect(Ticket.total_price(conference, user, false)).to eq(Money.new(100000, 'USD')) end end @@ -138,7 +158,7 @@ describe Ticket do it 'returns the correct total price' do total_price = Money.new(200000, 'USD') - expect(Ticket.total_price(conference, user, 'f')).to eq(total_price) + expect(Ticket.total_price(conference, user, false)).to eq(total_price) end end end