-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e78491c
commit e9abbde
Showing
7 changed files
with
173 additions
and
5 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import logging | ||
|
||
from shared.celery_config import activate_account_user_task_name | ||
from shared.django_apps.codecov_auth.models import Account, Owner | ||
|
||
from app import celery_app | ||
from tasks.base import BaseCodecovTask | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class ActivateAccountUserTask(BaseCodecovTask, name=activate_account_user_task_name): | ||
def run_impl( | ||
self, | ||
*, | ||
user_ownerid: int, | ||
org_ownerid: int, | ||
**kwargs, | ||
): | ||
""" | ||
Runs the task to activate a user onto an account. | ||
:param user_ownerid: the user's owner id | ||
:param org_ownerid: the organization owner id | ||
""" | ||
log_context = {"user_ownerid": user_ownerid, "org_ownerid": org_ownerid} | ||
log.info( | ||
"Syncing account for user", | ||
extra=log_context, | ||
) | ||
|
||
owner_user: Owner = Owner.objects.get(pk=user_ownerid) | ||
|
||
# NOTE: We're currently ignoring organizations that don't have an account. | ||
org_owner: Owner = Owner.objects.get(pk=org_ownerid) | ||
account: Account | None = org_owner.account | ||
if not account: | ||
log.info( | ||
"Organization does not have an account. Skipping account user activation." | ||
) | ||
return {"successful": True} | ||
|
||
if account.can_activate_user(owner_user.user): | ||
account.activate_owner_user_onto_account(owner_user) | ||
account.save() | ||
else: | ||
log.info( | ||
"User was not able to activate on account. It could be that the user is already activated, " | ||
"or the account is in an inconsistent state.", | ||
extra=log_context, | ||
) | ||
|
||
log.info( | ||
"Successfully synced account for user", | ||
extra=log_context, | ||
) | ||
|
||
return {"successful": True} | ||
|
||
|
||
RegisteredActivateAccountUserTask = celery_app.register_task(ActivateAccountUserTask()) | ||
activate_account_user_task = celery_app.tasks[ActivateAccountUserTask.name] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import pytest | ||
from shared.django_apps.codecov_auth.models import AccountsUsers | ||
from shared.django_apps.codecov_auth.tests.factories import ( | ||
AccountFactory, | ||
OwnerFactory, | ||
UserFactory, | ||
) | ||
|
||
from tasks.activate_account_user import ActivateAccountUserTask | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_activate_account_user_skip_no_account(caplog): | ||
user = OwnerFactory() | ||
org = OwnerFactory() | ||
org.account = None | ||
ActivateAccountUserTask().run_impl( | ||
user_ownerid=user.ownerid, org_ownerid=org.ownerid | ||
) | ||
assert len(caplog.records) == 2 | ||
assert ( | ||
caplog.records[1].message | ||
== "Organization does not have an account. Skipping account user activation." | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"plan_seat_count,free_seat_count,is_user_student,expected_user_count", | ||
[ | ||
pytest.param(0, 0, False, 0, id="cannot_activate_no_seats_available"), | ||
pytest.param(1, 0, False, 1, id="activate_with_seats_available"), | ||
pytest.param(0, 1, False, 1, id="activate_with_free_seats_available"), | ||
pytest.param(2, 0, True, 1, id="activate_github_student"), | ||
pytest.param(2, 1, True, 1, id="activate_github_student"), | ||
], | ||
) | ||
@pytest.mark.django_db | ||
def test_activate_account_user( | ||
plan_seat_count: int, | ||
free_seat_count: int, | ||
is_user_student: bool, | ||
expected_user_count: int, | ||
): | ||
user = OwnerFactory() | ||
user.student = is_user_student | ||
user.user = UserFactory() | ||
org = OwnerFactory() | ||
account = AccountFactory( | ||
plan_seat_count=plan_seat_count, free_seat_count=free_seat_count | ||
) | ||
org.account = account | ||
org.save() | ||
assert AccountsUsers.objects.count() == 0 | ||
|
||
ActivateAccountUserTask().run_impl( | ||
user_ownerid=user.ownerid, org_ownerid=org.ownerid | ||
) | ||
assert AccountsUsers.objects.count() == expected_user_count | ||
if expected_user_count > 0: | ||
assert AccountsUsers.objects.first().account == account | ||
assert AccountsUsers.objects.first().user.owners.first() == user | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_activate_account_user_already_exists(): | ||
user = OwnerFactory() | ||
user.user = UserFactory() | ||
org = OwnerFactory() | ||
account = AccountFactory() | ||
org.account = account | ||
org.save() | ||
|
||
account.users.add(user.user) | ||
account.save() | ||
user.save() | ||
|
||
assert AccountsUsers.objects.filter(account=account, user=user.user).count() == 1 | ||
|
||
ActivateAccountUserTask().run_impl( | ||
user_ownerid=user.ownerid, org_ownerid=org.ownerid | ||
) | ||
|
||
# Nothing happens... user already exists. | ||
assert AccountsUsers.objects.filter(account=account, user=user.user).count() == 1 |
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