-
-
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.
* chore: wip/adding a new labels resource to list all labels * chore: wip/added pagination to labels response and refactored the response * chore: missed a file * chore: updated the spec * chore: updated the decorator and wrote a spec for repository * chore: added labels decorator spec * fix: rubocop failure * fix: fixed the failing test * fix: fixed the wrong syntax * test: fixed another failing test * chore: added test for service layer of Label service * chore: added spacing * chore: added spec for paginated response
- Loading branch information
Showing
11 changed files
with
217 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require_relative "base_decorator" | ||
require_relative "pagination_links" | ||
require_relative "label_decorator" | ||
require "pact_broker/domain/label" | ||
|
||
module PactBroker | ||
module Api | ||
module Decorators | ||
class LabelsDecorator < BaseDecorator | ||
collection :entries, :as => :labels, :class => PactBroker::Domain::Label, :extend => PactBroker::Api::Decorators::LabelDecorator, embedded: true | ||
|
||
include PaginationLinks | ||
|
||
link :self do | options | | ||
{ | ||
title: "Labels", | ||
href: options.fetch(:resource_url) | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require "pact_broker/api/resources/base_resource" | ||
require "pact_broker/api/decorators/labels_decorator" | ||
require "pact_broker/api/resources/pagination_methods" | ||
|
||
module PactBroker | ||
module Api | ||
module Resources | ||
class Labels < BaseResource | ||
include PaginationMethods | ||
|
||
def content_types_provided | ||
[["application/hal+json", :to_json]] | ||
end | ||
|
||
def allowed_methods | ||
["GET", "OPTIONS"] | ||
end | ||
|
||
def policy_name | ||
:'labels::labels' | ||
end | ||
|
||
def to_json | ||
decorator_class(:labels_decorator).new(labels).to_json( | ||
**decorator_options( | ||
hide_label_decorator_links: true, | ||
) | ||
) | ||
end | ||
|
||
def labels | ||
label_service.get_all_unique_labels(pagination_options) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
describe "Get labels" do | ||
|
||
let(:path) { "/labels" } | ||
let(:response_body_hash) { JSON.parse(subject.body, symbolize_names: true) } | ||
|
||
subject { get(path) } | ||
|
||
context "when labels exists" do | ||
before do | ||
td.create_pacticipant("foo") | ||
.create_label("ios") | ||
.create_label("consumer") | ||
.create_pacticipant("bar") | ||
.create_label("ios") | ||
.create_label("consumer") | ||
end | ||
|
||
it "returns a 200 OK" do | ||
expect(subject).to be_a_hal_json_success_response | ||
end | ||
|
||
it "returns the labels in the body" do | ||
expect(response_body_hash[:_embedded][:labels].map { |label| label[:name] }).to contain_exactly("ios", "consumer") | ||
end | ||
|
||
context "with pagination options" do | ||
subject { get(path, { "size" => "1", "page" => "1" }) } | ||
|
||
it "only returns the number of items specified in the page" do | ||
expect(response_body_hash[:_embedded][:labels].size).to eq 1 | ||
end | ||
|
||
it_behaves_like "a paginated response" | ||
end | ||
end | ||
end |
43 changes: 43 additions & 0 deletions
43
spec/lib/pact_broker/api/decorators/labels_decorator_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,43 @@ | ||
require "pact_broker/api/decorators/labels_decorator" | ||
require "pact_broker/labels/service" | ||
|
||
module PactBroker | ||
module Api | ||
module Decorators | ||
|
||
describe LabelsDecorator do | ||
before do | ||
td.create_consumer("Foo") | ||
.create_label("consumer") | ||
.create_consumer("Bar") | ||
.create_label("provider") | ||
.create_consumer("Wiffle") | ||
.create_label("provider") | ||
end | ||
|
||
let(:label) do | ||
PactBroker::Labels::Service.get_all_unique_labels | ||
end | ||
|
||
let(:options) { { user_options: { resource_url: "http://example.org/labels", hide_label_decorator_links: true } } } | ||
subject { JSON.parse LabelsDecorator.new(label).to_json(options), symbolize_names: true } | ||
|
||
it "includes the label names" do | ||
expect(subject[:_embedded][:labels].map { |label| label[:name] }).to contain_exactly("provider", "consumer") | ||
end | ||
|
||
it "includes the resource url" do | ||
expect(subject[:_links][:self][:href]).to eq "http://example.org/labels" | ||
end | ||
|
||
it "labels field doest not include any links" do | ||
expect(subject[:_embedded][:labels][0][:_links]).to be_nil | ||
end | ||
|
||
it "doest not include createdAt" do | ||
expect(subject[:_embedded][:labels][0][:createdAt]).to be_nil | ||
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