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:
parent
c0a4c44e94
commit
c0fe10def6
|
@ -72,7 +72,9 @@ def ensure_partner_user_exists_for_user(
|
||||||
link_request: PartnerLinkRequest, sl_user: User, partner: Partner
|
link_request: PartnerLinkRequest, sl_user: User, partner: Partner
|
||||||
) -> PartnerUser:
|
) -> PartnerUser:
|
||||||
# Find partner_user by user_id
|
# 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:
|
if not res:
|
||||||
res = PartnerUser.create(
|
res = PartnerUser.create(
|
||||||
user_id=sl_user.id,
|
user_id=sl_user.id,
|
||||||
|
|
|
@ -4,6 +4,7 @@ from arrow import Arrow
|
||||||
from app.account_linking import (
|
from app.account_linking import (
|
||||||
process_link_case,
|
process_link_case,
|
||||||
get_login_strategy,
|
get_login_strategy,
|
||||||
|
ensure_partner_user_exists_for_user,
|
||||||
NewUserStrategy,
|
NewUserStrategy,
|
||||||
ExistingUnlinedUserStrategy,
|
ExistingUnlinedUserStrategy,
|
||||||
LinkedWithAnotherPartnerUserStrategy,
|
LinkedWithAnotherPartnerUserStrategy,
|
||||||
|
@ -14,7 +15,8 @@ from app.account_linking import (
|
||||||
)
|
)
|
||||||
from app.proton.proton_callback_handler import get_proton_partner
|
from app.proton.proton_callback_handler import get_proton_partner
|
||||||
from app.db import Session
|
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 app.utils import random_string
|
||||||
|
|
||||||
from tests.utils import random_email
|
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():
|
def test_cannot_create_instance_of_base_strategy():
|
||||||
with pytest.raises(Exception):
|
with pytest.raises(Exception):
|
||||||
ClientMergeStrategy(random_link_request(), None, get_proton_partner())
|
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,
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in a new issue