|
86 | 86 | } |
87 | 87 | end |
88 | 88 |
|
| 89 | + let(:merchants_with_unexpected_fields_response) do |
| 90 | + { |
| 91 | + "status_code": "200", |
| 92 | + "status_message": "Merchants have been successfully retrieved.", |
| 93 | + "unexpected_root_field": "some_value", |
| 94 | + "merchants": [ |
| 95 | + { |
| 96 | + "merchant_id": "G221991147", |
| 97 | + "merchant_name": "MSN KAP Agus Ubaidillah dan Rekan01", |
| 98 | + "merchant_phone_number": "+621000000000", |
| 99 | + "email": "dhany+39457_79@mekari.com", |
| 100 | + "unexpected_field": "unexpected_value", |
| 101 | + "another_unexpected_field": { |
| 102 | + "nested": "data" |
| 103 | + } |
| 104 | + }, |
| 105 | + { |
| 106 | + "merchant_id": "G539217527", |
| 107 | + "merchant_name": "MSN KAP Agus Ubaidillah dan Rekan01", |
| 108 | + "merchant_phone_number": "+621000000000", |
| 109 | + "email": "dhany+39457_80@mekari.com", |
| 110 | + "extra_field": 12345, |
| 111 | + "boolean_field": true |
| 112 | + } |
| 113 | + ] |
| 114 | + } |
| 115 | + end |
| 116 | + |
89 | 117 | describe '#get' do |
90 | 118 | context 'with keyword parameter' do |
91 | 119 | it 'returns expected response' do |
|
232 | 260 | expect(second_merchant.email).to be_nil |
233 | 261 | end |
234 | 262 | end |
| 263 | + |
| 264 | + context 'when response contains unexpected fields' do |
| 265 | + it 'ignores unexpected fields and processes only expected ones' do |
| 266 | + partner_id = '739' |
| 267 | + params = {} |
| 268 | + |
| 269 | + stub_request(:get, "#{client.config.api_url}/#{client.config.api_version}/merchants") |
| 270 | + .to_return(status: 200, body: merchants_with_unexpected_fields_response.to_json) |
| 271 | + |
| 272 | + merchant_api = described_class.new(client) |
| 273 | + response = merchant_api.get(params, partner_id) |
| 274 | + |
| 275 | + expect(response).to be_instance_of MidtransApi::Model::Merchant::Get |
| 276 | + expect(response.merchants).to be_an(Array) |
| 277 | + expect(response.merchants.length).to eq(2) |
| 278 | + |
| 279 | + # Verify that unexpected root-level fields are ignored |
| 280 | + expect(response).not_to respond_to(:unexpected_root_field) |
| 281 | + |
| 282 | + merchant_list = response.merchant_list |
| 283 | + expect(merchant_list.length).to eq(2) |
| 284 | + |
| 285 | + # Test first merchant - should only have expected fields |
| 286 | + first_merchant = merchant_list.first |
| 287 | + expect(first_merchant.merchant_id).to eq('G221991147') |
| 288 | + expect(first_merchant.merchant_name).to eq('MSN KAP Agus Ubaidillah dan Rekan01') |
| 289 | + expect(first_merchant.merchant_phone_number).to eq('+621000000000') |
| 290 | + expect(first_merchant.email).to eq('dhany+39457_79@mekari.com') |
| 291 | + |
| 292 | + # Verify that unexpected merchant fields are ignored |
| 293 | + expect(first_merchant).not_to respond_to(:unexpected_field) |
| 294 | + expect(first_merchant).not_to respond_to(:another_unexpected_field) |
| 295 | + |
| 296 | + # Test second merchant - should only have expected fields |
| 297 | + second_merchant = merchant_list.last |
| 298 | + expect(second_merchant.merchant_id).to eq('G539217527') |
| 299 | + expect(second_merchant.merchant_name).to eq('MSN KAP Agus Ubaidillah dan Rekan01') |
| 300 | + expect(second_merchant.merchant_phone_number).to eq('+621000000000') |
| 301 | + expect(second_merchant.email).to eq('dhany+39457_80@mekari.com') |
| 302 | + |
| 303 | + # Verify that unexpected merchant fields are ignored |
| 304 | + expect(second_merchant).not_to respond_to(:extra_field) |
| 305 | + expect(second_merchant).not_to respond_to(:boolean_field) |
| 306 | + |
| 307 | + # Verify to_h method only returns expected fields |
| 308 | + first_merchant_hash = first_merchant.to_h |
| 309 | + expect(first_merchant_hash.keys).to contain_exactly(:merchant_id, :merchant_name, :merchant_phone_number, :email) |
| 310 | + expect(first_merchant_hash[:merchant_id]).to eq('G221991147') |
| 311 | + end |
| 312 | + end |
235 | 313 | end |
236 | 314 | end |
0 commit comments