1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-13 23:48:55 +01:00
fab-manager/test/integration/wallets_test.rb

114 lines
3.7 KiB
Ruby
Raw Normal View History

2020-03-13 17:10:38 +01:00
# frozen_string_literal: true
2016-07-18 18:16:54 +02:00
require 'test_helper'
2020-03-13 17:10:38 +01:00
class WalletsTest < ActionDispatch::IntegrationTest
2016-07-18 18:16:54 +02:00
# Called before every test method runs. Can be used
# to set up fixture information.
def setup
2016-07-11 11:40:20 +02:00
@vlonchamp = User.find_by(username: 'vlonchamp')
login_as(@vlonchamp, scope: :user)
2016-07-18 18:16:54 +02:00
end
# Called after every test method runs. Can be used to tear
# down fixture information.
def teardown
# Do nothing
end
test 'get my wallet' do
2016-07-11 11:40:20 +02:00
get "/api/wallet/by_user/#{@vlonchamp.id}"
2016-07-18 18:16:54 +02:00
assert_equal 200, response.status
2020-03-13 17:10:38 +01:00
assert_equal Mime[:json], response.content_type
2016-07-18 18:16:54 +02:00
wallet = json_response(response.body)
assert_equal @vlonchamp.wallet.invoicing_profile_id, wallet[:invoicing_profile_id]
2016-07-11 11:40:20 +02:00
assert_equal @vlonchamp.wallet.amount, wallet[:amount]
2016-07-18 18:16:54 +02:00
end
test 'admin can get wallet by user id' do
2016-11-23 16:30:19 +01:00
@admin = User.find_by(username: 'admin')
2016-07-18 18:16:54 +02:00
login_as(@admin, scope: :user)
@user1 = User.first
get "/api/wallet/by_user/#{@user1.id}"
assert_equal 200, response.status
2020-03-13 17:10:38 +01:00
assert_equal Mime[:json], response.content_type
2016-07-18 18:16:54 +02:00
wallet = json_response(response.body)
assert_equal @user1.wallet.invoicing_profile_id, wallet[:invoicing_profile_id]
2016-07-18 18:16:54 +02:00
assert_equal @user1.wallet.amount, wallet[:amount]
end
2016-07-05 13:20:25 +02:00
test 'cant get wallet of an user if not admin' do
2016-07-11 11:40:20 +02:00
user5 = users(:user_4)
2016-07-05 13:20:25 +02:00
get "/api/wallet/by_user/#{user5.id}"
assert_equal 403, response.status
end
test 'get all transactions of wallet' do
2016-07-11 11:40:20 +02:00
w = @vlonchamp.wallet
2016-07-05 13:20:25 +02:00
get "/api/wallet/#{w.id}/transactions"
assert_equal 200, response.status
2020-03-13 17:10:38 +01:00
assert_equal Mime[:json], response.content_type
2016-07-05 13:20:25 +02:00
transactions = json_response(response.body)
assert_equal w.wallet_transactions.count, transactions.size
assert_equal wallet_transactions(:transaction1).id, transactions.first[:id]
end
test 'only admin and wallet owner can show their transactions' do
2016-07-11 11:40:20 +02:00
user5 = users(:user_4)
2016-07-05 13:20:25 +02:00
get "/api/wallet/#{user5.wallet.id}/transactions"
2016-07-18 18:16:54 +02:00
assert_equal 403, response.status
end
test 'admin can credit amount to a wallet' do
admin = users(:user_1)
login_as(admin, scope: :user)
2016-07-11 11:40:20 +02:00
w = @vlonchamp.wallet
2016-07-11 16:04:15 +02:00
amount = 10.5
expected_amount = w.amount + amount
put "/api/wallet/#{w.id}/credit",
2020-03-13 17:10:38 +01:00
params: { amount: amount }
assert_equal 200, response.status
2020-03-13 17:10:38 +01:00
assert_equal Mime[:json], response.content_type
wallet = json_response(response.body)
w.reload
assert_equal w.amount, expected_amount
assert_equal w.amount, wallet[:amount]
# no refund invoices should have been generated
2021-05-27 15:58:55 +02:00
assert_empty InvoiceItem.where(object: w.wallet_transactions.last)
end
test 'admin credit wallet with refund invoice generation' do
admin = users(:user_1)
login_as(admin, scope: :user)
w = @vlonchamp.wallet
amount = 10
avoir_date = Time.current.end_of_day
expected_amount = w.amount + amount
put "/api/wallet/#{w.id}/credit",
2020-03-13 17:10:38 +01:00
params: {
amount: amount,
avoir: true,
avoir_date: avoir_date,
avoir_description: 'Some text'
}
assert_equal 200, response.status
2020-03-13 17:10:38 +01:00
assert_equal Mime[:json], response.content_type
wallet = json_response(response.body)
w.reload
assert_equal w.amount, expected_amount
assert_equal w.amount, wallet[:amount]
# refund invoice must be generated
2021-05-27 15:58:55 +02:00
item = InvoiceItem.find_by(object: w.wallet_transactions.last)
invoice = item.invoice
assert_equal amount, (invoice.total / 100.0), 'Avoir total does not match the amount credited to the wallet'
assert_equal amount, (invoice.invoice_items.first.amount / 100.0), 'Invoice item amount does not match'
assert_invoice_pdf invoice
2021-05-31 12:19:28 +02:00
assert_not_nil invoice.debug_footprint
end
2016-07-18 18:16:54 +02:00
end