def test_read_users(self, session: Session, client: TestClient): user_1 = User( name="Harry Potter", email="*****@*****.**", password_hash="magic_hashed", ) user_2 = User( name="Hermione Granger", email="*****@*****.**", password_hash="leviosaaaa_hashed", ) session.add(user_1) session.add(user_2) session.commit() response = client.get(API_PREFIX + "/users/") data = response.json() assert response.status_code == 200 assert len(data) == 2 assert data[0]["name"] == user_1.name assert data[0]["email"] == user_1.email assert data[0]["id"] == user_1.id assert "password" not in data[0] assert "password_hash" not in data[0]
def test_generate_demo_data(self, session: Session, model: SQLModel, patch_stripe): # data does not exist before assert session.query(model).count() == 0 data.generate_demo_data(session) # data exists after assert session.query(model).count() != 0
def test_delete_course(self, course: Course, session: Session, authenticated_client: TestClient): response = authenticated_client.delete(API_PREFIX + f"/courses/{course.id}") assert response.status_code == 200 course_in_db = session.get(Course, course.id) assert course_in_db is None
def test_delete_resource(self, resource: Resource, session: Session, authenticated_client: TestClient): response = authenticated_client.delete(API_PREFIX + f"/resources/{resource.id}") assert response.status_code == 200 resource_in_db = session.get(Resource, resource.id) assert resource_in_db is None
def authentication_token_from_email(*, client: TestClient, email: str, db: Session) -> Dict[str, str]: """ Return a valid token for the user with given email. If the user doesn't exist it is created first. """ password = random_lower_string() password_hash = security.get_password_hash(password) user = db.query(User).where(User.email == email).first() if not user: # TODO(TOM): put Create op in some util function user = User(name=email.split("@")[0], email=email, password_hash=password_hash) else: user.password_hash = password_hash db.add(user) db.commit() db.refresh(user) return user_authentication_headers(client=client, email=email, password=password)
def test_delete_user( self, user: User, session: Session, authenticated_client: TestClient ): response = authenticated_client.delete(API_PREFIX + f"/users/{user.id}") user_in_db = session.get(User, user.id) assert response.status_code == 200 assert user_in_db is None
def test_generate_demo_data_preexisting_data( self, session: Session, model: SQLModel, count: int ): user = User( name="pre-exisitng user", email="*****@*****.**", password_hash="password123", ) session.add(user) session.commit() assert session.query(model).count() == count data.generate_demo_data(session) # check it did not generate additional data assert session.query(model).count() == count