-
Notifications
You must be signed in to change notification settings - Fork 0
🧪 Add tests for user registration error paths #66
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
Open
DivyanshuChipa
wants to merge
1
commit into
master
Choose a base branch
from
testing-improvement-users-register-14722163909695510411
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import pytest | ||
| from backend.users import register_user | ||
|
|
||
| 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." | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
from backend.users import register_usermakes test collection fail under a standardpytestrun whenPYTHONPATHis not explicitly set (it raisesModuleNotFoundError: 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 👍 / 👎.