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

87 lines
3.1 KiB
Ruby
Raw Normal View History

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)
printer_training = availability.trainings.first
post '/api/prices/compute',
{
2019-01-21 11:49:35 +01:00
reservation: {
user_id: user.id,
reservable_id: printer_training.id,
reservable_type: printer_training.class.name,
slots_attributes: [
{
availability_id: availability.id,
end_at: availability.end_at,
offered: false,
start_at: availability.start_at
}
]
}
2016-12-12 16:44:56 +01:00
}.to_json,
default_headers
# Check response format & status
assert_equal 200, response.status, response.body
assert_equal Mime::JSON, response.content_type
# 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.where(group_id: user.group_id, interval: 'month').first
post '/api/prices/compute',
{
2019-01-21 11:49:35 +01:00
reservation: {
user_id: user.id,
reservable_id: laser.id,
reservable_type: laser.class.name,
plan_id: plan.id,
slots_attributes: [
{
availability_id: availability.id,
end_at: (availability.start_at + 1.hour).strftime('%Y-%m-%d %H:%M:%S.%9N Z'),
offered: true,
start_at: availability.start_at.strftime('%Y-%m-%d %H:%M:%S.%9N Z')
},
{
availability_id: availability.id,
end_at: (availability.start_at + 2.hour).strftime('%Y-%m-%d %H:%M:%S.%9N Z'),
offered: false,
start_at: (availability.start_at + 1.hour).strftime('%Y-%m-%d %H:%M:%S.%9N Z')
}
]
}
2016-12-12 16:44:56 +01:00
}.to_json,
default_headers
# Check response format & status
assert_equal 200, response.status, response.body
assert_equal Mime::JSON, response.content_type
# 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