Raise proper exception on account already linked error (#1069)

* Raise proper exception on account already linked error

* Update app/account_linking.py

Co-authored-by: Adrià Casajús <acasajus@users.noreply.github.com>

* Fix FMT

Co-authored-by: Adrià Casajús <acasajus@users.noreply.github.com>
This commit is contained in:
Carlos Quintana 2022-06-10 12:23:04 +02:00 committed by GitHub
parent c0a4c44e94
commit c0fe10def6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 2 deletions

View File

@ -72,7 +72,9 @@ def ensure_partner_user_exists_for_user(
link_request: PartnerLinkRequest, sl_user: User, partner: Partner
) -> PartnerUser:
# Find partner_user by user_id
res = PartnerUser.get_by(user_id=sl_user.id, partner_id=partner.id)
res = PartnerUser.get_by(user_id=sl_user.id)
if res and res.partner_id != partner.id:
raise AccountAlreadyLinkedToAnotherPartnerException()
if not res:
res = PartnerUser.create(
user_id=sl_user.id,

View File

@ -4,6 +4,7 @@ from arrow import Arrow
from app.account_linking import (
process_link_case,
get_login_strategy,
ensure_partner_user_exists_for_user,
NewUserStrategy,
ExistingUnlinedUserStrategy,
LinkedWithAnotherPartnerUserStrategy,
@ -14,7 +15,8 @@ from app.account_linking import (
)
from app.proton.proton_callback_handler import get_proton_partner
from app.db import Session
from app.models import PartnerUser, User
from app.errors import AccountAlreadyLinkedToAnotherPartnerException
from app.models import Partner, PartnerUser, User
from app.utils import random_string
from tests.utils import random_email
@ -237,3 +239,47 @@ def test_link_account_with_proton_account_different_address_and_linked_to_other_
def test_cannot_create_instance_of_base_strategy():
with pytest.raises(Exception):
ClientMergeStrategy(random_link_request(), None, get_proton_partner())
def test_ensure_partner_user_exists_for_user_raises_exception_when_linked_to_another_partner():
# Setup test data:
# - partner_1
# - partner_2
# - user
user_email = random_email()
user = create_user(user_email)
external_id_1 = random_string()
partner_1 = Partner.create(
name=random_string(),
contact_email=random_email(),
)
external_id_2 = random_string()
partner_2 = Partner.create(
name=random_string(),
contact_email=random_email(),
)
# Link user with partner_1
ensure_partner_user_exists_for_user(
PartnerLinkRequest(
name=random_string(),
email=user_email,
external_user_id=external_id_1,
plan=SLPlan(type=SLPlanType.Free, expiration=None),
),
user,
partner_1,
)
# Try to link user with partner_2 and confirm the exception
with pytest.raises(AccountAlreadyLinkedToAnotherPartnerException):
ensure_partner_user_exists_for_user(
PartnerLinkRequest(
name=random_string(),
email=user_email,
external_user_id=external_id_2,
plan=SLPlan(type=SLPlanType.Free, expiration=None),
),
user,
partner_2,
)