Skip to content

Conversation

@JudahSan
Copy link
Collaborator

@JudahSan JudahSan commented Jun 9, 2025

Enhance Security, Testing, and Code Compatibility

This pull request introduces several key enhancements focused on improving application security, enabling more robust testing, and ensuring future Rails compatibility.

Key Changes & Improvements:

  • Cloudflare Turnstile Integration for Enhanced Security:

    • Implemented invisible reCAPTCHA using the rails-cloudflare-turnstile gem.
    • This adds an additional layer of bot protection during user registration, making it harder for automated scripts to create accounts.
    • The verification is configured to run specifically in the production environment via a before_action and includes appropriate error handling for failed challenges.
  • GitHub Account Verification for User Registration: TEMPORARY!!

    • Introduced a new GithubAccountVerifier service responsible for validating the existence of GitHub usernames via the GitHub API.
    • Integrated this service into the User model with a custom validation to ensure only valid GitHub accounts can be registered.
    • Added presence: true and uniqueness: true validations for the github_username field.
    • Included a robust format validation for GitHub usernames, ensuring they conform to GitHub's naming conventions.
    • Updated the user registration form (devise/registrations/new.html.erb) to include the GitHub username field.
    • Relevant tests for user registration and the GithubAccountVerifier service have been updated/added to ensure correct functionality and error handling.
  • Improved Testing Capabilities with Mocha:

    • Added the mocha gem to the test environment.
    • This gem provides powerful mocking and stubbing capabilities, which are essential for writing isolated and efficient unit tests. It is particularly useful for components that interact with external services (like the GithubAccountVerifier), allowing us to simulate API responses without making actual network calls.
  • Rails 8 Compatibility Update for User Model enum:

    • Updated the enum definition for the role attribute in the User model.
    • Switched from the deprecated keyword argument syntax (enum role: { ... }) to the new positional argument syntax (enum :role, { ... }).
    • This change addresses a deprecation warning and proactively ensures the code remains compatible with future Rails 8 releases.

How to Test/Verify:

  1. User Registration (Manual):

    • Attempt to register a new user:
      • With a valid GitHub username.
      • With a non-existent GitHub username.
      • With a blank GitHub username.
      • With an invalid format GitHub username (e.g., user--name).
    • Verify that error messages are displayed correctly for invalid GitHub usernames.
    • Observe the Cloudflare Turnstile widget on the registration page (in development, you might need to temporarily remove if Rails.env.production? from the before_action in RegistrationsController to see it in action).
  2. Run Test Suite:

    • Execute bundle exec rails test to ensure all existing and new tests pass, especially for the User model and GithubAccountVerifier service.
  3. Code Review:

    • Verify the updated enum syntax in app/models/user.rb.
    • Review app/services/github_account_verifier.rb and its corresponding test file.
    • Check app/controllers/users/registrations_controller.rb for Turnstile integration and app/views/devise/registrations/new.html.erb for the Turnstile tag and GitHub username field.

* feat: add invisible reCAPTCHA to enhance security

* Add GitHub account verification for user registration

Introduced `GithubAccountVerifier` service to validate the existence of GitHub accounts via API. Integrated the verification into the user model with appropriate validations and error handling. Updated tests and the registration form to ensure a valid GitHub username is required during user registration.

* feat: implement recaptcha using cloudflare-turnstile gem

* fix rubocop offenses

* setup master key

* add mocha and change `enum` definition in user model

- add mocha gem to load and provide the mockk and stubs methods for testing
- Changed enum definition in the user model from using a keyword argument to a positional argument to keep code compatible with future Rails 8

* Add recaptcha, mocha, gh verification, and change `enum` definition in user model

- add invisible reCAPTCHA using cloudflare-turnstile gem to enhance security
- add mocha gem to load and provide the mockk and stubs methods for testing
- Changed enum definition in the user model from using a keyword argument to a positional argument to keep code compatible with future Rails 8
- Add GitHub account verification for user registration
- Introduced `GithubAccountVerifier` service to validate the existence of GitHub accounts via API. Integrated the verification into the user model with appropriate validations and error handling. Updated tests and the registration form to ensure a valid GitHub username is required during user registration.
@JudahSan JudahSan requested a review from adamswonder June 9, 2025 18:28
@JudahSan JudahSan added the enhancement New feature or request label Jun 9, 2025
@JudahSan JudahSan linked an issue Jun 9, 2025 that may be closed by this pull request
3 tasks
JudahSan and others added 12 commits June 10, 2025 22:55
This reverts commit d6f5654.
* feat: Add Turnstile keys for test and development

* refactor: Update GitHub Actions to use GITHUB_OUTPUT

Replace deprecated `set-output` command with `$GITHUB_OUTPUT` for setting step outputs

* refactor: replace cloudflare-turnstile gem with manual verification in sessions and registrations

- Removed dependency on cloudflare-turnstile gem
- Implemented direct API verification for Turnstile
- Integrated validation into SessionsController#create(login) and RegistrationsController#create (sign up)
- Improved error handling with localized messages

* refactor: remove JS bundling, switch to importmap, and cleanup dependencies

- Replaced `jsbundling-rails` with `importmap-rails`
- Removed unnecessary JavaScript dependencies (e.g., autoprefixer, esbuild, postcss, tailwindcss)
- Updated Turnstile controller for improved handling and compatibility
- Adjusted app layout and asset imports to support importmap

* chore: add docs and update CI workflow dependencies and environment setup

- Upgrade `actions/checkout` to v5
- Update Node.js version to 22 and install Yarn globally
- Add system dependency installation for `libpq-dev`
- Configure environment variables for Postgres in tests
- Update test commands to streamline setup and execution

* chore: simplify CI workflow by removing unused Node.js and JS dependencies

- Dropped Node.js setup, Yarn installation, and related caching steps
- Streamlined test setup and execution steps to focus on Ruby environment

* chore: update asset pipeline configuration and CI workflow

- Add CSS builds directory to asset paths
- Update manifest to exclude JS compilation via Sprockets
- Modify CI workflow tasks to align with asset pipeline changes

* Revert "chore: update asset pipeline configuration and CI workflow"

This reverts commit f08918b.

* Revert "chore: simplify CI workflow by removing unused Node.js and JS dependencies"

This reverts commit 4891ccd.

* Revert "chore: add docs and update CI workflow dependencies and environment setup"

This reverts commit cc76d07.

* Revert "refactor: remove JS bundling, switch to importmap, and cleanup dependencies"

This reverts commit 2b2689a.

* chore: upgrade Ruby to 3.4.4 and enhance Turnstile controller

- Updated Ruby version across configurations (Gemfile, .ruby-version, .tool-versions, and Gemfile.lock).
- Refactored Turnstile Stimulus controller for improved rendering, defaults, and error handling.
- Registered the Turnstile controller in JavaScript controllers.

* fix linting issues
* feat: Add Turnstile keys for test and development

* refactor: Update GitHub Actions to use GITHUB_OUTPUT

Replace deprecated `set-output` command with `$GITHUB_OUTPUT` for setting step outputs

* refactor: replace cloudflare-turnstile gem with manual verification in sessions and registrations

- Removed dependency on cloudflare-turnstile gem
- Implemented direct API verification for Turnstile
- Integrated validation into SessionsController#create(login) and RegistrationsController#create (sign up)
- Improved error handling with localized messages

* refactor: remove JS bundling, switch to importmap, and cleanup dependencies

- Replaced `jsbundling-rails` with `importmap-rails`
- Removed unnecessary JavaScript dependencies (e.g., autoprefixer, esbuild, postcss, tailwindcss)
- Updated Turnstile controller for improved handling and compatibility
- Adjusted app layout and asset imports to support importmap

* chore: add docs and update CI workflow dependencies and environment setup

- Upgrade `actions/checkout` to v5
- Update Node.js version to 22 and install Yarn globally
- Add system dependency installation for `libpq-dev`
- Configure environment variables for Postgres in tests
- Update test commands to streamline setup and execution

* chore: simplify CI workflow by removing unused Node.js and JS dependencies

- Dropped Node.js setup, Yarn installation, and related caching steps
- Streamlined test setup and execution steps to focus on Ruby environment

* chore: update asset pipeline configuration and CI workflow

- Add CSS builds directory to asset paths
- Update manifest to exclude JS compilation via Sprockets
- Modify CI workflow tasks to align with asset pipeline changes

* Revert "chore: update asset pipeline configuration and CI workflow"

This reverts commit f08918b.

* Revert "chore: simplify CI workflow by removing unused Node.js and JS dependencies"

This reverts commit 4891ccd.

* Revert "chore: add docs and update CI workflow dependencies and environment setup"

This reverts commit cc76d07.

* Revert "refactor: remove JS bundling, switch to importmap, and cleanup dependencies"

This reverts commit 2b2689a.

* chore: upgrade Ruby to 3.4.4 and enhance Turnstile controller

- Updated Ruby version across configurations (Gemfile, .ruby-version, .tool-versions, and Gemfile.lock).
- Refactored Turnstile Stimulus controller for improved rendering, defaults, and error handling.
- Registered the Turnstile controller in JavaScript controllers.

* fix linting issues

* fix: disable "Create Account" button and update styles
Copy link
Collaborator

@adamswonder adamswonder left a comment

Choose a reason for hiding this comment

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

Jolly good! Tested different scenarios

@adamswonder adamswonder merged commit 7cebf5e into African-Ruby-Community:main Aug 22, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Cloudflare CAPTCHA for User Registration

2 participants