|
3 | 3 |
|
4 | 4 | require "spec_helper" |
5 | 5 |
|
6 | | -describe DeliverWebhookWorker, :vcr do |
| 6 | +describe DeliverWebhookWorker do |
7 | 7 | let(:scraper) { create(:scraper) } |
8 | | - let(:run) { create(:run) } |
| 8 | + let(:run) do |
| 9 | + create(:run, |
| 10 | + git_revision: "c9fabbc7a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6", |
| 11 | + records_added: 100, |
| 12 | + records_removed: 200, |
| 13 | + status_code: 3, |
| 14 | + started_at: 400.4.seconds.ago, |
| 15 | + finished_at: 0.1.seconds.ago, |
| 16 | + auto: true) |
| 17 | + end |
| 18 | + |
| 19 | + describe "test to actual site" do |
| 20 | + it "works" do |
| 21 | + VCR.use_cassette("webhook_delivery") do |
| 22 | + webhook = Webhook.create!(scraper: scraper, url: "http://requestb.in/x3pcr8x3") |
| 23 | + webhook_delivery = webhook.deliveries.create!(run: run) |
| 24 | + described_class.new.perform(webhook_delivery.id) |
| 25 | + webhook_delivery.reload |
| 26 | + expect(webhook_delivery.response_code).to eq(200) |
| 27 | + expect(webhook_delivery.sent_at).to be_within(1.minute).of(DateTime.now) |
| 28 | + end |
| 29 | + end |
| 30 | + end |
9 | 31 |
|
10 | | - it "works" do |
11 | | - VCR.use_cassette("webhook_delivery") do |
12 | | - webhook = Webhook.create!(scraper: scraper, url: "http://requestb.in/x3pcr8x3") |
| 32 | + describe "error handling", :webmock do |
| 33 | + it "records response code and time on success" do |
| 34 | + webhook = Webhook.create!(scraper: scraper, url: "https://example.com/hook") |
13 | 35 | webhook_delivery = webhook.deliveries.create!(run: run) |
| 36 | + |
| 37 | + stub_request(:post, "https://example.com/hook").to_return(status: 200) |
| 38 | + |
14 | 39 | described_class.new.perform(webhook_delivery.id) |
| 40 | + |
15 | 41 | webhook_delivery.reload |
16 | | - expect(webhook_delivery.response_code).to be(200) |
| 42 | + expect(webhook_delivery.response_code).to eq(200) |
17 | 43 | expect(webhook_delivery.sent_at).to be_within(1.minute).of(DateTime.now) |
18 | 44 | end |
| 45 | + |
| 46 | + it "logs connection failures without raising" do |
| 47 | + webhook = Webhook.create!(scraper: scraper, url: "http://example.com/hook") |
| 48 | + webhook_delivery = webhook.deliveries.create!(run: run) |
| 49 | + |
| 50 | + stub_request(:post, "http://example.com/hook").to_raise(Faraday::ConnectionFailed.new("Connection refused")) |
| 51 | + |
| 52 | + allow(Rails.logger).to receive(:error) |
| 53 | + |
| 54 | + expect do |
| 55 | + described_class.new.perform(webhook_delivery.id) |
| 56 | + end.not_to raise_error |
| 57 | + |
| 58 | + expect(Rails.logger).to have_received(:error).with(/Webhook delivery failure/) |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + describe "URL substitution", :webmock do |
| 63 | + it "substitutes all placeholders correctly" do |
| 64 | + substitutions = { |
| 65 | + "ADDED" => run.records_added, |
| 66 | + "AUTO" => run.auto, |
| 67 | + "COMMIT" => run.git_revision, |
| 68 | + "OUTCOME" => "failed", |
| 69 | + "REMOVED" => run.records_removed, |
| 70 | + "REVISION" => "c9fabbc7", |
| 71 | + "RUN_TIME" => run.wall_time.to_i, |
| 72 | + "SCRAPER" => run.scraper.full_name, |
| 73 | + "STATUS_CODE" => run.status_code |
| 74 | + } |
| 75 | + |
| 76 | + url_template = "https://example.com/?#{substitutions.keys.map { |k| "#{k.downcase}=#{k}" }.join('&')}" |
| 77 | + expected_url = "https://example.com/?#{substitutions.map { |k, v| "#{k.downcase}=#{v}" }.join('&')}" |
| 78 | + |
| 79 | + webhook = Webhook.create!(scraper: scraper, url: url_template) |
| 80 | + webhook_delivery = webhook.deliveries.create!(run: run) |
| 81 | + |
| 82 | + stub = stub_request(:post, expected_url).to_return(status: 200) |
| 83 | + |
| 84 | + described_class.new.perform(webhook_delivery.id) |
| 85 | + |
| 86 | + expect(stub).to have_been_requested.once |
| 87 | + end |
| 88 | + |
| 89 | + it "uses 'success' for OUTCOME when run succeeds" do |
| 90 | + run.update!(status_code: 0) |
| 91 | + webhook = Webhook.create!(scraper: scraper, url: "https://example.com/?outcome=OUTCOME") |
| 92 | + webhook_delivery = webhook.deliveries.create!(run: run) |
| 93 | + |
| 94 | + stub = stub_request(:post, "https://example.com/?outcome=success").to_return(status: 200) |
| 95 | + |
| 96 | + described_class.new.perform(webhook_delivery.id) |
| 97 | + |
| 98 | + expect(stub).to have_been_requested.once |
| 99 | + end |
19 | 100 | end |
20 | 101 | end |
0 commit comments