async def _(): repository = FakeUsersRepository(users=[]) request = MagicMock() request.headers = {} result = await JWTAuthBackend(repository).authenticate(request) assert not result
async def _(): user = User( id=1, username="******", password="******", email="*****@*****.**", fullname="Marco Acierno", name="Marco", gender="", date_birth=None, open_to_newsletter=False, open_to_recruiting=False, country="", date_joined=datetime(2020, 1, 1), is_staff=False, is_superuser=False, is_active=True, ) repository = FakeUsersRepository(users=[user]) logged_user = await login( LoginInputModel(email="*****@*****.**", password="******"), users_repository=repository, ) assert logged_user.id == user.id
async def _(): user = User( id=1, email="*****@*****.**", name="Name", is_staff=False, is_active=True, date_joined=datetime.utcnow(), ) repository = FakeUsersRepository(users=[user]) with patch( "users.auth.entities.get_jwt_metadata", return_value={ "exp": datetime(1980, 1, 1), "iat": datetime(1980, 1, 1) }, ): jwt_token = user.generate_token() request = MagicMock() request.headers = {"Authorization": f"Bearer {jwt_token}"} with raises(AuthenticationError): await JWTAuthBackend(repository).authenticate(request)
async def _(): repository = FakeUsersRepository(users=[]) with time_machine.travel("2020-10-10 10:10:00Z", tick=False): user = await social_login( SocialLoginInput( email="*****@*****.**", social_account=SocialAccount( social_id="1", fullname="Test Account", first_name="Test", last_name="Account", ), ), users_repository=repository, ) assert user assert user.id is not None assert user.date_joined == datetime(2020, 10, 10, 10, 10, 0, tzinfo=timezone.utc) assert not user.has_usable_password() assert user.fullname == "Test Account" assert user.name == "Test" assert user.email == "*****@*****.**"
async def _(): user = User( id=1, username="******", password="******", email="*****@*****.**", fullname="Marco Acierno", name="Marco", gender="", date_birth=None, open_to_newsletter=False, open_to_recruiting=False, country="", date_joined=datetime(2020, 1, 1), is_staff=False, is_superuser=False, is_active=True, ) repository = FakeUsersRepository(users=[user]) with raises(UserIsNotAdminError): await login( LoginInputModel(email="*****@*****.**", password="******"), reject_non_admins=True, users_repository=repository, )
async def _(): user = User( id=1, username="******", password="******", email="*****@*****.**", fullname="Marco Acierno", name="Marco", gender="", date_birth=None, open_to_newsletter=False, open_to_recruiting=False, country="", date_joined=datetime(2020, 1, 1), is_staff=False, is_superuser=False, is_active=True, ) repository = FakeUsersRepository(users=[user]) with time_machine.travel("2020-10-10 10:10:00Z", tick=False): logged_user = await login( LoginInputModel(email="*****@*****.**", password="******"), users_repository=repository, ) assert logged_user.id == user.id assert logged_user.last_login == datetime( 2020, 10, 10, 10, 10, 00, tzinfo=timezone.utc )
async def _(): user = User( id=10, email="*****@*****.**", date_joined=datetime.now(timezone.utc), password="******", is_active=False, jwt_auth_id=1, name="Old name", fullname="Old fullname", gender="f", open_to_recruiting=True, open_to_newsletter=True, date_birth=date(1900, 1, 1), country="US", ) with raises(UserIsNotActiveError): await update_profile( user.id, UpdateProfileInput( name="New name", full_name="Full name", gender="m", open_to_recruiting=False, open_to_newsletter=False, date_birth=date(2020, 10, 1), country="IT", ), users_repository=FakeUsersRepository([user]), )
async def _(): repository = FakeUsersRepository( users=[ User( id=10, email="*****@*****.**", date_joined=datetime.now(), password="******", fullname="Hello World", name="Hello", ) ] ) user = await social_login( SocialLoginInput( email="*****@*****.**", social_account=SocialAccount( social_id="1", fullname="Test Account", first_name="Test", last_name="Account", ), ), users_repository=repository, ) assert user assert user.has_usable_password() assert user.check_password("my_password") assert user.id == 10 assert user.fullname == "Hello World" assert user.name == "Hello" assert user.email == "*****@*****.**"
async def _(): user = User( id=10, email="*****@*****.**", date_joined=datetime.now(timezone.utc), password="******", is_active=True, jwt_auth_id=1, ) with time_machine.travel("2021-10-10 15:00:00Z", tick=False): token = jwt.encode( { "user_id": 10, "exp": datetime.now(timezone.utc) + timedelta(minutes=30), "iat": datetime.now(timezone.utc), "iss": "users", "aud": "users/not-reset-password", }, str(SECRET_KEY), ) with raises(ResetPasswordTokenInvalidError): await reset_password( ResetPasswordInput(token=token, new_password="******"), repository=FakeUsersRepository([user]), )
async def _(): repository = FakeUsersRepository(users=[]) with raises(WrongEmailOrPasswordError): await login( LoginInputModel(email="*****@*****.**", password="******"), users_repository=repository, )
async def _(): repository = FakeUsersRepository( users=[User(email="*****@*****.**", date_joined=datetime.utcnow())]) with raises(EmailAlreadyUsedError): await register( RegisterInputModel(email="*****@*****.**", password="******"), users_repository=repository, )
async def _(): repository = FakeUsersRepository(users=[]) user = await register( RegisterInputModel(email="*****@*****.**", password="******"), users_repository=repository, ) assert repository.committed assert user.id is not None assert user.email == "*****@*****.**" assert user.check_password("hello_world")
async def _(): user = User( id=10, email="*****@*****.**", date_joined=datetime.now(timezone.utc), is_active=False, ) with raises(UserIsNotActiveError): await reset_password( ResetPasswordInput(token=user.create_reset_password_token(), new_password="******"), repository=FakeUsersRepository([user]), )
async def _(): with raises(UserDoesNotExistError): await update_profile( 5, UpdateProfileInput( name="New name", full_name="Full name", gender="m", open_to_recruiting=False, open_to_newsletter=False, date_birth=date(2020, 10, 1), country="IT", ), users_repository=FakeUsersRepository([]), )
async def _(): user = User( id=1, email="*****@*****.**", name="Name", is_staff=False, date_joined=datetime.utcnow(), ) repository = FakeUsersRepository(users=[user]) request = MagicMock() request.headers = {"Authorization": f"token abc"} result = await JWTAuthBackend(repository).authenticate(request) assert not result
async def _(): user = User( id=1, email="*****@*****.**", name="Name", is_staff=False, date_joined=datetime.utcnow(), ) repository = FakeUsersRepository(users=[user]) request = MagicMock() request.headers = {"Authorization": f"Bearer abc"} with raises(AuthenticationError): await JWTAuthBackend(repository).authenticate(request)
async def _(): user = User( id=10, email="*****@*****.**", date_joined=datetime.now(timezone.utc), password="******", is_active=True, jwt_auth_id=1, ) token = user.create_reset_password_token() user.jwt_auth_id = 2 with raises(ResetPasswordTokenInvalidError): await reset_password( ResetPasswordInput(token=token, new_password="******"), repository=FakeUsersRepository([user]), )
async def _(): user = User( id=10, email="*****@*****.**", date_joined=datetime.now(timezone.utc), password="******", is_active=True, jwt_auth_id=1, ) with time_machine.travel("2020-10-10 10:10:10Z", tick=False): token = user.create_reset_password_token() with time_machine.travel( "2020-10-10 15:10:10Z", tick=False), raises(ResetPasswordTokenExpiredError): await reset_password( ResetPasswordInput(token=token, new_password="******"), repository=FakeUsersRepository([user]), )
async def _(): user = User( id=10, email="*****@*****.**", date_joined=datetime.now(timezone.utc), password="******", is_active=True, jwt_auth_id=1, ) await reset_password( ResetPasswordInput(token=user.create_reset_password_token(), new_password="******"), repository=FakeUsersRepository([user]), ) assert user.new_password == "testnewpassword" # we did column + 1 so the value updated in the DB and not python assert user.jwt_auth_id.left.name == "jwt_auth_id" assert user.jwt_auth_id.right.value == 1
async def _(): user = User( id=1, email="*****@*****.**", name="Name", is_staff=False, date_joined=datetime.utcnow(), ) repository = FakeUsersRepository(users=[user]) jwt_token = user.generate_token() request = MagicMock() request.headers = {"Authorization": f"Bearer {jwt_token}"} credentials, logged_user = await JWTAuthBackend(repository).authenticate( request) assert credentials.scopes == [Credential.AUTHENTICATED] assert logged_user.id == user.id