|
| 1 | +require 'pact_broker/api/resources/tagged_pact_versions' |
| 2 | + |
| 3 | +module PactBroker |
| 4 | + module Api |
| 5 | + module Resources |
| 6 | + describe TaggedPactVersions do |
| 7 | + include_context "stubbed services" |
| 8 | + |
| 9 | + before do |
| 10 | + allow(pacticipant_service).to receive(:find_pacticipant_by_name).with("Foo").and_return(consumer) |
| 11 | + allow(pacticipant_service).to receive(:find_pacticipant_by_name).with("Bar").and_return(provider) |
| 12 | + end |
| 13 | + |
| 14 | + let(:path) { "/pacts/provider/Bar/consumer/Foo/tag/prod" } |
| 15 | + let(:consumer) { double('Bar') } |
| 16 | + let(:provider) { double('Foo') } |
| 17 | + |
| 18 | + context "GET" do |
| 19 | + before do |
| 20 | + allow(PactBroker::Api::Decorators::TaggedPactVersionsDecorator).to receive(:new).and_return(decorator) |
| 21 | + allow(pact_service).to receive(:find_all_pact_versions_between).and_return(pact_versions) |
| 22 | + end |
| 23 | + |
| 24 | + let(:decorator) { instance_double(PactBroker::Api::Decorators::TaggedPactVersionsDecorator, to_json: 'json') } |
| 25 | + let(:pact_versions) { double('pacts') } |
| 26 | + |
| 27 | + subject { get(path) } |
| 28 | + |
| 29 | + let(:user_options) do |
| 30 | + { |
| 31 | + base_url: "http://example.org", |
| 32 | + resource_url: "http://example.org/pacts/provider/Bar/consumer/Foo/tag/prod", |
| 33 | + consumer_name: "Foo", |
| 34 | + provider_name: "Bar", |
| 35 | + tag: "prod" |
| 36 | + } |
| 37 | + end |
| 38 | + |
| 39 | + it "finds all the pacts with the given consumer/provider/tag" do |
| 40 | + expect(pact_service).to receive(:find_all_pact_versions_between).with("Foo", and: "Bar", tag: "prod") |
| 41 | + subject |
| 42 | + end |
| 43 | + |
| 44 | + it "returns a 200 OK hal+json response" do |
| 45 | + expect(subject).to be_a_hal_json_success_response |
| 46 | + end |
| 47 | + |
| 48 | + it "creates a JSON representation of the pact versions" do |
| 49 | + expect(PactBroker::Api::Decorators::TaggedPactVersionsDecorator).to receive(:new).with(pact_versions) |
| 50 | + expect(decorator).to receive(:to_json).with(user_options: hash_including(user_options)) |
| 51 | + subject |
| 52 | + end |
| 53 | + |
| 54 | + it "returns the JSON representation of the pact versions" do |
| 55 | + expect(subject.body).to eq 'json' |
| 56 | + end |
| 57 | + |
| 58 | + context "with the consumer or provider do not exist" do |
| 59 | + let(:consumer) { nil } |
| 60 | + |
| 61 | + it "returns a 404" do |
| 62 | + expect(subject).to be_a_404_response |
| 63 | + end |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + context "DELETE" do |
| 68 | + before do |
| 69 | + allow(pact_service).to receive(:delete_all_pact_versions_between) |
| 70 | + end |
| 71 | + |
| 72 | + subject { delete(path) } |
| 73 | + |
| 74 | + it "deletes all the pacts with the given consumer/provider/tag" do |
| 75 | + expect(pact_service).to receive(:delete_all_pact_versions_between).with("Foo", and: "Bar", tag: "prod") |
| 76 | + subject |
| 77 | + end |
| 78 | + |
| 79 | + it "returns a 204" do |
| 80 | + expect(subject.status).to eq 204 |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | +end |
0 commit comments