-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests/unit_tests: update user related tests
Integrate `mongomock_motor` package to mock async mongo client for Beanie initialization. Add fixture to initialize Beanie on app startup. Remove fixtures and add methods to get current active user. Update `app.dependency_overrides` dictionary to mock dependency callable from `fastapi-users` to get users while creating `TestClient`. Update all the tests accordingly. Signed-off-by: Jeny Sadadia <[email protected]>
- Loading branch information
Jeny Sadadia
committed
Oct 19, 2023
1 parent
18a67d0
commit faf5742
Showing
6 changed files
with
68 additions
and
53 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -6,3 +6,4 @@ pytest-dependency==0.5.1 | |
pytest-mock==3.6.1 | ||
pytest-order==1.0.1 | ||
httpx==0.23.3 | ||
mongomock_motor==0.0.21 |
This file contains 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 |
---|---|---|
|
@@ -15,9 +15,18 @@ | |
import fakeredis.aioredis | ||
from fastapi.testclient import TestClient | ||
import pytest | ||
|
||
from api.main import app | ||
from api.models import User, UserGroup, UserProfile | ||
from mongomock_motor import AsyncMongoMockClient | ||
from beanie import init_beanie | ||
from fastapi import Request, HTTPException, status | ||
|
||
from api.main import ( | ||
app, | ||
versioned_app, | ||
get_current_user, | ||
get_current_superuser, | ||
) | ||
from api.models import UserGroup | ||
from api.user_models import User | ||
from api.pubsub import PubSub | ||
|
||
BEARER_TOKEN = "Bearer \ | ||
|
@@ -36,7 +45,10 @@ | |
@pytest.fixture | ||
def test_client(): | ||
"""Fixture to get FastAPI Test client instance""" | ||
with TestClient(app=app, base_url=BASE_URL) as client: | ||
# Mock dependency callables for getting current user | ||
app.dependency_overrides[get_current_user] = mock_get_current_user | ||
app.dependency_overrides[get_current_superuser] = mock_get_current_admin_user | ||
with TestClient(app=versioned_app, base_url=BASE_URL) as client: | ||
return client | ||
|
||
|
||
|
@@ -117,46 +129,47 @@ def mock_db_find_one_by_attributes(mocker): | |
return async_mock | ||
|
||
|
||
@pytest.fixture | ||
def mock_get_current_user(mocker): | ||
def mock_get_current_user(request: Request): | ||
""" | ||
Mocks async call to Authentication class method | ||
used to get current user | ||
Get current active user | ||
""" | ||
async_mock = AsyncMock() | ||
profile = UserProfile( | ||
token = request.headers.get('authorization') | ||
if not token: | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Missing token", | ||
) | ||
return User( | ||
username='bob', | ||
hashed_password='$2b$12$CpJZx5ooxM11bCFXT76/z.o6HWs2sPJy4iP8.' | ||
'xCZGmM8jWXUXJZ4K', | ||
email='[email protected]' | ||
email='[email protected]', | ||
is_active=True, | ||
is_superuser=False, | ||
is_verified=True | ||
) | ||
user = User(profile=profile, active=True) | ||
mocker.patch('api.auth.Authentication.get_current_user', | ||
side_effect=async_mock) | ||
async_mock.return_value = user, None | ||
return async_mock | ||
|
||
|
||
@pytest.fixture | ||
def mock_get_current_admin_user(mocker): | ||
def mock_get_current_admin_user(request: Request): | ||
""" | ||
Mocks async call to Authentication class method | ||
used to get current user | ||
Get current active admin user | ||
""" | ||
async_mock = AsyncMock() | ||
profile = UserProfile( | ||
token = request.headers.get('authorization') | ||
if not token: | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Missing token", | ||
) | ||
return User( | ||
username='admin', | ||
hashed_password='$2b$12$CpJZx5ooxM11bCFXT76/z.o6HWs2sPJy4iP8.' | ||
'xCZGmM8jWXUXJZ4K', | ||
email='[email protected]', | ||
groups=[UserGroup(name='admin')]) | ||
user = User( | ||
profile=profile, | ||
active=True) | ||
mocker.patch('api.auth.Authentication.get_current_user', | ||
side_effect=async_mock) | ||
async_mock.return_value = user, None | ||
return async_mock | ||
groups=[UserGroup(name='admin')], | ||
is_active=True, | ||
is_superuser=True, | ||
is_verified=True | ||
) | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
|
@@ -238,3 +251,15 @@ def mock_unsubscribe(mocker): | |
mocker.patch('api.pubsub.PubSub.unsubscribe', | ||
side_effect=async_mock) | ||
return async_mock | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
async def mock_init_beanie(mocker): | ||
"""Mocks async call to Database method to initialize Beanie""" | ||
async_mock = AsyncMock() | ||
client = AsyncMongoMockClient() | ||
init = await init_beanie( | ||
document_models=[User], database=client.get_database(name="db")) | ||
mocker.patch('api.db.Database.initialize_beanie', | ||
side_effect=async_mock, return_value=init) | ||
return async_mock |
This file contains 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 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 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 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