Skip to content

[SUBTASK]-Improve Code Quality and Add Tests for Remaining Functions in auth.service.spec.ts #114

@cagataycode

Description

@cagataycode

There are several areas in the current test file that could be improved for better maintainability, readability, and type safety.
generateAuthenticationOptions and verifyAuthenticationOptions functions are not yet covered by tests.

Areas for Improvement

  1. Repetitive Mocking
  • The mockRepository setup is reused across multiple test cases but contains repetitive implementations for methods like findOne, save, create, etc.
  • Action: Create a shared utility function for mocking repositories.
const createMockRepository = () => ({
     findOne: jest.fn(),
     save: jest.fn(),
     create: jest.fn(),
     update: jest.fn(),
     delete: jest.fn(),
 });
  1. Verbose Test Assertions
  • Some test assertions are overly detailed, checking every property of mocked objects.
  • Action: Simplify assertions to focus on key aspects relevant to the test.
  1. MockConfigService Implementation
  • The mockConfigService.get implementation uses a lookup table, which adds unnecessary complexity.
  • Action: Simplify to directly return required values for each test case.
const mockConfigService = {
      get: jest.fn().mockReturnValue('mock-value'),
  };
  1. Inline ESLint Disabling
  • Frequent use of // eslint-disable-next-line comments indicates potential issues with type safety.
  • Action: Address root causes by improving typings for mocks instead of suppressing warnings.
  1. Environment Variable Dependency
  • The service relies on environment variables like RPID and RPORIGIN.
  • Action: Add fallback values or better error messages to handle cases where these variables are missing.

Missing Tests

  1. generateAuthenticationOptions
  • This function is not currently covered by tests.
  • Action: Write tests to cover the following scenarios:
    • Happy path: Successfully generates authentication options and saves the challenge.
    • Error handling: Throws an error if the user is not found or if the repository fails.
  1. verifyAuthenticationOptions
  • This function is not currently covered by tests.
  • Action: Write tests to cover the following scenarios:
    • Happy path: Successfully verifies the authentication response and deletes the challenge.
    • Error handling: Throws an error if the challenge is not found, verification fails, or the repository fails.

Suggested Labels

  • refactor
  • testing
  • enhancement

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

Status

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions