From 80b18953683e030255b782acf51d9328c6370800 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Wed, 6 Oct 2021 17:07:47 +1100 Subject: [PATCH] test: update webhook docs tests --- .../webhooks_documentation_spec.rb | 187 +++++++++--------- spec/support/documentation.rb | 64 ++++++ 2 files changed, 159 insertions(+), 92 deletions(-) create mode 100644 spec/support/documentation.rb diff --git a/spec/integration/webhooks_documentation_spec.rb b/spec/integration/webhooks_documentation_spec.rb index 31989248f..85c8dc878 100644 --- a/spec/integration/webhooks_documentation_spec.rb +++ b/spec/integration/webhooks_documentation_spec.rb @@ -101,95 +101,44 @@ let(:http_method) { "GET" } let(:path) do - parameter_values.merge(custom_parameter_values).inject(path_template) do | new_path, (name, value) | - new_path.gsub(/:#{name}(\/|$)/, value + '\1') - end + build_path(path_template, parameter_values, custom_parameter_values) end let(:approval_request_example_name) do | example | - "docs_webhooks_" + pact_broker_example_name.gsub(" ", "_") + "_" + http_method + build_approval_name(category, pact_broker_example_name, http_method) end let(:pact_broker_example_name) do | example | example.example_group.parent_groups[-2].description end - def remove_deprecated_links(thing) - case thing - when Hash then remove_deprecated_links_from_hash(thing) - when Array then thing.collect { |value| remove_deprecated_links(value) } - else thing - end - end - - def remove_deprecated_links_from_hash(body) - body.each_with_object({}) do | (key, value), new_body | - if key == "_links" - links = value.select do | key, _value | - key.start_with?("pb:", "self", "next", "previous", "curies") - end - new_body["_links"] = links - else - new_body[key] = remove_deprecated_links(value) - end - end - end - subject { send(http_method.downcase, path, http_params, rack_headers) } shared_examples "request" do it "returns a body" do | example | subject - expectated_body = subject.headers["Content-Type"]&.include?("json") && subject.body && subject.body != "" ? remove_deprecated_links(JSON.parse(subject.body)) : subject.body - expected_response = { - status: subject.status, - headers: determinate_headers(subject.headers), - body: expectated_body - } - request = { - method: http_method, - path_template: path_template, - path: path, - headers: rack_env_to_http_headers(rack_headers), - body: http_params.is_a?(String) ? JSON.parse(http_params) : nil - }.compact - - to_approve = { - category: category, - name: pact_broker_example_name, - order: WEBHOOK_TESTED_PATHS.size, - request: request, - response: expected_response - } - Approvals.verify(to_approve, :name => approval_request_example_name, format: :json) + Approvals.verify(expected_interaction(subject, WEBHOOK_TESTED_PATHS.size), :name => approval_request_example_name, format: :json) end end shared_examples "supports GET" do - describe "GET" do - its(:status) { is_expected.to eq 200 } - - include_examples "request" - end + its(:status) { is_expected.to eq 200 } + include_examples "request" end shared_examples "supports POST" do - describe "POST" do - before do - allow(PactBroker::Webhooks::Service).to receive(:next_uuid).and_return("dCGCl-Ba3PqEFJ_iE9mJkQ") - end - let(:http_method) { "POST" } - let(:rack_headers) do - { - "CONTENT_TYPE" => "application/json", - "ACCEPT" => "application/hal+json" - } - end - - its(:status) { is_expected.to be_between(200, 201) } - include_examples "request" + let(:http_method) { "POST" } + let(:rack_headers) do + { + "CONTENT_TYPE" => "application/json", + "ACCEPT" => "application/hal+json" + } end + + its(:status) { is_expected.to be_between(200, 201) } + include_examples "request" + end shared_examples "supports PUT" do @@ -209,58 +158,85 @@ def remove_deprecated_links_from_hash(body) end shared_examples "supports OPTIONS" do - describe "OPTIONS" do - let(:http_method) { "OPTIONS" } + let(:http_method) { "OPTIONS" } - its(:status) { is_expected.to eq 200 } - include_examples "request" - end + its(:status) { is_expected.to eq 200 } + include_examples "request" end describe "Webhook" do let(:path_template) { "/webhooks/:uuid" } let(:custom_parameter_values) { { uuid: webhook_uuid } } - include_examples "supports GET" + describe "GET" do + include_examples "supports GET" + end describe "PUT" do let(:http_params) { webhook_hash.to_json } include_examples "supports PUT" end - include_examples "supports OPTIONS" + + describe "OPTIONS" do + include_examples "supports OPTIONS" + end end describe "Webhooks" do let(:path_template) { "/webhooks" } - include_examples "supports GET" + describe "GET" do + include_examples "supports GET" + end describe "POST" do + before do + allow(PactBroker::Webhooks::Service).to receive(:next_uuid).and_return("dCGCl-Ba3PqEFJ_iE9mJkQ") + end + let(:http_params) { webhook_hash.to_json } include_examples "supports POST" end - include_examples "supports OPTIONS" + + describe "OPTIONS" do + include_examples "supports OPTIONS" + end end describe "Webhooks for consumer" do let(:path_template) { "/webhooks/consumer/:consumer_name" } - include_examples "supports GET" - include_examples "supports OPTIONS" + describe "GET" do + include_examples "supports GET" + end + + describe "OPTIONS" do + include_examples "supports OPTIONS" + end end describe "Webhooks for a provider" do let(:path_template) { "/webhooks/provider/:provider_name" } - include_examples "supports GET" - include_examples "supports OPTIONS" + describe "GET" do + include_examples "supports GET" + end + + describe "OPTIONS" do + include_examples "supports OPTIONS" + end end describe "Webhooks for consumer and provider" do let(:path_template) { "/webhooks/provider/:provider_name/consumer/:consumer_name" } - include_examples "supports GET" - include_examples "supports OPTIONS" + describe "GET" do + include_examples "supports GET" + end + + describe "OPTIONS" do + include_examples "supports OPTIONS" + end end describe "Pact webhooks" do @@ -268,8 +244,13 @@ def remove_deprecated_links_from_hash(body) "/pacts/provider/:provider_name/consumer/:consumer_name/webhooks" end - include_examples "supports GET" - include_examples "supports OPTIONS" + describe "GET" do + include_examples "supports GET" + end + + describe "OPTIONS" do + include_examples "supports OPTIONS" + end end describe "Webhooks status" do @@ -277,8 +258,13 @@ def remove_deprecated_links_from_hash(body) "/pacts/provider/:provider_name/consumer/:consumer_name/webhooks/status" end - include_examples "supports GET" - include_examples "supports OPTIONS" + describe "GET" do + include_examples "supports GET" + end + + describe "OPTIONS" do + include_examples "supports OPTIONS" + end end describe "Executing a saved webhook" do @@ -301,7 +287,9 @@ def remove_deprecated_links_from_hash(body) describe "Executing an unsaved webhook" do let(:path_template) { "/webhooks/execute" } - include_examples "supports OPTIONS" + describe "OPTIONS" do + include_examples "supports OPTIONS" + end describe "POST" do before do @@ -321,8 +309,13 @@ def remove_deprecated_links_from_hash(body) "/pacts/provider/:provider_name/consumer/:consumer_name/version/:consumer_version_number/triggered-webhooks" end - include_examples "supports GET" - include_examples "supports OPTIONS" + describe "GET" do + include_examples "supports GET" + end + + describe "OPTIONS" do + include_examples "supports OPTIONS" + end end describe "Triggered webhooks for verification publication" do @@ -330,15 +323,25 @@ def remove_deprecated_links_from_hash(body) "/pacts/provider/:provider_name/consumer/:consumer_name/pact-version/:pact_version_sha/verification-results/:verification_number/triggered-webhooks" end - include_examples "supports GET" - include_examples "supports OPTIONS" + describe "GET" do + include_examples "supports GET" + end + + describe "OPTIONS" do + include_examples "supports OPTIONS" + end end describe "Logs of triggered webhook" do let(:path_template) { "/triggered-webhooks/:uuid/logs" } let(:custom_parameter_values) { { uuid: triggered_webhook_uuid } } - include_examples "supports GET" - include_examples "supports OPTIONS" + describe "GET" do + include_examples "supports GET" + end + + describe "OPTIONS" do + include_examples "supports OPTIONS" + end end end diff --git a/spec/support/documentation.rb b/spec/support/documentation.rb new file mode 100644 index 000000000..186891e6a --- /dev/null +++ b/spec/support/documentation.rb @@ -0,0 +1,64 @@ +module PactBroker + module Documentation + + def remove_deprecated_links(thing) + case thing + when Hash then remove_deprecated_links_from_hash(thing) + when Array then thing.collect { |value| remove_deprecated_links(value) } + else thing + end + end + + def remove_deprecated_links_from_hash(body) + body.each_with_object({}) do | (key, value), new_body | + if key == "_links" + links = value.select do | key, _value | + key.start_with?("pb:", "self", "next", "previous", "curies") + end + new_body["_links"] = links + else + new_body[key] = remove_deprecated_links(value) + end + end + end + + def build_approval_name(category, example_name, http_method) + "docs_#{category}_" + example_name.gsub(" ", "_") + "_" + http_method + end + + def build_path(path_template, parameter_values, custom_parameter_values) + parameter_values.merge(custom_parameter_values).inject(path_template) do | new_path, (name, value) | + new_path.gsub(/:#{name}(\/|$)/, value + '\1') + end + end + + def expected_interaction(response, order) + expectated_body = response.headers["Content-Type"]&.include?("json") && response.body && response.body != "" ? remove_deprecated_links(JSON.parse(response.body)) : response.body + expected_response = { + status: response.status, + headers: determinate_headers(response.headers), + body: expectated_body + } + request = { + method: http_method, + path_template: path_template, + path: path, + headers: rack_env_to_http_headers(rack_headers), + body: http_params.is_a?(String) ? JSON.parse(http_params) : nil + }.compact + + to_approve = { + category: category, + name: pact_broker_example_name, + order: order, + request: request, + response: expected_response + } + end + + end +end + +RSpec.configure do | config | + config.include(PactBroker::Documentation) +end \ No newline at end of file