-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created tests for background task that updates from active to expire
- Loading branch information
Showing
2 changed files
with
75 additions
and
4 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
70 changes: 70 additions & 0 deletions
70
services/web/server/tests/unit/with_dbs/03/test_users_bg_tasks.py
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# pylint: disable=redefined-outer-name | ||
# pylint: disable=unused-argument | ||
# pylint: disable=unused-variable | ||
|
||
from datetime import datetime, timedelta | ||
|
||
import pytest | ||
from aiohttp import web | ||
from aiohttp.test_utils import TestClient | ||
from faker import Faker | ||
from pytest import MonkeyPatch | ||
from pytest_mock import MockerFixture | ||
from pytest_simcore.helpers.utils_assert import assert_status | ||
from pytest_simcore.helpers.utils_envs import EnvVarsDict, setenvs_from_dict | ||
from pytest_simcore.helpers.utils_login import NewUser | ||
from simcore_postgres_database.models.users import UserStatus | ||
from simcore_service_webserver.users_bg_tasks import ( | ||
APP_DB_ENGINE_KEY, | ||
update_expired_users, | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def app_environment( | ||
app_environment: EnvVarsDict, monkeypatch: MonkeyPatch, mocker: MockerFixture | ||
) -> EnvVarsDict: | ||
# disables bg task in users! | ||
async def _fake_on_cleanup(app: web.Application): | ||
yield | ||
|
||
mocker.patch( | ||
"simcore_service_webserver.users.run_background_task_to_monitor_expiration_trial_accounts", | ||
autospec=True, | ||
side_effect=_fake_on_cleanup, | ||
) | ||
|
||
# disables GC | ||
return app_environment | setenvs_from_dict( | ||
monkeypatch, {"WEBSERVER_GARBAGE_COLLECTOR": "null"} | ||
) | ||
|
||
|
||
async def test_update_expired_users(client: TestClient, faker: Faker): | ||
|
||
yesterday = datetime.utcnow() - timedelta(days=1) | ||
async with NewUser( | ||
{ | ||
"email": faker.email(), | ||
"status": UserStatus.ACTIVE.name, | ||
"expires_at": yesterday, | ||
}, | ||
client.app, | ||
) as user: | ||
|
||
async def _rq_login(): | ||
return await client.post( | ||
f"{client.app.router['auth_login'].url_for()}", | ||
json={ | ||
"email": user["email"], | ||
"password": user["raw_password"], | ||
}, | ||
) | ||
|
||
r1 = await _rq_login() | ||
await assert_status(r1, web.HTTPOk) | ||
|
||
await update_expired_users(client.app[APP_DB_ENGINE_KEY]) | ||
|
||
r2 = await _rq_login() | ||
await assert_status(r2, web.HTTPUnauthorized) |