mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2025-08-15 19:30:47 +08:00

* Add routes for user activation (#403) * Add routes for user activation Generate a token after creating the user in register route, passing to `activation_callback`, if `activation_callback` supplied Create new `/activate` route that will verify the token and activate the user Add new error codes to `fastapi_users/router/common.py` Update documentation Add tests Co-authored-by: Mark Todd <markpeter.todd@hotmail.co.uk> * Rework routes for user activation * Separate verification logic and token generation into `/fastapi_users/router/verify.py`, with per-route callbacks for custom behaviour * Return register router to original state * Added `is_verified` property to user models * Added `requires_verification` argument to `get_users_router`and `get_auth_router` * Additional dependencies added for verification in `fastapi_users/authentication/__init__.py` * Update tests for new behaviour * Update `README.md` to describe a workaround for possible problems during testing, by exceeding ulimit file descriptor limit Co-authored-by: Mark Todd <markpeter.todd@hotmail.co.uk> * Restored docs to original state. * All other modifications reqested added Kebab-case on request-verify-token SECRET now used as test string Other minor changes Co-authored-by: Mark Todd <markpeter.todd@hotmail.co.uk> * Embed token in body in verify route * Reorganize checks in verify route and add unit test * Ignore coverage on Protocol classes * Tweak verify_user function to take full user in parameter * Improve unit tests structure regarding parametrized test client * Make after_verification_request optional to be more consistent with other routers * Tweak status codes on verify routes * Write documentation for verification feature * Add not released warning on verify docs Co-authored-by: Edd Salkield <edd@salkield.uk> Co-authored-by: Mark Todd <markpeter.todd@hotmail.co.uk>
72 lines
2.2 KiB
Python
72 lines
2.2 KiB
Python
import pytest
|
|
|
|
from fastapi_users.user import (
|
|
CreateUserProtocol,
|
|
UserAlreadyExists,
|
|
UserAlreadyVerified,
|
|
VerifyUserProtocol,
|
|
get_create_user,
|
|
get_verify_user,
|
|
)
|
|
from tests.conftest import UserCreate, UserDB
|
|
|
|
|
|
@pytest.fixture
|
|
def create_user(
|
|
mock_user_db,
|
|
) -> CreateUserProtocol:
|
|
return get_create_user(mock_user_db, UserDB)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
class TestCreateUser:
|
|
@pytest.mark.parametrize(
|
|
"email", ["king.arthur@camelot.bt", "King.Arthur@camelot.bt"]
|
|
)
|
|
async def test_existing_user(self, email, create_user):
|
|
user = UserCreate(email=email, password="guinevere")
|
|
with pytest.raises(UserAlreadyExists):
|
|
await create_user(user)
|
|
|
|
@pytest.mark.parametrize("email", ["lancelot@camelot.bt", "Lancelot@camelot.bt"])
|
|
async def test_regular_user(self, email, create_user):
|
|
user = UserCreate(email=email, password="guinevere")
|
|
created_user = await create_user(user)
|
|
assert type(created_user) == UserDB
|
|
|
|
@pytest.mark.parametrize("safe,result", [(True, False), (False, True)])
|
|
async def test_superuser(self, create_user, safe, result):
|
|
user = UserCreate(
|
|
email="lancelot@camelot.b", password="guinevere", is_superuser=True
|
|
)
|
|
created_user = await create_user(user, safe)
|
|
assert type(created_user) == UserDB
|
|
assert created_user.is_superuser is result
|
|
|
|
@pytest.mark.parametrize("safe,result", [(True, True), (False, False)])
|
|
async def test_is_active(self, create_user, safe, result):
|
|
user = UserCreate(
|
|
email="lancelot@camelot.b", password="guinevere", is_active=False
|
|
)
|
|
created_user = await create_user(user, safe)
|
|
assert type(created_user) == UserDB
|
|
assert created_user.is_active is result
|
|
|
|
|
|
@pytest.fixture
|
|
def verify_user(
|
|
mock_user_db,
|
|
) -> VerifyUserProtocol:
|
|
return get_verify_user(mock_user_db)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
class TestVerifyUser:
|
|
async def test_already_verified_user(self, verify_user, verified_user):
|
|
with pytest.raises(UserAlreadyVerified):
|
|
await verify_user(verified_user)
|
|
|
|
async def test_non_verified_user(self, verify_user, user):
|
|
user = await verify_user(user)
|
|
assert user.is_verified
|