Skip to content

Commit

Permalink
feat(webhooks): support template parameters in header values, usernam…
Browse files Browse the repository at this point in the history
…e and password
  • Loading branch information
bethesque committed Jul 19, 2019
1 parent c10a6f2 commit a800ac2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 20 deletions.
6 changes: 5 additions & 1 deletion lib/pact_broker/string_refinements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ module PactBroker
module StringRefinements
refine String do
def not_blank?
self && self.strip.size > 0
!blank?
end

def blank?
self.strip.size == 0
end
end
end
Expand Down
41 changes: 28 additions & 13 deletions lib/pact_broker/webhooks/webhook_request_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
require 'pact_broker/webhooks/render'
require 'cgi'
require 'pact_broker/domain/webhook_request'
require 'pact_broker/string_refinements'

module PactBroker
module Webhooks
class WebhookRequestTemplate

include PactBroker::Logging
include PactBroker::Messages
using PactBroker::StringRefinements

HEADERS_TO_REDACT = [/authorization/i, /token/i]

attr_accessor :method, :url, :headers, :body, :username, :password, :uuid
Expand All @@ -31,24 +34,15 @@ def build(template_params)
attributes = {
method: http_method,
url: build_url(template_params),
headers: headers,
username: username,
password: password,
headers: build_headers(template_params),
username: build_string(username, template_params),
password: build_string(password, template_params),
uuid: uuid,
body: build_body(template_params)
}
PactBroker::Domain::WebhookRequest.new(attributes)
end

def build_url(template_params)
URI(PactBroker::Webhooks::Render.call(url, template_params){ | value | CGI::escape(value) if !value.nil? } ).to_s
end

def build_body(template_params)
body_string = String === body ? body : body.to_json
PactBroker::Webhooks::Render.call(body_string, template_params)
end

def description
"#{http_method.upcase} #{URI(url.gsub(PactBroker::Webhooks::Render::TEMPLATE_PARAMETER_REGEXP, 'placeholder')).host}"
end
Expand All @@ -68,11 +62,32 @@ def headers= headers
@headers = Rack::Utils::HeaderHash.new(headers)
end

private

def to_s
"#{method.upcase} #{url}, username=#{username}, password=#{display_password}, headers=#{redacted_headers}, body=#{body}"
end

private

def build_url(template_params)
URI(PactBroker::Webhooks::Render.call(url, template_params){ | value | CGI::escape(value) if !value.nil? } ).to_s
end

def build_body(template_params)
body_string = String === body ? body : body.to_json
build_string(body_string, template_params)
end

def build_headers(template_params)
headers.each_with_object(Rack::Utils::HeaderHash.new) do | (key, value), new_headers |
new_headers[key] = build_string(value, template_params)
end
end

def build_string(string, template_params)
return string if string.nil? || string.blank?
PactBroker::Webhooks::Render.call(string, template_params)
end
end
end
end
41 changes: 35 additions & 6 deletions spec/lib/pact_broker/webhooks/webhook_request_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ module Webhooks
{
method: 'POST',
url: url,
username: "foo",
password: "bar",
username: "username",
password: "password",
uuid: "1234",
body: body,
headers: {'Foo' => 'bar'}
headers: {'headername' => 'headervalue'}
}
end

let(:new_attributes) do
{
method: 'POST',
url: built_url,
username: "foo",
password: "bar",
username: "usernameBUILT",
password: "passwordBUILT",
uuid: "1234",
body: built_body,
headers: {'Foo' => 'bar'}
headers: {'headername' => 'headervalueBUILT'}
}
end

Expand Down Expand Up @@ -73,10 +73,39 @@ module Webhooks
end
end

it "renders each header value" do
expect(PactBroker::Webhooks::Render).to receive(:call).with('headervalue', params_hash)
subject
end

it "renders the username" do
expect(PactBroker::Webhooks::Render).to receive(:call).with('username', params_hash)
subject
end

it "renders the password" do
expect(PactBroker::Webhooks::Render).to receive(:call).with('password', params_hash)
subject
end

it "creates a new PactBroker::Domain::WebhookRequest" do
expect(PactBroker::Domain::WebhookRequest).to receive(:new).with(new_attributes)
subject
end

context "when optional attributes are missing" do
let(:attributes) do
{
method: 'POST',
url: url,
uuid: "1234",
}
end

it "does not blow up" do
subject
end
end
end
end
end
Expand Down

0 comments on commit a800ac2

Please sign in to comment.