Skip to content

Commit

Permalink
Merge pull request #146 from etalab/harden/flaky-test
Browse files Browse the repository at this point in the history
Try to fix flaky test
  • Loading branch information
skelz0r authored Apr 4, 2024
2 parents 4340115 + 390478f commit 9e5afbb
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions spec/jobs/deliver_authorization_request_webhook_job_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
require 'rails_helper'

RSpec.describe DeliverAuthorizationRequestWebhookJob do
subject { job_instance.perform_now }
subject(:deliver_authorization_request_webhook) { job_instance.perform_now }

before do
ActiveJob::Base.queue_adapter = :inline
end

after do
ActiveJob::Base.queue_adapter = :test

Rails.application.credentials.webhooks.api_entreprise.url = nil
Rails.application.credentials.webhooks.api_entreprise.token = nil
end
Expand Down Expand Up @@ -44,7 +50,7 @@
end

it 'does nothing' do
subject
deliver_authorization_request_webhook

expect(webhook_post_request).not_to have_been_requested
end
Expand All @@ -56,7 +62,7 @@
end

it 'does nothing' do
subject
deliver_authorization_request_webhook

expect(webhook_post_request).not_to have_been_requested
end
Expand All @@ -69,14 +75,12 @@
end

it "performs a post request on target api's webhook url with payload and headers" do
subject
deliver_authorization_request_webhook

expect(webhook_post_request).to have_been_requested
end

describe "target's api webhook url status" do
subject { job_instance.perform_now }

before do
allow(job_instance).to receive(:webhook_fail!)
end
Expand All @@ -85,13 +89,13 @@
it 'does not reschedule worker' do
expect(job_instance).not_to receive(:webhook_fail!)

subject
deliver_authorization_request_webhook
end

it 'does not track on sentry' do
expect(Sentry).not_to receive(:capture_message)

subject
deliver_authorization_request_webhook
end

context 'when body is a json with a token_id key' do
Expand All @@ -105,7 +109,7 @@

it 'stores this token id in authorization_request' do
expect {
subject
deliver_authorization_request_webhook
}.to change { authorization_request.reload.linked_token_manager_id }.to(token_id)
end
end
Expand All @@ -117,27 +121,27 @@
it 'reschedules job' do
expect(job_instance).to receive(:webhook_fail!)

subject
deliver_authorization_request_webhook
end

it 'tracks on sentry' do
expect(Sentry).to receive(:capture_message)

subject
deliver_authorization_request_webhook
end

context 'when last retry_on attempt fails' do
subject { job_instance.perform_now }

let(:tries_count) { described_class::TOTAL_ATTEMPTS }
let(:response) { instance_double(Faraday::Response, status:, body:) }

before do
allow(job_instance).to receive_messages(request: response, attempts: described_class::TOTAL_ATTEMPTS)

ActiveJob::Base.queue_adapter = :test
end

it 'sends an email through WebhookMailer' do
subject
it 'sends an email through WebhookMailer to instructors' do
deliver_authorization_request_webhook

expect(ActionMailer::MailDeliveryJob).to(
have_been_enqueued.with(
Expand Down

0 comments on commit 9e5afbb

Please sign in to comment.