Ejemplo n.º 1
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.º 2
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.º 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_invalid_token(self, user_authentication: UserAuthentication):
     user_token = "invalid_token"
     with pytest.raises(InvalidCredentialsException):
         user_authentication.get_authorized_user(user_token)
Ejemplo n.º 5
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)
Ejemplo n.º 6
0
 def test_unknown_user(self, user_authentication: UserAuthentication):
     with pytest.raises(InvalidCredentialsException):
         user_authentication.login("*****@*****.**", "password")
Ejemplo n.º 7
0
 def test_wrong_password(self, user_authentication: UserAuthentication,
                         user_entry_squirtle: UserEntry):
     with pytest.raises(InvalidCredentialsException):
         user_authentication.login(user_entry_squirtle.email,
                                   "wrongpassword")
Ejemplo n.º 8
0
 def test_valid_login(self, user_authentication: UserAuthentication,
                      user_entry_squirtle: UserEntry):
     user_token = user_authentication.login(user_entry_squirtle.email,
                                            "password")
     assert isinstance(user_token, UserToken)
     assert user_token.user_id == user_entry_squirtle.id
Ejemplo n.º 9
0
def fixture_user_authentication(user_profile):
    return UserAuthentication(user_profile)