diff --git a/test/fixtures/addresses.yml b/test/fixtures/addresses.yml index 16302d97f..bf160f322 100644 --- a/test/fixtures/addresses.yml +++ b/test/fixtures/addresses.yml @@ -74,3 +74,15 @@ address_6: placeable_type: InvoicingProfile created_at: 2016-08-02 11:16:24.412236000 Z updated_at: 2016-08-02 11:16:24.412236000 Z + +address_7: + id: 7 + address: 14 rue du Général Choucroute, 44000 NANTES + street_number: + route: + locality: + postal_code: + placeable_id: 10 + placeable_type: InvoicingProfile + created_at: 2016-08-02 11:16:24.412236000 Z + updated_at: 2016-08-02 11:16:24.412236000 Z diff --git a/test/fixtures/prepaid_packs.yml b/test/fixtures/prepaid_packs.yml new file mode 100644 index 000000000..5c819918f --- /dev/null +++ b/test/fixtures/prepaid_packs.yml @@ -0,0 +1,15 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +prepaid_pack_1: + id: 1 + priceable_type: Machine + priceable_id: 1 + group_id: 1 + amount: 1000 + minutes: 600 + validity_interval: month + validity_count: 12 diff --git a/test/fixtures/statistic_profile_prepaid_packs.yml b/test/fixtures/statistic_profile_prepaid_packs.yml new file mode 100644 index 000000000..987890d48 --- /dev/null +++ b/test/fixtures/statistic_profile_prepaid_packs.yml @@ -0,0 +1,13 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +statistic_profile_prepaid_pack_1: + prepaid_pack_id: 1 + statistic_profile_id: 10 + minutes_used: 600 + expires_at: <%= 11.month.from_now.utc.strftime('%Y-%m-%d %H:%M:%S.%9N Z') %> + +statistic_profile_prepaid_pack_2: + prepaid_pack_id: 1 + statistic_profile_id: 10 + minutes_used: 0 + expires_at: <%= 12.month.from_now.utc.strftime('%Y-%m-%d %H:%M:%S.%9N Z') %> diff --git a/test/integration/reservations/pay_with_prepaid_pack_test.rb b/test/integration/reservations/pay_with_prepaid_pack_test.rb new file mode 100644 index 000000000..015c121d9 --- /dev/null +++ b/test/integration/reservations/pay_with_prepaid_pack_test.rb @@ -0,0 +1,76 @@ +# frozen_string_literal: true + +require 'test_helper' + +module Reservations; end + +class Reservations::PayWithPrepaidPackTest < ActionDispatch::IntegrationTest + setup do + @acamus = User.find_by(username: 'acamus') + end + + test 'user reserves a machine and pay by prepaid pack with success' do + login_as(@acamus, scope: :user) + + machine = Machine.first + availability = machine.availabilities.first + + reservations_count = Reservation.count + invoice_count = Invoice.count + invoice_items_count = InvoiceItem.count + + VCR.use_cassette('reservations_create_for_machine_with_prepaid_pack_success') do + post '/api/local_payment/confirm_payment', + params: { + items: [ + { + reservation: { + reservable_id: machine.id, + reservable_type: machine.class.name, + slots_reservations_attributes: [ + { + slot_id: availability.slots.first.id + } + ] + } + } + ] + }.to_json, headers: default_headers + end + + # 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 + + # reservation assertions + reservation = Reservation.last + + assert reservation.original_invoice + assert_equal 1, reservation.original_invoice.invoice_items.count + + # invoice_items assertions + invoice_item = InvoiceItem.last + + assert_equal invoice_item.amount, 0 + assert invoice_item.check_footprint + + # invoice assertions + item = InvoiceItem.find_by(object: reservation) + invoice = item.invoice + assert_invoice_pdf invoice + assert_not_nil invoice.debug_footprint + + assert invoice.payment_gateway_object.blank? + assert_not invoice.total.blank? + assert invoice.check_footprint + + # notification + assert_not_empty Notification.where(attached_object: reservation) + + # prepaid pack + minutes_available = PrepaidPackService.minutes_available(@acamus, machine) + assert_equal minutes_available, 540 + end +end diff --git a/test/models/statistic_profile_prepaid_pack_test.rb b/test/models/statistic_profile_prepaid_pack_test.rb new file mode 100644 index 000000000..f57539386 --- /dev/null +++ b/test/models/statistic_profile_prepaid_pack_test.rb @@ -0,0 +1,11 @@ +require 'test_helper' + +class StatisticProfilePrepaidPackTest < ActiveSupport::TestCase + test 'coupon have a expries date' do + prepaid_pack = PrepaidPack.first + user = User.find_by(username: 'jdupond') + p = StatisticProfilePrepaidPack.create!(prepaid_pack: prepaid_pack, statistic_profile: user.statistic_profile) + expires_at = DateTime.current + 12.months + assert p.expires_at.strftime('%Y-%m-%d'), expires_at.strftime('%Y-%m-%d') + end +end diff --git a/test/services/prepaid_pack_service_test.rb b/test/services/prepaid_pack_service_test.rb new file mode 100644 index 000000000..221ed90b3 --- /dev/null +++ b/test/services/prepaid_pack_service_test.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +require 'test_helper' + +class PrepaidPackServiceTest < ActiveSupport::TestCase + setup do + @acamus = User.find_by(username: 'acamus') + @machine = Machine.first + end + + test 'get user packs' do + packs = PrepaidPackService.user_packs(@acamus, @machine) + p = StatisticProfilePrepaidPack.where(statistic_profile_id: @acamus.statistic_profile.id) + assert_not_empty packs + assert_equal packs.length, 1 + assert_equal p.length, 2 + assert_equal packs.first.id, p.last.id + end + + test 'total number of prepaid minutes available' do + minutes_available = PrepaidPackService.minutes_available(@acamus, @machine) + assert_equal minutes_available, 600 + end + + test 'update user pack minutes' do + availabilities_service = Availabilities::AvailabilitiesService.new(@acamus) + + slots = availabilities_service.machines([@machine], @acamus, { start: DateTime.now, end: 1.day.from_now }) + reservation = Reservation.create( + reservable_id: @machine.id, + reservable_type: Machine.name, + slots: [slots[0], slots[1]], + statistic_profile_id: @acamus.statistic_profile.id + ) + + PrepaidPackService.update_user_minutes(@acamus, reservation) + minutes_available = PrepaidPackService.minutes_available(@acamus, @machine) + assert_equal minutes_available, 480 + end +end