Skip to content

Conversation

@ErisDS
Copy link
Member

@ErisDS ErisDS commented Apr 10, 2025

ref https://linear.app/ghost/issue/ENG-2074/adjust-verification-copy-if-2fa-is-enabled-for-all-logins ref https://linear.app/ghost/issue/ENG-2134/editor-reauthentication-flow-doesnt-work-how-i-expect-and-is-untested

  • Whilst working towards being able to do better differentiation of what 2fa flow we are in based on the error response from the server I came across the re-authentication unit test file
  • This file has 3 tests, 2 are essentially testing the same thing, and all 3 tests stub out too much and don't directly test any other code except the stubs
  • The first two tests are verifying the isTwoFactorAuthenticationError logic. In moving that to the ajax service integration tests, I found issues with the way the payload is handled
  • This change therefore also includes a fix to ensure the payload is always JSON that we can inspect
  • I've deleted the re-authentication tests because they don't actually test anything
  • I did attempt to replace the intention of testing the modal flow, with an integration or acceptance test, but I couldn't find any way to do this quickly
  • I've raised https://linear.app/ghost/issue/ENG-2134/editor-reauthentication-flow-doesnt-work-how-i-expect-and-is-untested to come back to later

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 10, 2025

Walkthrough

This pull request introduces a new function, getJSONPayload, to the AJAX service for parsing JSON payloads, particularly within the TwoFactorTokenRequiredError class and the isTwoFactorTokenRequiredError function. The inline JSON parsing logic has been removed and replaced with this centralized function. Additionally, two new functions, isTwoFactorTokenRequiredError and getErrorCode, have been added to enhance error recognition. The integration tests have been expanded with three new cases to assess the AJAX service's response to 403 errors related to two-factor authentication. Furthermore, the file containing unit tests for re-authentication modals has been deleted, as it focused on scenarios involving two-factor authentication errors.

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ghost/admin/app/services/ajax.js

Oops! Something went wrong! :(

ESLint: 8.57.1

Error: Failed to load parser '@babel/eslint-parser' declared in 'ghost/admin/.eslintrc.js': Cannot find module '@babel/eslint-parser'
Require stack:

  • /ghost/admin/.eslintrc.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1248:15)
    at Function.resolve (node:internal/modules/helpers:145:19)
    at Object.resolve (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2346:46)
    at ConfigArrayFactory._loadParser (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3325:39)
    at ConfigArrayFactory._normalizeObjectConfigDataBody (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3099:43)
    at _normalizeObjectConfigDataBody.next ()
    at ConfigArrayFactory._normalizeObjectConfigData (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3040:20)
    at _normalizeObjectConfigData.next ()
    at ConfigArrayFactory.loadInDirectory (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2886:28)
    at CascadingConfigArrayFactory._loadConfigInAncestors (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3871:46)
ghost/admin/tests/integration/services/ajax-test.js

Oops! Something went wrong! :(

ESLint: 8.57.1

Error: Failed to load parser '@babel/eslint-parser' declared in 'ghost/admin/.eslintrc.js': Cannot find module '@babel/eslint-parser'
Require stack:

  • /ghost/admin/.eslintrc.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1248:15)
    at Function.resolve (node:internal/modules/helpers:145:19)
    at Object.resolve (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2346:46)
    at ConfigArrayFactory._loadParser (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3325:39)
    at ConfigArrayFactory._normalizeObjectConfigDataBody (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3099:43)
    at _normalizeObjectConfigDataBody.next ()
    at ConfigArrayFactory._normalizeObjectConfigData (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3040:20)
    at _normalizeObjectConfigData.next ()
    at ConfigArrayFactory.loadInDirectory (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2886:28)
    at CascadingConfigArrayFactory._loadConfigInAncestors (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3871:46)

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fe99b4e48533b879232e4a7baafdf806437cd4a2 and 9fcedee.

📒 Files selected for processing (3)
  • ghost/admin/app/services/ajax.js (2 hunks)
  • ghost/admin/tests/integration/services/ajax-test.js (2 hunks)
  • ghost/admin/tests/unit/components/editor/modals/re-authenticate-test.js (0 hunks)
💤 Files with no reviewable changes (1)
  • ghost/admin/tests/unit/components/editor/modals/re-authenticate-test.js
🚧 Files skipped from review as they are similar to previous changes (1)
  • ghost/admin/app/services/ajax.js
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Unit tests (Node 22.13.1)
  • GitHub Check: Unit tests (Node 20.11.1)
  • GitHub Check: Unit tests (Node 18.12.1)
  • GitHub Check: Admin tests - Chrome
🔇 Additional comments (4)
ghost/admin/tests/integration/services/ajax-test.js (4)

5-16: LGTM: Import statements have been well-organized.

The imports have been nicely organized by separating Ghost-specific functions from Ember's built-in ones. The new functions getErrorCode and isTwoFactorTokenRequiredError are correctly placed in alphabetical order.


190-206: LGTM: Well-structured test for 2FA token validation.

The test correctly verifies that a 403 error with a 2FA_TOKEN_REQUIRED code is properly identified by the new isTwoFactorTokenRequiredError function, and that getErrorCode successfully extracts the error code.

There's a minor inconsistency in the assertion style - line 200 uses expect(false).to.be.true without parentheses, while other similar assertions in the file (e.g., line 62) use expect(false).to.be.true() with parentheses. Consider standardizing for consistency.


208-223: LGTM: Appropriately handles 403 errors without error codes.

This test correctly verifies that a 403 error without an error code is not identified as a 2FA token error, following the negative test case pattern.


225-240: LGTM: Properly validates error codes.

The test properly confirms that a 403 error with an incorrect code (WRONG_CODE) is not identified as a 2FA token error. This completes the test coverage for the error identification logic.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

ref https://linear.app/ghost/issue/ENG-2074/adjust-verification-copy-if-2fa-is-enabled-for-all-logins
ref https://linear.app/ghost/issue/ENG-2134/editor-reauthentication-flow-doesnt-work-how-i-expect-and-is-untested

- Whilst working towards being able to do better differentiation of what 2fa flow we are in
  based on the error response from the server I came across the re-authentication unit test file
- This file has 3 tests, 2 are essentially testing the same thing, and all 3 tests stub out too much
  and don't directly test any other code except the stubs
- The first two tests are verifying the isTwoFactorAuthenticationError logic. In moving that to the ajax
  service integration tests, I found issues with the way the payload is handled
- This change therefore also includes a fix to ensure the payload is always JSON that we can inspect
- I've deleted the re-authentication tests because they don't actually test anything
- I did attempt to replace the intention of testing the modal flow, with an integration or
  acceptance test, but I couldn't find any way to do this quickly
- I've raised https://linear.app/ghost/issue/ENG-2134/editor-reauthentication-flow-doesnt-work-how-i-expect-and-is-untested
  to come back to later
@ErisDS ErisDS merged commit c187e72 into main Apr 10, 2025
22 checks passed
@ErisDS ErisDS deleted the 2fa-ajax branch April 10, 2025 12:45
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.

2 participants