|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +RSpec.describe MidtransApi::Api::Transaction::Expire do |
| 6 | + let(:client) do |
| 7 | + MidtransApi::Client.new( |
| 8 | + client_key: 'client_key', |
| 9 | + server_key: 'server_key', |
| 10 | + sandbox: true, |
| 11 | + notification_url: 'someapps://callback' |
| 12 | + ) |
| 13 | + end |
| 14 | + |
| 15 | + describe '#post' do |
| 16 | + context 'with valid params' do |
| 17 | + dummy_response = { |
| 18 | + "status_code": "407", |
| 19 | + "status_message": "Success, transaction is expired", |
| 20 | + "transaction_id": "24dd3e24-2b1f-4e09-9176-f4a15df3c869", |
| 21 | + "order_id": "eb046679-285a-4136-8977-e4c429cc3254", |
| 22 | + "merchant_id": "M045108", |
| 23 | + "gross_amount": "1000000.00", |
| 24 | + "currency": "IDR", |
| 25 | + "payment_type": "bank_transfer", |
| 26 | + "transaction_time": "2023-03-15 15:19:23", |
| 27 | + "transaction_status": "expire", |
| 28 | + "fraud_status": "accept" |
| 29 | + } |
| 30 | + dummy_order_id = "eb046679-285a-4136-8977-e4c429cc3254" |
| 31 | + it 'using described class returns success response' do |
| 32 | + stub_request(:post, "#{client.config.api_url}/#{client.config.api_version}/#{dummy_order_id}/expire") |
| 33 | + .with(body: {}) |
| 34 | + .to_return(status: 200, body: dummy_response.to_json) |
| 35 | + expire_api = described_class.new(client) |
| 36 | + response = expire_api.post(order_id: dummy_order_id) |
| 37 | + |
| 38 | + expect(response).to be_instance_of MidtransApi::Model::Transaction::Expire |
| 39 | + expect(response.order_id).to eq dummy_order_id |
| 40 | + expect(response.transaction_status).to eq "expire" |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + context 'with invalid params' do |
| 45 | + it 'raise NotFound; invalid transaction_id' do |
| 46 | + dummy_response = { |
| 47 | + "status_code": '404', |
| 48 | + "status_message": 'Transaction doesn\'t exist.', |
| 49 | + "id": 'b19510c2-6ad3-4c5b-92bf-049cfd5846c9' |
| 50 | + } |
| 51 | + dummy_order_id = "eb046679-28512312323254" |
| 52 | + stub_request(:post, "#{client.config.api_url}/#{client.config.api_version}/#{dummy_order_id}/expire") |
| 53 | + .with(body: {}) |
| 54 | + .to_return(status: 200, body: dummy_response.to_json) |
| 55 | + expect do |
| 56 | + expire_api = described_class.new(client) |
| 57 | + expire_api.post(order_id: dummy_order_id) |
| 58 | + end.to raise_error(MidtransApi::Errors::NotFound, 'Transaction doesn\'t exist.') |
| 59 | + end |
| 60 | + |
| 61 | + it 'raise CannotModifyTransaction; cannot modify transaction' do |
| 62 | + dummy_response = { |
| 63 | + "status_code": '412', |
| 64 | + "status_message": 'Transaction status cannot be updated.', |
| 65 | + "id": '95c25db5-08a4-4b3e-b7ce-8d56ebed1dac' |
| 66 | + } |
| 67 | + dummy_order_id = "2d44562e-6cb6-44f5-8cbd-751acb6e9cd2" |
| 68 | + stub_request(:post, "#{client.config.api_url}/#{client.config.api_version}/#{dummy_order_id}/expire") |
| 69 | + .with(body: {}) |
| 70 | + .to_return(status: 200, body: dummy_response.to_json) |
| 71 | + expect do |
| 72 | + expire_api = described_class.new(client) |
| 73 | + expire_api.post(order_id: dummy_order_id) |
| 74 | + end.to raise_error(MidtransApi::Errors::CannotModifyTransaction, 'Transaction status cannot be updated.') |
| 75 | + end |
| 76 | + end |
| 77 | + end |
| 78 | +end |
0 commit comments