From d3e314ed5a4f36623ccae61f0a9ba4982a2093cd Mon Sep 17 00:00:00 2001 From: M Waleed Kadous Date: Sun, 31 Mar 2024 19:07:40 -0700 Subject: [PATCH 1/2] Make shared threads fully public., --- main_api.py | 19 ++++++------------- tests/test_main_api.py | 3 ++- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/main_api.py b/main_api.py index 116842d..daff094 100644 --- a/main_api.py +++ b/main_api.py @@ -324,7 +324,6 @@ def share_thread( def get_snapshot( share_uuid_str: str, cors_ok: bool = Depends(validate_cors), - token_params: dict = Depends(db.validate_token), ): """ Take a snapshot of a thread at this time and make it shareable. @@ -332,18 +331,12 @@ def get_snapshot( """ logger.info(f"Incoming share_uuid is {share_uuid_str}") share_uuid = uuid.UUID(share_uuid_str) - if cors_ok and token_params: - logger.info(f"Token_params is {token_params}") - # TODO(mwk): check that the user_id in the token matches the - # user_id associated with the thread_id. - try: - content = db.get_snapshot(share_uuid) - return {"status": "success", "content": content} - except psycopg2.Error as e: - logger.critical(f"Error: {e}") - raise HTTPException(status_code=500, detail="Database error") - else: - raise HTTPException(status_code=403, detail="CORS not permitted") + try: + content = db.get_snapshot(share_uuid) + return {"status": "success", "content": content} + except psycopg2.Error as e: + logger.critical(f"Error: {e}") + raise HTTPException(status_code=500, detail="Database error") @app.get("/api/v2/threads/{thread_id}") diff --git a/tests/test_main_api.py b/tests/test_main_api.py index 53fef39..4867826 100644 --- a/tests/test_main_api.py +++ b/tests/test_main_api.py @@ -272,7 +272,8 @@ async def test_share_thread(login_user, create_thread): response = client.get( f"api/v2/share/{share_uuid}", headers={ - "Authorization": f"Bearer {login_user}", + # NOTE: We do not need to pass the Authorization header here + # Accessing a shared thread does not require authentication "x-mobile-ansari": "ANSARI", }, ) From 6fe1f1b263a10af4ab6310cbfd3f1bd57ce355db Mon Sep 17 00:00:00 2001 From: abdullah-alnahas Date: Mon, 1 Apr 2024 07:25:38 +0300 Subject: [PATCH 2/2] Fix TypeError in error logging with f-strings This commit replaces logger.warning("Error is", e) with logger.warning(f"Error is {e}") to resolve a TypeError caused by inconsistent string formatting. --- ansari_db.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/ansari_db.py b/ansari_db.py index 67eb24d..eed8a61 100644 --- a/ansari_db.py +++ b/ansari_db.py @@ -140,7 +140,7 @@ def register(self, email, first_name, last_name, password_hash): self.conn.commit() return {"status": "success"} except Exception as e: - logger.warning("Error is ", e) + logger.warning(f"Error is {e}") return {"status": "failure", "error": str(e)} finally: if cur: @@ -154,7 +154,7 @@ def account_exists(self, email): result = cur.fetchone() return result is not None except Exception as e: - logger.warning("Error is ", e) + logger.warning(f"Error is {e}") return False finally: if cur: @@ -169,7 +169,7 @@ def save_token(self, user_id, token): self.conn.commit() return {"status": "success", "token": token} except Exception as e: - logger.warning("Error is ", e) + logger.warning(f"Error is {e}") return {"status": "failure", "error": str(e)} finally: if cur: @@ -184,7 +184,7 @@ def save_reset_token(self, user_id, token): self.conn.commit() return {"status": "success", "token": token} except Exception as e: - logger.warning("Error is ", e) + logger.warning(f"Error is {e}") return {"status": "failure", "error": str(e)} finally: if cur: @@ -202,7 +202,7 @@ def retrieve_user_info(self, email): last_name = result[3] return user_id, existing_hash, first_name, last_name except Exception as e: - logger.warning("Error is ", e) + logger.warning(f"Error is {e}") return None, None, None, None finally: if cur: @@ -218,7 +218,7 @@ def add_feedback(self, user_id, thread_id, message_id, feedback_class, comment): ) return {"status": "success"} except Exception as e: - logger.warning("Error is ", e) + logger.warning(f"Error is {e}") return {"status": "failure", "error": str(e)} finally: if cur: @@ -234,7 +234,7 @@ def create_thread(self, user_id): return {"status": "success", "thread_id": inserted_id} except Exception as e: - logger.warning("Error is ", e) + logger.warning(f"Error is {e}") return {"status": "failure", "error": str(e)} finally: @@ -254,7 +254,7 @@ def get_all_threads(self, user_id): for x in result ] except Exception as e: - logger.warning("Error is ", e) + logger.warning(f"Error is {e}") return [] finally: if cur: @@ -277,7 +277,7 @@ def set_thread_name(self, thread_id, user_id, thread_name): self.conn.commit() return {"status": "success"} except Exception as e: - logger.warning("Error is ", e) + logger.warning(f"Error is {e}") return {"status": "failure", "error": str(e)} finally: if cur: @@ -296,7 +296,7 @@ def append_message(self, user_id, thread_id, role, content, function_name=None): self.conn.commit() return {"status": "success"} except Exception as e: - logger.warning("Error is ", e) + logger.warning(f"Error is {e}") return {"status": "failure", "error": str(e)} finally: if cur: @@ -360,7 +360,7 @@ def get_thread_llm(self, thread_id, user_id): } return retval except Exception as e: - logger.warning("Error is ", e) + logger.warning(f"Error is {e}") return {} finally: if cur: @@ -384,7 +384,7 @@ def snapshot_thread(self, thread_id, user_id): logger.info(f"Result is {result}") return result except Exception as e: - logger.warning("Error is ", e) + logger.warning(f"Error is {e}") return {"status": "failure", "error": str(e)} finally: if cur: @@ -399,7 +399,7 @@ def get_snapshot(self, share_uuid): result = cur.fetchone()[0] return result except Exception as e: - logger.warning("Error is ", e) + logger.warning(f"Error is {e}") return {} finally: if cur: @@ -420,7 +420,7 @@ def delete_thread(self, thread_id, user_id): self.conn.commit() return {"status": "success"} except Exception as e: - logger.warning("Error is ", e) + logger.warning(f"Error is {e}") return {"status": "failure", "error": str(e)} finally: if cur: @@ -434,7 +434,7 @@ def logout(self, user_id): self.conn.commit() return {"status": "success"} except Exception as e: - logger.warning("Error is ", e) + logger.warning(f"Error is {e}") return {"status": "failure", "error": str(e)} finally: if cur: @@ -471,7 +471,7 @@ def update_password(self, user_id, new_password_hash): cur.close() return {"status": "success"} except Exception as e: - logger.warning("Error is ", e) + logger.warning(f"Error is {e}") return {"status": "failure", "error": str(e)} finally: if cur: