|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'test_helper' |
| 4 | + |
| 5 | +def stub_find_transaction_request_with_error |
| 6 | + stub_request(:get, %r{/transactions/(.*)}) |
| 7 | + .to_return_json(body: { error: 'balance with ID \'transaction_id\' not found' }, status: 400) |
| 8 | +end |
| 9 | + |
| 10 | +def transaction_response_body # rubocop:disable Metrics/MethodLength |
| 11 | + { |
| 12 | + precise_amount: 7500, |
| 13 | + amount: 75, |
| 14 | + rate: 0, |
| 15 | + precision: 100, |
| 16 | + transaction_id: 'txn_bb8e13c5-1d16-4e94-add7-efd377fd551a', |
| 17 | + parent_transaction: '', |
| 18 | + source: 'bln_216507cb-2fb1-4238-a184-805c455f8fe2', |
| 19 | + destination: 'bln_469f93bc-40e9-4e0e-b6ab-d11c3638c15d', |
| 20 | + reference: 'ref_005', |
| 21 | + currency: 'BRLX', |
| 22 | + description: 'For fees', |
| 23 | + status: 'QUEUED', |
| 24 | + hash: '74a2fecf01e73a31c005ce3bd840d285edce87e3bdb3e0ed99e0af124a6165f8', |
| 25 | + allow_overdraft: true, |
| 26 | + inflight: false, |
| 27 | + created_at: '2024-06-27T20:23:17.737289826Z', |
| 28 | + scheduled_for: '0001-01-01T00:00:00Z', |
| 29 | + inflight_expiry_date: '0001-01-01T00:00:00Z' |
| 30 | + } |
| 31 | +end |
| 32 | + |
| 33 | +def stub_find_transaction_request_with_success |
| 34 | + stub_request(:get, %r{/transactions/(.*)}) |
| 35 | + .to_return_json(body: transaction_response_body, status: 200) |
| 36 | +end |
| 37 | + |
| 38 | +def stub_create_transaction_request_with_success |
| 39 | + stub_request(:post, %r{/transactions}) |
| 40 | + .to_return_json(body: transaction_response_body, status: 200) |
| 41 | +end |
| 42 | + |
| 43 | +def stub_create_transaction_request_with_error |
| 44 | + stub_request(:post, %r{/transactions}) |
| 45 | + .to_return_json(body: { error: 'missing transaction_id' }, status: 400) |
| 46 | +end |
| 47 | + |
| 48 | +# NOTE: This route does not exists, at least for now |
| 49 | +def stub_all_transaction_request_with_error |
| 50 | + stub_request(:get, %r{/transactions}) |
| 51 | + .to_return_json(body: { error: 'internal_server_error' }, status: 500) |
| 52 | +end |
| 53 | + |
| 54 | +def stub_all_transaction_request_with_success |
| 55 | + stub_request(:get, %r{/transactions}) |
| 56 | + .to_return_json(body: [transaction_response_body], status: 200) |
| 57 | +end |
| 58 | + |
| 59 | +class TestTransaction < Minitest::Test |
| 60 | + def test_that_transaction_not_found |
| 61 | + stub_find_transaction_request_with_error |
| 62 | + find = Blnk::Transaction.find 'transaction_id' |
| 63 | + |
| 64 | + assert find.status.bad_request? |
| 65 | + end |
| 66 | + |
| 67 | + def test_that_transaction_find_success |
| 68 | + stub_find_transaction_request_with_success |
| 69 | + find = Blnk::Transaction.find 'transaction_id' |
| 70 | + |
| 71 | + assert find.is_a?(Blnk::Transaction) |
| 72 | + assert find.transaction_id.eql?(transaction_response_body[:transaction_id]) |
| 73 | + end |
| 74 | + |
| 75 | + # NOTE: /transactions route does not exist, at moment |
| 76 | + # def test_that_transaction_all_error |
| 77 | + # stub_all_transaction_request_with_error |
| 78 | + |
| 79 | + # all = Blnk::Transaction.all |
| 80 | + |
| 81 | + # assert !all.status.success? |
| 82 | + # end |
| 83 | + |
| 84 | + # def test_that_transaction_all_success |
| 85 | + # stub_all_transaction_request_with_success |
| 86 | + |
| 87 | + # all = Blnk::Transaction.all |
| 88 | + |
| 89 | + # assert all.is_a?(Array) |
| 90 | + # assert all.first.is_a?(Blnk::Transaction) |
| 91 | + # assert all.first.transaction_id.eql?(transaction_response_body[:transaction_id]) |
| 92 | + # end |
| 93 | + |
| 94 | + def test_that_transaction_create_errosr |
| 95 | + stub_create_transaction_request_with_error |
| 96 | + |
| 97 | + create = Blnk::Transaction.create |
| 98 | + |
| 99 | + assert create.status.bad_request? |
| 100 | + end |
| 101 | + |
| 102 | + def test_that_transaction_create_success |
| 103 | + stub_create_transaction_request_with_success |
| 104 | + |
| 105 | + create = Blnk::Transaction.create(ledger_id: 'ledger_id', currency: 'USD') |
| 106 | + |
| 107 | + assert create.is_a?(Blnk::Transaction) |
| 108 | + assert create.transaction_id.eql?(transaction_response_body[:transaction_id]) |
| 109 | + assert create.name.eql?(transaction_response_body[:name]) |
| 110 | + end |
| 111 | +end |
0 commit comments