-
-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add deployed version resource, supporting marking deployed vers…
…ion as undeployed
- Loading branch information
Showing
15 changed files
with
292 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
require 'pact_broker/api/resources/base_resource' | ||
require 'pact_broker/api/decorators/deployed_version_decorator' | ||
require 'pact_broker/messages' | ||
|
||
module PactBroker | ||
module Api | ||
module Resources | ||
class DeployedVersion < BaseResource | ||
include PactBroker::Messages | ||
|
||
def initialize | ||
super | ||
@currently_deployed_param = params(default: {})[:currentlyDeployed] | ||
end | ||
|
||
def content_types_provided | ||
[ | ||
["application/hal+json", :to_json] | ||
] | ||
end | ||
|
||
def content_types_accepted | ||
[ | ||
["application/merge-patch+json", :from_merge_patch_json] | ||
] | ||
end | ||
|
||
def allowed_methods | ||
["GET", "PATCH", "OPTIONS"] | ||
end | ||
|
||
def resource_exists? | ||
!!deployed_version | ||
end | ||
|
||
def malformed_request? | ||
if request.patch? | ||
return invalid_json? | ||
else | ||
false | ||
end | ||
end | ||
|
||
def to_json | ||
decorator_class(:deployed_version_decorator).new(deployed_version).to_json(decorator_options) | ||
end | ||
|
||
def from_merge_patch_json | ||
if request.patch? | ||
if resource_exists? | ||
process_currently_deployed_param | ||
else | ||
404 | ||
end | ||
else | ||
415 | ||
end | ||
end | ||
|
||
def policy_name | ||
:'versions::version' | ||
end | ||
|
||
def policy_record | ||
deployed_version&.version | ||
end | ||
|
||
private | ||
|
||
attr_reader :currently_deployed_param | ||
|
||
def process_currently_deployed_param | ||
if currently_deployed_param == false | ||
@deployed_version = deployed_version_service.record_version_undeployed(deployed_version) | ||
response.body = to_json | ||
elsif currently_deployed_param == true | ||
set_json_validation_error_messages(currentlyDeployed: [message("errors.validation.cannot_set_currently_deployed_true")]) | ||
422 | ||
else | ||
response.body = to_json | ||
end | ||
end | ||
|
||
def deployed_version | ||
@deployed_version ||= deployed_version_service.find_by_uuid(uuid) | ||
end | ||
|
||
def uuid | ||
identifier_from_path[:uuid] | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
spec/features/get_currently_deployed_versions_for_environment_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
RSpec.describe "Get currently deployed versions for environment" do | ||
let!(:version) { td.create_consumer("Foo").create_consumer_version("1").and_return(:consumer_version) } | ||
let!(:test_environment) { td.create_environment("test").and_return(:environment) } | ||
let!(:prod_environment) { td.create_environment("prod").and_return(:environment) } | ||
let!(:deployed_version) do | ||
td.create_deployed_version_for_consumer_version(environment_name: "test", target: "customer-1", created_at: DateTime.now - 2) | ||
.create_deployed_version_for_consumer_version(environment_name: "prod", created_at: DateTime.now - 1) | ||
.create_provider("Bar") | ||
.create_provider_version("4") | ||
.create_deployed_version_for_provider_version(environment_name: "test", target: "customer-1") | ||
.create_provider_version("5") | ||
.create_deployed_version_for_provider_version(environment_name: "test", target: "customer-2") | ||
|
||
end | ||
|
||
let(:path) do | ||
PactBroker::Api::PactBrokerUrls.currently_deployed_versions_for_environment_url( | ||
test_environment | ||
) | ||
end | ||
|
||
let(:response_body_hash) { JSON.parse(subject.body, symbolize_names: true) } | ||
|
||
subject { get(path, nil, { "HTTP_ACCEPT" => "application/hal+json" }) } | ||
|
||
it "returns a list of deployed versions" do | ||
expect(response_body_hash[:_embedded][:deployedVersions]).to be_a(Array) | ||
expect(response_body_hash[:_embedded][:deployedVersions].size).to eq 3 | ||
expect(response_body_hash[:_links][:self][:title]).to eq "Currently deployed versions for Test" | ||
expect(response_body_hash[:_links][:self][:href]).to end_with(path) | ||
end | ||
|
||
context "with query params" do | ||
subject { get(path, { pacticipant: "Bar", target: "customer-1" }, { "HTTP_ACCEPT" => "application/hal+json" }) } | ||
|
||
it "returns a list of matching deployed versions" do | ||
expect(response_body_hash[:_embedded][:deployedVersions].size).to eq 1 | ||
expect(response_body_hash[:_embedded][:deployedVersions].first[:_embedded][:version][:number]).to eq "4" | ||
end | ||
end | ||
end |
Oops, something went wrong.