2019-09-20 17:40:27 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-06-29 14:15:36 +02:00
|
|
|
require 'test_helper'
|
|
|
|
|
2023-02-24 17:26:55 +01:00
|
|
|
module Credits; end
|
2017-12-21 14:24:50 +01:00
|
|
|
|
2023-02-24 17:26:55 +01:00
|
|
|
class Credits::TrainingTest < ActionDispatch::IntegrationTest
|
|
|
|
# Called before every test method runs. Can be used
|
|
|
|
# to set up fixture information.
|
|
|
|
def setup
|
|
|
|
admin = User.with_role(:admin).first
|
|
|
|
login_as(admin, scope: :user)
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'create machine credit' do
|
|
|
|
# First, we create a new credit
|
|
|
|
post '/api/credits',
|
|
|
|
params: {
|
|
|
|
credit: {
|
|
|
|
creditable_id: 5,
|
|
|
|
creditable_type: 'Machine',
|
|
|
|
hours: 1,
|
|
|
|
plan_id: 1
|
|
|
|
}
|
|
|
|
}.to_json,
|
|
|
|
headers: default_headers
|
2017-12-21 14:24:50 +01:00
|
|
|
|
2023-02-24 17:26:55 +01:00
|
|
|
# Check response format & status
|
|
|
|
assert_equal 201, response.status, response.body
|
|
|
|
assert_match Mime[:json].to_s, response.content_type
|
2017-12-21 14:24:50 +01:00
|
|
|
|
2023-02-24 17:26:55 +01:00
|
|
|
# Check the credit was created correctly
|
|
|
|
credit = json_response(response.body)
|
|
|
|
c = Credit.where(id: credit[:id]).first
|
|
|
|
assert_not_nil c, 'Credit was not created in database'
|
2017-12-21 14:24:50 +01:00
|
|
|
|
2023-02-24 17:26:55 +01:00
|
|
|
# Check that 1 hour is associated with the credit
|
|
|
|
assert_equal 1, c.hours
|
|
|
|
end
|
2017-12-21 14:24:50 +01:00
|
|
|
|
2023-02-24 17:26:55 +01:00
|
|
|
test 'update a credit' do
|
|
|
|
put '/api/credits/13',
|
|
|
|
params: {
|
|
|
|
credit: {
|
|
|
|
creditable_id: 4,
|
|
|
|
creditable_type: 'Machine',
|
|
|
|
hours: 5,
|
|
|
|
plan_id: 3
|
|
|
|
}
|
|
|
|
}.to_json,
|
|
|
|
headers: default_headers
|
2017-12-21 14:24:50 +01:00
|
|
|
|
2023-02-24 17:26:55 +01:00
|
|
|
# Check response format & status
|
|
|
|
assert_equal 200, response.status, response.body
|
|
|
|
assert_match Mime[:json].to_s, response.content_type
|
2017-12-21 14:24:50 +01:00
|
|
|
|
2023-02-24 17:26:55 +01:00
|
|
|
# Check the credit was correctly updated
|
|
|
|
credit = json_response(response.body)
|
|
|
|
assert_equal 13, credit[:id]
|
|
|
|
c = Credit.find(credit[:id])
|
|
|
|
assert c.updated_at > 1.minute.ago
|
2017-12-21 14:24:50 +01:00
|
|
|
|
2023-02-24 17:26:55 +01:00
|
|
|
assert_equal 5, c.hours
|
2017-12-21 14:24:50 +01:00
|
|
|
end
|
2020-03-13 17:10:38 +01:00
|
|
|
end
|