Skip to content

Refactor acceptance_tests.rb: Add DRY helper methods to reduce duplication #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 13, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 12, 2025

This PR refactors spec/acceptance/acceptance_tests.rb to make it more DRY, concise, and maintainable while preserving all existing logic and test coverage.

Changes Made

File size reduction: 536 → 452 lines (84 lines removed, ~15.7% reduction)

Added 10 helper methods to eliminate code duplication:

  • make_request() - Unified HTTP request handling for GET/POST operations
  • expect_response() - Standardized response validation with optional body content checking
  • parse_json_response() - Consistent JSON parsing across all tests
  • json_headers() - Reusable header construction with Content-Type defaults
  • generate_hmac_signature() - Generic HMAC signature generation with configurable algorithms
  • generate_hmac_with_timestamp() - HMAC signature generation with timestamp patterns
  • generate_slack_signature() - Slack-specific signature format (v0= prefix)
  • current_timestamp() - ISO8601 timestamp helper
  • unix_timestamp() - Unix timestamp helper
  • expired_timestamp() / expired_unix_timestamp() - Test data helpers for expired timestamps

Before/After Comparison

Before (repetitive):

response = http.post("/webhooks/github", payload.to_json, {
  "Content-Type" => "application/json",
  "X-Hub-Signature-256" => "sha256=" + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), FAKE_HMAC_SECRET, payload.to_json)
})
expect(response).to be_a(Net::HTTPSuccess)
body = JSON.parse(response.body)
expect(body["status"]).to eq("success")

After (DRY):

json_payload = payload.to_json
signature = generate_hmac_signature(json_payload, FAKE_HMAC_SECRET)
headers = json_headers("X-Hub-Signature-256" => signature)
response = make_request(:post, "/webhooks/github", json_payload, headers)
expect_response(response, Net::HTTPSuccess)
body = parse_json_response(response)
expect(body["status"]).to eq("success")

Benefits

  • Eliminated repetitive code - No more duplicate HTTP request setup, header construction, or response validation
  • Standardized patterns - Consistent approach to HMAC signature generation and response handling
  • Improved readability - Tests focus on business logic rather than boilerplate
  • Easier maintenance - Changes to common patterns can be made in one place
  • Preserved functionality - All existing test logic maintained with 100% test coverage

Testing

  • ✅ All unit tests pass (502 examples, 0 failures)
  • ✅ All integration tests pass (8 examples, 0 failures)
  • ✅ 100% test coverage maintained
  • ✅ Linting passes with no offenses
  • ✅ No behavioral changes to any tests

Fixes #39.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] task: improve spec/acceptance/acceptance_tests.rb Refactor acceptance_tests.rb: Add DRY helper methods to reduce duplication Jun 12, 2025
@Copilot Copilot AI requested a review from GrantBirki June 12, 2025 23:11
Copilot finished work on behalf of GrantBirki June 12, 2025 23:11
@GrantBirki GrantBirki marked this pull request as ready for review June 12, 2025 23:55
@Copilot Copilot AI review requested due to automatic review settings June 12, 2025 23:55
@GrantBirki GrantBirki merged commit 3344477 into main Jun 13, 2025
22 checks passed
@GrantBirki GrantBirki deleted the copilot/fix-39 branch June 13, 2025 00:05
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors spec/acceptance/acceptance_tests.rb by extracting common logic into helper methods to reduce duplication and improve maintainability, and bumps the library version.

  • Introduced DRY helper methods for request handling, JSON parsing, and signature generation.
  • Reduced file size by ~15.7% while preserving all existing test logic and coverage.
  • Updated VERSION constant to 0.0.4 in lib/hooks/version.rb.

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.

task: improve spec/acceptance/acceptance_tests.rb
2 participants