spec/integrations/webhook_spec.rb in onfido-2.9.0 vs spec/integrations/webhook_spec.rb in onfido-3.0.0
- old
+ new
@@ -1,110 +1,62 @@
# frozen_string_literal: true
+require_relative '../shared_contexts/with_onfido'
+
describe Onfido::Webhook do
- include_context 'fake onfido api'
+ describe 'Webhook' do
+ include_context 'with onfido'
- subject(:webhook) { onfido.webhook }
-
- describe '#create' do
- let(:params) do
- {
- url: 'https://webhookendpoint.url',
- enabled: true,
- events: [
- 'report.completed',
- 'check.completed'
- ]
- }
+ let(:webhook_builder) do
+ Onfido::WebhookBuilder.new({
+ url: 'https://example.com',
+ events: ['check.completed', 'report.completed'],
+ })
end
- it 'creates the webhook' do
- response = webhook.create(**params)
+ let(:webhook) { onfido_api.create_webhook(webhook_builder) }
+ let(:webhook_id) { webhook.id }
- expect(response['id']).to_not be_nil
+ it 'creates a webhook' do
+ expect(webhook).to be_an_instance_of Onfido::Webhook
+ expect(webhook_id).to_not be_nil
+ expect(webhook.url).to eq 'https://example.com'
+ expect(webhook.events).to eq ['check.completed', 'report.completed']
end
- it 'responds with the right url' do
- response = webhook.create(**params)
+ it 'updates a webhook' do
+ webhook_updater = Onfido::WebhookUpdater.new({
+ url: 'https://example.co.uk',
+ events: ['check.completed'],
+ })
- expect(response['url']).to eq params[:url]
+ updated_webhook = onfido_api.update_webhook(webhook_id, webhook_updater)
+ expect(updated_webhook.id).to eq webhook_id
+ expect(updated_webhook.url).to eq 'https://example.co.uk'
+ expect(updated_webhook.events).to eq ['check.completed']
end
- end
- describe '#find' do
- it 'returns the webhook' do
- webhook_id = 'fcb73186-0733-4f6f-9c57-d9d5ef979443'
+ it 'lists webhooks' do
+ list_of_webhooks = onfido_api.list_webhooks()
- response = webhook.find(webhook_id)
-
- expect(response['id']).to eq(webhook_id)
+ expect(list_of_webhooks).to be_an_instance_of Onfido::WebhooksList
+ expect(list_of_webhooks.webhooks.size).to be > 0
end
- end
- describe '#destroy' do
- it 'removes the webhook' do
- webhook_id = 'fcb73186-0733-4f6f-9c57-d9d5ef979443'
- expect { webhook.destroy(webhook_id) }.not_to raise_error
- end
- end
+ it 'finds a webhook' do
+ get_webhook = onfido_api.find_webhook(webhook_id)
- describe '#all' do
- it 'returns all the registered webhooks' do
- response = webhook.all
-
- expect(response['webhooks'].count).to eq 2
+ expect(get_webhook).to be_an_instance_of(Onfido::Webhook)
+ expect(get_webhook.id).to eq webhook_id
end
- it 'returns with id' do
- response = webhook.all
+ it 'deletes a webhook' do
+ onfido_api.delete_webhook(webhook_id)
- expect(response['webhooks'][0]['id']).to_not be_nil
- expect(response['webhooks'][1]['id']).to_not be_nil
- end
- end
-
- describe '.valid?' do
- subject(:valid?) do
- described_class.valid?(request_body, request_signature, token)
- end
-
- let(:request_body) { '{"foo":"bar"}' }
- let(:request_signature) do
- '89e60408fec20bfb26bb0f993d5e88307818982f50f23b361a00d679bae8b1dc'
- end
- let(:token) { 'very_secret_token' }
-
- it { is_expected.to be(true) }
-
- context 'with an invalid signature' do
- let(:request_signature) do
- 's21fd54ew2w1f5d15642132f3d7727ff9a32a7c87072ce514df1f6d3228bec'
- end
- it { is_expected.to be(false) }
- end
-
- context 'with a nil request signature' do
- let(:request_signature) { nil }
- specify { expect { valid? }.to raise_error(ArgumentError) }
- end
-
- context 'with a token other than the one used to sign the request' do
- let(:token) { 'quite_secret_token' }
- it { is_expected.to be(false) }
- end
-
- context 'with a nil token' do
- let(:token) { nil }
- specify { expect { valid? }.to raise_error(ArgumentError) }
- end
-
- context 'with a modified request body' do
- let(:request_body) { '{"bar":"baz"}' }
- it { is_expected.to be(false) }
- end
-
- context 'with a nil request body' do
- let(:request_body) { nil }
- specify { expect { valid? }.to raise_error(ArgumentError) }
+ expect {
+ onfido_api.find_webhook(webhook_id)
+ }.to raise_error(Onfido::ApiError) { |e|
+ expect(e.code).to eq(404)
+ }
end
end
end