delete unused methods, add feature tests

This commit is contained in:
Rishabh Saxena 2016-07-15 02:46:31 +05:30
parent 3b4c7d70c0
commit 223a0eead9
7 changed files with 165 additions and 47 deletions

View file

@ -17,7 +17,7 @@ class PaymentsController < ApplicationController
@total_amount_to_pay = Ticket.total_price(@conference, current_user, paid: false) @total_amount_to_pay = Ticket.total_price(@conference, current_user, paid: false)
if @payment.purchase(current_user, @conference, price_in_cents) && @payment.save 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.' } redirect_to conference_conference_registration_path(@conference.short_title), flash: { success: 'Thanks! You have purchased your tickets successfully.' }
else else
render 'new' render 'new'
@ -30,11 +30,11 @@ class PaymentsController < ApplicationController
(@payment.amount * 100).round (@payment.amount * 100).round
end end
def update_purchased_ticket_purchases(conference, user, payment) def update_purchased_ticket_purchases
paid_ticket_purchases = current_user.ticket_purchases.by_conference(conference).unpaid paid_ticket_purchases = current_user.ticket_purchases.by_conference(@conference).unpaid
paid_ticket_purchases.each do |ticket| paid_ticket_purchases.each do |ticket|
ticket.paid = true ticket.paid = true
ticket.payment_id = payment.id ticket.payment_id = @payment.id
ticket.save ticket.save
end end
end end

View file

@ -22,8 +22,7 @@ class Ticket < ActiveRecord::Base
end end
def quantity_bought_by(user, paid: false) def quantity_bought_by(user, paid: false)
purchased_tickets = ticket_purchases.paid.by_user(user) ticket_purchases.by_user(user).where(paid: paid).sum(:quantity)
quantity = purchased_tickets.sum(:quantity)
end end
def unpaid?(user) def unpaid?(user)

View 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

View file

@ -32,14 +32,6 @@ feature Registration do
expect(flash).to eq('Please pay here to purchase tickets.') expect(flash).to eq('Please pay here to purchase tickets.')
purchase = TicketPurchase.where(user_id: participant.id, ticket_id: ticket.id).first purchase = TicketPurchase.where(user_id: participant.id, ticket_id: ticket.id).first
expect(purchase.quantity).to eq(2) 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 end
end end

View file

@ -1,15 +1,12 @@
require 'spec_helper' require 'spec_helper'
describe PaymentsHelper, type: :helper do describe PaymentsHelper, type: :helper do
let(:conference) { create(:conference) }
let(:event) { create(:event, program: conference.program) }
describe '#months' do describe '#months' do
it 'returns the correct strings for 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], expect(months).to match_array(Array([['1 - January', 1], ['2 - February', 2], ['3 - March', 3],
["4 - April", 4], ["5 - May", 5], ["6 - June", 6], ['4 - April', 4], ['5 - May', 5], ['6 - June', 6],
["7 - July", 7], ["8 - August", 8], ["9 - September", 9], ['7 - July', 7], ['8 - August', 8], ['9 - September', 9],
["10 - October", 10], ["11 - November", 11], ["12 - December", 12]])) ['10 - October', 10], ['11 - November', 11], ['12 - December', 12]]))
end end
end end

View file

@ -65,14 +65,11 @@ describe Payment do
ticket_id: ticket_1.id).first ticket_id: ticket_1.id).first
expect(TicketPurchase.count).to eq(1) expect(TicketPurchase.count).to eq(1)
# expect(purchase.quantity).to eq(1) expect(purchase.quantity).to eq(1)
expect(message.blank?).to be true expect(message.blank?).to be true
payment = Payment.new payment = Payment.new
payment.purchase(participant, conference, 1000) payment.purchase(participant, conference, 1000)
expect(Payment.count).to eq(1)
expect(payment.blank?).to be true
end end
end end
end end

View file

@ -102,12 +102,13 @@ describe Ticket do
end end
describe '#quantity_bought_by' do 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, create(:ticket_purchase,
user: user, user: user,
ticket: ticket, ticket: ticket,
quantity: 20) 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 end
it 'returns zero if the user has not bought this ticket' do it 'returns zero if the user has not bought this ticket' do
@ -115,13 +116,24 @@ describe Ticket do
end end
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 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, create(:ticket_purchase,
user: user, user: user,
ticket: ticket, ticket: ticket,
quantity: 20) 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 end
it 'returns zero if the user has not bought this ticket' do it 'returns zero if the user has not bought this ticket' do
@ -129,6 +141,16 @@ describe Ticket do
end end
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 describe 'self.total_price' do
let(:diversity_supporter_ticket) { create(:ticket, conference: conference, price: 500) } 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) create(:ticket_purchase, ticket: ticket, user: user, quantity: 20)
end end
it 'returns 0 as total price unless paid' do it 'returns the correct total price' do
expect(Ticket.total_price(conference, user, paid: false)).to eq(Money.new(0, 'USD')) expect(Ticket.total_price(conference, user, paid: false)).to eq(Money.new(100000, 'USD'))
end end
end end
@ -155,8 +177,8 @@ describe Ticket do
create(:ticket_purchase, ticket: diversity_supporter_ticket, user: user, quantity: 2) create(:ticket_purchase, ticket: diversity_supporter_ticket, user: user, quantity: 2)
end end
it 'returns 0 as total price unless paid' do it 'returns the correct total price' do
total_price = Money.new(0, 'USD') total_price = Money.new(200000, 'USD')
expect(Ticket.total_price(conference, user, paid: false)).to eq(total_price) expect(Ticket.total_price(conference, user, paid: false)).to eq(total_price)
end end
end end