diff --git a/spec/factories/plan.rb b/spec/factories/plan.rb index 5d43eb8..d7727fa 100644 --- a/spec/factories/plan.rb +++ b/spec/factories/plan.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true FactoryBot.define do - factory :plan, class: OpenStruct do + factory :create_plan, class: OpenStruct do name { 'plan1' } invoice_display_name { 'PLN1' } code { 'plan_code' } diff --git a/spec/fixtures/api/fee.json b/spec/fixtures/api/fee.json index 8346885..cb6ffe8 100644 --- a/spec/fixtures/api/fee.json +++ b/spec/fixtures/api/fee.json @@ -11,7 +11,10 @@ "code": "fee_code", "name": "Fee Code", "invoice_display_name": "Fee C1", - "group_invoice_display_name": "Transactions - ACH" + "group_invoice_display_name": "Transactions - ACH", + "grouped_by": { + "agent_name": "aragorn" + } }, "amount_cents": 120, "amount_currency": "EUR", diff --git a/spec/fixtures/api/plan.json b/spec/fixtures/api/plan.json new file mode 100644 index 0000000..703aefc --- /dev/null +++ b/spec/fixtures/api/plan.json @@ -0,0 +1,41 @@ +{ + "plan": { + "lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90", + "name": "plan1", + "invoice_display_name": "PLN1", + "created_at": "2022-04-29T08:59:51Z", + "code": "plan_code", + "interval": "monthly", + "description": "desc", + "amount_cents": 100, + "amount_currency": "EUR", + "trial_period": 2, + "pay_in_advance": false, + "bill_charges_monthly": false, + "active_subscriptions_count": 0, + "draft_invoices_count": 0, + "charges": [ + { + "lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90", + "lago_billable_metric_id": "1a901a90-1a90-1a90-1a90-1a901a901a90", + "billable_metric_code": "bm_code", + "created_at": "2022-04-29T08:59:51Z", + "charge_model": "standard", + "pay_in_advance": false, + "invoiceable": true, + "min_amount_cents": 0, + "properties": { + "amount": "0.22", + "grouped_by": [ + "agent_name" + ] + } + } + ], + "taxes": [ + { + "code": "tax_code" + } + ] + } +} diff --git a/spec/fixtures/api/plans.json b/spec/fixtures/api/plans.json new file mode 100644 index 0000000..8da4129 --- /dev/null +++ b/spec/fixtures/api/plans.json @@ -0,0 +1,51 @@ +{ + "plans": [ + { + "lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90", + "name": "plan1", + "invoice_display_name": "PLN1", + "created_at": "2022-04-29T08:59:51Z", + "code": "plan_code", + "interval": "monthly", + "description": "desc", + "amount_cents": 100, + "amount_currency": "EUR", + "trial_period": 2, + "pay_in_advance": false, + "bill_charges_monthly": false, + "active_subscriptions_count": 0, + "draft_invoices_count": 0, + "charges": [ + { + "lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90", + "lago_billable_metric_id": "1a901a90-1a90-1a90-1a90-1a901a901a90", + "billable_metric_code": "bm_code", + "created_at": "2022-04-29T08:59:51Z", + "charge_model": "standard", + "invoice_display_name": "Charge 1", + "pay_in_advance": false, + "invoiceable": true, + "min_amount_cents": 0, + "properties": { + "amount": "0.22", + "grouped_by": [ + "agent_name" + ] + } + } + ], + "taxes": [ + { + "code": "tax_code" + } + ] + } + ], + "meta": { + "current_page": 1, + "next_page": 2, + "prev_page": null, + "total_pages": 7, + "total_count": 63 + } +} diff --git a/spec/lago/api/resources/plan_spec.rb b/spec/lago/api/resources/plan_spec.rb index 968ebb3..02163ad 100644 --- a/spec/lago/api/resources/plan_spec.rb +++ b/spec/lago/api/resources/plan_spec.rb @@ -6,41 +6,14 @@ subject(:resource) { described_class.new(client) } let(:client) { Lago::Api::Client.new } - let(:factory_plan) { build(:plan) } - let(:response) do - { - 'plan' => { - 'lago_id' => 'this-is-lago-id', - 'name' => factory_plan.name, - 'invoice_display_name' => factory_plan.invoice_display_name, - 'created_at' => '2022-04-29T08:59:51Z', - 'code' => factory_plan.code, - 'interval' => factory_plan.amount_cents, - 'description' => factory_plan.amount_currency, - 'amount_cents' => factory_plan.expiration, - 'amount_currency' => factory_plan.expiration_duration, - 'trial_period' => 2, - 'pay_in_advance' => false, - 'bill_charges_monthly' => false, - 'active_subscriptions_count' => 0, - 'draft_invoices_count' => 0, - 'charges' => [ - { - 'lago_id' => 'id1', - 'lago_billable_metric_id' => factory_plan.charges.first[:lago_billable_metric_id], - 'billable_metric_code' => 'bm_code', - 'created_at' => '2022-04-29T08:59:51Z', - 'charge_model' => factory_plan.charges.first[:charge_model], - 'pay_in_advance' => false, - 'invoiceable' => true, - 'min_amount_cents' => 0, - 'properties' => factory_plan.charges.first[:properties], - }, - ], - 'taxes' => [{ 'code' => 'tax_code' }], - } - }.to_json - end + + let(:plan_response) { load_fixture('plan') } + let(:plan_json) { JSON.parse(plan_response)['plan'] } + let(:plan_id) { plan_json['lago_id'] } + let(:plan_code) { plan_json['code'] } + let(:plan_name) { plan_json['name'] } + let(:plan_dsplay_name) { plan_json['invoice_display_name'] } + let(:error_response) do { 'status' => 422, @@ -51,7 +24,7 @@ describe '#create' do let(:tax_codes) { ['tax_code'] } - let(:params) { factory_plan.to_h.merge(tax_codes: tax_codes) } + let(:params) { create(:create_plan).to_h.merge(tax_codes: tax_codes) } let(:body) do { 'plan' => params } end @@ -60,15 +33,15 @@ before do stub_request(:post, 'https://api.getlago.com/api/v1/plans') .with(body: body) - .to_return(body: response, status: 200) + .to_return(body: plan_response, status: 200) end it 'returns an plan' do plan = resource.create(params) - expect(plan.lago_id).to eq('this-is-lago-id') - expect(plan.name).to eq(factory_plan.name) - expect(plan.invoice_display_name).to eq(factory_plan.invoice_display_name) + expect(plan.lago_id).to eq(plan_id) + expect(plan.name).to eq(plan_name) + expect(plan.invoice_display_name).to eq(plan_dsplay_name) expect(plan.taxes.map(&:code)).to eq(tax_codes) end end @@ -87,38 +60,33 @@ end describe '#update' do - let(:params) { factory_plan.to_h } - let(:body) do - { - 'plan' => factory_plan.to_h, - } - end + let(:params) { create(:create_plan).to_h } context 'when plan is successfully updated' do before do - stub_request(:put, "https://api.getlago.com/api/v1/plans/#{factory_plan.code}") - .with(body: body) - .to_return(body: response, status: 200) + stub_request(:put, "https://api.getlago.com/api/v1/plans/#{plan_code}") + .with(body: { plan: params }) + .to_return(body: plan_response, status: 200) end it 'returns an plan' do - plan = resource.update(params, factory_plan.code) + plan = resource.update(params, plan_code) - expect(plan.lago_id).to eq('this-is-lago-id') - expect(plan.name).to eq(factory_plan.name) - expect(plan.invoice_display_name).to eq(factory_plan.invoice_display_name) + expect(plan.lago_id).to eq(plan_id) + expect(plan.name).to eq(plan_name) + expect(plan.invoice_display_name).to eq(plan_dsplay_name) end end context 'when plan failed to update' do before do - stub_request(:put, "https://api.getlago.com/api/v1/plans/#{factory_plan.code}") - .with(body: body) + stub_request(:put, "https://api.getlago.com/api/v1/plans/#{plan_code}") + .with(body: { plan: params }) .to_return(body: error_response, status: 422) end it 'raises an error' do - expect { resource.update(params, factory_plan.code) }.to raise_error Lago::Api::HttpError + expect { resource.update(params, plan_code) }.to raise_error Lago::Api::HttpError end end end @@ -126,27 +94,27 @@ describe '#get' do context 'when plan is successfully fetched' do before do - stub_request(:get, "https://api.getlago.com/api/v1/plans/#{factory_plan.code}") - .to_return(body: response, status: 200) + stub_request(:get, "https://api.getlago.com/api/v1/plans/#{plan_code}") + .to_return(body: plan_response, status: 200) end it 'returns an plan' do - plan = resource.get(factory_plan.code) + plan = resource.get(plan_code) - expect(plan.lago_id).to eq('this-is-lago-id') - expect(plan.name).to eq(factory_plan.name) - expect(plan.invoice_display_name).to eq(factory_plan.invoice_display_name) + expect(plan.lago_id).to eq(plan_id) + expect(plan.name).to eq(plan_name) + expect(plan.invoice_display_name).to eq(plan_dsplay_name) end end context 'when there is an issue' do before do - stub_request(:get, "https://api.getlago.com/api/v1/plans/#{factory_plan.code}") + stub_request(:get, "https://api.getlago.com/api/v1/plans/#{plan_code}") .to_return(body: error_response, status: 422) end it 'raises an error' do - expect { resource.get(factory_plan.code) }.to raise_error Lago::Api::HttpError + expect { resource.get(plan_code) }.to raise_error Lago::Api::HttpError end end end @@ -154,89 +122,48 @@ describe '#destroy' do context 'when plan is successfully destroyed' do before do - stub_request(:delete, "https://api.getlago.com/api/v1/plans/#{factory_plan.code}") - .to_return(body: response, status: 200) + stub_request(:delete, "https://api.getlago.com/api/v1/plans/#{plan_code}") + .to_return(body: plan_response, status: 200) end it 'returns an plan' do - plan = resource.destroy(factory_plan.code) + plan = resource.destroy(plan_code) - expect(plan.lago_id).to eq('this-is-lago-id') - expect(plan.name).to eq(factory_plan.name) - expect(plan.invoice_display_name).to eq(factory_plan.invoice_display_name) + expect(plan.lago_id).to eq(plan_id) + expect(plan.name).to eq(plan_name) + expect(plan.invoice_display_name).to eq(plan_dsplay_name) end end context 'when there is an issue' do before do - stub_request(:delete, "https://api.getlago.com/api/v1/plans/#{factory_plan.code}") + stub_request(:delete, "https://api.getlago.com/api/v1/plans/#{plan_code}") .to_return(body: error_response, status: 422) end it 'raises an error' do - expect { resource.destroy(factory_plan.code) }.to raise_error Lago::Api::HttpError + expect { resource.destroy(plan_code) }.to raise_error Lago::Api::HttpError end end end describe '#get_all' do - let(:response) do - { - 'plans' => [ - { - 'lago_id' => 'this-is-lago-id', - 'name' => factory_plan.name, - 'invoice_display_name' => factory_plan.invoice_display_name, - 'created_at' => '2022-04-29T08:59:51Z', - 'code' => factory_plan.code, - 'interval' => factory_plan.amount_cents, - 'description' => factory_plan.amount_currency, - 'amount_cents' => factory_plan.expiration, - 'amount_currency' => factory_plan.expiration_duration, - 'trial_period' => 2, - 'pay_in_advance' => false, - 'bill_charges_monthly' => false, - 'active_subscriptions_count' => 0, - 'draft_invoices_count' => 0, - 'charges' => [ - { - 'lago_id' => 'id', - 'lago_billable_metric_id' => factory_plan.charges.first[:lago_billable_metric_id], - 'billable_metric_code' => 'bm_code', - 'created_at' => '2022-04-29T08:59:51Z', - 'charge_model' => factory_plan.charges.first[:charge_model], - 'invoice_display_name' => factory_plan.charges.first[:invoice_display_name], - 'pay_in_advance' => false, - 'min_amount_cents' => 0, - 'properties' => factory_plan.charges.first[:properties], - 'group_properties' => [], - } - ] - } - ], - 'meta': { - 'current_page' => 1, - 'next_page' => 2, - 'prev_page' => nil, - 'total_pages' => 7, - 'total_count' => 63, - } - }.to_json - end + let(:plans_response) { load_fixture('plans') } context 'when there is no options' do before do stub_request(:get, 'https://api.getlago.com/api/v1/plans') - .to_return(body: response, status: 200) + .to_return(body: plans_response, status: 200) end it 'returns plans on the first page' do response = resource.get_all - expect(response['plans'].first['lago_id']).to eq('this-is-lago-id') - expect(response['plans'].first['name']).to eq(factory_plan.name) - expect(response['plans'].first['invoice_display_name']).to eq(factory_plan.invoice_display_name) - expect(response['plans'].first['charges'].first['invoice_display_name']).to eq(factory_plan.charges.first[:invoice_display_name]) + expect(response['plans'].first['lago_id']).to eq(plan_id) + expect(response['plans'].first['name']).to eq(plan_name) + expect(response['plans'].first['invoice_display_name']).to eq(plan_dsplay_name) + expect(response['plans'].first['charges'].first['invoice_display_name']).to eq('Charge 1') + expect(response['plans'].first['charges'].first['properties']['grouped_by']).to eq(['agent_name']) expect(response['meta']['current_page']).to eq(1) end end @@ -244,15 +171,15 @@ context 'when options are present' do before do stub_request(:get, 'https://api.getlago.com/api/v1/plans?per_page=2&page=1') - .to_return(body: response, status: 200) + .to_return(body: plans_response, status: 200) end it 'returns plans on selected page' do response = resource.get_all({ per_page: 2, page: 1 }) - expect(response['plans'].first['lago_id']).to eq('this-is-lago-id') - expect(response['plans'].first['name']).to eq(factory_plan.name) - expect(response['plans'].first['invoice_display_name']).to eq(factory_plan.invoice_display_name) + expect(response['plans'].first['lago_id']).to eq(plan_id) + expect(response['plans'].first['name']).to eq(plan_name) + expect(response['plans'].first['invoice_display_name']).to eq(plan_dsplay_name) expect(response['meta']['current_page']).to eq(1) end end