Skip to content

Add test coverage for PermissionsPolicy error and empty branches - #58880

Open
hammadxcm wants to merge 1 commit into
rails:mainfrom
hammadxcm:test-permissions-policy-branch-coverage
Open

hammadxcm wants to merge 1 commit into
rails:mainfrom
hammadxcm:test-permissions-policy-branch-coverage

Conversation

@hammadxcm

Copy link
Copy Markdown
Contributor

Motivation / Background

This Pull Request has been created because three reachable branches in ActionDispatch::PermissionsPolicy had no test coverage, while the matching branches in ContentSecurityPolicy are tested (test_missing_context_for_dynamic_source, test_raises_runtime_error_when_unexpected_source).

Line coverage from test/dispatch/ + test/controller/ showed these lines never ran:

  • apply_mapping: raise ArgumentError, "Unknown HTTP permissions policy source mapping: ..."
  • resolve_source: raise RuntimeError, "Missing context for the dynamic permissions policy source: ..."
  • Middleware#call: headers.delete(FEATURE_POLICY) when the policy has no directives

Detail

This Pull Request adds three tests, one per branch:

  • test_unknown_source_mapping: geolocation :non_existent raises ArgumentError
  • test_missing_context_for_dynamic_source: build with no context and a Proc source raises RuntimeError (mirrors the CSP test of the same name)
  • "empty policies will not set a header": the middleware leaves no Feature-Policy header for a policy with no directives

To support the third test, the middleware test's PolicyConfigMiddleware now takes the policy as an argument (default unchanged).

I mutation-tested each branch to confirm the tests actually catch breakage:

Mutation in permissions_policy.rb Failing test
unknown mapping returns nil instead of raising test_unknown_source_mapping
if context.nil? → if false test_missing_context_for_dynamic_source
headers.delete(...) removed empty policies will not set a header

Additional information

Test-only change, so no CHANGELOG entry.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

Three reachable branches in ActionDispatch::PermissionsPolicy had no
test coverage, even though the equivalent branches in
ContentSecurityPolicy are tested:

- An unknown symbol source (e.g. `geolocation :non_existent`) raises
  ArgumentError "Unknown HTTP permissions policy source mapping".
- Building a policy with a Proc source and no context raises
  RuntimeError "Missing context for the dynamic permissions policy
  source".
- The middleware removes the Feature-Policy header when the request's
  policy has no directives.

The middleware test's PolicyConfigMiddleware now takes the policy as
an argument so a test can pass an empty one.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant