Skip to content

Add comprehensive tests for all Pundit policies#2457

Merged
olleolleolle merged 1 commit intocodebar:masterfrom
mroderick:policy-tests
Feb 2, 2026
Merged

Add comprehensive tests for all Pundit policies#2457
olleolleolle merged 1 commit intocodebar:masterfrom
mroderick:policy-tests

Conversation

@mroderick
Copy link
Collaborator

Summary

Adds test coverage for all 11 authorization policy files that were previously untested. Policies control access to admin functions and sensitive data, making this coverage critical for security.

Test Coverage Results

  • 62 new policy tests (all passing)
  • 10 of 11 policies: 100% coverage
  • ApplicationPolicy: 92.59% (base class with helper methods)
  • Overall project coverage: 95.22% (up from 95.08%)

Policies Tested

  • AdminPortalPolicy (2 tests)
  • ApplicationPolicy (6 tests)
  • ChapterPolicy (12 tests)
  • ContactPolicy (2 tests)
  • EventPolicy (4 tests)
  • GroupPolicy (4 tests)
  • MemberNotePolicy (2 tests)
  • OrganiserPolicy (6 tests)
  • SponsorPolicy (10 tests)
  • TestimonialPolicy (2 tests)
  • WorkshopPolicy (12 tests)

Testing Pattern

Each policy test follows a consistent pattern testing authorization boundaries:

  • Happy path: Admin user can perform action
  • Sad path: Regular member without roles is denied

Tests use Fabrication for test data and Rolify for role assignment, matching existing codebase patterns.

Verification

Run policy tests:

bundle exec rspec spec/policies/

Expected: 62 examples, 0 failures

Security Impact

This PR establishes test coverage for security-critical authorization logic. Before this PR, policy bugs could allow unauthorized access to admin functions or sensitive member data without being caught by tests.

Adds test coverage for all 11 authorization policy files that were
previously untested. Policies control access to admin functions and
sensitive data, making this coverage critical for security.

Test Coverage Results:
- 62 new policy tests (all passing)
- 10 of 11 policies: 100% coverage
- ApplicationPolicy: 92.59% (base class)
- Overall project: 95.22% coverage (up from 95.08%)

Policies Tested:
- AdminPortalPolicy (2 tests)
- ApplicationPolicy (6 tests)
- ChapterPolicy (12 tests)
- ContactPolicy (2 tests)
- EventPolicy (4 tests)
- GroupPolicy (4 tests)
- MemberNotePolicy (2 tests)
- OrganiserPolicy (6 tests)
- SponsorPolicy (10 tests)
- TestimonialPolicy (2 tests)
- WorkshopPolicy (12 tests)

Testing Pattern:
Each policy test follows a consistent pattern testing authorization
boundaries with happy path (admin succeeds) and sad path (regular
member denied) scenarios. Tests use Fabrication for test data and
Rolify for role assignment, matching existing codebase patterns.
@mroderick mroderick marked this pull request as ready for review February 1, 2026 17:41
@@ -0,0 +1,24 @@
RSpec.describe AdminPortalPolicy do
subject { described_class.new(user, :admin_portal) }
Copy link
Collaborator

@olleolleolle olleolleolle Feb 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
subject { described_class.new(user, :admin_portal) }
subject(:policy) { described_class.new(user, :admin_portal) }

RSpec lets use name subjects, which improves the legibility of the individual tests, while preserving the possibility to use RSpec shared examples, and the like.

Let's call this feedback minor, not a blocker to merging.

Copy link
Collaborator

@olleolleolle olleolleolle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@olleolleolle olleolleolle merged commit dde8b09 into codebar:master Feb 2, 2026
2 checks passed
@biggianteye
Copy link
Contributor

  • 10 of 11 policies: 100% coverage

This line is weird. What does it mean? Is it saying that ten policies have 100% coverage and the 11th policy has ... something else? Or is it mistakenly saying that 10/11 = 100% (rather than 91%)?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants