mirror of
https://github.com/open-webui/open-webui
synced 2025-06-26 18:26:48 +00:00
feat(sqlalchemy): remove session reference from router
This commit is contained in:
@@ -31,7 +31,6 @@ class TestAuths(AbstractPostgresTest):
|
||||
from utils.utils import get_password_hash
|
||||
|
||||
user = self.auths.insert_new_auth(
|
||||
self.db_session,
|
||||
email="john.doe@openwebui.com",
|
||||
password=get_password_hash("old_password"),
|
||||
name="John Doe",
|
||||
@@ -45,7 +44,7 @@ class TestAuths(AbstractPostgresTest):
|
||||
json={"name": "John Doe 2", "profile_image_url": "/user2.png"},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
db_user = self.users.get_user_by_id(self.db_session, user.id)
|
||||
db_user = self.users.get_user_by_id(user.id)
|
||||
assert db_user.name == "John Doe 2"
|
||||
assert db_user.profile_image_url == "/user2.png"
|
||||
|
||||
@@ -53,7 +52,6 @@ class TestAuths(AbstractPostgresTest):
|
||||
from utils.utils import get_password_hash
|
||||
|
||||
user = self.auths.insert_new_auth(
|
||||
self.db_session,
|
||||
email="john.doe@openwebui.com",
|
||||
password=get_password_hash("old_password"),
|
||||
name="John Doe",
|
||||
@@ -69,11 +67,11 @@ class TestAuths(AbstractPostgresTest):
|
||||
assert response.status_code == 200
|
||||
|
||||
old_auth = self.auths.authenticate_user(
|
||||
self.db_session, "john.doe@openwebui.com", "old_password"
|
||||
"john.doe@openwebui.com", "old_password"
|
||||
)
|
||||
assert old_auth is None
|
||||
new_auth = self.auths.authenticate_user(
|
||||
self.db_session, "john.doe@openwebui.com", "new_password"
|
||||
"john.doe@openwebui.com", "new_password"
|
||||
)
|
||||
assert new_auth is not None
|
||||
|
||||
@@ -81,7 +79,6 @@ class TestAuths(AbstractPostgresTest):
|
||||
from utils.utils import get_password_hash
|
||||
|
||||
user = self.auths.insert_new_auth(
|
||||
self.db_session,
|
||||
email="john.doe@openwebui.com",
|
||||
password=get_password_hash("password"),
|
||||
name="John Doe",
|
||||
@@ -144,7 +141,6 @@ class TestAuths(AbstractPostgresTest):
|
||||
|
||||
def test_get_admin_details(self):
|
||||
self.auths.insert_new_auth(
|
||||
self.db_session,
|
||||
email="john.doe@openwebui.com",
|
||||
password="password",
|
||||
name="John Doe",
|
||||
@@ -162,7 +158,6 @@ class TestAuths(AbstractPostgresTest):
|
||||
|
||||
def test_create_api_key_(self):
|
||||
user = self.auths.insert_new_auth(
|
||||
self.db_session,
|
||||
email="john.doe@openwebui.com",
|
||||
password="password",
|
||||
name="John Doe",
|
||||
@@ -178,31 +173,29 @@ class TestAuths(AbstractPostgresTest):
|
||||
|
||||
def test_delete_api_key(self):
|
||||
user = self.auths.insert_new_auth(
|
||||
self.db_session,
|
||||
email="john.doe@openwebui.com",
|
||||
password="password",
|
||||
name="John Doe",
|
||||
profile_image_url="/user.png",
|
||||
role="admin",
|
||||
)
|
||||
self.users.update_user_api_key_by_id(self.db_session, user.id, "abc")
|
||||
self.users.update_user_api_key_by_id(user.id, "abc")
|
||||
with mock_webui_user(id=user.id):
|
||||
response = self.fast_api_client.delete(self.create_url("/api_key"))
|
||||
assert response.status_code == 200
|
||||
assert response.json() == True
|
||||
db_user = self.users.get_user_by_id(self.db_session, user.id)
|
||||
db_user = self.users.get_user_by_id(user.id)
|
||||
assert db_user.api_key is None
|
||||
|
||||
def test_get_api_key(self):
|
||||
user = self.auths.insert_new_auth(
|
||||
self.db_session,
|
||||
email="john.doe@openwebui.com",
|
||||
password="password",
|
||||
name="John Doe",
|
||||
profile_image_url="/user.png",
|
||||
role="admin",
|
||||
)
|
||||
self.users.update_user_api_key_by_id(self.db_session, user.id, "abc")
|
||||
self.users.update_user_api_key_by_id(user.id, "abc")
|
||||
with mock_webui_user(id=user.id):
|
||||
response = self.fast_api_client.get(self.create_url("/api_key"))
|
||||
assert response.status_code == 200
|
||||
|
||||
@@ -18,7 +18,6 @@ class TestChats(AbstractPostgresTest):
|
||||
|
||||
self.chats = Chats
|
||||
self.chats.insert_new_chat(
|
||||
self.db_session,
|
||||
"2",
|
||||
ChatForm(
|
||||
**{
|
||||
@@ -46,7 +45,7 @@ class TestChats(AbstractPostgresTest):
|
||||
with mock_webui_user(id="2"):
|
||||
response = self.fast_api_client.delete(self.create_url("/"))
|
||||
assert response.status_code == 200
|
||||
assert len(self.chats.get_chats(self.db_session)) == 0
|
||||
assert len(self.chats.get_chats()) == 0
|
||||
|
||||
def test_get_user_chat_list_by_user_id(self):
|
||||
with mock_webui_user(id="3"):
|
||||
@@ -84,14 +83,13 @@ class TestChats(AbstractPostgresTest):
|
||||
assert data["title"] == "New Chat"
|
||||
assert data["updated_at"] is not None
|
||||
assert data["created_at"] is not None
|
||||
assert len(self.chats.get_chats(self.db_session)) == 2
|
||||
assert len(self.chats.get_chats()) == 2
|
||||
|
||||
def test_get_user_chats(self):
|
||||
self.test_get_session_user_chat_list()
|
||||
|
||||
def test_get_user_archived_chats(self):
|
||||
self.chats.archive_all_chats_by_user_id(self.db_session, "2")
|
||||
self.db_session.commit()
|
||||
self.chats.archive_all_chats_by_user_id("2")
|
||||
with mock_webui_user(id="2"):
|
||||
response = self.fast_api_client.get(self.create_url("/all/archived"))
|
||||
assert response.status_code == 200
|
||||
@@ -114,12 +112,11 @@ class TestChats(AbstractPostgresTest):
|
||||
with mock_webui_user(id="2"):
|
||||
response = self.fast_api_client.post(self.create_url("/archive/all"))
|
||||
assert response.status_code == 200
|
||||
assert len(self.chats.get_archived_chats_by_user_id(self.db_session, "2")) == 1
|
||||
assert len(self.chats.get_archived_chats_by_user_id("2")) == 1
|
||||
|
||||
def test_get_shared_chat_by_id(self):
|
||||
chat_id = self.chats.get_chats(self.db_session)[0].id
|
||||
self.chats.update_chat_share_id_by_id(self.db_session, chat_id, chat_id)
|
||||
self.db_session.commit()
|
||||
chat_id = self.chats.get_chats()[0].id
|
||||
self.chats.update_chat_share_id_by_id(chat_id, chat_id)
|
||||
with mock_webui_user(id="2"):
|
||||
response = self.fast_api_client.get(self.create_url(f"/share/{chat_id}"))
|
||||
assert response.status_code == 200
|
||||
@@ -136,7 +133,7 @@ class TestChats(AbstractPostgresTest):
|
||||
assert data["title"] == "New Chat"
|
||||
|
||||
def test_get_chat_by_id(self):
|
||||
chat_id = self.chats.get_chats(self.db_session)[0].id
|
||||
chat_id = self.chats.get_chats()[0].id
|
||||
with mock_webui_user(id="2"):
|
||||
response = self.fast_api_client.get(self.create_url(f"/{chat_id}"))
|
||||
assert response.status_code == 200
|
||||
@@ -153,7 +150,7 @@ class TestChats(AbstractPostgresTest):
|
||||
assert data["user_id"] == "2"
|
||||
|
||||
def test_update_chat_by_id(self):
|
||||
chat_id = self.chats.get_chats(self.db_session)[0].id
|
||||
chat_id = self.chats.get_chats()[0].id
|
||||
with mock_webui_user(id="2"):
|
||||
response = self.fast_api_client.post(
|
||||
self.create_url(f"/{chat_id}"),
|
||||
@@ -181,14 +178,14 @@ class TestChats(AbstractPostgresTest):
|
||||
assert data["user_id"] == "2"
|
||||
|
||||
def test_delete_chat_by_id(self):
|
||||
chat_id = self.chats.get_chats(self.db_session)[0].id
|
||||
chat_id = self.chats.get_chats()[0].id
|
||||
with mock_webui_user(id="2"):
|
||||
response = self.fast_api_client.delete(self.create_url(f"/{chat_id}"))
|
||||
assert response.status_code == 200
|
||||
assert response.json() is True
|
||||
|
||||
def test_clone_chat_by_id(self):
|
||||
chat_id = self.chats.get_chats(self.db_session)[0].id
|
||||
chat_id = self.chats.get_chats()[0].id
|
||||
with mock_webui_user(id="2"):
|
||||
response = self.fast_api_client.get(self.create_url(f"/{chat_id}/clone"))
|
||||
|
||||
@@ -209,31 +206,30 @@ class TestChats(AbstractPostgresTest):
|
||||
assert data["user_id"] == "2"
|
||||
|
||||
def test_archive_chat_by_id(self):
|
||||
chat_id = self.chats.get_chats(self.db_session)[0].id
|
||||
chat_id = self.chats.get_chats()[0].id
|
||||
with mock_webui_user(id="2"):
|
||||
response = self.fast_api_client.get(self.create_url(f"/{chat_id}/archive"))
|
||||
assert response.status_code == 200
|
||||
|
||||
chat = self.chats.get_chat_by_id(self.db_session, chat_id)
|
||||
chat = self.chats.get_chat_by_id(chat_id)
|
||||
assert chat.archived is True
|
||||
|
||||
def test_share_chat_by_id(self):
|
||||
chat_id = self.chats.get_chats(self.db_session)[0].id
|
||||
chat_id = self.chats.get_chats()[0].id
|
||||
with mock_webui_user(id="2"):
|
||||
response = self.fast_api_client.post(self.create_url(f"/{chat_id}/share"))
|
||||
assert response.status_code == 200
|
||||
|
||||
chat = self.chats.get_chat_by_id(self.db_session, chat_id)
|
||||
chat = self.chats.get_chat_by_id(chat_id)
|
||||
assert chat.share_id is not None
|
||||
|
||||
def test_delete_shared_chat_by_id(self):
|
||||
chat_id = self.chats.get_chats(self.db_session)[0].id
|
||||
chat_id = self.chats.get_chats()[0].id
|
||||
share_id = str(uuid.uuid4())
|
||||
self.chats.update_chat_share_id_by_id(self.db_session, chat_id, share_id)
|
||||
self.db_session.commit()
|
||||
self.chats.update_chat_share_id_by_id(chat_id, share_id)
|
||||
with mock_webui_user(id="2"):
|
||||
response = self.fast_api_client.delete(self.create_url(f"/{chat_id}/share"))
|
||||
assert response.status_code
|
||||
|
||||
chat = self.chats.get_chat_by_id(self.db_session, chat_id)
|
||||
chat = self.chats.get_chat_by_id(chat_id)
|
||||
assert chat.share_id is None
|
||||
|
||||
@@ -14,7 +14,7 @@ class TestDocuments(AbstractPostgresTest):
|
||||
|
||||
def test_documents(self):
|
||||
# Empty database
|
||||
assert len(self.documents.get_docs(self.db_session)) == 0
|
||||
assert len(self.documents.get_docs()) == 0
|
||||
with mock_webui_user(id="2"):
|
||||
response = self.fast_api_client.get(self.create_url("/"))
|
||||
assert response.status_code == 200
|
||||
@@ -34,7 +34,7 @@ class TestDocuments(AbstractPostgresTest):
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.json()["name"] == "doc_name"
|
||||
assert len(self.documents.get_docs(self.db_session)) == 1
|
||||
assert len(self.documents.get_docs()) == 1
|
||||
|
||||
# Get the document
|
||||
with mock_webui_user(id="2"):
|
||||
@@ -61,7 +61,7 @@ class TestDocuments(AbstractPostgresTest):
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.json()["name"] == "doc_name 2"
|
||||
assert len(self.documents.get_docs(self.db_session)) == 2
|
||||
assert len(self.documents.get_docs()) == 2
|
||||
|
||||
# Get all documents
|
||||
with mock_webui_user(id="2"):
|
||||
@@ -95,7 +95,7 @@ class TestDocuments(AbstractPostgresTest):
|
||||
assert data["content"] == {
|
||||
"tags": [{"name": "testing-tag"}, {"name": "another-tag"}]
|
||||
}
|
||||
assert len(self.documents.get_docs(self.db_session)) == 2
|
||||
assert len(self.documents.get_docs()) == 2
|
||||
|
||||
# Delete the first document
|
||||
with mock_webui_user(id="2"):
|
||||
@@ -103,4 +103,4 @@ class TestDocuments(AbstractPostgresTest):
|
||||
self.create_url("/doc/delete?name=doc_name rework")
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert len(self.documents.get_docs(self.db_session)) == 1
|
||||
assert len(self.documents.get_docs()) == 1
|
||||
|
||||
@@ -68,6 +68,16 @@ class TestPrompts(AbstractPostgresTest):
|
||||
assert data["content"] == "description Updated"
|
||||
assert data["user_id"] == "3"
|
||||
|
||||
# Get prompt by command
|
||||
with mock_webui_user(id="2"):
|
||||
response = self.fast_api_client.get(self.create_url("/command/my-command2"))
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["command"] == "/my-command2"
|
||||
assert data["title"] == "Hello World Updated"
|
||||
assert data["content"] == "description Updated"
|
||||
assert data["user_id"] == "3"
|
||||
|
||||
# Delete prompt
|
||||
with mock_webui_user(id="2"):
|
||||
response = self.fast_api_client.delete(
|
||||
|
||||
@@ -33,7 +33,6 @@ class TestUsers(AbstractPostgresTest):
|
||||
def setup_method(self):
|
||||
super().setup_method()
|
||||
self.users.insert_new_user(
|
||||
self.db_session,
|
||||
id="1",
|
||||
name="user 1",
|
||||
email="user1@openwebui.com",
|
||||
@@ -41,7 +40,6 @@ class TestUsers(AbstractPostgresTest):
|
||||
role="user",
|
||||
)
|
||||
self.users.insert_new_user(
|
||||
self.db_session,
|
||||
id="2",
|
||||
name="user 2",
|
||||
email="user2@openwebui.com",
|
||||
|
||||
Reference in New Issue
Block a user