Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import sys
from unittest.mock import MagicMock
import os
import pytest

# Mocking passlib since it is not installed in the environment
mock_passlib = MagicMock()
mock_passlib.hash.pbkdf2_sha256.hash.side_effect = lambda x: f"hashed_{x}"
mock_passlib.hash.pbkdf2_sha256.verify.side_effect = lambda pw, h: h == f"hashed_{pw}"

sys.modules["passlib"] = mock_passlib
sys.modules["passlib.hash"] = mock_passlib.hash

@pytest.fixture(autouse=True)
def setup_test_db(monkeypatch, tmp_path):
# Set a temporary database for testing
test_db = tmp_path / "test_users.db"

# Patch the DATABASE_NAME in backend.users
import backend.users
monkeypatch.setattr(backend.users, "DATABASE_NAME", str(test_db))

# Initialize the database
backend.users.init_db()

yield str(test_db)

# Cleanup (handled by tmp_path)
42 changes: 42 additions & 0 deletions backend/tests/test_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import pytest
from backend.users import register_user
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Remove hard dependency on PYTHONPATH in test import

Using from backend.users import register_user makes test collection fail under a standard pytest run when PYTHONPATH is not explicitly set (it raises ModuleNotFoundError: No module named 'backend'), so the new suite is not runnable out of the box. This blocks the registration tests in default local/CI environments unless extra path configuration is added; switch to an import path that works with the repo layout (or add explicit pytest path config) so tests collect reliably.

Useful? React with 👍 / 👎.


def test_register_user_success():
"""Test that a new user can be registered successfully."""
username = "new_user"
password = "secure_password"

response = register_user(username, password)

assert response["success"] is True
assert response["message"] == "User registered successfully"

def test_register_user_duplicate():
"""Test that registering a duplicate username fails and returns the correct error message."""
username = "duplicate_user"
password = "password123"

# Register the user for the first time
first_response = register_user(username, password)
assert first_response["success"] is True

# Try to register the same username again
second_response = register_user(username, password)

assert second_response["success"] is False
assert second_response["message"] == "Username already taken"

def test_register_user_approval_needed(monkeypatch):
"""Test registration when admin approval is required."""
from backend.users import set_require_approval

# Enable require approval
set_require_approval(True)

username = "waiting_user"
password = "waiting_password"

response = register_user(username, password)

assert response["success"] is True
assert response["message"] == "Registration successful! Wait for Admin approval."