diff --git a/test/integration/wallets_test.rb b/test/integration/wallets_test.rb index 19a68a1fa..d279edc28 100644 --- a/test/integration/wallets_test.rb +++ b/test/integration/wallets_test.rb @@ -74,5 +74,38 @@ class WalletsTest < ActionDispatch::IntegrationTest w.reload assert_equal w.amount, expected_amount assert_equal w.amount, wallet[:amount] + + # no refund invoices should have been generated + assert_empty Invoice.where(invoiced: 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.now.end_of_day + expected_amount = w.amount + amount + put "/api/wallet/#{w.id}/credit", + { + amount: amount, + avoir: true, + avoir_date: avoir_date, + avoir_description: 'Some text' + } + + assert_equal 200, response.status + 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 + invoice = Invoice.where(invoiced: w.wallet_transactions.last).first + 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 end end