Skip to content

Commit

Permalink
feat(pacticipant): expose repositoryUrl in resource
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Oct 31, 2017
1 parent 6da6e02 commit 8f0f16a
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 28 deletions.
6 changes: 2 additions & 4 deletions lib/pact_broker/api/resources/pacticipant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -62,9 +62,7 @@ def pacticipant
def pacticipant_name
identifier_from_path[:name]
end

end
end

end
end
4 changes: 1 addition & 3 deletions lib/pact_broker/api/resources/pacticipants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
12 changes: 8 additions & 4 deletions lib/pact_broker/pacticipants/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/features/create_pacticipant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
22 changes: 10 additions & 12 deletions spec/features/update_pacticipant_spec.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 0 additions & 2 deletions spec/lib/pact_broker/api/resources/pacticipant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ module Resources
end
end
end

end
end

end
2 changes: 1 addition & 1 deletion spec/lib/pact_broker/api/resources/pacticipants_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 16 additions & 1 deletion spec/lib/pact_broker/pacticipants/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8f0f16a

Please sign in to comment.