From 223a0eead9dbfe7f392ef1c9dd5ab5048af7be7f Mon Sep 17 00:00:00 2001 From: Rishabh Saxena Date: Fri, 15 Jul 2016 02:46:31 +0530 Subject: [PATCH] delete unused methods, add feature tests --- app/controllers/payments_controller.rb | 8 +- app/models/ticket.rb | 3 +- spec/features/payments_spec.rb | 111 +++++++++++++++++++++++++ spec/features/ticket_purchases_spec.rb | 8 -- spec/helpers/payments_helper_spec.rb | 11 +-- spec/models/payment_spec.rb | 9 +- spec/models/ticket_spec.rb | 62 +++++++++----- 7 files changed, 165 insertions(+), 47 deletions(-) create mode 100644 spec/features/payments_spec.rb diff --git a/app/controllers/payments_controller.rb b/app/controllers/payments_controller.rb index aeb2ca49..890d0d9e 100644 --- a/app/controllers/payments_controller.rb +++ b/app/controllers/payments_controller.rb @@ -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 diff --git a/app/models/ticket.rb b/app/models/ticket.rb index 198c836d..2a2cae65 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -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) diff --git a/spec/features/payments_spec.rb b/spec/features/payments_spec.rb new file mode 100644 index 00000000..ae6b662a --- /dev/null +++ b/spec/features/payments_spec.rb @@ -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 diff --git a/spec/features/ticket_purchases_spec.rb b/spec/features/ticket_purchases_spec.rb index 5c34ae01..3d42473f 100644 --- a/spec/features/ticket_purchases_spec.rb +++ b/spec/features/ticket_purchases_spec.rb @@ -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 diff --git a/spec/helpers/payments_helper_spec.rb b/spec/helpers/payments_helper_spec.rb index 5dded1b2..91feb177 100644 --- a/spec/helpers/payments_helper_spec.rb +++ b/spec/helpers/payments_helper_spec.rb @@ -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 diff --git a/spec/models/payment_spec.rb b/spec/models/payment_spec.rb index dfefbae2..70ea5e9e 100644 --- a/spec/models/payment_spec.rb +++ b/spec/models/payment_spec.rb @@ -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 + payment = Payment.new + payment.purchase(participant, conference, 1000) end end end diff --git a/spec/models/ticket_spec.rb b/spec/models/ticket_spec.rb index 0f781023..631ca59a 100644 --- a/spec/models/ticket_spec.rb +++ b/spec/models/ticket_spec.rb @@ -102,30 +102,52 @@ describe Ticket do end describe '#quantity_bought_by' do - it 'returns 0 if the user has bought but not paid for this ticket' do - create(:ticket_purchase, - user: user, - ticket: ticket, - quantity: 20) - expect(ticket.quantity_bought_by(user, paid: false)).to eq(0) + 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 - it 'returns zero if the user has not bought this ticket' do - expect(ticket.quantity_bought_by(user, paid: false)).to eq(0) + 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 - create(:ticket_purchase, - user: user, - ticket: ticket, - quantity: 20) - expect(ticket.total_price(user, paid: false)).to eq(Money.new(0, '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, paid: false)).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 @@ -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