Skip to content

Commit

Permalink
fix: Update openapi_first and use it's coverage thing (#783)
Browse files Browse the repository at this point in the history
* Update openapi_first and use it's coverage thing

This removes skip_oas_request_validation, because the app wrapped by OpenapiFirst::Test.app does not raise errors, but just tracks requests/responses to collect coverage data.

* Add test about responding with application/problem+json

Fix API coverage issue. This results in removal of this test output:
  • Loading branch information
ahx authored Feb 20, 2025
1 parent bddfd2d commit b3da850
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 75 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ group :test do
gem "approvals", ">=0.0.24", "<1.0.0"
gem "tzinfo", "~>2.0"
gem "faraday-retry", "~>2.0"
gem "openapi_first", "~>0.20"
gem "openapi_first", ">= 2.3", "< 3"
end

group :pg, optional: true do
Expand Down
10 changes: 9 additions & 1 deletion spec/features/publish_pact_all_in_one_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
request_body_hash.delete(:pacticipantName)
end

it "returns a validation error response", skip_oas_request_validation: true do
it "returns a validation error response" do
expect(subject).to be_a_json_error_response("missing")
end
end
Expand All @@ -46,6 +46,14 @@
let(:contract) { "{\"key\": \"ABCDEFG\x8FDEF\" }" }

its(:status) { is_expected.to eq 400 }
its(:media_type) { is_expected.to eq "application/hal+json" }

context "when client accepts application/problem+json" do
let(:rack_headers) { { "CONTENT_TYPE" => "application/json", "HTTP_ACCEPT" => "application/problem+json" } }

its(:status) { is_expected.to eq 400 }
its(:media_type) { is_expected.to eq "application/problem+json" }
end
end

context "with a conflicting pact" do
Expand Down
16 changes: 16 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@

I18n.config.enforce_available_locales = false

require "openapi_first"
OpenapiFirst::Test.setup do |test|
test.register("pact_broker_oas.yaml")
end

if ENV["OAS_COVERAGE_CHECK_ENABLED"] == "true"
at_exit do
oas_coverage = OpenapiFirst::Test::Coverage.result.coverage
OpenapiFirst::Test.report_coverage
if oas_coverage < 100
puts "Exiting with status 2 (failure), because API coverage was #{oas_coverage}% instead of 100%!"
exit 2
end
end
end

RSpec.configure do | config |
config.before :each do
PactBroker.reset_configuration
Expand Down
35 changes: 0 additions & 35 deletions spec/support/openapi_first/pact_broker_coverage.rb

This file was deleted.

45 changes: 7 additions & 38 deletions spec/support/shared_context_for_app.rb
Original file line number Diff line number Diff line change
@@ -1,51 +1,20 @@
OAS_COVERAGE_CHECK_ENABLED = ENV["OAS_COVERAGE_CHECK_ENABLED"] == "true"

# fairly crappy OAS coverage check
if OAS_COVERAGE_CHECK_ENABLED
require "openapi_first"
require "openapi_first/coverage"
require "support/openapi_first/pact_broker_coverage"

endpoints_to_be_called = OpenapiFirst::PactBrokerCoverage.build_endpoints_list(OpenapiFirst.load("pact_broker_oas.yaml"))

RSpec.configure do | config |
config.after(:suite) do
if endpoints_to_be_called.any?
raise "Missing coverage of #{endpoints_to_be_called.join("\n")}"
end
end
end
end


RSpec.shared_context "app" do
let(:app) do | example |
require "openapi_first"
require "pact_broker/api"
require "pact_broker/application_context"
require "rack/pact_broker/application_context"
application_context = PactBroker::ApplicationContext.default_application_context
builder = Rack::Builder.new

# If the feature spec should be used to validate the OAS, add the metadata `validate_oas: true` to the top level spec
if example.metadata[:validate_oas]
# To test the validation responses, a deliberately invalid request must be sent.
# To stop the middleware raising that as an error,
# add the metadata `skip_oas_request_validation: true` to the individual spec.
unless example.metadata[:skip_oas_request_validation]
builder.use OpenapiFirst::RequestValidation, spec: "pact_broker_oas.yaml", raise_error: true
end

builder.use OpenapiFirst::ResponseValidation, spec: "pact_broker_oas.yaml", raise_error: true
end

if OAS_COVERAGE_CHECK_ENABLED
builder.use OpenapiFirst::PactBrokerCoverage, endpoints_to_be_called
end

builder.use(PactBroker::Middleware::MockPuma)
builder.use(Rack::PactBroker::ApplicationContext, application_context)
builder.run(PactBroker.build_api(application_context))
builder.to_app

# If the feature spec should be used to validate the OAS, add the metadata `validate_oas: true` to the top level spec
if example.metadata[:validate_oas]
OpenapiFirst::Test.app(builder.to_app)
else
builder.to_app
end
end
end

0 comments on commit b3da850

Please sign in to comment.