Exemple #1
0
    async def test_users_can_register_successfully(
        self,
        app: FastAPI,
        client: AsyncClient,
        db: Database,
    ) -> None:
        user_repo = UsersRepository(db)
        new_user = {
            'email': '*****@*****.**',
            'username': '******',
            'password': '******'
        }

        # make sure user doesn't exist yet
        user_in_db = await user_repo.get_user_by_email(email=new_user['email'])
        assert user_in_db is None

        # send post request to create user and ensure it is successful
        res = await client.post(app.url_path_for('users:register-new-user'),
                                json={'new_user': new_user})
        assert res.status_code == HTTP_201_CREATED

        # ensure that the user now exists in the db
        user_in_db = await user_repo.get_user_by_email(email=new_user['email'])
        assert user_in_db is not None
        assert user_in_db.email == new_user['email']
        assert user_in_db.username == new_user['username']

        # check that the user returned in the response is equal to the user in the database
        created_user = UserPublic(**res.json()).dict(exclude={'access_token'})
        assert created_user == user_in_db.dict(exclude={'password', 'salt'})
Exemple #2
0
    async def test_users_can_register_successfully(
        self,
        app: FastAPI,
        client: AsyncClient,
        db: Database,
    ) -> None:
        user_repo = UsersRepository(db)
        new_user = {
            "email": "*****@*****.**",
            "username": "******",
            "password": "******"
        }

        # make sure user doesn't exist yet
        user_in_db = await user_repo.get_user_by_email(email=new_user["email"])
        assert user_in_db is None

        # send post request to create user and ensure it is successful
        res = await client.post(app.url_path_for("users:register-new-user"),
                                json={"new_user": new_user})
        assert res.status_code == HTTP_201_CREATED

        # ensure that the user now exists in the db
        user_in_db = await user_repo.get_user_by_email(email=new_user["email"],
                                                       populate=False)
        assert user_in_db is not None
        assert user_in_db.email == new_user["email"]
        assert user_in_db.username == new_user["username"]

        # check that the user returned in the response is equal to the user in the database
        created_user = UserPublic(**res.json()).dict(
            exclude={"access_token", "profile"})
        assert created_user == user_in_db.dict(exclude={"password", "salt"})
Exemple #3
0
    async def test_users_can_register_successfully(self, app: FastAPI,
                                                   client: AsyncClient,
                                                   db: Database) -> None:
        user_repo = UsersRepository(db)
        new_user = {
            "email": "*****@*****.**",
            "username": "******",
            "password": "******",
        }

        # make sure user doesn't exist yet
        user_in_db = await user_repo.get_user_by_email(email=new_user["email"])
        assert user_in_db is None

        # send post request to create user and ensure it is successfuly
        res = await client.post(app.url_path_for("users:register-new-user"),
                                json={"new_user": new_user})
        assert res.status_code == HTTP_201_CREATED

        # ensure that the user now exist in db
        user_in_db = await user_repo.get_user_by_email(email=new_user["email"],
                                                       populate=False)
        assert user_in_db is not None
        assert user_in_db.email == new_user["email"]
        assert user_in_db.username == new_user["username"]

        # check that the user returned in the response is equal to the user in the database
        # We exclude the password and salt attributes from the user record queried from our
        # database, as those should not be made available by any request that returns a user.
        # created_user = UserInDB(**res.json(), password="******", salt="123").dict(
        #     exclude={"password", "salt"}
        # )
        created_user = UserPublic(**res.json()).dict(
            exclude={"access_token", "profile"})
        assert created_user == user_in_db.dict(exclude={"password", "salt"})
Exemple #4
0
 async def populate_user(self, *, user: UserInDB) -> UserInDB:
     return UserPublic(
         # unpack the user in db dict into the UserPublic model
         # which will remove "password" and "salt"
         **user.dict(),
         # fetch the user's profile from the profiles repo
         profile=await
         self.profiles_repo.get_profile_by_user_id(user_id=user.id))
Exemple #5
0
 async def populate_user(self, *, user: UserInDB) -> UserInDB:
     return UserPublic(
         # unpack the user in db instance,
         **user.dict(),
         # fetch the user's profile from the profiles_repo
         profile=await
         self.profiles_repo.get_profile_by_user_id(user_id=user.id),
     )
 async def test_profile_created_for_new_users(self, app: FastAPI, client: AsyncClient, db: Database) -> None:
     profile_repo = ProfilesRepository(db)
     new_user = {"email": "*****@*****.**", "username": "******", "password": "******"}
     res = await client.post(app.url_path_for("users:register-new-user"), json={"new_user": new_user})
     assert res.status_code == status.HTTP_201_CREATED
     created_user = UserPublic(**res.json())
     user_profile = await profile_repo.get_profile_by_user_id(user_id=created_user.id)
     assert user_profile is not None
     assert isinstance(user_profile, ProfileInDB)
Exemple #7
0
async def register_new_user(
    new_user: UserCreate = Body(..., embed=True),
    user_repo: UsersRepository = Depends(get_repository(UsersRepository)),
) -> UserPublic:
    created_user = await user_repo.register_new_user(new_user=new_user)
    access_token = AccessToken(
        access_token=auth_service.create_access_token_for_user(user=created_user), token_type="bearer"
    )
    return UserPublic(**created_user.dict(), access_token=access_token)
 async def populate_user(self, *, user: UserInDB) -> UserPublic:
     """
     Unpacks the user in db dict into the UserPublic model
     which will remove "password" and "salt".
     It also fetches the user's profile from the profiles repo and attaches it to the user.
     """
     return UserPublic(**user.dict(),
                       profile=await
                       self.profiles_repo.get_profile_by_user_id(
                           user_id=user.id))
Exemple #9
0
 async def test_authenticated_user_can_retrieve_own_data(
         self, app: FastAPI, authorized_client: AsyncClient,
         test_user: UserInDB) -> None:
     res = await authorized_client.get(
         app.url_path_for("users:get-current-user"))
     assert res.status_code == HTTP_200_OK
     user = UserPublic(**res.json())
     assert user.email == test_user.email
     assert user.username == test_user.username
     assert user.id == test_user.id
Exemple #10
0
 async def populate_user(self, *, user: UserInDB) -> UserInDB:
     return UserPublic(**user.dict(),
                       profile=await
                       self.profiles_repo.get_profile_by_user_id(
                           user_id=user.id))