This commit is contained in:
Rishabh Saxena 2016-08-10 06:18:19 +00:00 committed by GitHub
commit 211410b6f0
33 changed files with 621 additions and 91 deletions

View file

@ -40,9 +40,8 @@ describe ConferenceRegistrationsController, type: :controller do
get :show, conference_id: conference.short_title
end
it 'assigns price of purchased tickets to total_price and purchased tickets to tickets' do
expect(assigns(:total_price)).to eq Money.new(10000, 'USD')
expect(assigns(:tickets)).to match_array [@purchased_ticket]
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')
end
end

View file

@ -0,0 +1,20 @@
FactoryGirl.define do
factory :payment do
user
conference
full_name { Faker::Hipster.word.to_s }
credit_card_number '4242424242424111'
card_verification_value '123'
expiration_month 6
expiration_year { Date.current.year + 2 }
amount 10
end
trait :invalid_credit_card do
credit_card_number '4242424242424222'
end
trait :exception_credit_card do
credit_card_number '4242424242424333'
end
end

View file

@ -0,0 +1,112 @@
require 'spec_helper'
feature Registration do
let!(:ticket) { create(:ticket) }
let!(:conference) { create(:conference, title: 'ExampleCon', tickets: [ticket], registration_period: create(:registration_period, start_date: 3.days.ago)) }
let!(:participant) { create(:user) }
context 'as a participant' do
before(:each) do
sign_in participant
end
after(:each) do
sign_out
end
context 'who is not registered' do
scenario 'purchases and pays for a ticket, with gateway producing error', feature: true, js: true do
visit root_path
click_link 'Register'
expect(current_path).to eq(new_conference_conference_registration_path(conference.short_title))
click_button 'Register'
fill_in "tickets__#{ticket.id}", with: '2'
expect(current_path).to eq(conference_tickets_path(conference.short_title))
click_button 'Continue'
expect(current_path).to eq(new_conference_payment_path(conference.short_title))
expect(flash).to eq('Please pay here to purchase tickets.')
purchase = TicketPurchase.where(user_id: participant.id, ticket_id: ticket.id).first
expect(purchase.quantity).to eq(2)
fill_in 'full_name', with: 'foo'
select Date.current.year + 2, from: 'expiration_year'
fill_in 'card_verification_value', with: '123'
fill_in 'credit_card_number', with: '4242424242423333'
click_button 'Charge Card'
expect(Payment.count).to eq(0)
expect(current_path).to eq(conference_conference_registration_path(conference.short_title))
end
scenario 'purchases and pays for a ticket, with card producing a transaction failure', feature: true, js: true do
visit root_path
click_link 'Register'
expect(current_path).to eq(new_conference_conference_registration_path(conference.short_title))
click_button 'Register'
fill_in "tickets__#{ticket.id}", with: '2'
expect(current_path).to eq(conference_tickets_path(conference.short_title))
click_button 'Continue'
expect(current_path).to eq(new_conference_payment_path(conference.short_title))
expect(flash).to eq('Please pay here to purchase tickets.')
purchase = TicketPurchase.where(user_id: participant.id, ticket_id: ticket.id).first
expect(purchase.quantity).to eq(2)
fill_in 'full_name', with: 'foo'
select Date.current.year + 2, from: 'expiration_year'
fill_in 'card_verification_value', with: '123'
fill_in 'credit_card_number', with: '4242424242422222'
click_button 'Charge Card'
expect(Payment.count).to eq(0)
expect(current_path).to eq(conference_conference_registration_path(conference.short_title))
end
scenario 'purchases and pays for a ticket successfully', feature: true, js: true do
visit root_path
click_link 'Register'
expect(current_path).to eq(new_conference_conference_registration_path(conference.short_title))
click_button 'Register'
fill_in "tickets__#{ticket.id}", with: '2'
expect(current_path).to eq(conference_tickets_path(conference.short_title))
click_button 'Continue'
expect(current_path).to eq(new_conference_payment_path(conference.short_title))
expect(flash).to eq('Please pay here to purchase tickets.')
purchase = TicketPurchase.where(user_id: participant.id, ticket_id: ticket.id).first
expect(purchase.quantity).to eq(2)
fill_in 'full_name', with: 'foo'
select Date.current.year + 2, from: 'expiration_year'
fill_in 'card_verification_value', with: '123'
fill_in 'credit_card_number', with: '4242424242421111'
click_button 'Charge Card'
expect(current_path).to eq(conference_conference_registration_path(conference.short_title))
expect(Payment.count).to eq(1)
payment = Payment.where(user_id: participant, conference_id: conference.id).first
expect(payment.amount).to eq(20)
expect(payment.status).to eq('success')
expect(payment.first_name).to eq('foo')
expect(payment.last_name).to eq('bar')
expect(payment.last4).not_to be_empty
expect(payment.authorization_code).not_to be_empty
expect(flash).to eq('Thanks! You have purchased your tickets successfully.')
end
end
end
end

View file

@ -26,26 +26,12 @@ feature Registration do
fill_in "tickets__#{ticket.id}", with: '2'
expect(current_path).to eq(conference_tickets_path(conference.short_title))
click_button 'Support'
click_button 'Continue'
expect(current_path).to eq(new_conference_payment_path(conference.short_title))
expect(flash).to eq('Please pay here to purchase tickets.')
purchase = TicketPurchase.where(user_id: participant.id, ticket_id: ticket.id).first
expect(purchase.quantity).to eq(2)
expect(current_path).to eq(conference_conference_registration_path(conference.short_title))
expect(flash).
to eq("Thank you for supporting #{conference.title} by purchasing a ticket.")
expect(page.has_content?("2 #{ticket.title} Tickets for 10")).to be true
end
scenario 'deletes a purchased ticket', feature: true, js: true do
create(:registration, conference: conference, user: participant)
create(:ticket_purchase, conference: conference, user: participant, ticket: ticket, quantity: 4)
visit conference_conference_registration_path(conference.short_title)
expect(page.has_content?("4 #{ticket.title} Tickets for 10")).to be true
click_link "ticket-#{ticket.id}-delete"
expect(flash).to eq('Ticket successfully deleted.')
expect(TicketPurchase.count).to eq(0)
end
end
end

View file

@ -0,0 +1,18 @@
require 'spec_helper'
describe PaymentsHelper, type: :helper do
describe '#months' do
it 'returns the correct strings for months' do
expect(months).to match_array(Array([['1 - January', 1], ['2 - February', 2], ['3 - March', 3],
['4 - April', 4], ['5 - May', 5], ['6 - June', 6],
['7 - July', 7], ['8 - August', 8], ['9 - September', 9],
['10 - October', 10], ['11 - November', 11], ['12 - December', 12]]))
end
end
describe '#years' do
it 'returns the correct set of options' do
expect(years).to match_array(Array(Date.current.year..Date.current.year + 15))
end
end
end

0
spec/models/conference_spec.rb Executable file → Normal file
View file

148
spec/models/payment_spec.rb Normal file
View 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

View file

@ -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