1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-19 13:54:25 +01:00

migration to object[]

TODO: fix invoices checksums
This commit is contained in:
Sylvain 2021-05-31 11:52:53 +02:00
parent d942d46632
commit db58ee28c0
15 changed files with 316 additions and 255 deletions

View File

@ -1,7 +1,7 @@
Metrics/LineLength:
Max: 140
Metrics/MethodLength:
Max: 36
Max: 35
Metrics/CyclomaticComplexity:
Max: 13
Metrics/PerceivedComplexity:

View File

@ -30,7 +30,6 @@ class Reservation < ApplicationRecord
after_commit :notify_member_create_reservation, on: :create
after_commit :notify_admin_member_create_reservation, on: :create
after_commit :update_credits, on: :create
after_commit :extend_subscription, on: :create
after_save :update_event_nb_free_places, if: proc { |reservation| reservation.reservable_type == 'Event' }
@ -111,10 +110,6 @@ class Reservation < ApplicationRecord
end
end
def update_credits
UsersCredits::Manager.new(reservation: self).update_credits
end
def extend_subscription
SubscriptionExtensionAfterReservation.new(self).extend_subscription_if_eligible
end

View File

@ -45,44 +45,74 @@ class ShoppingCart
}
end
# Build the dataset for the current ShoppingCart and save it into the database.
# Data integrity is guaranteed: all goes right or nothing is saved.
def build_and_save(payment_id, payment_type)
price = total
objects = []
payment = nil
ActiveRecord::Base.transaction do
items.each do |item|
object = item.to_object
object.save
objects.push(object)
raise ActiveRecord::Rollback unless object.errors.empty?
objects.push(save_item(item))
end
update_credits(objects)
payment = if price[:schedule]
PaymentScheduleService.new.create(
objects,
price[:before_coupon],
coupon: @coupon.coupon,
operator: @operator,
payment_method: @payment_method,
user: @customer,
payment_id: payment_id,
payment_type: payment_type
)
else
InvoicesService.create(
price,
@operator.invoicing_profile.id,
objects,
@customer,
payment_id: payment_id,
payment_type: payment_type,
payment_method: @payment_method
)
end
payment = create_payment_document(price, objects, payment_id, payment_type)
WalletService.debit_user_wallet(payment, @customer)
payment.save
payment.post_save(payment_id)
end
{ success: objects.map(&:errors).flatten.map(&:empty?).all?, payment: payment, errors: objects.map(&:errors).flatten }
end
private
# Save the object associated with the provided item or raise and Rollback if something wrong append.
def save_item(item)
object = item.to_object
object.save
raise ActiveRecord::Rollback unless object.errors.empty?
object
end
# Create the PaymentDocument associated with this ShoppingCart and return it
def create_payment_document(price, objects, payment_id, payment_type)
if price[:schedule]
PaymentScheduleService.new.create(
objects,
price[:before_coupon],
coupon: @coupon.coupon,
operator: @operator,
payment_method: @payment_method,
user: @customer,
payment_id: payment_id,
payment_type: payment_type
)
else
InvoicesService.create(
price,
@operator.invoicing_profile.id,
objects,
@customer,
payment_id: payment_id,
payment_type: payment_type,
payment_method: @payment_method
)
end
end
# Handle the update of the user's credits
# If a subscription has been bought, the credits must be reset first.
# Then, the credits related to reservation(s) can be deducted.
def update_credits(objects)
subscription = objects.find { |o| o.is_a? Subscription }
UsersCredits::Manager.new(user: @customer).reset_credits if subscription
reservations = objects.filter { |o| o.is_a? Reservation }
reservations.each do |r|
UsersCredits::Manager.new(reservation: r).update_credits
end
end
end

View File

@ -20,7 +20,6 @@ class Subscription < ApplicationRecord
after_save :notify_member_subscribed_plan
after_save :notify_admin_subscribed_plan
after_save :notify_partner_subscribed_plan, if: :of_partner_plan?
after_commit :update_credits, on: :create
def generate_and_save_invoice(operator_profile_id)
generate_invoice(operator_profile_id).save
@ -139,9 +138,4 @@ class Subscription < ApplicationRecord
def of_partner_plan?
plan.is_a?(PartnerPlan)
end
# init the user's credits
def update_credits
UsersCredits::Manager.new(user: user).reset_credits
end
end

View File

@ -15,8 +15,8 @@ class Subscriptions::Subscribe
new_sub = Subscription.create(
plan_id: subscription.plan_id,
statistic_profile_id: subscription.statistic_profile_id,
expiration_date: new_expiration_date
)
new_sub.expiration_date = new_expiration_date
if new_sub.save
schedule = subscription.original_payment_schedule

View File

@ -91,4 +91,20 @@ class WalletService
wallet_amount >= total ? total : wallet_amount
end
##
# Subtract the amount of the payment document (Invoice|PaymentSchedule) from the customer's wallet
##
def self.debit_user_wallet(payment, user)
wallet_amount = WalletService.wallet_amount_debit(payment, user)
return unless wallet_amount.present? && wallet_amount != 0
amount = wallet_amount / 100.0
wallet_transaction = WalletService.new(user: user, wallet: user.wallet).debit(amount)
# wallet debit success
raise DebitWalletError unless wallet_transaction
payment.set_wallet_transaction(wallet_amount, wallet_transaction.id)
end
end

View File

@ -14,7 +14,7 @@ class Stripe::Service < Payment::Service
case payment_schedule.main_object.object_type
when Reservation.name
subscription = payment_schedule.payment_schedule_objects.find(&:subscription)
subscription = payment_schedule.payment_schedule_objects.find(&:subscription).object
reservable_stp_id = payment_schedule.main_object.object.reservable&.payment_gateway_object&.gateway_object_id
when Subscription.name
subscription = payment_schedule.main_object.object

View File

@ -1,212 +1,214 @@
# frozen_string_literal: true
module Events
class AsAdminTest < ActionDispatch::IntegrationTest
setup do
admin = User.with_role(:admin).first
login_as(admin, scope: :user)
end
require 'test_helper'
test 'creation modification reservation and re-modification scenario' do
# First, we create a new event
post '/api/events',
params: {
event: {
title: 'OpenLab discovery day',
description: 'A day to discover the Fablab and try its machines and possibilities.',
start_date: 1.week.from_now.utc,
start_time: 1.week.from_now.utc.change(hour: 16),
end_date: 1.week.from_now.utc,
end_time: 1.week.from_now.utc.change(hour: 20),
category_id: Category.first.id,
amount: 0
}
}.to_json,
headers: default_headers
module Events; end
# Check response format & status
assert_equal 201, response.status, response.body
assert_equal Mime[:json], response.content_type
class Events::AsAdminTest < ActionDispatch::IntegrationTest
setup do
admin = User.with_role(:admin).first
login_as(admin, scope: :user)
end
# Check the event was created correctly
event = json_response(response.body)
e = Event.where(id: event[:id]).first
assert_not_nil e, 'Event was not created in database'
test 'creation modification reservation and re-modification scenario' do
# First, we create a new event
post '/api/events',
params: {
event: {
title: 'OpenLab discovery day',
description: 'A day to discover the Fablab and try its machines and possibilities.',
start_date: 1.week.from_now.utc,
start_time: 1.week.from_now.utc.change(hour: 16),
end_date: 1.week.from_now.utc,
end_time: 1.week.from_now.utc.change(hour: 20),
category_id: Category.first.id,
amount: 0
}
}.to_json,
headers: default_headers
# Check the remaining free places are not defined
assert_nil e.nb_free_places, "Free places shouldn't be defined"
# Check response format & status
assert_equal 201, response.status, response.body
assert_equal Mime[:json], response.content_type
# Then, modify the event to set a nb of places
put "/api/events/#{e.id}",
params: {
event: {
title: 'OpenLab discovery day',
description: 'A day to discover the Fablab and try its machines and possibilities.',
start_date: 1.week.from_now.utc,
start_time: 1.week.from_now.utc.change(hour: 16),
end_date: 1.week.from_now.utc,
end_time: 1.week.from_now.utc.change(hour: 20),
category_id: Category.first.id,
amount: 0,
nb_total_places: 10
}
# Check the event was created correctly
event = json_response(response.body)
e = Event.where(id: event[:id]).first
assert_not_nil e, 'Event was not created in database'
# Check the remaining free places are not defined
assert_nil e.nb_free_places, "Free places shouldn't be defined"
# Then, modify the event to set a nb of places
put "/api/events/#{e.id}",
params: {
event: {
title: 'OpenLab discovery day',
description: 'A day to discover the Fablab and try its machines and possibilities.',
start_date: 1.week.from_now.utc,
start_time: 1.week.from_now.utc.change(hour: 16),
end_date: 1.week.from_now.utc,
end_time: 1.week.from_now.utc.change(hour: 20),
category_id: Category.first.id,
amount: 0,
nb_total_places: 10
}
}
# Check response format & status
assert_equal 200, response.status, response.body
assert_equal Mime[:json], response.content_type
# Check response format & status
assert_equal 200, response.status, response.body
assert_equal Mime[:json], response.content_type
# Check the places numbers were updated successfully
e = Event.where(id: event[:id]).first
assert_equal 10, e.nb_total_places, 'Total number of places was not updated'
assert_equal 10, e.nb_free_places, 'Number of free places was not updated'
# Check the places numbers were updated successfully
e = Event.where(id: event[:id]).first
assert_equal 10, e.nb_total_places, 'Total number of places was not updated'
assert_equal 10, e.nb_free_places, 'Number of free places was not updated'
# Now, let's make a reservation on this event
post '/api/local_payment/confirm_payment',
params: {
customer_id: User.find_by(username: 'pdurand').id,
items: [
# Now, let's make a reservation on this event
post '/api/local_payment/confirm_payment',
params: {
customer_id: User.find_by(username: 'pdurand').id,
items: [
{
reservation: {
reservable_id: e.id,
reservable_type: 'Event',
nb_reserve_places: 2,
slots_attributes: [
{
start_at: e.availability.start_at,
end_at: e.availability.end_at,
availability_id: e.availability.id,
offered: false
}
]
}
}
]
}.to_json,
headers: default_headers
# Check response format & status
assert_equal 201, response.status, response.body
assert_equal Mime[:json], response.content_type
# Check the remaining places were updated successfully
e = Event.where(id: event[:id]).first
assert_equal 8, e.nb_free_places, 'Number of free places was not updated'
# Finally, modify the event to add some places
put "/api/events/#{e.id}",
params: {
event: {
title: 'OpenLab discovery day',
description: 'A day to discover the Fablab and try its machines and possibilities.',
start_date: 1.week.from_now.utc,
start_time: 1.week.from_now.utc.change(hour: 16),
end_date: 1.week.from_now.utc,
end_time: 1.week.from_now.utc.change(hour: 20),
category_id: Category.first.id,
amount: 0,
nb_total_places: 20
}
}
# Check response format & status
assert_equal 200, response.status, response.body
assert_equal Mime[:json], response.content_type
# Check the places numbers were updated successfully
e = Event.where(id: event[:id]).first
assert_equal 20, e.nb_total_places, 'Total number of places was not updated'
assert_equal 18, e.nb_free_places, 'Number of free places was not updated'
end
test 'create event with custom price and reserve it with success' do
price_category = PriceCategory.first
# First, we create a new event
post '/api/events',
params: {
event: {
title: 'Electronics initiation',
description: 'A workshop about electronics and the abilities to create robots.',
start_date: 1.week.from_now.utc + 2.days,
start_time: 1.week.from_now.utc.change(hour: 18) + 2.days,
end_date: 1.week.from_now.utc + 2.days,
end_time: 1.week.from_now.utc.change(hour: 22) + 2.days,
category_id: Category.last.id,
amount: 20,
nb_total_places: 10,
event_price_categories_attributes: [
{
reservation: {
reservable_id: e.id,
reservable_type: 'Event',
nb_reserve_places: 2,
slots_attributes: [
{
start_at: e.availability.start_at,
end_at: e.availability.end_at,
availability_id: e.availability.id,
offered: false
}
]
}
price_category_id: price_category.id.to_s,
amount: 16.to_s
}
]
}.to_json,
headers: default_headers
}
}.to_json,
headers: default_headers
# Check response format & status
assert_equal 201, response.status, response.body
assert_equal Mime[:json], response.content_type
# Check response format & status
assert_equal 201, response.status, response.body
assert_equal Mime[:json], response.content_type
# Check the remaining places were updated successfully
e = Event.where(id: event[:id]).first
assert_equal 8, e.nb_free_places, 'Number of free places was not updated'
# Check the event was created correctly
event = json_response(response.body)
e = Event.where(id: event[:id]).first
assert_not_nil e, 'Event was not created in database'
# Finally, modify the event to add some places
put "/api/events/#{e.id}",
params: {
event: {
title: 'OpenLab discovery day',
description: 'A day to discover the Fablab and try its machines and possibilities.',
start_date: 1.week.from_now.utc,
start_time: 1.week.from_now.utc.change(hour: 16),
end_date: 1.week.from_now.utc,
end_time: 1.week.from_now.utc.change(hour: 20),
category_id: Category.first.id,
amount: 0,
nb_total_places: 20
}
}
# Check the places numbers were set successfully
e = Event.where(id: event[:id]).first
assert_equal 10, e.nb_total_places, 'Total number of places was not updated'
assert_equal 10, e.nb_free_places, 'Number of free places was not updated'
# Check response format & status
assert_equal 200, response.status, response.body
assert_equal Mime[:json], response.content_type
# Check the places numbers were updated successfully
e = Event.where(id: event[:id]).first
assert_equal 20, e.nb_total_places, 'Total number of places was not updated'
assert_equal 18, e.nb_free_places, 'Number of free places was not updated'
end
test 'create event with custom price and reserve it with success' do
price_category = PriceCategory.first
# First, we create a new event
post '/api/events',
params: {
event: {
title: 'Electronics initiation',
description: 'A workshop about electronics and the abilities to create robots.',
start_date: 1.week.from_now.utc + 2.days,
start_time: 1.week.from_now.utc.change(hour: 18) + 2.days,
end_date: 1.week.from_now.utc + 2.days,
end_time: 1.week.from_now.utc.change(hour: 22) + 2.days,
category_id: Category.last.id,
amount: 20,
nb_total_places: 10,
event_price_categories_attributes: [
{
price_category_id: price_category.id.to_s,
amount: 16.to_s
}
]
}
}.to_json,
headers: default_headers
# Check response format & status
assert_equal 201, response.status, response.body
assert_equal Mime[:json], response.content_type
# Check the event was created correctly
event = json_response(response.body)
e = Event.where(id: event[:id]).first
assert_not_nil e, 'Event was not created in database'
# Check the places numbers were set successfully
e = Event.where(id: event[:id]).first
assert_equal 10, e.nb_total_places, 'Total number of places was not updated'
assert_equal 10, e.nb_free_places, 'Number of free places was not updated'
# Now, let's make a reservation on this event
post '/api/local_payment/confirm_payment',
params: {
customer_id: User.find_by(username: 'lseguin').id,
items: [
{
reservation: {
reservable_id: e.id,
reservable_type: 'Event',
nb_reserve_places: 4,
slots_attributes: [
{
start_at: e.availability.start_at,
end_at: e.availability.end_at,
availability_id: e.availability.id,
offered: false
}
],
tickets_attributes: [
{
event_price_category_id: e.event_price_categories.first.id,
booked: 4
}
]
}
# Now, let's make a reservation on this event
post '/api/local_payment/confirm_payment',
params: {
customer_id: User.find_by(username: 'lseguin').id,
items: [
{
reservation: {
reservable_id: e.id,
reservable_type: 'Event',
nb_reserve_places: 4,
slots_attributes: [
{
start_at: e.availability.start_at,
end_at: e.availability.end_at,
availability_id: e.availability.id,
offered: false
}
],
tickets_attributes: [
{
event_price_category_id: e.event_price_categories.first.id,
booked: 4
}
]
}
]
}.to_json,
headers: default_headers
}
]
}.to_json,
headers: default_headers
# Check response format & status
assert_equal 201, response.status, response.body
assert_equal Mime[:json], response.content_type
# Check response format & status
assert_equal 201, response.status, response.body
assert_equal Mime[:json], response.content_type
# Check the reservation match the required event
result = json_response(response.body)
i = Invoice.find(result[:id])
# Check the reservation match the required event
result = json_response(response.body)
i = Invoice.find(result[:id])
assert_equal e.id, i.main_item.object_id
assert_equal 'Event', i.main_item.object_type
assert_equal e.id, i.main_item.object.reservable_id
assert_equal 'Event', i.main_item.object.reservable_type
# Check the remaining places were updated successfully
e = Event.where(id: event[:id]).first
assert_equal 2, e.nb_free_places, 'Number of free places was not updated'
# Check the remaining places were updated successfully
e = Event.where(id: event[:id]).first
assert_equal 2, e.nb_free_places, 'Number of free places was not updated'
# Check the resulting invoice generation and it has right price
assert_invoice_pdf i
assert_equal (4 * 20) + (4 * 16), i.total / 100.0
end
# Check the resulting invoice generation and it has right price
assert_invoice_pdf i
assert_equal (4 * 20) + (4 * 16), i.total / 100.0
end
end

View File

@ -79,11 +79,11 @@ class Events::AsUserTest < ActionDispatch::IntegrationTest
# reservation assertions
reservation = Reservation.last
assert reservation.invoice
assert_equal 1, reservation.invoice.invoice_items.count
assert reservation.original_invoice
assert_equal 1, reservation.original_invoice.invoice_items.count
# invoice assertions
invoice = reservation.invoice
invoice = reservation.original_invoice
refute invoice.payment_gateway_object.blank?
refute invoice.total.blank?

View File

@ -473,7 +473,8 @@ class Reservations::CreateAsAdminTest < ActionDispatch::IntegrationTest
assert_equal plan.id, @user_without_subscription.subscribed_plan.id
# reservation assertions
reservation = Reservation.find(result[:id])
invoice = Invoice.find(result[:id])
reservation = invoice.main_item.object
assert reservation.original_invoice
assert_equal 2, reservation.original_invoice.invoice_items.count
@ -493,7 +494,7 @@ class Reservations::CreateAsAdminTest < ActionDispatch::IntegrationTest
# invoice_items
invoice_items = InvoiceItem.last(2)
assert(invoice_items.any? { |ii| ii.amount == plan.amount && !ii.subscription_id.nil? })
assert(invoice_items.any? { |ii| ii.amount == plan.amount && !ii.subscription.nil? })
assert(invoice_items.any? { |ii| ii.amount.zero? })
# invoice assertions

View File

@ -774,12 +774,14 @@ class Reservations::CreateTest < ActionDispatch::IntegrationTest
assert_equal plan.id, @user_without_subscription.subscribed_plan.id, "user's plan does not match"
# reservation assertions
assert reservation.payment_schedule
assert reservation.original_payment_schedule
assert_equal payment_schedule.main_object.object, reservation
# Check the answer
reservation = json_response(response.body)
assert_equal plan.id, reservation[:user][:subscribed_plan][:id], 'subscribed plan does not match'
result = json_response(response.body)
assert_equal payment_schedule.id, result[:id], 'payment schedule id does not match'
subscription = payment_schedule.payment_schedule_objects.find(&:subscription).object
assert_equal plan.id, subscription.plan_id, 'subscribed plan does not match'
end
test 'user reserves a machine and renew a subscription with payment schedule and coupon and wallet' do
@ -831,6 +833,7 @@ class Reservations::CreateTest < ActionDispatch::IntegrationTest
cart_items: {
coupon_code: 'GIME3EUR',
payment_schedule: true,
payment_method: 'card',
items: [
{
reservation: {
@ -880,7 +883,7 @@ class Reservations::CreateTest < ActionDispatch::IntegrationTest
assert_equal plan.id, user.subscribed_plan.id, "user's plan does not match"
# reservation assertions
assert reservation.payment_schedule
assert reservation.original_payment_schedule
assert_equal payment_schedule.main_object.object, reservation
# payment schedule assertions
@ -897,7 +900,9 @@ class Reservations::CreateTest < ActionDispatch::IntegrationTest
assert_equal payment_schedule.invoicing_profile_id, payment_schedule.operator_profile_id
# Check the answer
reservation = json_response(response.body)
assert_equal plan.id, reservation[:user][:subscribed_plan][:id], 'subscribed plan does not match'
result = json_response(response.body)
assert_equal payment_schedule.id, result[:id], 'payment schedule id does not match'
subscription = payment_schedule.payment_schedule_objects.find(&:subscription).object
assert_equal plan.id, subscription.plan_id, 'subscribed plan does not match'
end
end

View File

@ -2,6 +2,8 @@
require 'test_helper'
module Subscriptions; end
class Subscriptions::CreateAsAdminTest < ActionDispatch::IntegrationTest
setup do
@admin = User.find_by(username: 'admin')
@ -31,8 +33,11 @@ class Subscriptions::CreateAsAdminTest < ActionDispatch::IntegrationTest
assert_equal Mime[:json], response.content_type
# Check the correct plan was subscribed
subscription = json_response(response.body)
assert_equal plan.id, subscription[:plan_id], 'subscribed plan does not match'
result = json_response(response.body)
assert_equal Invoice.last.id, result[:id], 'invoice id does not match'
subscription = Invoice.find(result[:id]).invoice_items.first.object
assert_equal plan.id, subscription.plan_id, 'subscribed plan does not match'
# Check that the user has only one subscription
assert_equal 1, user.subscriptions.count
@ -51,12 +56,13 @@ class Subscriptions::CreateAsAdminTest < ActionDispatch::IntegrationTest
assert_equal 15, (printer.prices.find_by(group_id: user.group_id, plan_id: user.subscription.plan_id).amount / 100.00), 'machine hourly price does not match'
# Check notification was sent to the user
notification = Notification.find_by(notification_type_id: NotificationType.find_by_name('notify_member_subscribed_plan'), attached_object_type: 'Subscription', attached_object_id: subscription[:id])
notification = Notification.find_by(notification_type_id: NotificationType.find_by_name('notify_member_subscribed_plan'),
attached_object_type: 'Subscription', attached_object_id: subscription.id)
assert_not_nil notification, 'user notification was not created'
assert_equal user.id, notification.receiver_id, 'wrong user notified'
# Check generated invoice
item = InvoiceItem.find_by(object_type: 'Subscription', object_id: subscription[:id])
item = InvoiceItem.find_by(object_type: 'Subscription', object_id: subscription.id)
invoice = item.invoice
assert_invoice_pdf invoice
assert_equal plan.amount, invoice.total, 'Invoice total price does not match the bought subscription'
@ -121,8 +127,10 @@ class Subscriptions::CreateAsAdminTest < ActionDispatch::IntegrationTest
assert_equal payment_schedule_items_count + 12, PaymentScheduleItem.count, 'missing some payment schedule items'
# Check the correct plan was subscribed
subscription = json_response(response.body)
assert_equal plan.id, subscription[:plan_id], 'subscribed plan does not match'
result = json_response(response.body)
assert_equal PaymentSchedule.last.id, result[:id], 'payment schedule id does not match'
subscription = PaymentSchedule.find(result[:id]).payment_schedule_objects.first.object
assert_equal plan.id, subscription.plan_id, 'subscribed plan does not match'
# Check that the user has the correct subscription
assert_not_nil user.subscription, "user's subscription was not found"

View File

@ -32,8 +32,10 @@ class Subscriptions::CreateAsUserTest < ActionDispatch::IntegrationTest
assert_equal Mime[:json], response.content_type
# Check the correct plan was subscribed
subscription = json_response(response.body)
assert_equal plan.id, subscription[:plan_id], 'subscribed plan does not match'
result = json_response(response.body)
assert_equal Invoice.last.id, result[:id], 'invoice id does not match'
subscription = Invoice.find(result[:id]).invoice_items.first.object
assert_equal plan.id, subscription.plan_id, 'subscribed plan does not match'
# Check that the user has only one subscription
assert_equal 1, @user.subscriptions.count
@ -131,8 +133,10 @@ class Subscriptions::CreateAsUserTest < ActionDispatch::IntegrationTest
assert_equal Mime[:json], response.content_type
# Check the correct plan was subscribed
subscription = json_response(response.body)
assert_equal plan.id, subscription[:plan_id], 'subscribed plan does not match'
result = json_response(response.body)
assert_equal Invoice.last.id, result[:id], 'invoice id does not match'
subscription = Invoice.find(result[:id]).invoice_items.first.object
assert_equal plan.id, subscription.plan_id, 'subscribed plan does not match'
# Check that the user has the correct subscription
assert_not_nil @vlonchamp.subscription, "user's subscription was not found"
@ -237,8 +241,10 @@ class Subscriptions::CreateAsUserTest < ActionDispatch::IntegrationTest
assert_equal payment_schedule_items_count + 12, PaymentScheduleItem.count, 'missing some payment schedule items'
# Check the correct plan was subscribed
subscription = json_response(response.body)
assert_equal plan.id, subscription[:plan_id], 'subscribed plan does not match'
result = json_response(response.body)
assert_equal PaymentSchedule.last.id, result[:id], 'payment schedule id does not match'
subscription = PaymentSchedule.find(result[:id]).payment_schedule_objects.first.object
assert_equal plan.id, subscription.plan_id, 'subscribed plan does not match'
# Check that the user has the correct subscription
assert_not_nil @user.subscription, "user's subscription was not found"

View File

@ -31,8 +31,10 @@ class Subscriptions::RenewAsAdminTest < ActionDispatch::IntegrationTest
assert_equal Mime[:json], response.content_type
# Check the correct plan was subscribed
subscription = json_response(response.body)
assert_equal plan.id, subscription[:plan_id], 'subscribed plan does not match'
result = json_response(response.body)
assert_equal Invoice.last.id, result[:id], 'invoice id does not match'
subscription = Invoice.find(result[:id]).invoice_items.first.object
assert_equal plan.id, subscription.plan_id, 'subscribed plan does not match'
# Check that the user has the correct subscription
assert_not_nil user.subscription, "user's subscription was not found"
@ -41,8 +43,8 @@ class Subscriptions::RenewAsAdminTest < ActionDispatch::IntegrationTest
assert_equal plan.id, user.subscription.plan_id, "user's plan does not match"
# Check the expiration date
assert_equal (user.subscription.created_at + plan.interval_count.send(plan.interval)).iso8601,
subscription[:expired_at],
assert_equal (user.subscription.created_at + plan.interval_count.send(plan.interval)).to_i,
subscription.expiration_date.to_i,
'subscription expiration date does not match'
# Check the subscription was correctly saved

View File

@ -32,8 +32,10 @@ class Subscriptions::RenewAsUserTest < ActionDispatch::IntegrationTest
assert_equal Mime[:json], response.content_type
# Check the correct plan was subscribed
subscription = json_response(response.body)
assert_equal plan.id, subscription[:plan_id], 'subscribed plan does not match'
result = json_response(response.body)
assert_equal Invoice.last.id, result[:id], 'invoice id does not match'
subscription = Invoice.find(result[:id]).invoice_items.first.object
assert_equal plan.id, subscription.plan_id, 'subscribed plan does not match'
# Check the subscription was correctly saved
assert_equal 2, @user.subscriptions.count