Skip to content
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

Add db_session to ActivateAccountUserTask #563

Merged
merged 1 commit into from
Jul 17, 2024
Merged
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
2 changes: 2 additions & 0 deletions tasks/activate_account_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from shared.celery_config import activate_account_user_task_name
from shared.django_apps.codecov_auth.models import Account, Owner
from sqlalchemy.orm.session import Session

from app import celery_app
from tasks.base import BaseCodecovTask
Expand All @@ -12,6 +13,7 @@
class ActivateAccountUserTask(BaseCodecovTask, name=activate_account_user_task_name):
def run_impl(
self,
_db_session: Session,
Copy link
Contributor

Choose a reason for hiding this comment

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

Potentially dumb question, but does adding a session in any way affect any transaction related things? Like will the BaseCodecovTask use the session in any way that could silently interfere with anything within the task?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not a dumb question! I don't think it will affect the transaction because the session is created but isn't used. So there's nothing in an uncommitted transaction to cause issues. AFAICT, the ProcessFlakesTask also creates a session without using it, and there hasn't been any trouble with Django DB access.

Copy link
Contributor

Choose a reason for hiding this comment

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

Cool cool, yeah that makes sense, even if it were to commit something in the parent class, there's nothing in this task itself that's using the session itself

*,
user_ownerid: int,
org_ownerid: int,
Expand Down
24 changes: 18 additions & 6 deletions tasks/tests/unit/test_activate_account_user.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from typing import Any

import pytest
from pytest import LogCaptureFixture
from pytest_mock import MockFixture
from shared.django_apps.codecov_auth.models import AccountsUsers
from shared.django_apps.codecov_auth.tests.factories import (
AccountFactory,
Expand All @@ -9,13 +13,20 @@
from tasks.activate_account_user import ActivateAccountUserTask


@pytest.fixture
def mock_db_session(mocker: MockFixture) -> Any:
return mocker.Mock()


@pytest.mark.django_db
def test_activate_account_user_skip_no_account(caplog):
def test_activate_account_user_skip_no_account(
caplog: LogCaptureFixture, mock_db_session: Any
) -> None:
user = OwnerFactory()
org = OwnerFactory()
org.account = None
ActivateAccountUserTask().run_impl(
user_ownerid=user.ownerid, org_ownerid=org.ownerid
mock_db_session, user_ownerid=user.ownerid, org_ownerid=org.ownerid
)
assert len(caplog.records) == 2
assert (
Expand All @@ -40,7 +51,8 @@ def test_activate_account_user(
free_seat_count: int,
is_user_student: bool,
expected_user_count: int,
):
mock_db_session: Any,
) -> None:
user = OwnerFactory()
user.student = is_user_student
user.user = UserFactory()
Expand All @@ -53,7 +65,7 @@ def test_activate_account_user(
assert AccountsUsers.objects.count() == 0

ActivateAccountUserTask().run_impl(
user_ownerid=user.ownerid, org_ownerid=org.ownerid
mock_db_session, user_ownerid=user.ownerid, org_ownerid=org.ownerid
)
assert AccountsUsers.objects.count() == expected_user_count
if expected_user_count > 0:
Expand All @@ -62,7 +74,7 @@ def test_activate_account_user(


@pytest.mark.django_db
def test_activate_account_user_already_exists():
def test_activate_account_user_already_exists(mock_db_session: Any) -> None:
user = OwnerFactory()
user.user = UserFactory()
org = OwnerFactory()
Expand All @@ -77,7 +89,7 @@ def test_activate_account_user_already_exists():
assert AccountsUsers.objects.filter(account=account, user=user.user).count() == 1

ActivateAccountUserTask().run_impl(
user_ownerid=user.ownerid, org_ownerid=org.ownerid
mock_db_session, user_ownerid=user.ownerid, org_ownerid=org.ownerid
)

# Nothing happens... user already exists.
Expand Down
Loading