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 _(): user = User( id=1, email="*****@*****.**", name="Name", is_staff=False, is_active=False, date_joined=datetime.utcnow(), ) repository = FakeUsersRepository(users=[user]) jwt_token = user.generate_token() request = MagicMock() request.headers = {"Authorization": f"Bearer {jwt_token}"} with raises(AuthenticationError): await JWTAuthBackend(repository).authenticate(request)
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
def force_login(self, user: entities.User): self.auth_token = user.generate_token()
def from_domain(cls, user: entities.User) -> LoginSuccess: return cls(user=User.from_domain(user), token=user.generate_token())