Class: PactBroker::Test::HttpTestDataBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/pact_broker/test/http_test_data_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pact_broker_base_url, auth = {}) ⇒ HttpTestDataBuilder

Returns a new instance of HttpTestDataBuilder.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 13

def initialize(pact_broker_base_url, auth = {})
  @client = Faraday.new(url: pact_broker_base_url) do |faraday|
    faraday.request :json
    faraday.response :json, :content_type => /\bjson$/
    if ENV['DEBUG'] == 'true'
      faraday.response :logger, ::Logger.new($stdout), headers: false do | logger |
        logger.filter(/(Authorization: ).*/,'\1[REMOVED]')
      end
    end
    faraday.headers['Authorization'] = "Bearer #{auth[:token]}" if auth[:token]
    faraday.adapter  Faraday.default_adapter
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



11
12
13
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 11

def client
  @client
end

#last_consumer_nameObject (readonly)

Returns the value of attribute last_consumer_name.



11
12
13
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 11

def last_consumer_name
  @last_consumer_name
end

#last_consumer_version_numberObject (readonly)

Returns the value of attribute last_consumer_version_number.



11
12
13
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 11

def last_consumer_version_number
  @last_consumer_version_number
end

#last_provider_nameObject (readonly)

Returns the value of attribute last_provider_name.



11
12
13
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 11

def last_provider_name
  @last_provider_name
end

#last_provider_version_branchObject (readonly)

Returns the value of attribute last_provider_version_branch.



11
12
13
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 11

def last_provider_version_branch
  @last_provider_version_branch
end

#last_provider_version_numberObject (readonly)

Returns the value of attribute last_provider_version_number.



11
12
13
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 11

def last_provider_version_number
  @last_provider_version_number
end

#last_provider_version_tagObject (readonly)

Returns the value of attribute last_provider_version_tag.



11
12
13
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 11

def last_provider_version_tag
  @last_provider_version_tag
end

Instance Method Details

#can_i_deploy(pacticipant:, version:, to:) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 212

def can_i_deploy(pacticipant:, version:, to:)
  can_i_deploy_response = client.get("can-i-deploy", { pacticipant: pacticipant, version: version, to: to} ).tap { |response| check_for_error(response) }
  can = !!(can_i_deploy_response.body['summary'] || {})['deployable']
  puts "can-i-deploy #{pacticipant} version #{version} to #{to}: #{can ? 'yes' : 'no'}"
  summary = can_i_deploy_response.body['summary']
  verification_result_urls = (can_i_deploy_response.body['matrix'] || []).collect do | row |
    row.dig("verificationResult", "_links", "self", "href")
  end.compact
  summary.merge!("verification_result_urls" => verification_result_urls)
  puts summary.to_yaml
  separate
  self
end

#create_global_webhook_for_contract_changed(uuid: nil, url: "https://postman-echo.com/post") ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 150

def create_global_webhook_for_contract_changed(uuid: nil, url: "https://postman-echo.com/post")
  puts "Creating global webhook for contract changed event with uuid #{uuid}"
  uuid ||= SecureRandom.uuid
  request_body = {
    "description" => "A webhook for all consumers and providers",
    "events" => [{
      "name" => "contract_content_changed"
    }],
    "request" => {
      "method" => "POST",
      "url" => url
    }
  }
  path = "webhooks/#{uuid}"
  response = client.put(path, request_body.to_json).tap { |response| check_for_error(response) }
  separate
  self
end

#create_global_webhook_for_verification_published(uuid: nil, url: "https://postman-echo.com/post") ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 169

def create_global_webhook_for_verification_published(uuid: nil, url: "https://postman-echo.com/post")
  puts "Creating global webhook for contract changed event with uuid #{uuid}"
  uuid ||= SecureRandom.uuid
  request_body = {
    "description" => "A webhook for all consumers and providers",
    "events" => [{
      "name" => "contract_published"
    },{
      "name" => "provider_verification_published"
    }],
    "request" => {
      "method" => "POST",
      "url" => url,
      "headers" => { "Content-Type" => "application/json"},
      "body" => {
        "eventName" => "${pactbroker.eventName}",
        "consumerVersionNumber" => "${pactbroker.consumerVersionNumber}",
        "consumerVersionTags" => "${pactbroker.consumerVersionTags}",
        "githubVerificationStatus" => "${pactbroker.githubVerificationStatus}",
        "providerVersionNumber" => "${pactbroker.providerVersionNumber}",
        "providerVersionTags" => "${pactbroker.providerVersionTags}"
      }
    }
  }
  path = "webhooks/#{uuid}"
  response = client.put(path, request_body.to_json).tap { |response| check_for_error(response) }
  separate
  self
end

#create_pacticipant(name) ⇒ Object



63
64
65
66
67
68
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 63

def create_pacticipant(name)
  puts "Creating pacticipant with name #{name}"
  client.post("pacticipants", { name: name}).tap { |response| check_for_error(response) }
  separate
  self
end

#create_tag(pacticipant:, version:, tag:) ⇒ Object



43
44
45
46
47
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 43

def create_tag(pacticipant:, version:, tag:)
  puts "Creating tag '#{tag}' for #{pacticipant} version #{version}"
  client.put("pacticipants/#{encode(pacticipant)}/versions/#{encode(version)}/tags/#{encode(tag)}", {}).tap { |response| check_for_error(response) }
  self
end

#create_tagged_pacticipant_version(pacticipant:, version:, tag:) ⇒ Object



36
37
38
39
40
41
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 36

def create_tagged_pacticipant_version(pacticipant:, version:, tag:)
  [*tag].each do | tag |
    create_tag(pacticipant: pacticipant, version: version, tag: tag)
  end
  self
end

#create_version(pacticipant:, version:, branch:) ⇒ Object



49
50
51
52
53
54
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 49

def create_version(pacticipant:, version:, branch:)
  request_body = {
    branch: branch
  }
  client.put("pacticipants/#{encode(pacticipant)}/versions/#{encode(version)}", request_body).tap { |response| check_for_error(response) }
end

#delete_integration(consumer:, provider:) ⇒ Object



226
227
228
229
230
231
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 226

def delete_integration(consumer:, provider:)
  puts "Deleting all data for the integration between #{consumer} and #{provider}"
  client.delete("integrations/provider/#{encode(provider)}/consumer/#{encode(consumer)}").tap { |response| check_for_error(response) }
  separate
  self
end

#delete_pacticipant(name) ⇒ Object



233
234
235
236
237
238
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 233

def delete_pacticipant(name)
  puts "Deleting pacticipant #{name}"
  @publish_pact_response = client.delete("pacticipants/#{encode(name)}").tap { |response| check_for_error(response) }
  separate
  self
end

#delete_webhook(uuid:) ⇒ Object



199
200
201
202
203
204
205
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 199

def delete_webhook(uuid:)
  puts "Deleting webhook with uuid #{uuid}"
  path = "webhooks/#{uuid}"
  response = client.delete(path).tap { |response| check_for_error(response) }
  separate
  self
end

#deploy_to_prod(pacticipant:, version:) ⇒ Object



56
57
58
59
60
61
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 56

def deploy_to_prod(pacticipant:, version:)
  puts "Deploying #{pacticipant} version #{version} to prod"
  create_tag(pacticipant: pacticipant, version: version, tag: "prod")
  separate
  self
end

#generate_content(consumer_name, provider_name, content_id) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 240

def generate_content(consumer_name, provider_name, content_id)
  {
    consumer: {
      name: consumer_name
    },
    provider: {
      name: provider_name
    },
    interactions: [
      {
        description: "a request",
        request: {
          method: "GET",
          path: "/things/#{content_id}"
        },
        response: {
          status: 200
        }
      }
    ]
  }
end

#get_pacts_for_verification(provider: last_provider_name, provider_version_tag: nil, provider_version_branch: nil, consumer_version_selectors:, enable_pending: false, include_wip_pacts_since: nil) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 90

def get_pacts_for_verification(provider: last_provider_name, provider_version_tag: nil, provider_version_branch: nil, consumer_version_selectors:, enable_pending: false, include_wip_pacts_since: nil)
  @last_provider_name = provider
  @last_provider_version_tag = provider_version_tag
  @last_provder_version_branch = provider_version_branch
  puts "Fetching pacts for verification for #{provider}"
  request_body = {
    providerVersionTags: [*provider_version_tag],
    providerVersionBranch: provider_version_branch,
    consumerVersionSelectors: consumer_version_selectors,
    includePendingStatus: enable_pending,
    includeWipPactsSince: include_wip_pacts_since
  }.compact
  puts request_body.to_yaml
  puts ""
  @pacts_for_verification_response = client.post("pacts/provider/#{encode(provider)}/for-verification", request_body).tap { |response| check_for_error(response) }

  print_pacts_for_verification
  separate
  self
end


111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 111

def print_pacts_for_verification
  pacts = @pacts_for_verification_response.body&.dig("_embedded", "pacts")
  if pacts
    puts "Pacts for verification (#{pacts.count}):"
    pacts.each do | pact |
      puts({
        "url" => pact["_links"]["self"]["href"],
        "wip" => pact["verificationProperties"]["wip"],
        "pending" => pact["verificationProperties"]["pending"]
      }.to_yaml)
    end
  end
  self
end


207
208
209
210
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 207

def print_pacts_for_verification_response
  puts @pacts_for_verification_response.body
  self
end

#publish_pact(consumer: last_consumer_name, consumer_version:, provider: last_provider_name, content_id:, tag: nil, branch: nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 70

def publish_pact(consumer: last_consumer_name, consumer_version:, provider: last_provider_name, content_id:, tag: nil, branch: nil)
  @last_consumer_name = consumer
  @last_provider_name = provider
  @last_consumer_version_number = consumer_version

  create_version(pacticipant: consumer, version: consumer_version, branch: branch) if branch

  [*tag].each do | tag |
    create_tag(pacticipant: consumer, version: consumer_version, tag: tag)
  end
  puts "" if [*tag].any?

  content = generate_content(consumer, provider, content_id)
  puts "Publishing pact for consumer #{consumer} version #{consumer_version} and provider #{provider}"
  pact_path = "pacts/provider/#{encode(provider)}/consumer/#{encode(consumer)}/version/#{encode(consumer_version)}"
  @publish_pact_response = client.put(pact_path, content).tap { |response| check_for_error(response) }
  separate
  self
end

#separateObject



32
33
34
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 32

def separate
  puts "\n=============================================================\n\n"
end

#sleep(seconds = 0.5) ⇒ Object



27
28
29
30
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 27

def sleep(seconds = 0.5)
  Kernel.sleep(seconds)
  self
end

#verify_latest_pact_for_tag(success: true, provider: last_provider_name, consumer: last_consumer_name, consumer_version_tag:, provider_version:, provider_version_tag: nil) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 126

def verify_latest_pact_for_tag(success: true, provider: last_provider_name, consumer: last_consumer_name, consumer_version_tag: , provider_version:, provider_version_tag: nil)
  @last_provider_name = provider
  @last_consumer_name = consumer

  url_of_pact_to_verify = "pacts/provider/#{encode(provider)}/consumer/#{encode(consumer)}/latest/#{encode(consumer_version_tag)}"
  publish_verification_results(url_of_pact_to_verify, provider, provider_version, provider_version_tag, success)
  separate
  self
end

#verify_pact(index: 0, success:, provider: last_provider_name, provider_version_tag: last_provider_version_tag, provider_version_branch: last_provider_version_branch, provider_version:) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 136

def verify_pact(index: 0, success:, provider: last_provider_name, provider_version_tag: last_provider_version_tag, provider_version_branch: last_provider_version_branch, provider_version: )
  @last_provider_name = provider
  @last_provider_version_tag = provider_version_tag
  @last_provider_version_branch = provider_version_branch

  pact_to_verify = @pacts_for_verification_response.body["_embedded"]["pacts"][index]
  raise "No pact found to verify at index #{index}" unless pact_to_verify
  url_of_pact_to_verify = pact_to_verify["_links"]["self"]["href"]

  publish_verification_results(url_of_pact_to_verify, provider, provider_version, provider_version_tag, success)
  separate
  self
end