pact_broker-client 1.39.0 → 1.44.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +21 -0
  3. data/.gitignore +1 -0
  4. data/CHANGELOG.md +52 -0
  5. data/Gemfile +4 -0
  6. data/README.md +39 -18
  7. data/doc/pacts/markdown/Pact Broker Client - Pact Broker.md +79 -280
  8. data/lib/pact_broker/client/backports.rb +9 -0
  9. data/lib/pact_broker/client/base_command.rb +95 -0
  10. data/lib/pact_broker/client/can_i_deploy.rb +20 -3
  11. data/lib/pact_broker/client/cli/broker.rb +20 -75
  12. data/lib/pact_broker/client/cli/custom_thor.rb +12 -0
  13. data/lib/pact_broker/client/cli/deployment_commands.rb +74 -0
  14. data/lib/pact_broker/client/cli/environment_commands.rb +70 -0
  15. data/lib/pact_broker/client/cli/pacticipant_commands.rb +53 -0
  16. data/lib/pact_broker/client/cli/record_deployment_long_desc.txt +0 -55
  17. data/lib/pact_broker/client/cli/version_selector_options_parser.rb +4 -0
  18. data/lib/pact_broker/client/colorize_notices.rb +31 -0
  19. data/lib/pact_broker/client/deployments.rb +4 -0
  20. data/lib/pact_broker/client/deployments/record_deployment.rb +38 -0
  21. data/lib/pact_broker/client/deployments/record_release.rb +99 -0
  22. data/lib/pact_broker/client/deployments/record_undeployment.rb +120 -0
  23. data/lib/pact_broker/client/describe_text_formatter.rb +23 -0
  24. data/lib/pact_broker/client/environments.rb +6 -0
  25. data/lib/pact_broker/client/environments/create_environment.rb +31 -0
  26. data/lib/pact_broker/client/environments/delete_environment.rb +27 -0
  27. data/lib/pact_broker/client/environments/describe_environment.rb +26 -0
  28. data/lib/pact_broker/client/environments/environment_command.rb +66 -0
  29. data/lib/pact_broker/client/environments/list_environments.rb +30 -0
  30. data/lib/pact_broker/client/environments/text_formatter.rb +30 -0
  31. data/lib/pact_broker/client/environments/update_environment.rb +31 -0
  32. data/lib/pact_broker/client/generate_display_name.rb +27 -0
  33. data/lib/pact_broker/client/hal/entity.rb +14 -4
  34. data/lib/pact_broker/client/hal/http_client.rb +8 -2
  35. data/lib/pact_broker/client/hal/link.rb +8 -0
  36. data/lib/pact_broker/client/hal_client_methods.rb +1 -3
  37. data/lib/pact_broker/client/matrix.rb +4 -0
  38. data/lib/pact_broker/client/matrix/abbreviate_version_number.rb +15 -0
  39. data/lib/pact_broker/client/matrix/resource.rb +26 -1
  40. data/lib/pact_broker/client/matrix/text_formatter.rb +28 -17
  41. data/lib/pact_broker/client/pacticipants.rb +6 -0
  42. data/lib/pact_broker/client/pacticipants/create.rb +24 -34
  43. data/lib/pact_broker/client/pacticipants/describe.rb +33 -0
  44. data/lib/pact_broker/client/pacticipants/list.rb +34 -0
  45. data/lib/pact_broker/client/pacticipants/text_formatter.rb +41 -0
  46. data/lib/pact_broker/client/publish_pacts.rb +7 -3
  47. data/lib/pact_broker/client/string_refinements.rb +56 -0
  48. data/lib/pact_broker/client/version.rb +1 -1
  49. data/lib/pact_broker/client/versions.rb +4 -1
  50. data/lib/pact_broker/client/versions/describe.rb +3 -1
  51. data/lib/pact_broker/client/versions/formatter.rb +3 -1
  52. data/lib/pact_broker/client/versions/json_formatter.rb +5 -3
  53. data/lib/pact_broker/client/versions/text_formatter.rb +3 -1
  54. data/pact-broker-client.gemspec +2 -0
  55. data/script/approve-all.sh +6 -0
  56. data/script/publish-pact.sh +24 -6
  57. data/script/record-deployments-and-releases.sh +10 -0
  58. data/spec/fixtures/approvals/can_i_deploy_ignore.approved.txt +13 -0
  59. data/spec/fixtures/approvals/describe_environment.approved.txt +7 -0
  60. data/spec/fixtures/approvals/describe_pacticipant.approved.txt +2 -0
  61. data/spec/fixtures/approvals/list_environments.approved.txt +3 -0
  62. data/spec/integration/describe_environment_spec.rb +31 -0
  63. data/spec/lib/pact_broker/client/can_i_deploy_spec.rb +47 -5
  64. data/spec/lib/pact_broker/client/cli/broker_can_i_deploy_spec.rb +5 -5
  65. data/spec/lib/pact_broker/client/cli/broker_publish_spec.rb +1 -1
  66. data/spec/lib/pact_broker/client/cli/broker_run_webhook_commands_spec.rb +3 -3
  67. data/spec/lib/pact_broker/client/cli/version_selector_options_parser_spec.rb +21 -0
  68. data/spec/lib/pact_broker/client/deployments/record_deployment_spec.rb +204 -0
  69. data/spec/lib/pact_broker/client/environments/delete_environment_spec.rb +120 -0
  70. data/spec/lib/pact_broker/client/environments/describe_environment_spec.rb +89 -0
  71. data/spec/lib/pact_broker/client/environments/update_environment_spec.rb +167 -0
  72. data/spec/lib/pact_broker/client/generate_display_name_spec.rb +39 -0
  73. data/spec/lib/pact_broker/client/hal/entity_spec.rb +2 -2
  74. data/spec/lib/pact_broker/client/pacticipants/create_spec.rb +2 -2
  75. data/spec/pacts/pact_broker_client-pact_broker.json +88 -287
  76. data/spec/service_providers/create_environment_spec.rb +78 -0
  77. data/spec/service_providers/list_environments_spec.rb +77 -0
  78. data/spec/service_providers/pact_broker_client_matrix_ignore_spec.rb +98 -0
  79. data/spec/service_providers/pacticipants_create_spec.rb +5 -4
  80. data/spec/service_providers/publish_pacts_spec.rb +5 -2
  81. data/spec/service_providers/record_deployment_spec.rb +17 -36
  82. data/spec/service_providers/record_release_spec.rb +135 -0
  83. data/spec/spec_helper.rb +15 -2
  84. data/spec/support/approvals.rb +26 -0
  85. data/spec/support/shared_context.rb +6 -2
  86. data/tasks/pact.rake +19 -1
  87. metadata +93 -7
  88. data/lib/pact_broker/client/versions/record_deployment.rb +0 -109
  89. data/lib/pact_broker/client/versions/record_undeployment.rb +0 -102
  90. data/spec/lib/pact_broker/client/versions/record_deployment_spec.rb +0 -82
@@ -33,4 +33,6 @@ Gem::Specification.new do |gem|
33
33
  gem.add_development_dependency 'conventional-changelog', '~>1.3'
34
34
  gem.add_development_dependency 'pact', '~> 1.16'
35
35
  gem.add_development_dependency 'pact-support', '~> 1.16'
36
+ gem.add_development_dependency 'approvals', '>=0.0.24', '<1.0.0'
37
+ gem.add_development_dependency 'rspec-its', '~> 1.3'
36
38
  end
@@ -0,0 +1,6 @@
1
+ #!/bin/sh
2
+
3
+ for file in $(find spec/fixtures/approvals -ipath "*.received.*"); do
4
+ approved_path=$(echo "$file" | sed 's/received/approved/')
5
+ mv "$file" "$approved_path"
6
+ done;
@@ -1,3 +1,6 @@
1
+ export PACT_BROKER_BASE_URL="http://localhost:9292"
2
+ export PACT_BROKER_TOKEN="localhost"
3
+
1
4
  # bundle exec bin/pact-broker create-or-update-webhook http://localhost:9393 \
2
5
  # --uuid d40f38c3-aaa3-47f5-9161-95c07bc16b14 \
3
6
  # --request POST \
@@ -5,16 +8,31 @@
5
8
  # --contract-published
6
9
 
7
10
  bundle exec bin/pact-broker create-or-update-webhook http://localhost:9393 \
8
- --uuid d40f38c3-aaa3-47f5-9161-95c07bc16555 \
9
- --provider Bar \
11
+ --uuid d40f38c3-aaa3-47f5-9161-95csfadfsd7 \
12
+ --description "This is quite a long description for a webhook that I hope will be truncated" \
10
13
  --request POST \
11
14
  --contract-published
12
15
 
13
- bundle exec bin/pact-broker publish spec/pacts/pact_broker_client-pact_broker.json spec/pacts/foo-bar.json \
14
- --consumer-app-version 1.2.12 \
16
+ # bundle exec bin/pact-broker publish spec/pacts/pact_broker_client-pact_broker.json spec/fixtures/foo-bar.json \
17
+ # --consumer-app-version 1.2.12 \
18
+ # --broker-base-url http://localhost:9292 \
19
+ # --broker-username localhost --broker-password localhost \
20
+ # --auto-detect-version-properties \
21
+ # --build-url http://mybuild \
22
+ # --branch master --tag foo5
23
+
24
+ # bundle exec bin/pact-broker create-or-update-webhook http://localhost:9393 \
25
+ # --uuid d40f38c3-aaa3-47f5-9161-95c07bc16555 \
26
+ # --provider Bar \
27
+ # --request POST \
28
+ # --contract-published
29
+
30
+
31
+ PACT_BROKER_FEATURES=publish_contracts bundle exec bin/pact-broker publish spec/pacts/pact_broker_client-pact_broker.json \
32
+ --consumer-app-version 1.2.26 \
15
33
  --broker-base-url http://localhost:9292 \
16
- --broker-username localhost --broker-password localhost \
34
+ --broker-token localhost \
17
35
  --auto-detect-version-properties \
18
36
  --build-url http://mybuild \
19
- --branch master --tag foo5
37
+ --branch master --tag foo5 --tag foo6
20
38
 
@@ -0,0 +1,10 @@
1
+ export PACT_BROKER_FEATURES=deployments
2
+
3
+ bundle exec bin/pact-broker create-or-update-pacticipant --name Foo
4
+ bundle exec bin/pact-broker create-version-tag --pacticipant Foo --version 2 --tag main --auto-create-version
5
+ bundle exec bin/pact-broker describe-version --pacticipant Foo --version 2
6
+ bundle exec bin/pact-broker create-environment --name test
7
+ bundle exec bin/pact-broker can-i-deploy --pacticipant Foo --version 2 --to-environment test
8
+ bundle exec bin/pact-broker record-deployment --pacticipant Foo --version 2 --environment test --target foo
9
+ bundle exec bin/pact-broker record-undeployment --pacticipant Foo --version 2 --environment test --target foo
10
+ bundle exec bin/pact-broker record-release --pacticipant Foo --version 2 --environment test
@@ -0,0 +1,13 @@
1
+ Computer says yes \o/ 
2
+
3
+ CONSUMER | C.VERSION | PROVIDER | P.VERSION | SUCCESS? | RESULT#
4
+ ---------|-----------|----------|-----------|-----------------|--------
5
+ Foo | 1.2.3 | Bar | 4.5.6 | true | 1
6
+ Foo | 3.4.5 | Bar | 4.5.6 | false [ignored] | 2
7
+
8
+ VERIFICATION RESULTS
9
+ --------------------
10
+ 1. http://result (success)
11
+ 2. http://result (failure)
12
+
13
+ some notice
@@ -0,0 +1,7 @@
1
+ Name: existing name
2
+ Display Name: existing display name
3
+ Production: true
4
+ Contacts:
5
+ - Name: Someone
6
+ Details:
7
+ Email Address: foo@bar.com
@@ -0,0 +1,2 @@
1
+ Name: Pricing Service
2
+ Repository Url: git@git.realestate.com.au:business-systems/pricing-service
@@ -0,0 +1,3 @@
1
+ UUID | NAME | DISPLAY NAME | PRODUCTION
2
+ -------------------------------------|------|--------------|-----------
3
+ 78e85fb2-9df1-48da-817e-c9bea6294e01 | test | Test | false
@@ -0,0 +1,31 @@
1
+ require 'pact_broker/client/cli/broker'
2
+
3
+ RSpec.describe "describe-environment" do
4
+ let(:index_body_hash) do
5
+ {
6
+ _links: {
7
+ "pb:pacticipant" => {
8
+ href: "http://broker/pacticipants/{pacticipant}"
9
+ }
10
+ }
11
+ }
12
+ end
13
+
14
+ let(:pacticipant_body_hash) { JSON.parse(File.read("./spec/support/pacticipant_get.json")) }
15
+
16
+ let!(:index_request) do
17
+ stub_request(:get, "http://broker").to_return(status: 200, body: index_body_hash.to_json, headers: { "Content-Type" => "application/hal+json" } )
18
+ end
19
+
20
+ let!(:pacticipant_request) do
21
+ stub_request(:get, "http://broker/pacticipants/Foo").to_return(status: 200, body: pacticipant_body_hash.to_json, headers: { "Content-Type" => "application/hal+json" } )
22
+ end
23
+
24
+ let(:parameters) { %w{describe-pacticipant --name Foo --broker-base-url http://broker} }
25
+
26
+ subject { capture(:stdout) { PactBroker::Client::CLI::Broker.start(parameters) } }
27
+
28
+ it "prints the pacticipant properties" do
29
+ Approvals.verify(subject, :name => "describe_pacticipant", format: :txt)
30
+ end
31
+ end
@@ -6,22 +6,27 @@ module PactBroker
6
6
  describe CanIDeploy do
7
7
  let(:pact_broker_base_url) { 'http://example.org' }
8
8
  let(:version_selectors) { [{ pacticipant: "Foo", version: "1" }] }
9
- let(:matrix_options) { {} }
9
+ let(:matrix_options) { { } }
10
10
  let(:pact_broker_client_options) { { foo: 'bar' } }
11
11
  let(:matrix_client) { instance_double('PactBroker::Client::Matrix') }
12
12
  let(:matrix) do
13
13
  instance_double('Matrix::Resource',
14
- deployable?: true,
14
+ deployable?: deployable,
15
15
  reason: 'some reason',
16
16
  any_unknown?: any_unknown,
17
17
  supports_unknown_count?: supports_unknown_count,
18
- unknown_count: unknown_count)
18
+ supports_ignore?: supports_ignore,
19
+ unknown_count: unknown_count,
20
+ notices: notices)
19
21
  end
20
22
  let(:unknown_count) { 0 }
21
23
  let(:any_unknown) { unknown_count > 0 }
22
24
  let(:supports_unknown_count) { true }
23
25
  let(:retry_while_unknown) { 0 }
24
26
  let(:options) { { output: 'text', retry_while_unknown: retry_while_unknown, retry_interval: 5 } }
27
+ let(:notices) { nil }
28
+ let(:supports_ignore) { true }
29
+ let(:deployable) { true }
25
30
 
26
31
 
27
32
  before do
@@ -55,10 +60,19 @@ module PactBroker
55
60
  it "returns a success reason" do
56
61
  expect(subject.message).to include "some reason"
57
62
  end
63
+
64
+ context "when there are notices" do
65
+ let(:notices) { [Notice.new(text: "some notice", type: "info")] }
66
+
67
+ it "returns the notices instead of the reason" do
68
+ expect(subject.message).to_not include "some reason"
69
+ expect(subject.message).to include "some notice"
70
+ end
71
+ end
58
72
  end
59
73
 
60
74
  context "when the versions are not deployable" do
61
- let(:matrix) { instance_double('Matrix::Resource', deployable?: false, reason: 'some reason', any_unknown?: false) }
75
+ let(:matrix) { instance_double('Matrix::Resource', deployable?: false, reason: 'some reason', any_unknown?: false, notices: notices) }
62
76
 
63
77
  it "returns a failure response" do
64
78
  expect(subject.success).to be false
@@ -71,6 +85,15 @@ module PactBroker
71
85
  it "returns a failure reason" do
72
86
  expect(subject.message).to include "some reason"
73
87
  end
88
+
89
+ context "when there are notices" do
90
+ let(:notices) { [Notice.new(text: "some notice", type: "info")] }
91
+
92
+ it "returns the notices instead of the reason" do
93
+ expect(subject.message).to_not include "some reason"
94
+ expect(subject.message).to include "some notice"
95
+ end
96
+ end
74
97
  end
75
98
 
76
99
  context "when retry_while_unknown is greater than 0" do
@@ -115,6 +138,25 @@ module PactBroker
115
138
  end
116
139
  end
117
140
 
141
+ context "when there are ignore selectors but the matrix does not support ignoring" do
142
+ let(:matrix_options) { { ignore_selectors: [{ pacticipant_name: "Foo" }]} }
143
+ let(:supports_ignore) { false }
144
+
145
+ context "when deployable" do
146
+ it "returns a warning" do
147
+ expect(subject.message).to include "does not support"
148
+ end
149
+ end
150
+
151
+ context "when not deployable" do
152
+ let(:deployable) { false }
153
+
154
+ it "returns a warning" do
155
+ expect(subject.message).to include "does not support"
156
+ end
157
+ end
158
+ end
159
+
118
160
  context "when a PactBroker::Client::Error is raised" do
119
161
  before do
120
162
  allow(matrix_client).to receive(:get).and_raise(PactBroker::Client::Error.new('error text'))
@@ -125,7 +167,7 @@ module PactBroker
125
167
  end
126
168
 
127
169
  it "returns a failure message" do
128
- expect(subject.message).to eq "error text"
170
+ expect(subject.message).to include "error text"
129
171
  end
130
172
  end
131
173
 
@@ -37,7 +37,7 @@ module PactBroker
37
37
  end
38
38
 
39
39
  it "invokes the CanIDeploy service" do
40
- expect(CanIDeploy).to receive(:call).with('http://pact-broker', version_selectors, {to_tag: nil, to_environment: nil, limit: 1000}, {output: 'table', retry_while_unknown: 1, retry_interval: 2}, {verbose: 'verbose'})
40
+ expect(CanIDeploy).to receive(:call).with('http://pact-broker', version_selectors, { to_tag: nil, to_environment: nil, limit: 1000, ignore_selectors: []}, {output: 'table', retry_while_unknown: 1, retry_interval: 2}, { pact_broker_base_url: 'http://pact-broker', verbose: 'verbose' })
41
41
  invoke_can_i_deploy
42
42
  end
43
43
 
@@ -55,7 +55,7 @@ module PactBroker
55
55
  end
56
56
 
57
57
  it "passes the value as the matrix options" do
58
- expect(CanIDeploy).to receive(:call).with(anything, anything, {to_tag: 'prod', to_environment: nil, limit: 1000}, anything, anything)
58
+ expect(CanIDeploy).to receive(:call).with(anything, anything, {to_tag: 'prod', to_environment: nil, limit: 1000, ignore_selectors: []}, anything, anything)
59
59
  invoke_can_i_deploy
60
60
  end
61
61
  end
@@ -66,7 +66,7 @@ module PactBroker
66
66
  end
67
67
 
68
68
  it "passes the value as the matrix options" do
69
- expect(CanIDeploy).to receive(:call).with(anything, anything, {to_tag: nil, to_environment: 'prod', limit: 1000}, anything, anything)
69
+ expect(CanIDeploy).to receive(:call).with(anything, anything, {to_tag: nil, to_environment: 'prod', limit: 1000, ignore_selectors: []}, anything, anything)
70
70
  invoke_can_i_deploy
71
71
  end
72
72
  end
@@ -78,7 +78,7 @@ module PactBroker
78
78
  end
79
79
 
80
80
  it "invokes the CanIDeploy service with the basic auth credentials" do
81
- expect(CanIDeploy).to receive(:call).with(anything, anything, anything, anything, {basic_auth: {username: "foo", password: "bar"}, verbose: 'verbose'})
81
+ expect(CanIDeploy).to receive(:call).with(anything, anything, anything, anything, { pact_broker_base_url: 'http://pact-broker', basic_auth: {username: "foo", password: "bar"}, verbose: 'verbose'})
82
82
  invoke_can_i_deploy
83
83
  end
84
84
  end
@@ -89,7 +89,7 @@ module PactBroker
89
89
  end
90
90
 
91
91
  it "invokes the CanIDeploy service with the basic auth credentials" do
92
- expect(CanIDeploy).to receive(:call).with(anything, anything, anything, anything, {token: "some token", verbose: 'verbose'})
92
+ expect(CanIDeploy).to receive(:call).with(anything, anything, anything, anything, {pact_broker_base_url: 'http://pact-broker', token: "some token", verbose: 'verbose'})
93
93
  invoke_can_i_deploy
94
94
  end
95
95
  end
@@ -32,7 +32,7 @@ module PactBroker::Client::CLI
32
32
  ["spec/support/cli_test_pacts/foo.json"],
33
33
  { number: "1.2.3", tags: [], version_required: false },
34
34
  {},
35
- {}
35
+ { pact_broker_base_url: 'http://pact-broker' }
36
36
  )
37
37
  invoke_broker
38
38
  end
@@ -66,7 +66,7 @@ module PactBroker
66
66
  it "calls PactBroker::Client::Webhooks::Create with pact broker details" do
67
67
  expect(PactBroker::Client::Webhooks::Create).to receive(:call) do | _, broker_base_url, pact_broker_client_options |
68
68
  expect(broker_base_url).to eq "http://broker"
69
- expect(pact_broker_client_options).to eq(basic_auth: { username: "username", password: "password"}, verbose: true)
69
+ expect(pact_broker_client_options).to eq(pact_broker_base_url: 'http://broker', basic_auth: { username: "username", password: "password"}, verbose: true)
70
70
  command_result
71
71
  end
72
72
  subject
@@ -92,7 +92,7 @@ module PactBroker
92
92
 
93
93
  it "calls Webhooks::Create without basic auth" do
94
94
  expect(PactBroker::Client::Webhooks::Create).to receive(:call) do | _, _, pact_broker_client_options |
95
- expect(pact_broker_client_options).to eq(verbose: true)
95
+ expect(pact_broker_client_options).to eq(verbose: true, pact_broker_base_url: 'http://broker')
96
96
  command_result
97
97
  end
98
98
  subject
@@ -110,7 +110,7 @@ module PactBroker
110
110
  it "calls Webhooks::Create without basic auth" do
111
111
 
112
112
  expect(PactBroker::Client::Webhooks::Create).to receive(:call) do | _, _, pact_broker_client_options |
113
- expect(pact_broker_client_options).to eq(verbose: true, token: "token")
113
+ expect(pact_broker_client_options).to eq(verbose: true, token: "token", pact_broker_base_url: 'http://broker')
114
114
  command_result
115
115
  end
116
116
  subject
@@ -48,6 +48,27 @@ module PactBroker
48
48
  ],[
49
49
  ["--pacticipant", "Foo=Bar", "--version", "1.2.3"],
50
50
  [{ pacticipant: "Foo=Bar", version: "1.2.3" } ]
51
+ ],[
52
+ ["--ignore", "Foo", "--version", "1.2.3"],
53
+ [{ pacticipant: "Foo", version: "1.2.3", ignore: true }]
54
+ ],[
55
+ ["--ignore", "Foo", "--ignore", "Bar", "--version", "1.2.3"],
56
+ [{ pacticipant: "Foo", ignore: true }, { pacticipant: "Bar", version: "1.2.3", ignore: true }]
57
+ ],[
58
+ ["--ignore", "Foo", "--pacticipant", "Bar", "--version", "1.2.3"],
59
+ [{ pacticipant: "Foo", ignore: true }, { pacticipant: "Bar", version: "1.2.3" }]
60
+ ],[
61
+ ["--pacticipant", "Foo", "--version", "1", "--version", "2"],
62
+ [{ pacticipant: "Foo", version: "2" }]
63
+ ],[
64
+ ["--pacticipant", "Foo", "--version", "2", "--latest"],
65
+ [{ pacticipant: "Foo", version: "2", latest: true }]
66
+ ],[
67
+ ["--pacticipant", "Foo", "--version", "2", "--latest", "--latest"],
68
+ [{ pacticipant: "Foo", version: "2", latest: true }]
69
+ ],[
70
+ ["--version", "2"],
71
+ [{ pacticipant: nil, version: "2" }]
51
72
  ]
52
73
  ]
53
74
 
@@ -0,0 +1,204 @@
1
+ require 'pact_broker/client/deployments/record_deployment'
2
+
3
+ module PactBroker
4
+ module Client
5
+ module Deployments
6
+ describe RecordDeployment do
7
+ describe ".call" do
8
+ let(:broker_base_url) { "http://broker" }
9
+ let(:index_body_hash) do
10
+ {
11
+ _links: {
12
+ "pb:environments" => { href: "#{broker_base_url}/environments" },
13
+ "pb:pacticipant-version" => {
14
+ href: "#{broker_base_url}/pacticipants/{pacticipant}/versions/{version}"
15
+ }
16
+ }
17
+ }
18
+ end
19
+ let(:version_body_hash) do
20
+ {
21
+ "_links" => {
22
+ "pb:record-deployment" => [
23
+ {
24
+ name: "test",
25
+ href: "#{broker_base_url}/record-deployment"
26
+ }
27
+ ]
28
+ }
29
+ }
30
+ end
31
+ let(:environments_body_hash) do
32
+ {
33
+ "_links" => {
34
+ "pb:environments" => [
35
+ {
36
+ "name" => "test",
37
+ "href" => ""
38
+ }
39
+ ]
40
+ }
41
+
42
+ }
43
+ end
44
+ let(:record_deployment_body_hash) do
45
+ { "some" => "response" }
46
+ end
47
+ let!(:index_request) do
48
+ stub_request(:get, broker_base_url).to_return(status: 200, body: index_body_hash.to_json, headers: { "Content-Type" => "application/hal+json" } )
49
+ end
50
+ let!(:version_request) do
51
+ stub_request(:get, broker_base_url + "/pacticipants/Foo/versions/1").to_return(status: 200, body: version_body_hash.to_json, headers: { "Content-Type" => "application/hal+json" } )
52
+ end
53
+
54
+ let!(:environments_request) do
55
+ stub_request(:get, "http://broker/environments").
56
+ with(headers: { 'Accept'=>'application/hal+json' }).
57
+ to_return(status: 200, body: environments_body_hash.to_json, headers: { "Content-Type" => "application/hal+json" })
58
+ end
59
+ let!(:record_deployment_request) do
60
+ stub_request(:post, "http://broker/record-deployment").
61
+ to_return(status: 200, body: record_deployment_body_hash.to_json , headers: {})
62
+ end
63
+
64
+ let(:target) { "blue" }
65
+
66
+ let(:params) do
67
+ {
68
+ pacticipant_name: "Foo",
69
+ version_number: "1",
70
+ environment_name: "test",
71
+ target: target
72
+ }
73
+ end
74
+
75
+ let(:options) do
76
+ {
77
+ output: output,
78
+ verbose: true
79
+ }
80
+ end
81
+ let(:output) { "text" }
82
+ let(:pact_broker_client_options) { { pact_broker_base_url: broker_base_url} }
83
+
84
+ subject { RecordDeployment.call(params, options, pact_broker_client_options) }
85
+
86
+ context "when the pb:environments relation does not exist" do
87
+ let(:index_body_hash) do
88
+ {
89
+ "_links" => {}
90
+ }
91
+ end
92
+ it "returns an error response" do
93
+ expect(subject.success).to be false
94
+ expect(subject.message).to include "does not support"
95
+ end
96
+ end
97
+
98
+ context "when the specified version does not exist" do
99
+ let!(:version_request) do
100
+ stub_request(:get, broker_base_url + "/pacticipants/Foo/versions/1").to_return(status: 404)
101
+ end
102
+
103
+ it "returns an error response" do
104
+ expect(subject.success).to be false
105
+ expect(subject.message).to include "Foo version 1 not found"
106
+ end
107
+ end
108
+
109
+ context "when the pacticipant version does not support recording deployments" do
110
+ let(:version_body_hash) do
111
+ {
112
+ "_links" => {}
113
+ }
114
+ end
115
+ it "returns an error response" do
116
+ expect(subject.success).to be false
117
+ expect(subject.message).to include "does not support"
118
+ end
119
+ end
120
+
121
+ context "when the specified environment is not available for recording a deployment" do
122
+ let(:version_body_hash) do
123
+ {
124
+ "_links" => {
125
+ "pb:record-deployment" => [
126
+ {
127
+ "name" => "prod",
128
+ "href" => ""
129
+ }
130
+ ]
131
+ }
132
+ }
133
+ end
134
+
135
+ context "when the environment does not exist" do
136
+ let(:environments_body_hash) do
137
+ {
138
+ "_links" => {
139
+ "pb:environments" => [
140
+ {
141
+ "name" => "prod",
142
+ "href" => ""
143
+ },{
144
+ "name" => "uat",
145
+ "href" => ""
146
+ }
147
+ ]
148
+ }
149
+ }
150
+ end
151
+
152
+ it "returns an error response" do
153
+ expect(subject.success).to be false
154
+ expect(subject.message).to include "No environment found with name 'test'. Available options: prod"
155
+ end
156
+ end
157
+
158
+ context "when the environment does exist" do
159
+ it "returns an error response" do
160
+ expect(subject.success).to be false
161
+ expect(subject.message).to include "Environment 'test' is not an available option for recording a deployment of Foo. Available options: prod"
162
+ end
163
+ end
164
+ end
165
+
166
+ context "when the output is json" do
167
+ let(:output) { "json" }
168
+
169
+ it "returns the JSON payload" do
170
+ expect(JSON.parse(subject.message)).to eq record_deployment_body_hash
171
+ end
172
+ end
173
+
174
+ context "when the response headers contain Pactflow" do
175
+ before do
176
+ allow_any_instance_of(RecordDeployment).to receive(:check_if_command_supported)
177
+ allow_any_instance_of(RecordDeployment).to receive(:check_environment_exists)
178
+ allow_any_instance_of(RecordDeployment).to receive(:record_action)
179
+ allow_any_instance_of(RecordDeployment).to receive(:index_resource).and_return(index_resource)
180
+ end
181
+
182
+ let(:response_headers) { { "X-Pactflow-Sha" => "abc" } }
183
+
184
+ let(:index_resource) do
185
+ double('PactBroker::Client::Hal::Entity', response: double('response', headers: response_headers) )
186
+ end
187
+
188
+ it "indicates the API was Pactflow" do
189
+ expect(subject.message).to include "Recorded deployment of Foo version 1 to test environment (target blue) in Pactflow"
190
+ end
191
+
192
+ context "when target is nil" do
193
+ let(:target) { nil }
194
+
195
+ it "does not include the target in the result message" do
196
+ expect(subject.message).to include "Recorded deployment of Foo version 1 to test environment in"
197
+ end
198
+ end
199
+ end
200
+ end
201
+ end
202
+ end
203
+ end
204
+ end