2020-03-13 17:10:38 +01:00
|
|
|
# frozen_string_literal: true
|
2016-07-18 18:16:54 +02:00
|
|
|
|
2020-04-01 17:58:32 +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
|
2023-02-24 17:26:55 +01:00
|
|
|
assert_match Mime[:json].to_s, response.content_type
|
2016-07-18 18:16:54 +02:00
|
|
|
wallet = json_response(response.body)
|
2019-06-03 16:00:09 +02:00
|
|
|
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
|
2023-02-24 17:26:55 +01:00
|
|
|
assert_match Mime[:json].to_s, response.content_type
|
2016-07-18 18:16:54 +02:00
|
|
|
wallet = json_response(response.body)
|
2019-06-03 16:00:09 +02:00
|
|
|
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
|
2023-03-21 15:11:08 +01:00
|
|
|
user5 = users(:user4)
|
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
|
2023-02-24 17:26:55 +01:00
|
|
|
assert_match Mime[:json].to_s, 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
|
2023-03-21 15:11:08 +01:00
|
|
|
user5 = users(:user4)
|
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
|
2016-07-05 19:07:50 +02:00
|
|
|
|
|
|
|
test 'admin can credit amount to a wallet' do
|
2023-03-21 15:11:08 +01:00
|
|
|
admin = users(:user1)
|
2016-07-05 19:07:50 +02:00
|
|
|
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
|
2016-07-05 19:07:50 +02:00
|
|
|
expected_amount = w.amount + amount
|
|
|
|
put "/api/wallet/#{w.id}/credit",
|
2020-03-13 17:10:38 +01:00
|
|
|
params: { amount: amount }
|
2016-07-05 19:07:50 +02:00
|
|
|
|
|
|
|
assert_equal 200, response.status
|
2023-02-24 17:26:55 +01:00
|
|
|
assert_match Mime[:json].to_s, response.content_type
|
2016-07-05 19:07:50 +02:00
|
|
|
wallet = json_response(response.body)
|
|
|
|
w.reload
|
|
|
|
assert_equal w.amount, expected_amount
|
|
|
|
assert_equal w.amount, wallet[:amount]
|
2016-12-13 16:00:12 +01:00
|
|
|
|
|
|
|
# no refund invoices should have been generated
|
2021-05-27 15:58:55 +02:00
|
|
|
assert_empty InvoiceItem.where(object: w.wallet_transactions.last)
|
2016-12-13 16:00:12 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'admin credit wallet with refund invoice generation' do
|
2023-03-21 15:11:08 +01:00
|
|
|
admin = users(:user1)
|
2016-12-13 16:00:12 +01:00
|
|
|
login_as(admin, scope: :user)
|
|
|
|
w = @vlonchamp.wallet
|
|
|
|
amount = 10
|
2023-02-14 13:10:58 +01:00
|
|
|
avoir_date = Time.current.end_of_day
|
2016-12-13 16:00:12 +01:00
|
|
|
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'
|
|
|
|
}
|
2016-12-13 16:00:12 +01:00
|
|
|
|
|
|
|
assert_equal 200, response.status
|
2023-02-24 17:26:55 +01:00
|
|
|
assert_match Mime[:json].to_s, response.content_type
|
2016-12-13 16:00:12 +01:00
|
|
|
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
|
2016-12-13 16:00:12 +01:00
|
|
|
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
|
2016-07-05 19:07:50 +02:00
|
|
|
end
|
2016-07-18 18:16:54 +02:00
|
|
|
end
|