| 1 | # ruby -Ilib test/hbank_test.rb — starts sdks/testing/mock_server.py (Python 3 on PATH) and runs the SDK against it. |
| 2 | require "minitest/autorun" |
| 3 | require "net/http" |
| 4 | require "stringio" |
| 5 | require "hbank" |
| 6 | |
| 7 | PORT = rand(8300..8399) |
| 8 | BASE = "http://127.0.0.1:#{PORT}" |
| 9 | SERVER = IO.popen([Gem.win_platform? ? "python" : "python3", File.expand_path("../../testing/mock_server.py", __dir__), |
| 10 | "--port", PORT.to_s]) |
| 11 | SERVER.gets |
| 12 | Minitest.after_run { Process.kill("TERM", SERVER.pid) } |
| 13 | |
| 14 | class HBankTest < Minitest::Test |
| 15 | def bank = @bank ||= HBank::Client.new(api_key: "test_key", base_url: BASE) |
| 16 | def pay(tid) = Net::HTTP.post(URI("#{BASE}/_mock/pay/#{tid}"), "") |
| 17 | |
| 18 | def test_payment_flow |
| 19 | tid = bank.create_payment(amount: "24.90", currency: "EUR", description: "Order 1042", callback_url: "https://shop.example/cb") |
| 20 | assert_equal "#{BASE}/api/payment?tid=#{tid}", bank.checkout_url(tid) |
| 21 | assert bank.create_payment_code(tid)[:qr_svg].end_with?("/qr.svg") |
| 22 | assert_equal BigDecimal("24.90"), bank.get_payment(tid)[:amount] |
| 23 | pay(tid) |
| 24 | r = bank.refund(tid, amount: 10, reason: "Returned one item") |
| 25 | assert_equal BigDecimal("14.90"), r[:transaction][:refundable] |
| 26 | e = assert_raises(HBank::Error) { bank.refund(tid, amount: 100) } |
| 27 | assert_equal ["amount_too_high", 400], [e.code, e.status] |
| 28 | end |
| 29 | |
| 30 | def test_errors |
| 31 | assert_equal "invalid_amount", assert_raises(HBank::Error) { bank.create_payment(amount: 0, currency: "EUR") }.code |
| 32 | assert_equal 404, assert_raises(HBank::Error) { bank.get_payment("tx_NoSuchPayment0000000000000") }.status |
| 33 | assert_raises(HBank::AuthenticationError) { HBank::Client.new(api_key: "nope", base_url: BASE).list_subscriptions } |
| 34 | assert_raises(HBank::ConnectionError) { HBank::Client.new(api_key: "k", base_url: "http://127.0.0.1:1").rate("EUR", "USD") } |
| 35 | end |
| 36 | |
| 37 | def test_subscriptions |
| 38 | s = bank.create_subscription(amount: "9.99", currency: "EUR", interval: "month", description: "Pro plan") |
| 39 | assert_equal "offered", s[:status] |
| 40 | c = bank.checkout_subscription(s[:id]) |
| 41 | pay(c[:transaction_id]) |
| 42 | assert_equal "active", bank.get_subscription(s[:id])[:status] |
| 43 | assert_equal "merchant", bank.cancel_subscription(s[:id])[:canceled_by] |
| 44 | assert_equal "already_canceled", assert_raises(HBank::Error) { bank.cancel_subscription(s[:id]) }.code |
| 45 | end |
| 46 | |
| 47 | def test_several_subscriptions_in_one_checkout |
| 48 | storage = bank.create_subscription(amount: 5, currency: "EUR", interval: "month", description: "Storage") |
| 49 | c = bank.create_subscriptions_checkout([storage[:id], { amount: "2.50", currency: "EUR", interval: "week", description: "Support" }], |
| 50 | callback_url: "https://shop.example/cb") |
| 51 | assert_equal %w[Storage Support], c[:subscriptions].map { |x| x[:description] } |
| 52 | assert_equal storage[:id], c[:subscriptions][0][:id] |
| 53 | pay(c[:transaction_id]) |
| 54 | payment = bank.get_payment(c[:transaction_id]) |
| 55 | assert_equal BigDecimal("7.50"), payment[:amount] |
| 56 | assert_equal c[:subscriptions].map { |x| x[:id] }, payment[:subscription_ids] |
| 57 | c[:subscriptions].each { |x| assert_equal "active", bank.get_subscription(x[:id])[:status] } |
| 58 | assert_equal "not_open", assert_raises(HBank::Error) { bank.create_subscriptions_checkout([storage[:id]]) }.code |
| 59 | mixed = [{ amount: 1, currency: "EUR", interval: "month" }, { amount: 1, currency: "USD", interval: "month" }] |
| 60 | assert_equal "mixed_currencies", assert_raises(HBank::Error) { bank.create_subscriptions_checkout(mixed) }.code |
| 61 | end |
| 62 | |
| 63 | def test_currency |
| 64 | assert_equal BigDecimal("108.37"), bank.convert(100, "EUR", "USD") |
| 65 | assert_equal BigDecimal("1.0837"), bank.rate("EUR", "USD")[:to_rate] |
| 66 | end |
| 67 | |
| 68 | def test_callback |
| 69 | cb = HBank::Callback.parse('{"id": "tx_8fK2mQ7xRb4Tn9WcYz3LpH6v", "amount": 24.90, "currency": "EUR"}') |
| 70 | assert_equal ["tx_8fK2mQ7xRb4Tn9WcYz3LpH6v", BigDecimal("24.90")], [cb.id, cb.amount] |
| 71 | app = HBank::Callback.rack { |c| c.amount == BigDecimal("24.9") || "Wrong amount" } |
| 72 | status, headers, body = app.call("rack.input" => StringIO.new('{"id": "tx_8fK2mQ7xRb4Tn9WcYz3LpH6v", "amount": 5, "currency": "EUR"}')) |
| 73 | assert_equal [200, "application/json", '{"resume":false,"error":"Wrong amount"}'], [status, headers["content-type"], body.first] |
| 74 | assert_raises(HBank::Error) { HBank::Callback.parse("{}") } |
| 75 | end |
| 76 | |
| 77 | def test_callback_signature |
| 78 | body = '{"id":7310,"amount":24.9,"currency":"EUR","status":"awaiting_approval","description":"Order 1042"}' |
| 79 | header = "t=1700000000,v1=fcfc30b9667c3a8e8aea68178c36716153e075f79c61521bf3586088d9877281" |
| 80 | assert HBank::Callback.verify_signature("hbcb_test", header, body, now: 1_700_000_100) |
| 81 | refute HBank::Callback.verify_signature("hbcb_other", header, body, now: 1_700_000_100) |
| 82 | refute HBank::Callback.verify_signature("hbcb_test", header, "{}", now: 1_700_000_100) |
| 83 | refute HBank::Callback.verify_signature("hbcb_test", header, body, now: 1_700_000_400) |
| 84 | refute HBank::Callback.verify_signature("hbcb_test", "garbage", body, now: 1_700_000_100) |
| 85 | app = HBank::Callback.rack(secret: "hbcb_test") { true } |
| 86 | _, _, out = app.call("rack.input" => StringIO.new(body)) |
| 87 | assert_equal '{"resume":false,"error":"Invalid callback"}', out.first |
| 88 | end |
| 89 | end |