app-MAIL-temp/tests/proton/test_proton_callback_handler.py

92 lines
2.7 KiB
Python
Raw Normal View History

from arrow import Arrow
from app.account_linking import (
SLPlan,
SLPlanType,
)
from app.proton.proton_client import ProtonClient, UserInformation
2022-03-14 09:33:31 +01:00
from app.proton.proton_callback_handler import (
ProtonCallbackHandler,
get_proton_partner,
generate_account_not_allowed_to_log_in,
2022-03-14 09:33:31 +01:00
)
from app.models import User, PartnerUser
from app.utils import random_string
from typing import Optional
from tests.utils import random_email
2022-03-14 09:33:31 +01:00
class MockProtonClient(ProtonClient):
def __init__(self, user: Optional[UserInformation]):
2022-03-14 09:33:31 +01:00
self.user = user
def get_user(self) -> Optional[UserInformation]:
2022-03-14 09:33:31 +01:00
return self.user
def test_proton_callback_handler_unexistant_sl_user():
email = random_email()
name = random_string()
external_id = random_string()
handler = ProtonCallbackHandler(
MockProtonClient(
user=UserInformation(
email=email,
name=name,
id=external_id,
plan=SLPlan(
type=SLPlanType.Premium, expiration=Arrow.utcnow().shift(hours=2)
),
)
)
2022-03-14 09:33:31 +01:00
)
res = handler.handle_login(get_proton_partner())
2022-03-14 09:33:31 +01:00
assert res.user is not None
assert res.user.email == email
assert res.user.name == name
partner_user = PartnerUser.get_by(
partner_id=get_proton_partner().id, user_id=res.user.id
)
assert partner_user is not None
assert partner_user.external_user_id == external_id
2022-03-14 09:33:31 +01:00
def test_proton_callback_handler_existant_sl_user():
email = random_email()
sl_user = User.create(email, commit=True)
external_id = random_string()
user = UserInformation(
email=email,
name=random_string(),
id=external_id,
plan=SLPlan(type=SLPlanType.Premium, expiration=Arrow.utcnow().shift(hours=2)),
2022-03-14 09:33:31 +01:00
)
handler = ProtonCallbackHandler(MockProtonClient(user=user))
res = handler.handle_login(get_proton_partner())
2022-03-14 09:33:31 +01:00
assert res.user is not None
assert res.user.id == sl_user.id
2022-05-23 16:43:06 +02:00
sa = PartnerUser.get_by(user_id=sl_user.id, partner_id=get_proton_partner().id)
2022-03-14 09:33:31 +01:00
assert sa is not None
assert sa.partner_email == user.email
def test_proton_callback_handler_none_user_login():
handler = ProtonCallbackHandler(MockProtonClient(user=None))
res = handler.handle_login(get_proton_partner())
2022-03-14 09:33:31 +01:00
expected = generate_account_not_allowed_to_log_in()
assert res == expected
2022-03-14 09:33:31 +01:00
def test_proton_callback_handler_none_user_link():
sl_user = User.create(random_email(), commit=True)
handler = ProtonCallbackHandler(MockProtonClient(user=None))
res = handler.handle_link(sl_user, get_proton_partner())
2022-03-14 09:33:31 +01:00
expected = generate_account_not_allowed_to_log_in()
assert res == expected