diff --git a/lib/pact_broker/api/resources/pacticipant.rb b/lib/pact_broker/api/resources/pacticipant.rb index eb955047d..e958b1558 100644 --- a/lib/pact_broker/api/resources/pacticipant.rb +++ b/lib/pact_broker/api/resources/pacticipant.rb @@ -32,9 +32,9 @@ def known_methods def from_json if pacticipant - @pacticipant = pacticipant_service.update params.merge(name: pacticipant_name) + @pacticipant = pacticipant_service.update params_with_string_keys.merge('name' => pacticipant_name) else - @pacticipant = pacticipant_service.create params.merge(name: pacticipant_name) + @pacticipant = pacticipant_service.create params_with_string_keys.merge('name' => pacticipant_name) response.headers["Location"] = pacticipant_url(base_url, pacticipant) end response.body = to_json @@ -62,9 +62,7 @@ def pacticipant def pacticipant_name identifier_from_path[:name] end - end end - end end diff --git a/lib/pact_broker/api/resources/pacticipants.rb b/lib/pact_broker/api/resources/pacticipants.rb index 5ea131bef..f89017693 100644 --- a/lib/pact_broker/api/resources/pacticipants.rb +++ b/lib/pact_broker/api/resources/pacticipants.rb @@ -32,7 +32,7 @@ def post_is_create? end def from_json - created_model = pacticipant_service.create params + created_model = pacticipant_service.create params_with_string_keys response.body = decorator_for(created_model).to_json(user_options: decorator_context) end @@ -55,9 +55,7 @@ def decorator_for model def new_model @new_model ||= decorator_for(PactBroker::Domain::Pacticipant.new).from_json(request.body.to_s) end - end end - end end diff --git a/lib/pact_broker/pacticipants/service.rb b/lib/pact_broker/pacticipants/service.rb index 974998ec0..62b1600b8 100644 --- a/lib/pact_broker/pacticipants/service.rb +++ b/lib/pact_broker/pacticipants/service.rb @@ -70,13 +70,17 @@ def self.find_relationships end def self.update params - pacticipant = pacticipant_repository.find_by_name(params.fetch(:name)) - pacticipant.update(params) - pacticipant_repository.find_by_name(params.fetch(:name)) + pacticipant = pacticipant_repository.find_by_name(params.fetch('name')) + PactBroker::Api::Decorators::PacticipantDecorator.new(pacticipant).from_hash(params) + pacticipant.save + pacticipant_repository.find_by_name(params.fetch('name')) end def self.create params - pacticipant_repository.create(params) + pacticipant = PactBroker::Domain::Pacticipant.new + PactBroker::Api::Decorators::PacticipantDecorator.new(pacticipant).from_hash(params) + pacticipant.save + pacticipant end def self.delete name diff --git a/spec/features/create_pacticipant_spec.rb b/spec/features/create_pacticipant_spec.rb index 0ea4d67e1..f3d191047 100644 --- a/spec/features/create_pacticipant_spec.rb +++ b/spec/features/create_pacticipant_spec.rb @@ -4,7 +4,7 @@ let(:path) { "/pacticipants" } let(:headers) { {'CONTENT_TYPE' => 'application/json'} } let(:response_body) { JSON.parse(last_response.body, symbolize_names: true)} - let(:pacticipant_hash) { {name: 'Foo Thing'}} + let(:pacticipant_hash) { { name: 'Foo Thing' } } subject { post path, pacticipant_hash.to_json, headers; last_response } diff --git a/spec/features/update_pacticipant_spec.rb b/spec/features/update_pacticipant_spec.rb index 3a8e5080e..361623011 100644 --- a/spec/features/update_pacticipant_spec.rb +++ b/spec/features/update_pacticipant_spec.rb @@ -1,25 +1,23 @@ -xdescribe "Publishing a pact" do +describe "Publishing a pact" do - let(:request_body) { load_fixture('update_pacticipant.json') } + let(:request_body) { {'repositoryUrl' => 'http://foo'} } let(:path) { "/pacticipants/Some%20Consumer" } let(:response_body_json) { JSON.parse(subject.body) } - subject { patch path, request_body, {'CONTENT_TYPE' => 'application/json-patch+json' }; last_response } + subject { patch path, request_body.to_json, {'CONTENT_TYPE' => 'application/json' }; last_response } context "when the pacticipant exists" do + + before do + TestDataBuilder.new.create_pacticipant("Some Consumer") + end it "returns a 200 OK" do - expect(subject.status).to be 201 + puts subject.body unless subject.status == 200 + expect(subject.status).to be 200 end it "returns a json body with the updated pacticipant" do - expect(subject.headers['Content-Type']).to eq "application/json" - end - - end - - context "when the pacticipant does not exist" do - it "returns a 404" do - expect(subject.status).to be 404 + expect(subject.headers['Content-Type']).to eq "application/hal+json;charset=utf-8" end end end diff --git a/spec/lib/pact_broker/api/resources/pacticipant_spec.rb b/spec/lib/pact_broker/api/resources/pacticipant_spec.rb index 4539c010c..40cf5da9d 100644 --- a/spec/lib/pact_broker/api/resources/pacticipant_spec.rb +++ b/spec/lib/pact_broker/api/resources/pacticipant_spec.rb @@ -64,8 +64,6 @@ module Resources end end end - end end - end diff --git a/spec/lib/pact_broker/api/resources/pacticipants_spec.rb b/spec/lib/pact_broker/api/resources/pacticipants_spec.rb index 01bf3d964..98ea5013c 100644 --- a/spec/lib/pact_broker/api/resources/pacticipants_spec.rb +++ b/spec/lib/pact_broker/api/resources/pacticipants_spec.rb @@ -47,7 +47,7 @@ module Resources context "with valid JSON" do it "creates the pacticipant" do - expect(PactBroker::Pacticipants::Service).to receive(:create).with(params) + expect(PactBroker::Pacticipants::Service).to receive(:create).with('name' => 'New Consumer') subject end diff --git a/spec/lib/pact_broker/pacticipants/service_spec.rb b/spec/lib/pact_broker/pacticipants/service_spec.rb index 736f2871c..1a1962ddb 100644 --- a/spec/lib/pact_broker/pacticipants/service_spec.rb +++ b/spec/lib/pact_broker/pacticipants/service_spec.rb @@ -10,6 +10,22 @@ module Pacticipants subject{ Service } + let(:td) { TestDataBuilder.new } + + describe ".update" do + before do + td.create_pacticipant("Foo") + end + + let(:params) { { 'name' => 'Foo', 'repositoryUrl' => 'http://foo' } } + + subject { Service.update(params) } + + it "updates the repositoryUrl" do + expect(subject.repository_url).to eq 'http://foo' + end + end + describe ".messages_for_potential_duplicate_pacticipants" do let(:base_url) { 'http://example.org' } @@ -126,7 +142,6 @@ module Pacticipants it "returns a list of relationships" do expect(subject.find_relationships).to eq([PactBroker::Domain::Relationship.create(consumer, provider, pact, verification, webhooks)]) end - end describe "delete" do