Skip to content

Commit

Permalink
misc: Method json for parsed response body in specs
Browse files Browse the repository at this point in the history
  • Loading branch information
rsempe committed Sep 14, 2022
1 parent 32128dd commit 40f5802
Show file tree
Hide file tree
Showing 17 changed files with 265 additions and 422 deletions.
77 changes: 30 additions & 47 deletions spec/requests/api/v1/add_ons_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@

expect(response).to have_http_status(:success)

result = JSON.parse(response.body, symbolize_names: true)[:add_on]
expect(result[:lago_id]).to be_present
expect(result[:code]).to eq(create_params[:code])
expect(result[:name]).to eq(create_params[:name])
expect(result[:created_at]).to be_present
expect(json[:add_on][:lago_id]).to be_present
expect(json[:add_on][:code]).to eq(create_params[:code])
expect(json[:add_on][:name]).to eq(create_params[:name])
expect(json[:add_on][:created_at]).to be_present
end
end

Expand All @@ -38,22 +37,20 @@
code: code,
amount_cents: 123,
amount_currency: 'EUR',
description: 'description'
description: 'description',
}
end

it 'updates a add-on' do
put_with_token(organization,
"/api/v1/add_ons/#{add_on.code}",
{ add_on: update_params }
put_with_token(
organization,
"/api/v1/add_ons/#{add_on.code}",
{ add_on: update_params },
)

expect(response).to have_http_status(:success)

result = JSON.parse(response.body, symbolize_names: true)[:add_on]

expect(result[:lago_id]).to eq(add_on.id)
expect(result[:code]).to eq(update_params[:code])
expect(json[:add_on][:lago_id]).to eq(add_on.id)
expect(json[:add_on][:code]).to eq(update_params[:code])
end

context 'when add-on does not exist' do
Expand All @@ -71,9 +68,10 @@
before { add_on2 }

it 'returns unprocessable_entity error' do
put_with_token(organization,
"/api/v1/add_ons/#{add_on.code}",
{ add_on: update_params }
put_with_token(
organization,
"/api/v1/add_ons/#{add_on.code}",
{ add_on: update_params },
)

expect(response).to have_http_status(:unprocessable_entity)
Expand All @@ -87,23 +85,17 @@
it 'returns a add-on' do
get_with_token(
organization,
"/api/v1/add_ons/#{add_on.code}"
"/api/v1/add_ons/#{add_on.code}",
)

expect(response).to have_http_status(:success)

result = JSON.parse(response.body, symbolize_names: true)[:add_on]

expect(result[:lago_id]).to eq(add_on.id)
expect(result[:code]).to eq(add_on.code)
expect(json[:add_on][:lago_id]).to eq(add_on.id)
expect(json[:add_on][:code]).to eq(add_on.code)
end

context 'when add-on does not exist' do
it 'returns not found' do
get_with_token(
organization,
"/api/v1/add_ons/555"
)
get_with_token(organization, '/api/v1/add_ons/555')

expect(response).to have_http_status(:not_found)
end
Expand All @@ -124,11 +116,8 @@
delete_with_token(organization, "/api/v1/add_ons/#{add_on.code}")

expect(response).to have_http_status(:success)

result = JSON.parse(response.body, symbolize_names: true)[:add_on]

expect(result[:lago_id]).to eq(add_on.id)
expect(result[:code]).to eq(add_on.code)
expect(json[:add_on][:lago_id]).to eq(add_on.id)
expect(json[:add_on][:code]).to eq(add_on.code)
end

context 'when add-on does not exist' do
Expand All @@ -149,12 +138,9 @@
get_with_token(organization, '/api/v1/add_ons')

expect(response).to have_http_status(:success)

records = JSON.parse(response.body, symbolize_names: true)[:add_ons]

expect(records.count).to eq(1)
expect(records.first[:lago_id]).to eq(add_on.id)
expect(records.first[:code]).to eq(add_on.code)
expect(json[:add_ons].count).to eq(1)
expect(json[:add_ons].first[:lago_id]).to eq(add_on.id)
expect(json[:add_ons].first[:code]).to eq(add_on.code)
end

context 'with pagination' do
Expand All @@ -166,15 +152,12 @@
get_with_token(organization, '/api/v1/add_ons?page=1&per_page=1')

expect(response).to have_http_status(:success)

response_body = JSON.parse(response.body, symbolize_names: true)

expect(response_body[:add_ons].count).to eq(1)
expect(response_body[:meta][:current_page]).to eq(1)
expect(response_body[:meta][:next_page]).to eq(2)
expect(response_body[:meta][:prev_page]).to eq(nil)
expect(response_body[:meta][:total_pages]).to eq(2)
expect(response_body[:meta][:total_count]).to eq(2)
expect(json[:add_ons].count).to eq(1)
expect(json[:meta][:current_page]).to eq(1)
expect(json[:meta][:next_page]).to eq(2)
expect(json[:meta][:prev_page]).to eq(nil)
expect(json[:meta][:total_pages]).to eq(2)
expect(json[:meta][:total_count]).to eq(2)
end
end
end
Expand Down
20 changes: 8 additions & 12 deletions spec/requests/api/v1/applied_add_ons_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,20 @@

expect(response).to have_http_status(:success)

result = JSON.parse(response.body, symbolize_names: true)[:applied_add_on]

aggregate_failures do
expect(result[:lago_id]).to be_present
expect(result[:lago_add_on_id]).to eq(add_on.id)
expect(result[:lago_customer_id]).to eq(customer.id)
expect(result[:external_customer_id]).to eq(customer.external_id)
expect(result[:amount_cents]).to eq(add_on.amount_cents)
expect(result[:amount_currency]).to eq(add_on.amount_currency)
expect(result[:created_at]).to be_present
expect(json[:applied_add_on][:lago_id]).to be_present
expect(json[:applied_add_on][:lago_add_on_id]).to eq(add_on.id)
expect(json[:applied_add_on][:lago_customer_id]).to eq(customer.id)
expect(json[:applied_add_on][:external_customer_id]).to eq(customer.external_id)
expect(json[:applied_add_on][:amount_cents]).to eq(add_on.amount_cents)
expect(json[:applied_add_on][:amount_currency]).to eq(add_on.amount_currency)
expect(json[:applied_add_on][:created_at]).to be_present
end
end

context 'with invalid params' do
let(:params) do
{
name: 'Foo Bar',
}
{ name: 'Foo Bar' }
end

it 'returns an unprocessable_entity' do
Expand Down
24 changes: 10 additions & 14 deletions spec/requests/api/v1/applied_coupons_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,22 @@

expect(response).to have_http_status(:success)

result = JSON.parse(response.body, symbolize_names: true)[:applied_coupon]

aggregate_failures do
expect(result[:lago_id]).to be_present
expect(result[:lago_coupon_id]).to eq(coupon.id)
expect(result[:lago_customer_id]).to eq(customer.id)
expect(result[:external_customer_id]).to eq(customer.external_id)
expect(result[:amount_cents]).to eq(coupon.amount_cents)
expect(result[:amount_currency]).to eq(coupon.amount_currency)
expect(result[:expiration_date]).to be_nil
expect(result[:created_at]).to be_present
expect(result[:terminated_at]).to be_nil
expect(json[:applied_coupon][:lago_id]).to be_present
expect(json[:applied_coupon][:lago_coupon_id]).to eq(coupon.id)
expect(json[:applied_coupon][:lago_customer_id]).to eq(customer.id)
expect(json[:applied_coupon][:external_customer_id]).to eq(customer.external_id)
expect(json[:applied_coupon][:amount_cents]).to eq(coupon.amount_cents)
expect(json[:applied_coupon][:amount_currency]).to eq(coupon.amount_currency)
expect(json[:applied_coupon][:expiration_date]).to be_nil
expect(json[:applied_coupon][:created_at]).to be_present
expect(json[:applied_coupon][:terminated_at]).to be_nil
end
end

context 'with invalid params' do
let(:params) do
{
name: 'Foo Bar',
}
{ name: 'Foo Bar' }
end

it 'returns an unprocessable_entity' do
Expand Down
63 changes: 22 additions & 41 deletions spec/requests/api/v1/billable_metrics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@
post_with_token(organization, '/api/v1/billable_metrics', { billable_metric: create_params })

expect(response).to have_http_status(:success)

result = JSON.parse(response.body, symbolize_names: true)[:billable_metric]
expect(result[:lago_id]).to be_present
expect(result[:code]).to eq(create_params[:code])
expect(result[:name]).to eq(create_params[:name])
expect(result[:created_at]).to be_present
expect(json[:billable_metric][:lago_id]).to be_present
expect(json[:billable_metric][:code]).to eq(create_params[:code])
expect(json[:billable_metric][:name]).to eq(create_params[:name])
expect(json[:billable_metric][:created_at]).to be_present
end
end

Expand All @@ -50,11 +48,8 @@
)

expect(response).to have_http_status(:success)

result = JSON.parse(response.body, symbolize_names: true)[:billable_metric]

expect(result[:lago_id]).to eq(billable_metric.id)
expect(result[:code]).to eq(update_params[:code])
expect(json[:billable_metric][:lago_id]).to eq(billable_metric.id)
expect(json[:billable_metric][:code]).to eq(update_params[:code])
end

context 'when billable metric does not exist' do
Expand Down Expand Up @@ -93,11 +88,8 @@
)

expect(response).to have_http_status(:success)

result = JSON.parse(response.body, symbolize_names: true)[:billable_metric]

expect(result[:lago_id]).to eq(billable_metric.id)
expect(result[:code]).to eq(billable_metric.code)
expect(json[:billable_metric][:lago_id]).to eq(billable_metric.id)
expect(json[:billable_metric][:code]).to eq(billable_metric.code)
end

context 'when billable metric does not exist' do
Expand Down Expand Up @@ -126,11 +118,8 @@
delete_with_token(organization, "/api/v1/billable_metrics/#{billable_metric.code}")

expect(response).to have_http_status(:success)

result = JSON.parse(response.body, symbolize_names: true)[:billable_metric]

expect(result[:lago_id]).to eq(billable_metric.id)
expect(result[:code]).to eq(billable_metric.code)
expect(json[:billable_metric][:lago_id]).to eq(billable_metric.id)
expect(json[:billable_metric][:code]).to eq(billable_metric.code)
end

context 'when billable metric does not exist' do
Expand All @@ -156,11 +145,9 @@

aggregate_failures do
expect(response).to have_http_status(:method_not_allowed)

result = JSON.parse(response.body, symbolize_names: true)
expect(result[:status]).to eq(405)
expect(result[:error]).to eq('Method Not Allowed')
expect(result[:code]).to eq('attached_to_an_active_subscription')
expect(json[:status]).to eq(405)
expect(json[:error]).to eq('Method Not Allowed')
expect(json[:code]).to eq('attached_to_an_active_subscription')
end
end
end
Expand All @@ -175,12 +162,9 @@
get_with_token(organization, '/api/v1/billable_metrics')

expect(response).to have_http_status(:success)

records = JSON.parse(response.body, symbolize_names: true)[:billable_metrics]

expect(records.count).to eq(1)
expect(records.first[:lago_id]).to eq(billable_metric.id)
expect(records.first[:code]).to eq(billable_metric.code)
expect(json[:billable_metrics].count).to eq(1)
expect(json[:billable_metrics].first[:lago_id]).to eq(billable_metric.id)
expect(json[:billable_metrics].first[:code]).to eq(billable_metric.code)
end

context 'with pagination' do
Expand All @@ -192,15 +176,12 @@
get_with_token(organization, '/api/v1/billable_metrics?page=1&per_page=1')

expect(response).to have_http_status(:success)

response_body = JSON.parse(response.body, symbolize_names: true)

expect(response_body[:billable_metrics].count).to eq(1)
expect(response_body[:meta][:current_page]).to eq(1)
expect(response_body[:meta][:next_page]).to eq(2)
expect(response_body[:meta][:prev_page]).to eq(nil)
expect(response_body[:meta][:total_pages]).to eq(2)
expect(response_body[:meta][:total_count]).to eq(2)
expect(json[:billable_metrics].count).to eq(1)
expect(json[:meta][:current_page]).to eq(1)
expect(json[:meta][:next_page]).to eq(2)
expect(json[:meta][:prev_page]).to eq(nil)
expect(json[:meta][:total_pages]).to eq(2)
expect(json[:meta][:total_count]).to eq(2)
end
end
end
Expand Down
Loading

0 comments on commit 40f5802

Please sign in to comment.