|
| 1 | +require 'support/test_data_builder' |
| 2 | +require 'webmock/rspec' |
| 3 | +require 'rack/pact_broker/database_transaction' |
| 4 | + |
| 5 | +describe "Execute a webhook" do |
| 6 | + |
| 7 | + let(:td) { TestDataBuilder.new } |
| 8 | + |
| 9 | + before do |
| 10 | + td.create_pact_with_hierarchy("Some Consumer", "1", "Some Provider") |
| 11 | + .create_webhook(method: 'POST') |
| 12 | + end |
| 13 | + |
| 14 | + let(:path) { "/webhooks/#{td.webhook.uuid}/execute" } |
| 15 | + let(:response_body) { JSON.parse(last_response.body, symbolize_names: true)} |
| 16 | + |
| 17 | + subject { post path; last_response } |
| 18 | + |
| 19 | + context "when the execution is successful" do |
| 20 | + let!(:request) do |
| 21 | + stub_request(:post, /http/).to_return(:status => 200) |
| 22 | + end |
| 23 | + |
| 24 | + it "performs the HTTP request" do |
| 25 | + subject |
| 26 | + expect(request).to have_been_made |
| 27 | + end |
| 28 | + |
| 29 | + it "returns a 200 response" do |
| 30 | + puts subject.body |
| 31 | + expect(subject.status).to be 200 |
| 32 | + end |
| 33 | + |
| 34 | + it "saves a TriggeredWebhook" do |
| 35 | + expect { subject }.to change { PactBroker::Webhooks::TriggeredWebhook.count }.by(1) |
| 36 | + end |
| 37 | + |
| 38 | + it "saves a WebhookExecution" do |
| 39 | + expect { subject }.to change { PactBroker::Webhooks::Execution.count }.by(1) |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + context "when an error occurs", no_db_clean: true do |
| 44 | + let(:app) { Rack::PactBroker::DatabaseTransaction.new(PactBroker::API, PactBroker::DB.connection) } |
| 45 | + |
| 46 | + let!(:request) do |
| 47 | + stub_request(:post, /http/).to_raise(Errno::ECONNREFUSED) |
| 48 | + end |
| 49 | + |
| 50 | + before do |
| 51 | + PactBroker::Database.truncate |
| 52 | + td.create_pact_with_hierarchy("Some Consumer", "1", "Some Provider") |
| 53 | + .create_webhook(method: 'POST') |
| 54 | + end |
| 55 | + |
| 56 | + after do |
| 57 | + PactBroker::Database.truncate |
| 58 | + end |
| 59 | + |
| 60 | + subject { post path; last_response } |
| 61 | + |
| 62 | + it "returns a 500 response" do |
| 63 | + expect(subject.status).to be 500 |
| 64 | + end |
| 65 | + |
| 66 | + it "saves a TriggeredWebhook" do |
| 67 | + expect { subject }.to change { PactBroker::Webhooks::TriggeredWebhook.count }.by(1) |
| 68 | + end |
| 69 | + |
| 70 | + it "saves a WebhookExecution" do |
| 71 | + expect { subject }.to change { PactBroker::Webhooks::Execution.count }.by(1) |
| 72 | + end |
| 73 | + end |
| 74 | +end |
0 commit comments