Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON boolean being converted to strings when testing post call in test env #1755

Closed
peco8 opened this issue Nov 24, 2016 · 6 comments
Closed

Comments

@peco8
Copy link

peco8 commented Nov 24, 2016

I've been testing a JSON request to our API, It will respond with JSON. It seems like all the boolean within the JSON get converted to strings as we post them the endpoint only under test environment.

However RAILS_ENV=development bundle exec guard works ok (no conversion), but once RAILS_ENV=test bundle exec guard converts all the boolean to string, which is unexpected.

This is my test case using mock.

  it 'create proration item' do
    # guess need to create a dummy account with stripe_customer_id
    account = create(:account, stripe_customer_id: 'cus_00000000000000')
    invoice = create(:invoice, stripe_invoice_id: 'in_00000000000000' )

    # create a plan
    plan = create(:plan)
    stripe_helper.create_plan(id: plan.stripe_plan_id)

    Stripe::InvoiceItem.create(
      amount: 300,
      currency: 'jpy',
      description: "#{300} charged",
      proration: true,
      type: 'invoiceitem'
    )
    item_objs = Stripe::InvoiceItem.list(:limit => 10)

    # create a event mock based on objs above
    event = StripeMock.mock_webhook_event('invoice.payment_succeeded', {
      id: invoice.stripe_invoice_id,
      lines: item_objs,
      customer: account.stripe_customer_id
    })

    # Mocking up private method :fetch_invoice_details
    balance_txn = Stripe::BalanceTransaction.retrieve('txn_05RsQX2eZvKYlo2C0FRTGSSA')
    allow_any_instance_of(InvoicePaymentSucceeded).to receive(:fetch_invoice_details).and_return(balance_txn)

    # when it's false (This should be changed depending on cases). Here we don't test private method.
    allow_any_instance_of(InvoicePaymentSucceeded).to receive(:initial_invoice?).and_return(false)

    post '/stripe-events', event.as_json
    expect(response.status).to eq 200
  end

Within end point handler, I could see the values below.

Under **test environment**,

#<Stripe::Event:0x3fe2ea57e95c id=test_evt_2> JSON: {
  "id": "test_evt_2",
  "created": "1326853478",
  "livemode": "false",
  "type": "invoice.payment_succeeded",
  "object": "event",
  "data": {"object":{"id":"in_00000000000000","date":"1394018368","period_start":"1394018368","period_end":"1394018368","lines":{"object":"list","data":[{"id":"test_ii_1","object":"invoiceitem","date":"1349738920","amount":"300","livemode":"false","proration":"true","currency":"jpy","customer":"cus_test","description":"300 charged","invoice":null,"subscription":null,"type":"invoiceitem"}],"url":"/v1/hashs","has_more":"false"},"subtotal":"30000","total":"30000","customer":"cus_00000000000000","object":"invoice","attempted":"true","closed":"true","paid":"true","livemode":"false","attempt_count":"1","amount_due":"0","currency":"usd","starting_balance":"0","ending_balance":"0","next_payment_attempt":null,"charge":"ch_00000000000000","discount":null,"application_fee":null,"subscription":"su_00000000000000","description":null}},
  "controller": "stripe_event/webhook",
  "action": "event"
}

Under **development environment**,

#<Stripe::Event:0x3fce66d141f0 id=test_evt_2> JSON: {
  "id": "test_evt_2",
  "created": 1326853478,
  "livemode": false,
  "type": "invoice.payment_succeeded",
  "object": "event",
  "data": {"object":{"id":"in_00000000000000","date":1394018368,"period_start":1394018368,"period_end":1394018368,"lines":{"object":"list","data":[{"id":"test_ii_1","object":"invoiceitem","date":1349738920,"amount":300,"livemode":false,"proration":true,"currency":"jpy","customer":"cus_test","description":"300 charged","metadata":{},"invoice":null,"subscription":null,"type":"invoiceitem"}],"url":"/v1/hashs","has_more":false},"subtotal":30000,"total":30000,"customer":"cus_00000000000000","object":"invoice","attempted":true,"closed":true,"paid":true,"livemode":false,"attempt_count":1,"amount_due":0,"currency":"usd","starting_balance":0,"ending_balance":0,"next_payment_attempt":null,"charge":"ch_00000000000000","discount":null,"application_fee":null,"subscription":"su_00000000000000","metadata":{},"description":null}}
}

Any ideas about pitfalls in test under test environment?

@JonRowe
Copy link
Member

JonRowe commented Nov 24, 2016

as_json returns a hash, which is being converted to string params for the controller, this is the expected behaviour from Rails (we don't tamper with it), use to_json instead.

@peco8
Copy link
Author

peco8 commented Nov 25, 2016

@JonRowe I just wonder why it's happened only under test environment. Any ideas?

@JonRowe
Copy link
Member

JonRowe commented Nov 25, 2016 via email

@peco8
Copy link
Author

peco8 commented Nov 25, 2016

I’m little confused…
Why under only test environment does it post in hash? Is this expected behaviour from Rails?
(Both testcases are exactly the same.)

Values returned from event.as_json are the exactly same under test, and development.

@JonRowe
Copy link
Member

JonRowe commented Nov 25, 2016 via email

@peco8
Copy link
Author

peco8 commented Nov 25, 2016

Thanks I think I understood now,
So you mean in my testcases hash is always sent.
And Only under development it's converted into hash, but it does not happen in test environment. That's why I get the error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants