Skip to content

test : add unit tests for init_api_key function #1064

Description

@tmdeveloper007

Summary of What Needs to be Done:

Add unit tests for the init_api_key function in backend/secuscan/auth.py. This function handles API key file creation and loading, which is testable with a temporary directory.

Function to test:

  • init_api_key(data_dir: str) -> str — generates/loads a 32-byte hex API key from <data_dir>/.api_key

Changes that Need to be Made:

Create testing/backend/unit/test_auth.py with pytest tests covering:

  • First call: generates a new key file with correct permissions (0o600), returns the key
  • Second call: loads the existing key file, returns the same key
  • SECUSCAN_API_KEY_FILE env var overrides the default path
  • Invalid path (directory does not exist): function creates parent directories
  • Key file contains expected 64-character hex string (secrets.token_hex(32))
  • Two init_api_key calls with same data_dir return identical keys
  • init_api_key with different data_dirs return different keys

Use tmp_path fixture for temporary data directories. Mock os.environ with patch.dict.

Important: reset module state between tests — the function sets module-level _api_key. Use a fixture that saves and restores backend.secuscan.auth._api_key and the key file after each test.

Import: from backend.secuscan.auth import init_api_key

Impact that it would Provide:

API key initialization is a security-critical path. Tests guard against permission or path handling regressions when the auth module is refactored. No test coverage currently exists for this module.

Note: This task is being handled by tmdeveloper007 — please assign to that account when picking it up.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions