Skip to content
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

authentication tests #40

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open

authentication tests #40

wants to merge 11 commits into from

Conversation

snebo
Copy link
Contributor

@snebo snebo commented Nov 9, 2024

Test implementation for AuthController

this pull request is designed to include tests for the AuthController and is focused on authentication-related functionalities (ie registration, login, password reset, and email verification token resending).

AuthController and AuthService are imported and injected, and controller routes are tested directly, bypassing the database by using mocked methods on the user schema. Mocks are also made for Authservice, JwtService, and UserModel to simulate authentication behaviors.

Testing Approach

  • Mock Data and Requests: Sample user data (mockUser) and HTTP request objects (mockReq) are created to simulate realistic inputs.
  • User Schema Methods: Static methods on the User schema, like signUp, verifyEmail, and forgetPassword, are mocked to simulate specific outcomes, such as successful user registration, email verification, and password recovery.
  • Error Handling: Tests include scenarios that check for error responses, such as invalid tokens and non-existent email accounts.

Why did i mock static methods on the user schema?

simple, it kept fri**ing calling redis and Mongoose ser-...-cough

The User schema contains several static methods (signUp, verifyEmail, forgetPassword, etc.) used for core user actions in the authentication flow. Mocking these static methods allows for:

  • Database Independence: By mocking static methods, the tests avoid direct database calls, making them faster and more isolated. This allows testing logic without being dependent on the database layer.
  • Controlled Test Environment: Mocking enables us to set specific return values and error conditions, which is useful for testing various scenarios such as successful registrations, invalid tokens, and errors during password reset.
  • Simplified Setup: Mocked methods simplify test setup by simulating complex behaviors (e.g., email verification) without invoking the actual underlying database logic. (totally not me using chat gpt to justify reasons)

oh, this should also contain like 2-3 tests for the admin controllers.

src/shared/auth/db.module.mock.ts Outdated Show resolved Hide resolved
removed redudant mock model db file
renamed controller for better representation
slight prettier adjustments
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