add tests to email
This commit is contained in:
@@ -103,6 +103,7 @@ def unlink(org_id: str, user_id: str):
|
||||
def _create_user(user: User, org: Org) -> bool:
|
||||
now_ = now()
|
||||
user_id = uuid4()
|
||||
email_verified = '@users.noreply.saladeaula.digital' in user.email
|
||||
|
||||
try:
|
||||
with dyn.transact_writer() as transact:
|
||||
@@ -111,7 +112,7 @@ def _create_user(user: User, org: Org) -> bool:
|
||||
**user.model_dump(),
|
||||
'id': user_id,
|
||||
'sk': '0',
|
||||
'email_verified': False,
|
||||
'email_verified': email_verified,
|
||||
'tenant_id': {org.id},
|
||||
# Post-migration: uncomment the folloing line
|
||||
# 'org_id': {org.id},
|
||||
@@ -123,7 +124,7 @@ def _create_user(user: User, org: Org) -> bool:
|
||||
'id': user_id,
|
||||
# Post-migration: rename `emails` to `EMAIL`
|
||||
'sk': f'emails#{user.email}',
|
||||
'email_verified': False,
|
||||
'email_verified': email_verified,
|
||||
'email_primary': True,
|
||||
'created_at': now_,
|
||||
}
|
||||
|
||||
@@ -68,8 +68,8 @@ def add(
|
||||
)
|
||||
transact.put(
|
||||
item={
|
||||
'id': 'EMAIL_VERIFICATION',
|
||||
'sk': uuid4(),
|
||||
'id': user_id,
|
||||
'sk': f'EMAIL_VERIFICATION#{uuid4()}',
|
||||
'name': name,
|
||||
'email': email,
|
||||
'user_id': user_id,
|
||||
@@ -81,6 +81,27 @@ def add(
|
||||
return JSONResponse(status_code=HTTPStatus.CREATED)
|
||||
|
||||
|
||||
@router.post('/<user_id>/emails/<email>/request-verification')
|
||||
def request_verification(user_id: str, email: Annotated[EmailStr, Path]):
|
||||
now_ = now()
|
||||
name = dyn.collection.get_item(
|
||||
KeyPair(user_id, SortKey('0', path_spec='name')),
|
||||
raise_on_error=False,
|
||||
)
|
||||
dyn.put_item(
|
||||
item={
|
||||
'id': user_id,
|
||||
'sk': f'EMAIL_VERIFICATION#{uuid4()}',
|
||||
'name': name,
|
||||
'email': email,
|
||||
'user_id': user_id,
|
||||
'ttl': ttl(start_dt=now_, days=30),
|
||||
'created_at': now_,
|
||||
}
|
||||
)
|
||||
return JSONResponse(status_code=HTTPStatus.NO_CONTENT)
|
||||
|
||||
|
||||
class EmailVerificationNotFoundError(NotFoundError): ...
|
||||
|
||||
|
||||
@@ -88,15 +109,18 @@ class EmailVerificationNotFoundError(NotFoundError): ...
|
||||
def verify(user_id: str, hash: str):
|
||||
email = dyn.collection.get_item(
|
||||
KeyPair(
|
||||
pk='EMAIL_VERIFICATION',
|
||||
sk=SortKey(hash, path_spec='email'),
|
||||
pk=user_id,
|
||||
sk=SortKey(f'EMAIL_VERIFICATION#{hash}', path_spec='email'),
|
||||
),
|
||||
exc_cls=EmailVerificationNotFoundError,
|
||||
)
|
||||
|
||||
with dyn.transact_writer() as transact:
|
||||
transact.delete(key=KeyPair('EMAIL_VERIFICATION', hash))
|
||||
transact.delete(
|
||||
key=KeyPair(user_id, f'EMAIL_VERIFICATION#{hash}'),
|
||||
)
|
||||
transact.update(
|
||||
# Post-migration (users): rename `emails` to `EMAIL`
|
||||
key=KeyPair(user_id, f'emails#{email}'),
|
||||
update_expr='SET email_verified = :true, updated_at = :now',
|
||||
expr_attr_values={
|
||||
|
||||
Reference in New Issue
Block a user