Ejemplo n.º 1
0
 def test_expired_token(self, user_authentication: UserAuthentication,
                        user_entry_squirtle: UserEntry):
     user_token = user_authentication.sign_token(
         UserToken(
             user_id=user_entry_squirtle.id,
             exp=datetime.utcnow() - timedelta(minutes=10),
         ).dict())
     with pytest.raises(InvalidCredentialsException):
         user_authentication.get_authorized_user(user_token)
Ejemplo n.º 2
0
 def test_valid_token(self, user_authentication: UserAuthentication,
                      user_entry_squirtle: UserEntry):
     user_token = user_authentication.sign_token(
         user_authentication.login(user_entry_squirtle.email,
                                   "password").dict())
     authorized_user_entry = user_authentication.get_authorized_user(
         user_token)
     assert isinstance(authorized_user_entry, UserEntry)
     assert authorized_user_entry.id == user_entry_squirtle.id
Ejemplo n.º 3
0
def login_user(
    form_data: OAuth2PasswordRequestForm = Depends(),
    user_authentication: UserAuthentication = Depends(),
) -> dict:
    try:
        user_token = user_authentication.login(
            email=form_data.username, password=form_data.password
        )
    except InvalidCredentialsException as e:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Invalid login credentials.",
        ) from e
    return {
        "access_token": user_authentication.sign_token(user_token.dict()),
        "token_type": "bearer",
    }
Ejemplo n.º 4
0
 def test_sign_token(self, user_authentication: UserAuthentication):
     signed_token = user_authentication.sign_token({
         "user_id": 1,
         "exp": datetime.utcnow()
     })
     assert isinstance(signed_token, str)