Ejemplo n.º 1
0
async def create_user(user: UserInDB,
                      token: str = Depends(auth.oauth2_scheme)):
    user_db = await crud.get_by_username(user.username)
    if user_db:
        raise HTTPException(
            status_code=HTTPStatus.CONFLICT,
            detail=
            f"The username '{user.username}' already exists. Please use a different username.",
        )
    user.password = auth.get_password_hash(user.password)
    last_record_id = await crud.create_user(user)
    response = {**user.dict(), "id": last_record_id}
    logger.info(f"User ID: {last_record_id} created")
    return response
Ejemplo n.º 2
0
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
    user_db = await get_by_username(form_data.username)
    if not user_db:
        raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail="Incorrect username or password")

    user = UserInDB(**user_db)
    if not verify_password(form_data.password, user.password):
        raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail="Incorrect username or password")

    return {"access_token": user.username, "token_type": "bearer"}
Ejemplo n.º 3
0
def mock_get_current_user():
    dummy_user_name = "user_A"
    datetime_now = datetime.now()
    user_info = {
        "id": 1,
        "username": dummy_user_name,
        "hashed_password": "******",
        "disabled": False,
        "created_at": datetime_now,
    }
    return UserInDB(**user_info)
Ejemplo n.º 4
0
 async def test_get_current_active_user_without_HTTP_400_BAD_REQUEST(self):
     datetime_now = datetime.now()
     dummy_username = "******"
     user_info = {
         "id": 1,
         "username": dummy_username,
         "hashed_password": "******",
         "disabled": False,
         "created_at": datetime_now,
     }
     dummy_user = UserInDB(**user_info)
     response = await get_current_active_user()
     self.assertEqual(response, dummy_user)
Ejemplo n.º 5
0
 def test_get_existing_user(self, mock_mysql):
     dummy_user_name = "user_A"
     datetime_now = datetime.now()
     query_result = {
         "id": 1,
         "username": dummy_user_name,
         "hashed_password": "******",
         "disabled": False,
         "created_at": datetime_now,
     }
     mock_mysql.execute_fetch_query.return_value = (list(
         query_result.values()), )
     response = get_user(dummy_user_name)
     self.assertEqual(response, UserInDB(**query_result))
Ejemplo n.º 6
0
 async def test_get_current_active_user_with_HTTP_400_BAD_REQUEST(self):
     datetime_now = datetime.now()
     dummy_username = "******"
     user_info = {
         "id": 1,
         "username": dummy_username,
         "hashed_password": "******",
         "disabled": True,
         "created_at": datetime_now,
     }
     dummy_user = UserInDB(**user_info)
     with self.assertRaises(HTTPException):
         response = await get_current_active_user(dummy_user)
         self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
         self.assertEqual(response.json(), {"detail": "Inactive user"})
Ejemplo n.º 7
0
 def test_login_for_access_token_without_HTTPException(
         self, mock_authenticate_user):
     mock_authenticate_user.return_value = UserInDB(
         **{
             "id": 1,
             "username": "******",
             "hashed_password": "******",
             "created_at": datetime.now(),
             "disabled": False,
         })
     response = client.post("/token")
     response_body = response.json()
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     self.assertEqual(list(response_body.keys()),
                      ["access_token", "token_type"])
     self.assertEqual(response_body["token_type"], "bearer")
Ejemplo n.º 8
0
    def test_authenticate_user_with_success(self, mock_get_user,
                                            mock_verify_password):
        datetime_now = datetime.now()
        dummy_username = "******"
        dummy_password = "******"
        user_info = {
            "id": 1,
            "username": dummy_username,
            "hashed_password": "******",
            "disabled": False,
            "created_at": datetime_now,
        }

        dummy_user = UserInDB(**user_info)
        mock_get_user.return_value = dummy_user
        mock_verify_password.return_value = True

        response = authenticate_user(dummy_username, dummy_password)

        mock_get_user.assert_called_once_with(dummy_username)
        mock_verify_password.assert_called_once_with(
            dummy_password, dummy_user.hashed_password)
        self.assertEqual(response, dummy_user)