pact_broker-client 1.41.0 → 1.46.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +21 -0
  3. data/CHANGELOG.md +57 -0
  4. data/README.md +69 -48
  5. data/doc/pacts/markdown/Pact Broker Client - Pact Broker.md +79 -102
  6. data/doc/pacts/markdown/Pact Broker Client - Pactflow.md +94 -0
  7. data/doc/pacts/markdown/README.md +1 -0
  8. data/lib/pact_broker/client/backports.rb +9 -0
  9. data/lib/pact_broker/client/base_command.rb +98 -0
  10. data/lib/pact_broker/client/can_i_deploy.rb +40 -5
  11. data/lib/pact_broker/client/cli/broker.rb +18 -160
  12. data/lib/pact_broker/client/cli/custom_thor.rb +12 -16
  13. data/lib/pact_broker/client/cli/deployment_commands.rb +94 -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/webhook_commands.rb +122 -0
  17. data/lib/pact_broker/client/deployments.rb +4 -0
  18. data/lib/pact_broker/client/deployments/record_deployment.rb +38 -0
  19. data/lib/pact_broker/client/deployments/record_release.rb +99 -0
  20. data/lib/pact_broker/client/deployments/record_support_ended.rb +103 -0
  21. data/lib/pact_broker/client/deployments/record_undeployment.rb +127 -0
  22. data/lib/pact_broker/client/describe_text_formatter.rb +23 -0
  23. data/lib/pact_broker/client/environments.rb +6 -0
  24. data/lib/pact_broker/client/environments/create_environment.rb +31 -0
  25. data/lib/pact_broker/client/environments/delete_environment.rb +27 -0
  26. data/lib/pact_broker/client/environments/describe_environment.rb +26 -0
  27. data/lib/pact_broker/client/environments/environment_command.rb +66 -0
  28. data/lib/pact_broker/client/environments/list_environments.rb +30 -0
  29. data/lib/pact_broker/client/environments/text_formatter.rb +30 -0
  30. data/lib/pact_broker/client/environments/update_environment.rb +31 -0
  31. data/lib/pact_broker/client/generate_display_name.rb +27 -0
  32. data/lib/pact_broker/client/hal/entity.rb +31 -6
  33. data/lib/pact_broker/client/hal/http_client.rb +8 -2
  34. data/lib/pact_broker/client/hal/link.rb +8 -0
  35. data/lib/pact_broker/client/hal_client_methods.rb +1 -3
  36. data/lib/pact_broker/client/matrix/text_formatter.rb +21 -13
  37. data/lib/pact_broker/client/pacticipants.rb +6 -0
  38. data/lib/pact_broker/client/pacticipants/create.rb +24 -34
  39. data/lib/pact_broker/client/pacticipants/describe.rb +33 -0
  40. data/lib/pact_broker/client/pacticipants/list.rb +34 -0
  41. data/lib/pact_broker/client/pacticipants/text_formatter.rb +41 -0
  42. data/lib/pact_broker/client/string_refinements.rb +56 -0
  43. data/lib/pact_broker/client/version.rb +1 -1
  44. data/lib/pact_broker/client/versions.rb +4 -1
  45. data/lib/pact_broker/client/versions/describe.rb +3 -1
  46. data/lib/pact_broker/client/versions/formatter.rb +3 -1
  47. data/lib/pact_broker/client/versions/json_formatter.rb +5 -3
  48. data/lib/pact_broker/client/versions/text_formatter.rb +3 -1
  49. data/lib/pact_broker/client/webhooks/create.rb +14 -8
  50. data/pact-broker-client.gemspec +1 -0
  51. data/script/record-deployments-and-releases.sh +18 -0
  52. data/script/webhook-commands.sh +12 -0
  53. data/spec/fixtures/approvals/can_i_deploy_failure_dry_run.approved.txt +7 -0
  54. data/spec/fixtures/approvals/can_i_deploy_success_dry_run.approved.txt +7 -0
  55. data/spec/fixtures/approvals/describe_environment.approved.txt +7 -0
  56. data/spec/fixtures/approvals/describe_pacticipant.approved.txt +2 -0
  57. data/spec/fixtures/approvals/list_environments.approved.txt +3 -0
  58. data/spec/integration/describe_environment_spec.rb +31 -0
  59. data/spec/lib/pact_broker/client/can_i_deploy_spec.rb +62 -2
  60. data/spec/lib/pact_broker/client/cli/broker_can_i_deploy_spec.rb +17 -4
  61. data/spec/lib/pact_broker/client/cli/broker_publish_spec.rb +1 -1
  62. data/spec/lib/pact_broker/client/cli/broker_run_webhook_commands_spec.rb +7 -5
  63. data/spec/lib/pact_broker/client/deployments/record_deployment_spec.rb +204 -0
  64. data/spec/lib/pact_broker/client/deployments/record_support_ended_spec.rb +208 -0
  65. data/spec/lib/pact_broker/client/deployments/record_undeployment_spec.rb +219 -0
  66. data/spec/lib/pact_broker/client/environments/delete_environment_spec.rb +120 -0
  67. data/spec/lib/pact_broker/client/environments/describe_environment_spec.rb +89 -0
  68. data/spec/lib/pact_broker/client/environments/update_environment_spec.rb +167 -0
  69. data/spec/lib/pact_broker/client/generate_display_name_spec.rb +39 -0
  70. data/spec/lib/pact_broker/client/hal/entity_spec.rb +2 -2
  71. data/spec/lib/pact_broker/client/pacticipants/create_spec.rb +2 -2
  72. data/spec/pacts/pact_broker_client-pact_broker.json +88 -108
  73. data/spec/pacts/pact_broker_client-pactflow.json +118 -0
  74. data/spec/service_providers/create_environment_spec.rb +78 -0
  75. data/spec/service_providers/list_environments_spec.rb +77 -0
  76. data/spec/service_providers/pact_broker_client_matrix_ignore_spec.rb +1 -1
  77. data/spec/service_providers/pact_broker_client_register_repository_spec.rb +2 -2
  78. data/spec/service_providers/pact_helper.rb +15 -10
  79. data/spec/service_providers/pactflow_webhooks_create_spec.rb +86 -0
  80. data/spec/service_providers/pacticipants_create_spec.rb +5 -4
  81. data/spec/service_providers/publish_pacts_spec.rb +3 -1
  82. data/spec/service_providers/record_deployment_spec.rb +14 -32
  83. data/spec/service_providers/record_release_spec.rb +132 -0
  84. data/spec/service_providers/record_undeployment_spec.rb +166 -0
  85. data/spec/service_providers/webhooks_create_spec.rb +1 -1
  86. data/spec/spec_helper.rb +15 -2
  87. data/spec/support/approvals.rb +1 -1
  88. data/spec/support/shared_context.rb +2 -1
  89. data/tasks/pact.rake +21 -1
  90. metadata +82 -7
  91. data/lib/pact_broker/client/versions/record_deployment.rb +0 -109
  92. data/lib/pact_broker/client/versions/record_undeployment.rb +0 -102
  93. data/spec/lib/pact_broker/client/versions/record_deployment_spec.rb +0 -82
@@ -0,0 +1,219 @@
1
+ require 'pact_broker/client/deployments/record_undeployment'
2
+
3
+ module PactBroker
4
+ module Client
5
+ module Deployments
6
+ describe RecordUndeployment do
7
+ let(:params) do
8
+ {
9
+ pacticipant_name: "Foo",
10
+ target: target,
11
+ environment_name: "test"
12
+ }
13
+ end
14
+ let(:target) { "customer-1" }
15
+ let(:output) { "text" }
16
+ let(:options) { { output: output, verbose: true } }
17
+ let(:pact_broker_base_url) { "http://broker" }
18
+ let(:pact_broker_client_options) { { pact_broker_base_url: pact_broker_base_url } }
19
+
20
+ let(:index_body_hash) do
21
+ {
22
+ _links: {
23
+ :'pb:environments' => {
24
+ href: environments_url
25
+ },
26
+ :'pb:pacticipant' => {
27
+ href: pacticipant_url
28
+ }
29
+ }
30
+ }
31
+ end
32
+
33
+ let(:environments_hash) do
34
+ {
35
+ _links: {
36
+ :'pb:environments' => [
37
+ {
38
+ name: "test",
39
+ href: test_environment_url
40
+ }
41
+ ]
42
+ }
43
+ }
44
+ end
45
+
46
+ let(:environment_hash) do
47
+ {
48
+ _links: {
49
+ :'pb:currently-deployed-versions' => {
50
+ href: currently_deployed_versions_url
51
+ }
52
+ }
53
+ }
54
+ end
55
+
56
+ let(:deployed_versions_hash) do
57
+ {
58
+ _embedded: {
59
+ deployedVersions: [
60
+ {
61
+ target: "customer-1",
62
+ _links: {
63
+ self: {
64
+ href: deployed_version_url_1
65
+ }
66
+ }
67
+ },
68
+ {
69
+ target: returned_target_2,
70
+ _links: {
71
+ self: {
72
+ href: deployed_version_url_2
73
+ }
74
+ }
75
+ }
76
+ ]
77
+ }
78
+ }
79
+ end
80
+
81
+ let(:returned_target_2) { nil }
82
+ let(:deployed_version_hash) do
83
+ {
84
+ _embedded: {
85
+ version: {
86
+ number: "2"
87
+ }
88
+ }
89
+ }
90
+ end
91
+
92
+ let(:environments_url) { "#{webmock_base_url}/environments" }
93
+ let(:test_environment_url) { "#{webmock_base_url}/environments/1234" }
94
+ let(:currently_deployed_versions_url) { "#{webmock_base_url}/currently-deployed-versions" }
95
+ let(:deployed_version_url_1) { "#{webmock_base_url}/deployed-version-1" }
96
+ let(:deployed_version_url_2) { "#{webmock_base_url}/deployed-version-2" }
97
+ let(:pacticipant_url) { "#{webmock_base_url}/pacticipant" }
98
+
99
+ let(:webmock_base_url) { "http://broker" }
100
+
101
+ let!(:index_request) do
102
+ stub_request(:get, "http://broker").to_return(status: 200, body: index_body_hash.to_json, headers: { "Content-Type" => "application/hal+json" } )
103
+ end
104
+
105
+ let!(:environments_request) do
106
+ stub_request(:get, "http://broker/environments").to_return(status: 200, body: environments_hash.to_json, headers: { "Content-Type" => "application/hal+json" } )
107
+ end
108
+
109
+ let!(:environment_request) do
110
+ stub_request(:get, test_environment_url).to_return(status: 200, body: environment_hash.to_json, headers: { "Content-Type" => "application/hal+json" } )
111
+ end
112
+
113
+ let!(:deployed_versions_request) do
114
+ stub_request(:get, currently_deployed_versions_url + "?pacticipant=Foo").to_return(status: 200, body: deployed_versions_hash.to_json, headers: { "Content-Type" => "application/hal+json" } )
115
+ end
116
+
117
+ let!(:deployed_version_patch_request_1) do
118
+ stub_request(:patch, deployed_version_url_1).with(body: { currentlyDeployed: false}.to_json).to_return(status: 200, body: deployed_version_hash.to_json, headers: { "Content-Type" => "application/hal+json" })
119
+ end
120
+
121
+ let!(:deployed_version_patch_request_2) do
122
+ stub_request(:patch, deployed_version_url_2).with(body: { currentlyDeployed: false}.to_json).to_return(status: 200, body: deployed_version_hash.to_json, headers: { "Content-Type" => "application/hal+json" })
123
+ end
124
+
125
+ let!(:pacticipant_request) do
126
+ stub_request(:get, pacticipant_url).to_return(status: pacticipant_request_status, body: {}.to_json, headers: { "Content-Type" => "application/hal+json" })
127
+ end
128
+
129
+ let(:pacticipant_request_status) { 200 }
130
+
131
+ subject { RecordUndeployment.call(params, options, pact_broker_client_options) }
132
+
133
+ its(:success) { is_expected.to eq true }
134
+ its(:message) { is_expected.to include "Recorded undeployment of Foo version 2 from test environment (target customer-1) in the Pact Broker" }
135
+
136
+ context "when there is no pb:environments relation in the index" do
137
+ let(:index_body_hash) do
138
+ {
139
+ _links: {}
140
+ }
141
+ end
142
+
143
+ its(:success) { is_expected.to be false }
144
+ its(:message) { is_expected.to include "support" }
145
+ end
146
+
147
+ context "when output is json" do
148
+ let(:output) { "json" }
149
+ its(:message) { is_expected.to eq [deployed_version_hash].to_json }
150
+ end
151
+
152
+ context "when there is an error returned from one of the deployed version updates (there should only ever be one deployed version, but testing just for the sake of it)" do
153
+ let!(:deployed_version_patch_request_2) do
154
+ stub_request(:patch, deployed_version_url_2).to_return(status: 400, body: { errors: { foo: ["some error"]}}.to_json, headers: { "Content-Type" => "application/hal+json" })
155
+ end
156
+
157
+ let(:returned_target_2) { "customer-1" }
158
+
159
+ its(:success) { is_expected.to be false }
160
+ its(:message) { is_expected.to include "Recorded undeployment of Foo version 2" }
161
+ its(:message) { is_expected.to include "some error" }
162
+ end
163
+
164
+ context "when there is no currently-deployed-versions relation in the environment resource" do
165
+ let(:environment_hash) do
166
+ {
167
+ _links: {}
168
+ }
169
+ end
170
+
171
+ its(:success) { is_expected.to be false }
172
+ its(:message) { is_expected.to include "support" }
173
+ end
174
+
175
+ context "when a target is provided and there is no deployed version with a matching target" do
176
+ let(:target) { "wrong" }
177
+ let(:expected_message) { "Foo is not currently deployed to target 'wrong' in test environment. Please specify one of the following targets to record the undeployment from: customer-1, <no target>" }
178
+
179
+ its(:success) { is_expected.to be false }
180
+ its(:message) { is_expected.to include expected_message }
181
+
182
+ context "when output is json" do
183
+ let(:output) { "json" }
184
+
185
+ its(:message) { is_expected.to eq({ error: { message: expected_message } }.to_json) }
186
+ end
187
+ end
188
+
189
+ context "when a target is not provided and there is no deployed verison without a target" do
190
+ let(:target) { nil }
191
+ let(:returned_target_2) { "customer-2" }
192
+
193
+ its(:success) { is_expected.to be false }
194
+ its(:message) { is_expected.to include "Please specify one of the following targets to record the undeployment from: customer-1, customer-2" }
195
+ end
196
+
197
+ context "when there are no deployed versions for the pacticipant" do
198
+ let(:deployed_versions_hash) do
199
+ {
200
+ _embedded: {
201
+ deployedVersions: []
202
+ }
203
+ }
204
+ end
205
+
206
+ its(:success) { is_expected.to be false }
207
+ its(:message) { is_expected.to include "Foo is not currently deployed to test environment. Cannot record undeployment." }
208
+
209
+ context "when the pacticipant does not exist" do
210
+ let(:pacticipant_request_status) { 404 }
211
+
212
+ its(:success) { is_expected.to be false }
213
+ its(:message) { is_expected.to include "No pacticipant with name 'Foo' found" }
214
+ end
215
+ end
216
+ end
217
+ end
218
+ end
219
+ end
@@ -0,0 +1,120 @@
1
+ require 'pact_broker/client/environments/delete_environment'
2
+
3
+ module PactBroker
4
+ module Client
5
+ module Environments
6
+ describe DeleteEnvironment do
7
+ before do
8
+ allow_any_instance_of(PactBroker::Client::Hal::HttpClient).to receive(:sleep)
9
+ allow_any_instance_of(PactBroker::Client::Hal::HttpClient).to receive(:default_max_tries).and_return(1)
10
+ end
11
+
12
+ let(:params) do
13
+ {
14
+ uuid: uuid,
15
+ name: "new name",
16
+ displayName: "new display name",
17
+ production: false,
18
+ output: output
19
+ }
20
+ end
21
+ let(:options) do
22
+ {
23
+ output: output,
24
+ verbose: verbose
25
+ }
26
+ end
27
+ let(:uuid) { "a9aa4c22-66bb-45d3-ba4c-4916ac8b48c5" }
28
+ let(:pact_broker_base_url) { "http://example.org" }
29
+ let(:pact_broker_client_options) { { pact_broker_base_url: pact_broker_base_url } }
30
+ let(:response_headers) { { "Content-Type" => "application/hal+json"} }
31
+ let(:output) { "text" }
32
+ let(:verbose) { false }
33
+
34
+ before do
35
+ stub_request(:get, "http://example.org/").to_return(status: 200, body: index_response_body, headers: response_headers)
36
+ stub_request(:get, "http://example.org/environments/#{uuid}").to_return(status: get_environment_response_status, body: get_environment_response_body, headers: response_headers)
37
+ stub_request(:delete, "http://example.org/environments/#{uuid}").to_return(status: delete_response_status, body: delete_environment_response_body, headers: response_headers)
38
+ end
39
+ let(:delete_response_status) { 200 }
40
+ let(:get_environment_response_status) { 200 }
41
+
42
+ let(:index_response_body) do
43
+ {
44
+ "_links" => {
45
+ "pb:environments" => {},
46
+ "pb:environment" => {
47
+ "href" => "http://example.org/environments/{uuid}"
48
+ }
49
+ }
50
+ }.to_json
51
+ end
52
+
53
+ let(:get_environment_response_body) do
54
+ {
55
+ name: "existing name",
56
+ displayName: "existing display name",
57
+ production: true
58
+ }.to_json
59
+ end
60
+
61
+ let(:delete_environment_response_body) do
62
+ JSON.parse(get_environment_response_body).merge("updatedAt" => "2021-05-28T13:34:54+10:00").to_json
63
+ end
64
+
65
+ subject { DeleteEnvironment.call(params, options, pact_broker_client_options) }
66
+
67
+ context "when delete is successful" do
68
+ its(:success) { is_expected.to be true }
69
+ its(:message) { is_expected.to include "Deleted environment existing name from the Pact Broker" }
70
+
71
+ context "when output is json" do
72
+ let(:output) { "json" }
73
+
74
+ its(:message) { is_expected.to eq delete_environment_response_body }
75
+ end
76
+ end
77
+
78
+ context "when environments are not supported" do
79
+ let(:index_response_body) { "{}" }
80
+
81
+ its(:success) { is_expected.to be false }
82
+ its(:message) { is_expected.to include "does not support environments" }
83
+ end
84
+
85
+ context "when the environment does not exist" do
86
+ let(:get_environment_response_status) { 404 }
87
+ let(:get_environment_response_body) { "" }
88
+ let(:delete_environment_response_body) { "" }
89
+
90
+ its(:success) { is_expected.to be false }
91
+ its(:message) { is_expected.to include get_environment_response_body }
92
+
93
+ context "when output is json" do
94
+ let(:output) { "json" }
95
+
96
+ its(:message) { is_expected.to eq "{}" }
97
+ end
98
+ end
99
+
100
+ context "when delete is unsuccessful" do
101
+ let(:delete_response_status) { 500 }
102
+ let(:delete_environment_response_body) do
103
+ {
104
+ "some" => "error"
105
+ }.to_json
106
+ end
107
+
108
+ its(:success) { is_expected.to be false }
109
+ its(:message) { is_expected.to include delete_environment_response_body }
110
+
111
+ context "when output is json" do
112
+ let(:output) { "json" }
113
+
114
+ its(:message) { is_expected.to eq delete_environment_response_body }
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,89 @@
1
+ require 'pact_broker/client/environments/describe_environment'
2
+
3
+ module PactBroker
4
+ module Client
5
+ module Environments
6
+ describe DescribeEnvironment do
7
+ before do
8
+ allow_any_instance_of(PactBroker::Client::Hal::HttpClient).to receive(:sleep)
9
+ allow_any_instance_of(PactBroker::Client::Hal::HttpClient).to receive(:default_max_tries).and_return(1)
10
+ end
11
+
12
+ let(:params) { { uuid: uuid } }
13
+ let(:options) { { output: output }}
14
+ let(:uuid) { "a9aa4c22-66bb-45d3-ba4c-4916ac8b48c5" }
15
+ let(:pact_broker_base_url) { "http://example.org" }
16
+ let(:pact_broker_client_options) { { pact_broker_base_url: pact_broker_base_url } }
17
+ let(:response_headers) { { "Content-Type" => "application/hal+json"} }
18
+ let(:output) { "text" }
19
+
20
+ before do
21
+ stub_request(:get, "http://example.org/").to_return(status: 200, body: index_response_body, headers: response_headers)
22
+ stub_request(:get, "http://example.org/environments/#{uuid}").to_return(status: get_environment_response_status, body: get_environment_response_body, headers: response_headers)
23
+ end
24
+
25
+ let(:get_environment_response_status) { 200 }
26
+ let(:index_response_body) do
27
+ {
28
+ "_links" => {
29
+ "pb:environments" => {},
30
+ "pb:environment" => {
31
+ "href" => "http://example.org/environments/{uuid}"
32
+ }
33
+ }
34
+ }.to_json
35
+ end
36
+
37
+ let(:get_environment_response_body) do
38
+ {
39
+ name: "existing name",
40
+ displayName: "existing display name",
41
+ production: true,
42
+ contacts: [
43
+ name: "Someone",
44
+ details: { emailAddress: "foo@bar.com" }
45
+ ]
46
+ }.to_json
47
+ end
48
+
49
+ subject { DescribeEnvironment.call(params, options, pact_broker_client_options) }
50
+
51
+ context "when the environment exists" do
52
+ its(:success) { is_expected.to be true }
53
+
54
+ it "describes the environment" do
55
+ Approvals.verify(subject.message, :name => "describe_environment", format: :txt)
56
+ end
57
+
58
+ context "when output is json" do
59
+ let(:output) { "json" }
60
+
61
+ its(:message) { is_expected.to eq get_environment_response_body }
62
+ end
63
+ end
64
+
65
+ context "when environments are not supported" do
66
+ let(:index_response_body) { "{}" }
67
+
68
+ its(:success) { is_expected.to be false }
69
+ its(:message) { is_expected.to include "does not support environments" }
70
+ end
71
+
72
+ context "when the environment does not exist" do
73
+ let(:get_environment_response_status) { 404 }
74
+ let(:get_environment_response_body) { "" }
75
+ let(:put_environment_response_body) { "" }
76
+
77
+ its(:success) { is_expected.to be false }
78
+ its(:message) { is_expected.to include get_environment_response_body }
79
+
80
+ context "when output is json" do
81
+ let(:output) { "json" }
82
+
83
+ its(:message) { is_expected.to eq "{}" }
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,167 @@
1
+ require 'pact_broker/client/environments/update_environment'
2
+
3
+ module PactBroker
4
+ module Client
5
+ module Environments
6
+ describe UpdateEnvironment do
7
+ before do
8
+ allow_any_instance_of(PactBroker::Client::Hal::HttpClient).to receive(:sleep)
9
+ allow_any_instance_of(PactBroker::Client::Hal::HttpClient).to receive(:default_max_tries).and_return(1)
10
+ end
11
+
12
+ let(:params) do
13
+ {
14
+ uuid: uuid,
15
+ name: "new name",
16
+ display_name: "new display name",
17
+ production: false
18
+ }
19
+ end
20
+ let(:options) do
21
+ {
22
+ output: output,
23
+ verbose: verbose
24
+ }
25
+ end
26
+ let(:uuid) { "a9aa4c22-66bb-45d3-ba4c-4916ac8b48c5" }
27
+ let(:pact_broker_base_url) { "http://example.org" }
28
+ let(:pact_broker_client_options) { { pact_broker_base_url: pact_broker_base_url } }
29
+ let(:response_headers) { { "Content-Type" => "application/hal+json"} }
30
+ let(:output) { "text" }
31
+ let(:verbose) { false }
32
+
33
+ before do
34
+ stub_request(:get, "http://example.org/").to_return(status: 200, body: index_response_body, headers: response_headers)
35
+ stub_request(:get, "http://example.org/environments/#{uuid}").to_return(status: get_environment_response_status, body: get_environment_response_body, headers: response_headers)
36
+ stub_request(:put, "http://example.org/environments/#{uuid}").to_return(status: put_response_status, body: put_environment_response_body, headers: response_headers)
37
+ end
38
+ let(:put_response_status) { 200 }
39
+ let(:get_environment_response_status) { 200 }
40
+ let(:put_request_body) do
41
+ {
42
+ name: "new name",
43
+ displayName: "new display name",
44
+ production: false
45
+ }.to_json
46
+ end
47
+
48
+ let(:index_response_body) do
49
+ {
50
+ "_links" => {
51
+ "pb:environments" => {},
52
+ "pb:environment" => {
53
+ "href" => "http://example.org/environments/{uuid}"
54
+ }
55
+ }
56
+ }.to_json
57
+ end
58
+
59
+ let(:get_environment_response_body) do
60
+ {
61
+ name: "existing name",
62
+ displayName: "existing display name",
63
+ production: true
64
+ }.to_json
65
+ end
66
+
67
+ let(:put_environment_response_body) do
68
+ JSON.parse(get_environment_response_body).merge("updatedAt" => "2021-05-28T13:34:54+10:00").to_json
69
+ end
70
+
71
+ subject { UpdateEnvironment.call(params, options, pact_broker_client_options) }
72
+
73
+ it "updates the environment" do
74
+ request = stub_request(:put, "http://example.org/environments/#{uuid}").with(body: put_request_body)
75
+ subject
76
+ expect(request).to have_been_made
77
+ end
78
+
79
+ context "when update is successful" do
80
+ its(:success) { is_expected.to be true }
81
+ its(:message) { is_expected.to include "Updated new name environment in the Pact Broker" }
82
+
83
+ context "when output is json" do
84
+ let(:output) { "json" }
85
+
86
+ its(:message) { is_expected.to eq put_environment_response_body }
87
+ end
88
+ end
89
+
90
+ context "when environments are not supported" do
91
+ let(:index_response_body) { "{}" }
92
+
93
+ its(:success) { is_expected.to be false }
94
+ its(:message) { is_expected.to include "does not support environments" }
95
+ end
96
+
97
+ context "when a StandardError occurs" do
98
+ before do
99
+ allow_any_instance_of(described_class).to receive(:do_call).and_raise(StandardError.new("Foo"))
100
+ end
101
+
102
+ its(:success) { is_expected.to be false }
103
+ its(:message) { is_expected.to include "StandardError - Foo" }
104
+
105
+ context "when verbose is on" do
106
+ let(:verbose) { true }
107
+
108
+ it "includes the message and class and backtrace in the error" do
109
+ expect(subject.message.split("\n").size).to be > 2
110
+ end
111
+ end
112
+
113
+ context "when output is json" do
114
+ let(:output) { "json" }
115
+
116
+ it "includes the message and class in the error" do
117
+ message_hash = JSON.parse(subject.message)
118
+ expect(message_hash).to eq "error" => { "message" => "Foo", "class" => "StandardError" }
119
+ end
120
+
121
+ context "when verbose is on" do
122
+ let(:verbose) { true }
123
+
124
+ it "includes the message and class and backtrace in the error" do
125
+ message_hash = JSON.parse(subject.message)
126
+ expect(message_hash["error"]["backtrace"]).to be_a(Array)
127
+ end
128
+ end
129
+ end
130
+ end
131
+
132
+ context "when the environment does not exist" do
133
+ let(:get_environment_response_status) { 404 }
134
+ let(:get_environment_response_body) { "" }
135
+ let(:put_environment_response_body) { "" }
136
+
137
+ its(:success) { is_expected.to be false }
138
+ its(:message) { is_expected.to include get_environment_response_body }
139
+
140
+ context "when output is json" do
141
+ let(:output) { "json" }
142
+
143
+ its(:message) { is_expected.to eq "{}" }
144
+ end
145
+ end
146
+
147
+ context "when update is unsuccessful" do
148
+ let(:put_response_status) { 400 }
149
+ let(:put_environment_response_body) do
150
+ {
151
+ "some" => "error"
152
+ }.to_json
153
+ end
154
+
155
+ its(:success) { is_expected.to be false }
156
+ its(:message) { is_expected.to include put_environment_response_body }
157
+
158
+ context "when output is json" do
159
+ let(:output) { "json" }
160
+
161
+ its(:message) { is_expected.to eq put_environment_response_body }
162
+ end
163
+ end
164
+ end
165
+ end
166
+ end
167
+ end