generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 14
refactor tests based on the comments of auth test pr #90
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
VaibhavSingh8
wants to merge
2
commits into
develop
Choose a base branch
from
refactor/auth-tests
base: develop
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.
+35
−87
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,6 @@ | ||
GOOGLE_OAUTH_FIXTURE = { | ||
"CLIENT_ID": "test-client-id", | ||
"CLIENT_SECRET": "test-client-secret", | ||
"REDIRECT_URI": "http://localhost:3000/auth/callback", | ||
"SCOPES": ["email", "profile"], | ||
} | ||
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
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
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 |
---|---|---|
|
@@ -5,14 +5,15 @@ | |
from todo.repositories.user_repository import UserRepository | ||
from todo.models.user import UserModel | ||
from todo.models.common.pyobjectid import PyObjectId | ||
from todo.exceptions.google_auth_exceptions import GoogleUserNotFoundException, GoogleAPIException | ||
from todo.exceptions.google_auth_exceptions import GoogleAPIException | ||
from todo.tests.fixtures.user import users_db_data | ||
from todo.constants.messages import RepositoryErrors | ||
|
||
|
||
class UserRepositoryTests(TestCase): | ||
def setUp(self) -> None: | ||
self.valid_user_data = {"google_id": "123456789", "email": "[email protected]", "name": "Test User"} | ||
self.valid_user_data = users_db_data[0].copy() | ||
self.valid_user_data["email"] = self.valid_user_data.pop("email_id") | ||
self.user_model = UserModel(**users_db_data[0]) | ||
self.mock_collection = MagicMock() | ||
self.mock_db_manager = MagicMock() | ||
|
@@ -39,15 +40,6 @@ def test_get_by_id_not_found(self, mock_db_manager): | |
result = UserRepository.get_by_id(user_id) | ||
self.assertIsNone(result) | ||
|
||
@patch("todo.repositories.user_repository.DatabaseManager") | ||
def test_get_by_id_database_error(self, mock_db_manager): | ||
mock_db_manager.return_value = self.mock_db_manager | ||
user_id = str(ObjectId()) | ||
self.mock_collection.find_one.side_effect = Exception("Database error") | ||
|
||
with self.assertRaises(GoogleUserNotFoundException): | ||
UserRepository.get_by_id(user_id) | ||
|
||
@patch("todo.repositories.user_repository.DatabaseManager") | ||
def test_create_or_update_success(self, mock_db_manager): | ||
mock_db_manager.return_value = self.mock_db_manager | ||
|
@@ -57,9 +49,14 @@ def test_create_or_update_success(self, mock_db_manager): | |
|
||
self.mock_collection.find_one_and_update.assert_called_once() | ||
call_args = self.mock_collection.find_one_and_update.call_args[0] | ||
update_doc = call_args[1] | ||
self.assertEqual(call_args[0], {"google_id": self.valid_user_data["google_id"]}) | ||
self.assertIsInstance(result, UserModel) | ||
self.assertEqual(result.google_id, users_db_data[0]["google_id"]) | ||
self.assertIn("$set", update_doc) | ||
self.assertIn("updated_at", update_doc["$set"]) | ||
self.assertIn("$setOnInsert", update_doc) | ||
self.assertIn("created_at", update_doc["$setOnInsert"]) | ||
|
||
@patch("todo.repositories.user_repository.DatabaseManager") | ||
def test_create_or_update_no_result(self, mock_db_manager): | ||
|
@@ -69,26 +66,3 @@ def test_create_or_update_no_result(self, mock_db_manager): | |
with self.assertRaises(GoogleAPIException) as context: | ||
UserRepository.create_or_update(self.valid_user_data) | ||
self.assertIn(RepositoryErrors.USER_OPERATION_FAILED, str(context.exception)) | ||
|
||
@patch("todo.repositories.user_repository.DatabaseManager") | ||
def test_create_or_update_database_error(self, mock_db_manager): | ||
mock_db_manager.return_value = self.mock_db_manager | ||
self.mock_collection.find_one_and_update.side_effect = Exception("Database error") | ||
|
||
with self.assertRaises(GoogleAPIException) as context: | ||
UserRepository.create_or_update(self.valid_user_data) | ||
self.assertIn(RepositoryErrors.USER_CREATE_UPDATE_FAILED.format("Database error"), str(context.exception)) | ||
|
||
@patch("todo.repositories.user_repository.DatabaseManager") | ||
def test_create_or_update_sets_timestamps(self, mock_db_manager): | ||
mock_db_manager.return_value = self.mock_db_manager | ||
self.mock_collection.find_one_and_update.return_value = users_db_data[0] | ||
|
||
UserRepository.create_or_update(self.valid_user_data) | ||
|
||
call_args = self.mock_collection.find_one_and_update.call_args[0] | ||
update_doc = call_args[1] | ||
self.assertIn("$set", update_doc) | ||
self.assertIn("updated_at", update_doc["$set"]) | ||
self.assertIn("$setOnInsert", update_doc) | ||
self.assertIn("created_at", update_doc["$setOnInsert"]) |
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 |
---|---|---|
|
@@ -5,19 +5,19 @@ | |
from todo.services.google_oauth_service import GoogleOAuthService | ||
from todo.exceptions.google_auth_exceptions import GoogleAPIException, GoogleAuthException | ||
from todo.constants.messages import ApiErrors | ||
from todo.tests.fixtures.google_oauth import GOOGLE_OAUTH_FIXTURE | ||
from todo.tests.fixtures.user import users_db_data | ||
|
||
|
||
class GoogleOAuthServiceTests(TestCase): | ||
def setUp(self) -> None: | ||
self.mock_settings = { | ||
"GOOGLE_OAUTH": { | ||
"CLIENT_ID": "test-client-id", | ||
"CLIENT_SECRET": "test-client-secret", | ||
"REDIRECT_URI": "http://localhost:3000/auth/callback", | ||
"SCOPES": ["email", "profile"], | ||
} | ||
self.mock_settings = {"GOOGLE_OAUTH": GOOGLE_OAUTH_FIXTURE.copy()} | ||
user = users_db_data[0] | ||
self.valid_user_info = { | ||
"id": user["google_id"], | ||
"email": user["email_id"], | ||
"name": user["name"], | ||
} | ||
self.valid_user_info = {"id": "123456789", "email": "[email protected]", "name": "Test User"} | ||
self.valid_tokens = {"access_token": "test-access-token", "refresh_token": "test-refresh-token"} | ||
|
||
@patch("todo.services.google_oauth_service.settings") | ||
|
@@ -116,20 +116,6 @@ def test_get_user_info_success(self, mock_get): | |
call_args = mock_get.call_args[1] | ||
self.assertEqual(call_args["headers"]["Authorization"], "Bearer test-token") | ||
|
||
@patch("todo.services.google_oauth_service.requests.get") | ||
def test_get_user_info_missing_fields(self, mock_get): | ||
mock_response = MagicMock() | ||
mock_response.status_code = 200 | ||
mock_response.json.return_value = {"id": "123"} | ||
mock_get.return_value = mock_response | ||
|
||
with self.assertRaises(GoogleAPIException) as context: | ||
GoogleOAuthService._get_user_info("test-token") | ||
error_msg = str(context.exception) | ||
self.assertIn(ApiErrors.MISSING_USER_INFO_FIELDS.split(":")[0], error_msg) | ||
for field in ("email", "name"): | ||
self.assertIn(field, error_msg) | ||
|
||
@patch("todo.services.google_oauth_service.requests.get") | ||
def test_get_user_info_error_response(self, mock_get): | ||
mock_response = MagicMock() | ||
|
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 |
---|---|---|
|
@@ -11,7 +11,8 @@ | |
|
||
class UserServiceTests(TestCase): | ||
def setUp(self) -> None: | ||
self.valid_google_user_data = {"google_id": "123456789", "email": "[email protected]", "name": "Test User"} | ||
self.valid_google_user_data = users_db_data[0].copy() | ||
self.valid_google_user_data["email"] = self.valid_google_user_data.pop("email_id") | ||
self.user_model = UserModel(**users_db_data[0]) | ||
|
||
@patch("todo.services.user_service.UserRepository") | ||
|
@@ -65,11 +66,13 @@ def test_validate_google_user_data_success(self): | |
self.fail("ValidationError raised unexpectedly!") | ||
|
||
def test_validate_google_user_data_missing_fields(self): | ||
test_cases = [ | ||
{"email": "[email protected]", "name": "Test User"}, | ||
{"google_id": "123", "name": "Test User"}, | ||
{"google_id": "123", "email": "[email protected]"}, | ||
] | ||
base_data = users_db_data[0].copy() | ||
base_data["email"] = base_data.pop("email_id") | ||
test_cases = [] | ||
for missing_key in ["google_id", "email", "name"]: | ||
case = base_data.copy() | ||
case.pop(missing_key) | ||
test_cases.append(case) | ||
|
||
for invalid_data in test_cases: | ||
with self.subTest(f"Testing missing field in {invalid_data}"): | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.