delete unused methods, add feature tests
This commit is contained in:
parent
3b4c7d70c0
commit
223a0eead9
7 changed files with 165 additions and 47 deletions
|
|
@ -17,7 +17,7 @@ class PaymentsController < ApplicationController
|
|||
@total_amount_to_pay = Ticket.total_price(@conference, current_user, paid: false)
|
||||
|
||||
if @payment.purchase(current_user, @conference, price_in_cents) && @payment.save
|
||||
update_purchased_ticket_purchases(@conference, current_user, @payment)
|
||||
update_purchased_ticket_purchases
|
||||
redirect_to conference_conference_registration_path(@conference.short_title), flash: { success: 'Thanks! You have purchased your tickets successfully.' }
|
||||
else
|
||||
render 'new'
|
||||
|
|
@ -30,11 +30,11 @@ class PaymentsController < ApplicationController
|
|||
(@payment.amount * 100).round
|
||||
end
|
||||
|
||||
def update_purchased_ticket_purchases(conference, user, payment)
|
||||
paid_ticket_purchases = current_user.ticket_purchases.by_conference(conference).unpaid
|
||||
def update_purchased_ticket_purchases
|
||||
paid_ticket_purchases = current_user.ticket_purchases.by_conference(@conference).unpaid
|
||||
paid_ticket_purchases.each do |ticket|
|
||||
ticket.paid = true
|
||||
ticket.payment_id = payment.id
|
||||
ticket.payment_id = @payment.id
|
||||
ticket.save
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -22,8 +22,7 @@ class Ticket < ActiveRecord::Base
|
|||
end
|
||||
|
||||
def quantity_bought_by(user, paid: false)
|
||||
purchased_tickets = ticket_purchases.paid.by_user(user)
|
||||
quantity = purchased_tickets.sum(:quantity)
|
||||
ticket_purchases.by_user(user).where(paid: paid).sum(:quantity)
|
||||
end
|
||||
|
||||
def unpaid?(user)
|
||||
|
|
|
|||
111
spec/features/payments_spec.rb
Normal file
111
spec/features/payments_spec.rb
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
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', 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 'first_name', with: 'foo'
|
||||
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: '4000000000000000'
|
||||
|
||||
click_button 'Charge Card'
|
||||
|
||||
expect(flash).to eq('Your card number is incorrect.')
|
||||
expect(current_path).to eq(new_conference_payment_path(conference.short_title))
|
||||
|
||||
fill_in 'first_name', with: 'foo'
|
||||
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: '4000000000000002'
|
||||
|
||||
click_button 'Charge Card'
|
||||
|
||||
expect(flash).to eq('Your card was declined.')
|
||||
expect(current_path).to eq(new_conference_payment_path(conference.short_title))
|
||||
|
||||
fill_in 'first_name', with: 'foo'
|
||||
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: '4000000000000127'
|
||||
|
||||
click_button 'Charge Card'
|
||||
|
||||
expect(flash).to eq("Your card's security code is incorrect.")
|
||||
expect(current_path).to eq(new_conference_payment_path(conference.short_title))
|
||||
|
||||
fill_in 'first_name', with: 'foo'
|
||||
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: '4000000000000069'
|
||||
|
||||
click_button 'Charge Card'
|
||||
|
||||
expect(flash).to eq('Your card has expired.')
|
||||
expect(current_path).to eq(new_conference_payment_path(conference.short_title))
|
||||
|
||||
fill_in 'first_name', with: 'foo'
|
||||
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: '4000000000000119'
|
||||
|
||||
click_button 'Charge Card'
|
||||
|
||||
expect(flash).to eq('An error occurred while processing your card. Try again in a little bit.')
|
||||
expect(current_path).to eq(new_conference_payment_path(conference.short_title))
|
||||
|
||||
fill_in 'first_name', with: 'foo'
|
||||
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'
|
||||
|
||||
click_button 'Charge Card'
|
||||
|
||||
payment = Payment.where(user_id: participant, conference_id: conference.id).first
|
||||
expect(payment.amount).to eq(20)
|
||||
expect(payment.status).to eq(1)
|
||||
expect(payment.first_name).to eq('foo')
|
||||
expect(payment.first_name).to eq('bar')
|
||||
expect(payment.last4).not_to be_empty
|
||||
expect(payment.authorization_code).not_to be_empty
|
||||
expect(current_path).to eq(conference_conference_registrations_path(conference.short_title))
|
||||
expect(flash).to eq('Thanks! You have purchased your tickets successfully.')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -32,14 +32,6 @@ feature Registration do
|
|||
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 'first_name', with: 'foo'
|
||||
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'
|
||||
|
||||
click_button 'Charge Card'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,15 +1,12 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe PaymentsHelper, type: :helper do
|
||||
let(:conference) { create(:conference) }
|
||||
let(:event) { create(:event, program: conference.program) }
|
||||
|
||||
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]]))
|
||||
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
|
||||
|
||||
|
|
|
|||
|
|
@ -65,14 +65,11 @@ describe Payment do
|
|||
ticket_id: ticket_1.id).first
|
||||
|
||||
expect(TicketPurchase.count).to eq(1)
|
||||
# expect(purchase.quantity).to eq(1)
|
||||
expect(purchase.quantity).to eq(1)
|
||||
expect(message.blank?).to be true
|
||||
|
||||
payment = Payment.new
|
||||
payment.purchase(participant, conference, 1000)
|
||||
|
||||
expect(Payment.count).to eq(1)
|
||||
expect(payment.blank?).to be true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -102,12 +102,13 @@ describe Ticket do
|
|||
end
|
||||
|
||||
describe '#quantity_bought_by' do
|
||||
it 'returns 0 if the user has bought but not paid for this ticket' 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(0)
|
||||
expect(ticket.quantity_bought_by(user, paid: false)).to eq(20)
|
||||
end
|
||||
|
||||
it 'returns zero if the user has not bought this ticket' do
|
||||
|
|
@ -115,13 +116,24 @@ describe Ticket do
|
|||
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 0 if the user has bought but not paid for this ticket' 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.total_price(user, paid: false)).to eq(Money.new(0, 'USD'))
|
||||
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
|
||||
|
|
@ -129,6 +141,16 @@ describe Ticket do
|
|||
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 this ticket' do
|
||||
expect(ticket.total_price(user, paid: true)).to eq(Money.new(100000, 'USD'))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'self.total_price' do
|
||||
let(:diversity_supporter_ticket) { create(:ticket, conference: conference, price: 500) }
|
||||
|
||||
|
|
@ -144,8 +166,8 @@ describe Ticket do
|
|||
create(:ticket_purchase, ticket: ticket, user: user, quantity: 20)
|
||||
end
|
||||
|
||||
it 'returns 0 as total price unless paid' do
|
||||
expect(Ticket.total_price(conference, user, paid: false)).to eq(Money.new(0, 'USD'))
|
||||
it 'returns the correct total price' do
|
||||
expect(Ticket.total_price(conference, user, paid: false)).to eq(Money.new(100000, 'USD'))
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -155,8 +177,8 @@ describe Ticket do
|
|||
create(:ticket_purchase, ticket: diversity_supporter_ticket, user: user, quantity: 2)
|
||||
end
|
||||
|
||||
it 'returns 0 as total price unless paid' do
|
||||
total_price = Money.new(0, 'USD')
|
||||
it 'returns the correct total price' do
|
||||
total_price = Money.new(200000, 'USD')
|
||||
expect(Ticket.total_price(conference, user, paid: false)).to eq(total_price)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue