1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-12 23:09:03 +01:00
fab-manager/test/integration/prices/compute_test.rb

96 lines
3.1 KiB
Ruby
Raw Normal View History

2020-03-13 17:10:38 +01:00
# frozen_string_literal: true
2021-07-02 15:45:52 +02:00
require 'test_helper'
2019-01-21 11:49:35 +01:00
module Prices
2016-12-12 16:44:56 +01:00
class AsAdminTest < ActionDispatch::IntegrationTest
setup do
admin = User.with_role(:admin).first
login_as(admin, scope: :user)
end
test 'compute price for a simple training' do
user = User.find_by(username: 'jdupond')
availability = Availability.find(2)
slot = Availability.find(2).slots.first
2016-12-12 16:44:56 +01:00
printer_training = availability.trainings.first
post '/api/prices/compute',
2020-03-13 17:10:38 +01:00
params: {
customer_id: user.id,
items: [
{
reservation: {
reservable_id: printer_training.id,
reservable_type: printer_training.class.name,
slots_reservations_attributes: [
{
slot_id: slot.id,
offered: false
}
]
2019-01-21 11:49:35 +01:00
}
}
]
2016-12-12 16:44:56 +01:00
}.to_json,
2020-03-13 17:10:38 +01:00
headers: default_headers
2016-12-12 16:44:56 +01:00
# Check response format & status
assert_equal 200, response.status, response.body
2020-03-13 17:10:38 +01:00
assert_equal Mime[:json], response.content_type
2016-12-12 16:44:56 +01:00
# Check the price was computed correctly
price = json_response(response.body)
2019-01-21 11:49:35 +01:00
assert_equal (printer_training.trainings_pricings.where(group_id: user.group_id).first.amount / 100.0),
price[:price],
'Computed price did not match training price'
2016-12-12 16:44:56 +01:00
end
test 'compute price for a machine reservation with an offered slot and a subscription' do
user = User.find_by(username: 'jdupond')
availability = Availability.find(3)
laser = availability.machines.where(id: 1).first
plan = Plan.find_by(group_id: user.group_id, interval: 'month')
2016-12-12 16:44:56 +01:00
post '/api/prices/compute',
2020-03-13 17:10:38 +01:00
params: {
customer_id: user.id,
items: [
{
reservation: {
reservable_id: laser.id,
reservable_type: laser.class.name,
slots_reservations_attributes: [
{
slot_id: availability.slots.first.id,
offered: true
},
{
slot_id: availability.slots.last.id,
offered: false
}
]
2019-01-21 11:49:35 +01:00
}
},
{
subscription: {
plan_id: plan.id
}
}
]
2016-12-12 16:44:56 +01:00
}.to_json,
2020-03-13 17:10:38 +01:00
headers: default_headers
2016-12-12 16:44:56 +01:00
# Check response format & status
assert_equal 200, response.status, response.body
2020-03-13 17:10:38 +01:00
assert_equal Mime[:json], response.content_type
2016-12-12 16:44:56 +01:00
# Check the event was created correctly
price = json_response(response.body)
2019-01-21 11:49:35 +01:00
assert_equal ((laser.prices.where(group_id: user.group_id, plan_id: plan.id).first.amount + plan.amount) / 100.0),
price[:price],
'Computed price did not match machine + subscription price'
2016-12-12 16:44:56 +01:00
end
end
end