pact_broker 2.81.0 → 2.82.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (143) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +1 -1
  3. data/CHANGELOG.md +30 -0
  4. data/DEVELOPER_SETUP.md +1 -1
  5. data/README.md +7 -5
  6. data/config.ru +3 -28
  7. data/db/migrations/20210722_add_index_to_triggered_webhooks_webhook_uuid.rb +7 -0
  8. data/db/migrations/20210810_set_allow_contract_modification.rb +17 -0
  9. data/docs/CONFIGURATION.md +398 -0
  10. data/docs/configuration.yml +320 -0
  11. data/example/Gemfile +4 -4
  12. data/example/README.md +15 -22
  13. data/example/config.ru +1 -24
  14. data/example/config/pact_broker.yml +9 -0
  15. data/example/config/puma.rb +3 -0
  16. data/lib/db.rb +1 -1
  17. data/lib/pact_broker/api/authorization/resource_access_policy.rb +68 -0
  18. data/lib/pact_broker/api/authorization/resource_access_rules.rb +40 -0
  19. data/lib/pact_broker/api/decorators/deployed_version_decorator.rb +2 -0
  20. data/lib/pact_broker/api/decorators/released_version_decorator.rb +2 -0
  21. data/lib/pact_broker/api/middleware/basic_auth.rb +63 -0
  22. data/lib/pact_broker/api/resources/pact.rb +15 -6
  23. data/lib/pact_broker/api/resources/tag.rb +1 -14
  24. data/lib/pact_broker/app.rb +52 -30
  25. data/lib/pact_broker/config/basic_auth_configuration.rb +38 -0
  26. data/lib/pact_broker/config/load.rb +21 -10
  27. data/lib/pact_broker/config/runtime_configuration.rb +188 -0
  28. data/lib/pact_broker/config/runtime_configuration_coercion_methods.rb +41 -0
  29. data/lib/pact_broker/config/runtime_configuration_database_methods.rb +119 -0
  30. data/lib/pact_broker/config/runtime_configuration_logging_methods.rb +61 -0
  31. data/lib/pact_broker/configuration.rb +67 -131
  32. data/lib/pact_broker/contracts/notice.rb +4 -0
  33. data/lib/pact_broker/contracts/service.rb +4 -4
  34. data/lib/pact_broker/db/models.rb +3 -0
  35. data/lib/pact_broker/db/validate_encoding.rb +0 -4
  36. data/lib/pact_broker/deployments/deployed_version.rb +8 -2
  37. data/lib/pact_broker/deployments/deployed_version_service.rb +13 -6
  38. data/lib/pact_broker/deployments/environment.rb +1 -1
  39. data/lib/pact_broker/deployments/released_version.rb +8 -0
  40. data/lib/pact_broker/deployments/released_version_service.rb +12 -0
  41. data/lib/pact_broker/doc/views/provider-pacts-for-verification.markdown +4 -0
  42. data/lib/pact_broker/domain/pacticipant.rb +17 -13
  43. data/lib/pact_broker/domain/verification.rb +4 -22
  44. data/lib/pact_broker/domain/version.rb +9 -5
  45. data/lib/pact_broker/domain/webhook.rb +4 -0
  46. data/lib/pact_broker/error.rb +1 -0
  47. data/lib/pact_broker/errors.rb +1 -1
  48. data/lib/pact_broker/feature_toggle.rb +3 -5
  49. data/lib/pact_broker/hash_refinements.rb +0 -1
  50. data/lib/pact_broker/index/service.rb +4 -6
  51. data/lib/pact_broker/initializers/database_connection.rb +80 -0
  52. data/lib/pact_broker/integrations/integration.rb +5 -0
  53. data/lib/pact_broker/integrations/service.rb +4 -2
  54. data/lib/pact_broker/locale/en.yml +1 -0
  55. data/lib/pact_broker/logging.rb +2 -1
  56. data/lib/pact_broker/matrix/integration.rb +1 -1
  57. data/lib/pact_broker/matrix/parse_can_i_deploy_query.rb +2 -2
  58. data/lib/pact_broker/matrix/quick_row.rb +10 -0
  59. data/lib/pact_broker/matrix/repository.rb +64 -3
  60. data/lib/pact_broker/metrics/service.rb +16 -13
  61. data/lib/pact_broker/pacticipants/repository.rb +4 -0
  62. data/lib/pact_broker/pacticipants/service.rb +9 -1
  63. data/lib/pact_broker/pacts/pact_publication.rb +10 -13
  64. data/lib/pact_broker/pacts/pact_publication_dataset_module.rb +6 -1
  65. data/lib/pact_broker/pacts/pact_publication_selector_dataset_module.rb +1 -2
  66. data/lib/pact_broker/pacts/pact_version.rb +25 -11
  67. data/lib/pact_broker/pacts/pacts_for_verification_repository.rb +54 -77
  68. data/lib/pact_broker/pacts/selected_pact.rb +1 -1
  69. data/lib/pact_broker/pacts/selector.rb +15 -2
  70. data/lib/pact_broker/pacts/selectors.rb +4 -0
  71. data/lib/pact_broker/pacts/service.rb +4 -0
  72. data/lib/pact_broker/repositories/scopes.rb +12 -1
  73. data/lib/pact_broker/string_refinements.rb +6 -0
  74. data/lib/pact_broker/tags/service.rb +8 -1
  75. data/lib/pact_broker/test/http_test_data_builder.rb +11 -5
  76. data/lib/pact_broker/ui/views/index/_css_and_js.haml +11 -9
  77. data/lib/pact_broker/ui/views/index/_pagination.haml +3 -1
  78. data/lib/pact_broker/ui/views/layouts/main.haml +5 -3
  79. data/lib/pact_broker/ui/views/matrix/show.haml +10 -8
  80. data/lib/pact_broker/verifications/required_verification.rb +28 -0
  81. data/lib/pact_broker/verifications/service.rb +49 -1
  82. data/lib/pact_broker/version.rb +1 -1
  83. data/lib/pact_broker/versions/repository.rb +15 -0
  84. data/lib/pact_broker/versions/service.rb +32 -2
  85. data/lib/pact_broker/webhooks/event_listener.rb +3 -0
  86. data/lib/pact_broker/webhooks/trigger_service.rb +30 -14
  87. data/lib/pact_broker/webhooks/triggered_webhook.rb +1 -0
  88. data/lib/pact_broker/webhooks/webhook.rb +2 -2
  89. data/lib/pact_broker/webhooks/webhook_event.rb +6 -1
  90. data/lib/semantic_logger/formatters/short.rb +29 -0
  91. data/pact_broker.gemspec +1 -0
  92. data/script/data/auto-create-things-for-tags.rb +19 -0
  93. data/script/data/contract-published-requiring-verification.rb +27 -0
  94. data/script/{reproduce-issue-expand-currently-deployed.rb → data/expand-currently-deployed.rb} +0 -0
  95. data/script/docs/generate-configuration-docs.rb +86 -0
  96. data/spec/features/get_latest_pact_badge_spec.rb +1 -0
  97. data/spec/features/get_matrix_badge_spec.rb +1 -0
  98. data/spec/features/publish_pact_spec.rb +21 -7
  99. data/spec/features/wip_pacts_spec.rb +1 -1
  100. data/spec/fixtures/approvals/matrix_integration_environment_spec.approved.json +62 -0
  101. data/spec/fixtures/approvals/matrix_integration_ignore_spec.approved.json +124 -0
  102. data/spec/fixtures/approvals/matrix_integration_spec.approved.json +173 -0
  103. data/spec/fixtures/approvals/publish_contract_no_branch.approved.json +9 -9
  104. data/spec/fixtures/approvals/publish_contract_nothing_exists.approved.json +7 -7
  105. data/spec/fixtures/approvals/publish_contract_nothing_exists_with_webhook.approved.json +5 -5
  106. data/spec/fixtures/approvals/publish_contract_verification_already_exists.approved.json +5 -5
  107. data/spec/lib/pact_broker/api/middleware/basic_auth_spec.rb +312 -0
  108. data/spec/lib/pact_broker/api/resources/tag_spec.rb +14 -39
  109. data/spec/lib/pact_broker/app_basic_auth_spec.rb +122 -0
  110. data/spec/lib/pact_broker/config/load_spec.rb +33 -6
  111. data/spec/lib/pact_broker/config/runtime_configuration_logging_methods_spec.rb +22 -0
  112. data/spec/lib/pact_broker/config/runtime_configuration_spec.rb +71 -0
  113. data/spec/lib/pact_broker/configuration_spec.rb +51 -25
  114. data/spec/lib/pact_broker/errors/error_logger_spec.rb +3 -0
  115. data/spec/lib/pact_broker/feature_toggle_spec.rb +18 -19
  116. data/spec/lib/pact_broker/matrix/integration_environment_spec.rb +12 -0
  117. data/spec/lib/pact_broker/matrix/integration_ignore_spec.rb +15 -3
  118. data/spec/lib/pact_broker/matrix/integration_spec.rb +47 -6
  119. data/spec/lib/pact_broker/matrix/parse_can_i_deploy_query_spec.rb +16 -1
  120. data/spec/lib/pact_broker/matrix/repository_dependency_spec.rb +0 -2
  121. data/spec/lib/pact_broker/matrix/repository_spec.rb +0 -2
  122. data/spec/lib/pact_broker/metrics/service_spec.rb +44 -0
  123. data/spec/lib/pact_broker/pacticipants/service_spec.rb +28 -5
  124. data/spec/lib/pact_broker/pacts/pact_publication_selector_dataset_module_spec.rb +25 -0
  125. data/spec/lib/pact_broker/pacts/pact_version_spec.rb +30 -1
  126. data/spec/lib/pact_broker/pacts/repository_find_for_verification_spec.rb +107 -20
  127. data/spec/lib/pact_broker/pacts/verifiable_pact_messages_spec.rb +1 -1
  128. data/spec/lib/pact_broker/tags/service_spec.rb +24 -8
  129. data/spec/lib/pact_broker/verifications/service_spec.rb +146 -0
  130. data/spec/lib/pact_broker/versions/repository_spec.rb +38 -2
  131. data/spec/lib/pact_broker/versions/service_spec.rb +93 -2
  132. data/spec/lib/pact_broker/webhooks/trigger_service_spec.rb +54 -2
  133. data/spec/lib/rack/pact_broker/invalid_uri_protection_spec.rb +3 -3
  134. data/spec/spec_helper.rb +2 -1
  135. data/spec/support/approvals.rb +29 -0
  136. metadata +52 -13
  137. data/example/basic_auth/Gemfile +0 -5
  138. data/example/basic_auth/Procfile +0 -1
  139. data/example/basic_auth/README.md +0 -43
  140. data/example/basic_auth/config.ru +0 -19
  141. data/example/example_data.sql +0 -19
  142. data/spec/lib/pact_broker/config/save_and_load_spec.rb +0 -25
  143. data/spec/lib/pact_broker/pacts/service_find_for_verification_spec.rb +0 -50
@@ -21,7 +21,8 @@ module PactBroker
21
21
  subject { parsed_options }
22
22
 
23
23
  its([:latestby]) { is_expected.to eq "cvp" }
24
- its([:latest]) { is_expected.to eq true }
24
+ its([:latest]) { is_expected.to eq nil }
25
+ its([:environment_name]) { is_expected.to eq "prod" }
25
26
  its([:ignore_selectors]) { is_expected.to eq [] }
26
27
 
27
28
  context "with pacticipants to ignore" do
@@ -36,6 +37,20 @@ module PactBroker
36
37
  ]
37
38
  end
38
39
  end
40
+
41
+ context "with a tag" do
42
+ let(:params) do
43
+ {
44
+ pacticipant: "foo",
45
+ version: "1",
46
+ to: "prod"
47
+ }
48
+ end
49
+
50
+ its([:latestby]) { is_expected.to eq "cvp" }
51
+ its([:latest]) { is_expected.to eq true }
52
+ its([:tag]) { is_expected.to eq "prod" }
53
+ end
39
54
  end
40
55
 
41
56
  describe "parsed_selectors" do
@@ -3,8 +3,6 @@ require "pact_broker/matrix/repository"
3
3
  module PactBroker
4
4
  module Matrix
5
5
  describe Repository do
6
- let(:td) { TestDataBuilder.new}
7
-
8
6
  def build_selectors(hash)
9
7
  hash.collect do | key, value |
10
8
  UnresolvedSelector.new(pacticipant_name: key, pacticipant_version_number: value)
@@ -4,8 +4,6 @@ require "pact_broker/matrix/unresolved_selector"
4
4
  module PactBroker
5
5
  module Matrix
6
6
  describe Repository do
7
- let(:td) { TestDataBuilder.new}
8
-
9
7
  def build_selectors(hash)
10
8
  hash.collect do | key, value |
11
9
  UnresolvedSelector.new(pacticipant_name: key, pacticipant_version_number: value)
@@ -6,6 +6,50 @@ module PactBroker
6
6
  describe "#metrics" do
7
7
  subject { Service.metrics }
8
8
 
9
+ describe "pacticipants" do
10
+ before do
11
+ td.create_consumer("Foo")
12
+ .create_consumer("Bar", main_branch: "foo")
13
+ end
14
+
15
+ its([:pacticipants, :count]) { is_expected.to eq 2 }
16
+ its([:pacticipants, :withMainBranchSetCount]) { is_expected.to eq 1 }
17
+ end
18
+
19
+ describe "pacticipant versions" do
20
+ before do
21
+ td.create_consumer("Foo")
22
+ .create_consumer_version("2")
23
+ .create_consumer_version("3", branch: "main")
24
+ end
25
+
26
+ its([:pacticipantVersions, :count]) { is_expected.to eq 2 }
27
+ its([:pacticipantVersions, :withBranchSetCount]) { is_expected.to eq 1 }
28
+ end
29
+
30
+ describe "environments, deployed versions, released versions" do
31
+ before do
32
+ td.create_environment("test")
33
+ .create_consumer("Foo")
34
+ .create_consumer_version("2")
35
+ .create_deployed_version_for_consumer_version(currently_deployed: false)
36
+ .create_consumer_version("3")
37
+ .create_deployed_version_for_consumer_version
38
+ .create_consumer_version("4")
39
+ .create_released_version_for_consumer_version(currently_supported: false)
40
+ .create_consumer_version("5")
41
+ .create_released_version_for_consumer_version
42
+ .create_consumer_version("6")
43
+ .create_released_version_for_consumer_version
44
+ end
45
+
46
+ its([:environments, :count]) { is_expected.to eq 1 }
47
+ its([:deployedVersions, :count]) { is_expected.to eq 2 }
48
+ its([:deployedVersions, :currentlyDeployedCount]) { is_expected.to eq 1 }
49
+ its([:releasedVersions, :count]) { is_expected.to eq 3 }
50
+ its([:releasedVersions, :currentlySupportedCount]) { is_expected.to eq 2 }
51
+ end
52
+
9
53
  describe "interactions latestCount" do
10
54
  before do
11
55
  td.create_consumer
@@ -10,10 +10,9 @@ module PactBroker
10
10
  allow(Service).to receive(:logger).and_return(logger)
11
11
  end
12
12
 
13
- let(:td) { TestDataBuilder.new }
14
13
  let(:logger) { double("logger").as_null_object }
15
14
 
16
- subject{ Service }
15
+ subject { Service }
17
16
 
18
17
  describe ".messages_for_potential_duplicate_pacticipants" do
19
18
 
@@ -100,10 +99,35 @@ module PactBroker
100
99
  end
101
100
  end
102
101
 
102
+ describe ".maybe_set_main_branch" do
103
+ before do
104
+ allow(PactBroker.configuration).to receive(:auto_detect_main_branch).and_return(true)
105
+ allow(PactBroker.configuration).to receive(:main_branch_candidates).and_return(["foo", "bar"])
106
+ td.create_pacticipant("Foo", main_branch: main_branch)
107
+ end
108
+
109
+ let(:main_branch) { nil }
110
+
111
+ subject { Service.maybe_set_main_branch(td.find_pacticipant("Foo"), "bar") }
112
+
113
+ context "when the main branch is nil and auto_detect_main_branch=true and the potential branch is in the list of candidate main branch names" do
114
+ it "sets the main branch" do
115
+ expect(subject.main_branch).to eq "bar"
116
+ end
117
+ end
118
+
119
+ context "when the branch is already set" do
120
+ let(:main_branch) { "main" }
121
+
122
+ it "does not overwrite it" do
123
+ expect(subject.main_branch).to eq "main"
124
+ end
125
+ end
126
+ end
127
+
103
128
  describe "delete" do
104
129
  before do
105
- TestDataBuilder.new
106
- .create_consumer("Consumer")
130
+ td.create_consumer("Consumer")
107
131
  .create_label("finance")
108
132
  .create_consumer_version("2.3.4")
109
133
  .create_provider("Provider")
@@ -186,7 +210,6 @@ module PactBroker
186
210
  it "deletes the verifications" do
187
211
  expect{ delete_provider }.to change{ PactBroker::Domain::Verification.count }.by(-1)
188
212
  end
189
-
190
213
  end
191
214
  end
192
215
  end
@@ -7,6 +7,31 @@ module PactBroker
7
7
 
8
8
  subject { PactPublication.for_provider_and_consumer_version_selector(provider, consumer_version_selector).all }
9
9
 
10
+ context "all_for_tag" do
11
+ before do
12
+ td.create_provider("Bar")
13
+ .create_consumer("Foo")
14
+ .create_consumer_version("1", tag_name: "prod")
15
+ .create_pact
16
+ .create_consumer_version("2", tag_name: "not-prod")
17
+ .create_pact
18
+ .create_consumer_version("3", tag_name: "prod")
19
+ .create_pact
20
+ .create_provider("NotBar")
21
+ .create_pact
22
+ end
23
+
24
+ let(:provider) { td.find_pacticipant("Bar") }
25
+ let(:consumer_version_selector) { Selector.all_for_tag("prod") }
26
+
27
+ it "returns all the pacts for the specified consumer version tag" do
28
+ expect(subject.size).to eq 2
29
+ expect(subject.first.values[:tag_name]).to eq "prod"
30
+ expect(subject.last.values[:tag_name]).to eq "prod"
31
+ end
32
+
33
+ end
34
+
10
35
  context "for main branch" do
11
36
  let(:consumer_version_selector) { Selector.for_main_branch }
12
37
  let(:provider) { td.find_pacticipant("Bar") }
@@ -3,7 +3,6 @@ require "pact_broker/pacts/pact_version"
3
3
  module PactBroker
4
4
  module Pacts
5
5
  describe PactVersion do
6
-
7
6
  describe "pacticipant names" do
8
7
  subject(:pact_version) do
9
8
  td.create_consumer("consumer")
@@ -36,6 +35,36 @@ module PactBroker
36
35
  end
37
36
  end
38
37
 
38
+ describe "#latest_verification" do
39
+ before do
40
+ td.create_pact_with_verification("Foo", "1", "Bar", "2")
41
+ .create_verification(provider_version: "3", number: 2)
42
+ .create_pact_with_verification("NotFoo", "1", "Bar", "4")
43
+ .create_verification(provider_version: "5", number: 3)
44
+ .create_pact_with_verification("NotFoo2", "1", "NotBar", "4")
45
+ .create_verification(provider_version: "6", number: 5)
46
+ .create_pact_with_verification("NotFoo3", "2", "NotBar", "5")
47
+ .create_verification(provider_version: "7", number: 6)
48
+ end
49
+
50
+ context "lazy loading" do
51
+ it "lazy loads" do
52
+ expect(PactPublication.order(:id).all.first.pact_version.latest_verification.provider_version_number).to eq "3"
53
+ expect(PactPublication.order(:id).all.last.pact_version.latest_verification.provider_version_number).to eq "7"
54
+ end
55
+ end
56
+
57
+ context "eager loading" do
58
+ let(:pact_version_1) { PactVersion.eager(:latest_verification).order(:id).all.first }
59
+ let(:pact_version_2) { PactVersion.eager(:latest_verification).order(:id).all.last }
60
+
61
+ it "eager loads" do
62
+ expect(pact_version_1.associations[:latest_verification].provider_version_number).to eq "3"
63
+ expect(pact_version_2.associations[:latest_verification].provider_version_number).to eq "7"
64
+ end
65
+ end
66
+ end
67
+
39
68
  describe "latest_consumer_version" do
40
69
  before do
41
70
  td.create_consumer("consumer")
@@ -15,26 +15,112 @@ module PactBroker
15
15
  subject { Repository.new.find_for_verification("Bar", consumer_version_selectors) }
16
16
 
17
17
  context "when there are no selectors" do
18
- before do
19
- td.create_pact_with_hierarchy("Foo", "foo-latest-prod-version", "Bar")
20
- .create_consumer_version_tag("prod")
21
- .create_consumer_version("not-latest-dev-version", tag_names: ["dev"])
22
- .comment("next pact not selected")
23
- .create_pact
24
- .create_consumer_version("foo-latest-dev-version", tag_names: ["dev"])
25
- .create_pact
26
- .create_consumer("Baz")
27
- .create_consumer_version("baz-latest-dev-version", tag_names: ["dev"])
28
- .create_pact
29
- end
18
+
19
+ let(:foo_main_branch) { nil }
30
20
 
31
21
  let(:consumer_version_selectors) { Selectors.new }
32
22
 
33
- it "returns the latest pact for each consumer" do
34
- expect(subject.size).to eq 2
35
- expect(find_by_consumer_name_and_consumer_version_number("Foo", "foo-latest-dev-version")).to_not be nil
36
- expect(find_by_consumer_name_and_consumer_version_number("Baz", "baz-latest-dev-version")).to_not be nil
37
- expect(subject.all?(&:overall_latest?)).to be true
23
+ context "when there is no main branch version" do
24
+ before do
25
+ td.create_consumer("Foo")
26
+ .create_pact_with_hierarchy("Foo", "foo-latest-prod-version", "Bar")
27
+ .create_consumer_version_tag("prod")
28
+ .create_consumer_version("not-latest-dev-version", tag_names: ["dev"])
29
+ .comment("next pact not selected")
30
+ .create_pact
31
+ .create_consumer_version("foo-latest-dev-version", tag_names: ["dev"])
32
+ .create_pact
33
+ .create_consumer("Baz")
34
+ .create_consumer_version("baz-latest-dev-version", tag_names: ["dev"])
35
+ .create_pact
36
+ end
37
+
38
+ it "returns the latest pact for each consumer" do
39
+ expect(subject.size).to eq 2
40
+ expect(find_by_consumer_name_and_consumer_version_number("Foo", "foo-latest-dev-version")).to_not be nil
41
+ expect(find_by_consumer_name_and_consumer_version_number("Baz", "baz-latest-dev-version")).to_not be nil
42
+ expect(subject.all?(&:overall_latest?)).to be true
43
+ end
44
+ end
45
+
46
+ context "when there is a version from the main branch" do
47
+ before do
48
+ td.create_consumer("Foo", main_branch: "main")
49
+ .create_consumer_version("1", branch: "main")
50
+ .create_provider("Bar")
51
+ .create_pact
52
+ .create_pact_with_hierarchy("Foo", "2", "Bar")
53
+ end
54
+
55
+ it "returns the latest version from the main branch" do
56
+ expect(subject.size).to eq 1
57
+ expect(find_by_consumer_name_and_consumer_version_number("Foo", "1")).to_not be_nil
58
+ expect(subject.first.selectors.first).to be_latest_for_main_branch
59
+ end
60
+ end
61
+
62
+ context "when there is a version with a tag with the name of the main branch" do
63
+ before do
64
+ td.create_consumer("Foo", main_branch: "main")
65
+ .create_consumer_version("1", tag_name: "main")
66
+ .create_provider("Bar")
67
+ .create_pact
68
+ .create_pact_with_hierarchy("Foo", "2", "Bar")
69
+ end
70
+
71
+ it "returns the latest version from the main branch" do
72
+ expect(subject.size).to eq 1
73
+ expect(find_by_consumer_name_and_consumer_version_number("Foo", "1")).to_not be_nil
74
+ expect(subject.first.selectors.first).to be_latest_for_tag
75
+ expect(subject.first.selectors.first.tag).to eq "main"
76
+ end
77
+ end
78
+
79
+ context "when there is a not version from the main branch" do
80
+ before do
81
+ td.create_pact_with_hierarchy("Foo", "1", "Bar")
82
+ .create_pact_with_hierarchy("Foo", "2", "Bar")
83
+ end
84
+
85
+ it "returns the latest version from the main branch" do
86
+ expect(subject.size).to eq 1
87
+ expect(find_by_consumer_name_and_consumer_version_number("Foo", "2")).to_not be_nil
88
+ expect(subject.first.selectors.first).to be_overall_latest
89
+ end
90
+ end
91
+
92
+ context "when there are currently deployed versons" do
93
+ before do
94
+ td.create_environment("test")
95
+ .create_pact_with_hierarchy("Foo", "1", "Bar")
96
+ .create_deployed_version_for_consumer_version(currently_deployed: false)
97
+ .create_pact_with_hierarchy("Foo", "2", "Bar")
98
+ .create_deployed_version_for_consumer_version
99
+ .create_pact_with_hierarchy("Foo", "3", "Bar")
100
+ end
101
+
102
+ it "returns the currently deployed pacts" do
103
+ expect(find_by_consumer_name_and_consumer_version_number("Foo", "1")).to be_nil
104
+ expect(find_by_consumer_name_and_consumer_version_number("Foo", "2")).to_not be_nil
105
+ expect(find_by_consumer_name_and_consumer_version_number("Foo", "2").selectors.first).to be_currently_deployed
106
+ end
107
+ end
108
+
109
+ context "when there are currently released+supported versions" do
110
+ before do
111
+ td.create_environment("test")
112
+ .create_pact_with_hierarchy("Foo", "1", "Bar")
113
+ .create_released_version_for_consumer_version(currently_supported: false)
114
+ .create_pact_with_hierarchy("Foo", "2", "Bar")
115
+ .create_released_version_for_consumer_version
116
+ .create_pact_with_hierarchy("Foo", "3", "Bar")
117
+ end
118
+
119
+ it "returns the currently deployed pacts" do
120
+ expect(find_by_consumer_name_and_consumer_version_number("Foo", "1")).to be_nil
121
+ expect(find_by_consumer_name_and_consumer_version_number("Foo", "2")).to_not be_nil
122
+ expect(find_by_consumer_name_and_consumer_version_number("Foo", "2").selectors.first).to be_currently_supported
123
+ end
38
124
  end
39
125
  end
40
126
 
@@ -159,9 +245,10 @@ module PactBroker
159
245
  expect(find_by_consumer_version_number("foo-latest-prod-version").selectors).to eq [Selector.all_for_tag_and_consumer("prod", "Foo").resolve(PactBroker::Domain::Version.for("Foo", "foo-latest-prod-version"))]
160
246
  end
161
247
 
162
- it "uses the latest consumer verison number as the resolved version when the same pact content is selected multiple times" do
248
+ it "includes all the selectors when the same pact content is selected multiple times (used to just use the latest, not sure about this)" do
163
249
  expect(find_by_consumer_version_number("3")).to be nil
164
- expect(find_by_consumer_version_number("4").selectors).to eq [Selector.all_for_tag_and_consumer("prod", "Foo").resolve(PactBroker::Domain::Version.for("Foo", "4"))]
250
+ expect(find_by_consumer_version_number("4").selectors.first).to eq Selector.all_for_tag_and_consumer("prod", "Foo").resolve(PactBroker::Domain::Version.for("Foo", "3"))
251
+ expect(find_by_consumer_version_number("4").selectors.last).to eq Selector.all_for_tag_and_consumer("prod", "Foo").resolve(PactBroker::Domain::Version.for("Foo", "4"))
165
252
  end
166
253
  end
167
254
  end
@@ -266,7 +353,7 @@ module PactBroker
266
353
  end
267
354
 
268
355
  it "does not set the tag name" do
269
- expect(find_by_consumer_version_number("foo-latest-dev-version").selectors).to eq [ResolvedSelector.new({ latest: true }, PactBroker::Domain::Version.find(number: "foo-latest-dev-version"))]
356
+ expect(find_by_consumer_version_number("foo-latest-dev-version").selectors).to eq [ResolvedSelector.new({ latest: true, consumer: "Foo" }, PactBroker::Domain::Version.find(number: "foo-latest-dev-version"))]
270
357
  expect(find_by_consumer_version_number("foo-latest-dev-version").overall_latest?).to be true
271
358
  end
272
359
  end
@@ -26,7 +26,7 @@ module PactBroker
26
26
  provider_branch: provider_branch
27
27
  )
28
28
  end
29
- let(:consumer_version) { double("version", number: "1234" )}
29
+ let(:consumer_version) { double("version", number: "1234", order: 1) }
30
30
  let(:environment) { instance_double("PactBroker::Deployments::Environment", name: "test") }
31
31
  let(:test_environment) { environment }
32
32
  let(:prod_environment) { instance_double("PactBroker::Deployments::Environment", name: "prod") }
@@ -4,33 +4,49 @@ require "pact_broker/tags/service"
4
4
  module PactBroker
5
5
  module Tags
6
6
  describe Service do
7
-
7
+ before do
8
+ allow(Service).to receive(:version_service).and_return(version_service)
9
+ allow(Service).to receive(:pacticipant_service).and_return(pacticipant_service)
10
+ allow(version_service).to receive(:maybe_set_version_branch_from_tag)
11
+ allow(pacticipant_service).to receive(:maybe_set_main_branch)
12
+ end
13
+ let(:pacticipant_service) { class_double("PactBroker::Pacticipants::Service").as_stubbed_const }
14
+ let(:version_service) { class_double("PactBroker::Versions::Service").as_stubbed_const }
15
+ let(:tag_reposistory) { instance_double("PactBroker::Tags::Repository", create: double("tag")) }
8
16
  let(:pacticipant_name) { "test_pacticipant" }
9
17
  let(:version_number) { "1.2.3" }
10
18
  let(:tag_name) { "prod" }
11
-
12
- let(:options) { {pacticipant_name: pacticipant_name, pacticipant_version_number: version_number, tag_name: tag_name}}
13
- let(:test_data_builder) { TestDataBuilder.new }
19
+ let(:options) { { pacticipant_name: pacticipant_name, pacticipant_version_number: version_number, tag_name: tag_name }}
20
+ let(:use_first_tag_as_branch) { false }
14
21
 
15
22
  describe ".create" do
16
-
17
23
  subject { Service.create(options) }
18
24
 
19
- # Naughty integration test... didn't seem much point unit testing this
20
-
21
25
  it "creates the new tag" do
22
26
  expect(subject.name).to eq tag_name
23
27
  expect(subject.version.number).to eq version_number
24
28
  expect(subject.version.pacticipant.name).to eq pacticipant_name
25
29
  end
26
30
 
31
+ it "calls the version_service.maybe_set_version_branch_from_tag before creating the tag" do
32
+ # so that we use the version before the tag is created (we have to detect if there are no tags present)
33
+ allow(Service).to receive(:tag_repository).and_return(tag_reposistory)
34
+ expect(version_service).to receive(:maybe_set_version_branch_from_tag).with(instance_of(PactBroker::Domain::Version), tag_name).ordered
35
+ expect(tag_reposistory).to receive(:create).ordered
36
+ subject
37
+ end
38
+
39
+ it "calls the pacticipant_service.maybe_set_main_branch" do
40
+ expect(pacticipant_service).to receive(:maybe_set_main_branch).with(instance_of(PactBroker::Domain::Pacticipant), tag_name)
41
+ subject
42
+ end
27
43
  end
28
44
 
29
45
  describe "delete" do
30
46
 
31
47
  let(:second_pacticipant_name) { "second_test_pacticipant" }
32
48
  let(:second_version_number) { "4.5.6" }
33
- let(:second_options_same_tag_name) { {pacticipant_name: second_pacticipant_name, pacticipant_version_number: second_version_number, tag_name: tag_name}}
49
+ let(:second_options_same_tag_name) { { pacticipant_name: second_pacticipant_name, pacticipant_version_number: second_version_number, tag_name: tag_name }}
34
50
 
35
51
  before do
36
52
  Service.create(options)