From da372cf8ad61ab5c2eae2168f7867c8466683880 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Mon, 26 Apr 2021 16:38:00 +0200 Subject: [PATCH] fix test Events::AsUserTest the event price calculalation was using a variable not reinitialized --- app/models/cart_item/event_reservation.rb | 5 +- test/integration/events/as_user_test.rb | 208 +++++++++++----------- 2 files changed, 108 insertions(+), 105 deletions(-) diff --git a/app/models/cart_item/event_reservation.rb b/app/models/cart_item/event_reservation.rb index 5dd922300..17098edb6 100644 --- a/app/models/cart_item/event_reservation.rb +++ b/app/models/cart_item/event_reservation.rb @@ -21,16 +21,17 @@ class CartItem::EventReservation < CartItem::Reservation end elements = { slots: [] } + total = 0 @slots.each do |slot| - amount += get_slot_price(amount, + total += get_slot_price(amount, slot, is_privileged, elements: elements, is_division: false) end - { elements: elements, amount: amount } + { elements: elements, amount: total } end def name diff --git a/test/integration/events/as_user_test.rb b/test/integration/events/as_user_test.rb index 73bee6418..7c9b22f50 100644 --- a/test/integration/events/as_user_test.rb +++ b/test/integration/events/as_user_test.rb @@ -1,114 +1,116 @@ # frozen_string_literal: true -module Events - class AsUserTest < ActionDispatch::IntegrationTest - test 'reserve event with many prices and payment means and VAT' do - vlonchamp = User.find_by(username: 'vlonchamp') - login_as(vlonchamp, scope: :user) +require 'test_helper' - radio = Event.find(4) - availability = radio.availability +module Events; end - reservations_count = Reservation.count - invoice_count = Invoice.count - invoice_items_count = InvoiceItem.count - users_credit_count = UsersCredit.count - wallet_transactions_count = WalletTransaction.count +class Events::AsUserTest < ActionDispatch::IntegrationTest + test 'reserve event with many prices and payment means and VAT' do + vlonchamp = User.find_by(username: 'vlonchamp') + login_as(vlonchamp, scope: :user) - # Enable the VAT at 19.6% - vat_active = Setting.find_by(name: 'invoice_VAT-active') - vat_active.value = 'true' - vat_active.save! + radio = Event.find(4) + availability = radio.availability - vat_rate = Setting.find_by(name: 'invoice_VAT-rate') - vat_rate.value = '19.6' - vat_rate.save! + reservations_count = Reservation.count + invoice_count = Invoice.count + invoice_items_count = InvoiceItem.count + users_credit_count = UsersCredit.count + wallet_transactions_count = WalletTransaction.count - # Reserve the 'radio' event - VCR.use_cassette('reserve_event_with_many_prices_and_payment_means') do - post '/api/stripe/confirm_payment', - params: { - payment_method_id: stripe_payment_method, + # Enable the VAT at 19.6% + vat_active = Setting.find_by(name: 'invoice_VAT-active') + vat_active.value = 'true' + vat_active.save! + + vat_rate = Setting.find_by(name: 'invoice_VAT-rate') + vat_rate.value = '19.6' + vat_rate.save! + + # Reserve the 'radio' event + VCR.use_cassette('reserve_event_with_many_prices_and_payment_means') do + post '/api/stripe/confirm_payment', + params: { + payment_method_id: stripe_payment_method, + cart_items: { customer_id: User.find_by(username: 'vlonchamp').id, - cart_items: { - reservation: { - reservable_id: radio.id, - reservable_type: 'Event', - nb_reserve_places: 2, - slots_attributes: [ - { - start_at: availability.start_at, - end_at: availability.end_at, - availability_id: availability.id, - offered: false - } - ], - tickets_attributes: [ - { - event_price_category_id: radio.event_price_categories[0].id, - booked: 2 - }, - { - event_price_category_id: radio.event_price_categories[1].id, - booked: 2 - } - ] - }, - coupon_code: 'SUNNYFABLAB' - } - }.to_json, headers: default_headers - end - - vlonchamp.wallet.reload - - # general assertions - assert_equal 201, response.status - assert_equal reservations_count + 1, Reservation.count - assert_equal invoice_count + 1, Invoice.count - assert_equal invoice_items_count + 1, InvoiceItem.count - assert_equal users_credit_count, UsersCredit.count - assert_equal wallet_transactions_count + 1, WalletTransaction.count - - # reservation assertions - reservation = Reservation.last - - assert reservation.invoice - assert_equal 1, reservation.invoice.invoice_items.count - - # invoice assertions - invoice = reservation.invoice - - refute invoice.payment_gateway_object.blank? - refute invoice.total.blank? - assert_equal 43_350, invoice.total # total minus coupon - - # invoice_items assertions - ## reservation - reservation_item = invoice.invoice_items.first - - assert_not_nil reservation_item - assert_equal 51_000, reservation_item.amount # full total - - # invoice assertions - invoice = Invoice.find_by(invoiced: reservation) - assert_invoice_pdf invoice - - VCR.use_cassette('reserve_event_with_many_prices_and_payment_means_retrieve_invoice_from_stripe') do - stp_intent = invoice.payment_gateway_object.gateway_object.retrieve - assert_equal stp_intent.amount, (invoice.total - invoice.wallet_amount) # total minus coupon minus wallet = amount really payed by the user - end - - # wallet assertions - assert_equal vlonchamp.wallet.amount, 0 - assert_equal vlonchamp.wallet.wallet_transactions.count, 2 - transaction = vlonchamp.wallet.wallet_transactions.last - assert_equal transaction.transaction_type, 'debit' - assert_equal transaction.amount, 10 - assert_equal transaction.amount, invoice.wallet_amount / 100.0 - - # notifications - assert_not_empty Notification.where(attached_object: reservation) - assert_not_empty Notification.where(attached_object: invoice) + reservation: { + reservable_id: radio.id, + reservable_type: 'Event', + nb_reserve_places: 2, + slots_attributes: [ + { + start_at: availability.start_at, + end_at: availability.end_at, + availability_id: availability.id, + offered: false + } + ], + tickets_attributes: [ + { + event_price_category_id: radio.event_price_categories[0].id, + booked: 2 + }, + { + event_price_category_id: radio.event_price_categories[1].id, + booked: 2 + } + ] + }, + coupon_code: 'SUNNYFABLAB' + } + }.to_json, headers: default_headers end + + vlonchamp.wallet.reload + + # general assertions + assert_equal 201, response.status + assert_equal reservations_count + 1, Reservation.count + assert_equal invoice_count + 1, Invoice.count + assert_equal invoice_items_count + 1, InvoiceItem.count + assert_equal users_credit_count, UsersCredit.count + assert_equal wallet_transactions_count + 1, WalletTransaction.count + + # reservation assertions + reservation = Reservation.last + + assert reservation.invoice + assert_equal 1, reservation.invoice.invoice_items.count + + # invoice assertions + invoice = reservation.invoice + + refute invoice.payment_gateway_object.blank? + refute invoice.total.blank? + assert_equal 43_350, invoice.total # total minus coupon + + # invoice_items assertions + ## reservation + reservation_item = invoice.invoice_items.first + + assert_not_nil reservation_item + assert_equal 51_000, reservation_item.amount # full total + + # invoice assertions + invoice = Invoice.find_by(invoiced: reservation) + assert_invoice_pdf invoice + + VCR.use_cassette('reserve_event_with_many_prices_and_payment_means_retrieve_invoice_from_stripe') do + stp_intent = invoice.payment_gateway_object.gateway_object.retrieve + assert_equal stp_intent.amount, (invoice.total - invoice.wallet_amount) # total minus coupon minus wallet = amount really paid by the user + end + + # wallet assertions + assert_equal vlonchamp.wallet.amount, 0 + assert_equal vlonchamp.wallet.wallet_transactions.count, 2 + transaction = vlonchamp.wallet.wallet_transactions.last + assert_equal transaction.transaction_type, 'debit' + assert_equal transaction.amount, 10 + assert_equal transaction.amount, invoice.wallet_amount / 100.0 + + # notifications + assert_not_empty Notification.where(attached_object: reservation) + assert_not_empty Notification.where(attached_object: invoice) end end