diff --git a/lib/lago/api/resources/event.rb b/lib/lago/api/resources/event.rb index bbbddc9..d90d007 100644 --- a/lib/lago/api/resources/event.rb +++ b/lib/lago/api/resources/event.rb @@ -12,11 +12,6 @@ def root_name 'event' end - def create(params) - payload = whitelist_params(params) - connection.post(payload) - end - def batch_create(params) uri = URI("#{client.base_api_url}#{api_resource}/batch") diff --git a/spec/fixtures/api/event.json b/spec/fixtures/api/event.json new file mode 100644 index 0000000..565fcde --- /dev/null +++ b/spec/fixtures/api/event.json @@ -0,0 +1,16 @@ +{ + "event": { + "lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90", + "transaction_id": "1a901a90-1a90-1a90-1a90-1a901a901a90", + "lago_customer_id": "1a901a90-1a90-1a90-1a90-1a901a901a90", + "external_customer_id": "1a901a90-1a90-1a90-1a90-1a901a901a90", + "code": "bm_code", + "timestamp": "2022-04-29T08:59:51.998Z", + "properties": { + "custom_field": 12 + }, + "lago_subscription_id": "1a901a90-1a90-1a90-1a90-1a901a901a90", + "external_subscription_id": "1a901a90-1a90-1a90-1a90-1a901a901a90", + "created_at": "2022-04-29T08:59:51Z" + } +} diff --git a/spec/lago/api/resources/event_spec.rb b/spec/lago/api/resources/event_spec.rb index 6621741..478a2dc 100644 --- a/spec/lago/api/resources/event_spec.rb +++ b/spec/lago/api/resources/event_spec.rb @@ -4,45 +4,36 @@ RSpec.describe Lago::Api::Resources::Event do subject(:resource) { described_class.new(client) } + let(:client) { Lago::Api::Client.new } - let(:factory_event) { build(:event) } - let(:factory_batch_event) { build(:batch_event) } + + let(:event_response) { load_fixture('event') } describe '#create' do - let(:params) { factory_event.to_h } - let(:body) do - { - 'event' => factory_event.to_h - } - end + let(:params) { create(:event).to_h } context 'when event is successfully processed' do before do stub_request(:post, 'https://api.getlago.com/api/v1/events') - .with(body: body) - .to_return(body: '', status: 200) + .with(body: { event: params }) + .to_return(body: event_response, status: 200) end it 'returns true' do - response = resource.create(params) + event = resource.create(params) - expect(response).to be true + expect(event.lago_id).to eq('1a901a90-1a90-1a90-1a90-1a901a901a90') end end end describe '#batch_create' do - let(:params) { factory_batch_event.to_h } - let(:body) do - { - 'event' => factory_batch_event.to_h - } - end + let(:params) { build(:batch_event).to_h } context 'when event is successfully processed' do before do stub_request(:post, 'https://api.getlago.com/api/v1/events/batch') - .with(body: body) + .with(body: { event: params }) .to_return(body: '', status: 200) end @@ -55,29 +46,31 @@ end describe '#get' do + let(:event_id) { '1a901a90-1a90-1a90-1a90-1a901a901a90"' } + context 'when the event exists' do before do - event_json = JSON.generate({ 'event' => factory_event.to_h }) - - stub_request(:get, 'https://api.getlago.com/api/v1/events/UNIQUE_ID') - .to_return(body: event_json, status: 200) + stub_request(:get, "https://api.getlago.com/api/v1/events/#{event_id}") + .to_return(body: event_response, status: 200) end it 'returns the matching event if it exists' do - response = resource.get(factory_event.transaction_id) + response = resource.get(event_id) - expect(response.transaction_id).to eq factory_event.transaction_id + expect(response.lago_id).to eq('1a901a90-1a90-1a90-1a90-1a901a901a90') end end context 'when the event does not exist' do + let(:event_id) { 'DOESNOTEXIST' } + before do - stub_request(:get, 'https://api.getlago.com/api/v1/events/DOESNOTEXIST') + stub_request(:get, "https://api.getlago.com/api/v1/events/#{event_id}") .to_return(body: JSON.generate({ status: 404, error: 'Not Found' }), status: 404) end it 'raises an error' do - expect { resource.get('DOESNOTEXIST') }.to raise_error Lago::Api::HttpError + expect { resource.get(event_id) }.to raise_error(Lago::Api::HttpError) end end end