2021-08-17 10:27:16 +00:00
|
|
|
# Standard Modules
|
2022-07-19 23:01:56 +00:00
|
|
|
from io import BytesIO
|
2022-09-12 13:42:41 +00:00
|
|
|
from urllib.parse import quote
|
2023-11-21 20:30:43 +00:00
|
|
|
|
2023-10-26 16:42:29 +00:00
|
|
|
import pytest
|
2023-11-21 20:30:43 +00:00
|
|
|
from fastapi import FastAPI
|
2021-08-17 10:27:16 +00:00
|
|
|
|
|
|
|
# External Packages
|
|
|
|
from fastapi.testclient import TestClient
|
2023-11-21 20:30:43 +00:00
|
|
|
from PIL import Image
|
2021-08-17 10:27:16 +00:00
|
|
|
|
|
|
|
# Internal Packages
|
2023-03-01 06:35:21 +00:00
|
|
|
from khoj.configure import configure_routes, configure_search_types
|
2023-11-21 20:30:43 +00:00
|
|
|
from khoj.database.adapters import EntryAdapters
|
|
|
|
from khoj.database.models import KhojApiUser, KhojUser
|
2023-11-22 06:11:32 +00:00
|
|
|
from khoj.processor.content.org_mode.org_to_entries import OrgToEntries
|
2023-11-21 20:30:43 +00:00
|
|
|
from khoj.search_type import image_search, text_search
|
2023-03-01 06:35:21 +00:00
|
|
|
from khoj.utils import state
|
2023-02-14 20:50:51 +00:00
|
|
|
from khoj.utils.rawconfig import ContentConfig, SearchConfig
|
2023-11-21 20:30:43 +00:00
|
|
|
from khoj.utils.state import config, content_index, search_models
|
2021-08-17 10:27:16 +00:00
|
|
|
|
|
|
|
|
2021-08-22 02:21:38 +00:00
|
|
|
# Test
|
2021-09-30 02:02:55 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 19:33:03 +00:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
|
|
|
def test_search_with_no_auth_key(client):
|
|
|
|
# Arrange
|
|
|
|
user_query = quote("How to call Khoj from Emacs?")
|
|
|
|
|
|
|
|
# Act
|
|
|
|
response = client.get(f"/api/search?q={user_query}")
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 403
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
|
|
|
def test_search_with_invalid_auth_key(client):
|
|
|
|
# Arrange
|
|
|
|
headers = {"Authorization": "Bearer invalid-token"}
|
|
|
|
user_query = quote("How to call Khoj from Emacs?")
|
|
|
|
|
|
|
|
# Act
|
|
|
|
response = client.get(f"/api/search?q={user_query}", headers=headers)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 403
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-03-01 01:27:12 +00:00
|
|
|
def test_search_with_invalid_content_type(client):
|
2021-09-30 02:02:55 +00:00
|
|
|
# Arrange
|
2023-10-26 19:33:03 +00:00
|
|
|
headers = {"Authorization": "Bearer kk-secret"}
|
2022-09-12 13:42:41 +00:00
|
|
|
user_query = quote("How to call Khoj from Emacs?")
|
2021-09-30 02:02:55 +00:00
|
|
|
|
|
|
|
# Act
|
2023-10-26 19:33:03 +00:00
|
|
|
response = client.get(f"/api/search?q={user_query}&t=invalid_content_type", headers=headers)
|
2021-09-30 02:02:55 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 422
|
|
|
|
|
|
|
|
|
2021-09-30 03:47:58 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 19:33:03 +00:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-03-01 01:27:12 +00:00
|
|
|
def test_search_with_valid_content_type(client):
|
2023-10-26 19:33:03 +00:00
|
|
|
headers = {"Authorization": "Bearer kk-secret"}
|
|
|
|
for content_type in ["all", "org", "markdown", "image", "pdf", "github", "notion", "plaintext"]:
|
2021-09-30 02:02:55 +00:00
|
|
|
# Act
|
2023-10-26 19:33:03 +00:00
|
|
|
response = client.get(f"/api/search?q=random&t={content_type}", headers=headers)
|
2021-09-30 02:02:55 +00:00
|
|
|
# Assert
|
2023-07-19 02:34:09 +00:00
|
|
|
assert response.status_code == 200, f"Returned status: {response.status_code} for content type: {content_type}"
|
2021-09-30 02:02:55 +00:00
|
|
|
|
|
|
|
|
2022-06-29 19:09:48 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 19:33:03 +00:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
|
|
|
def test_index_update_with_no_auth_key(client):
|
|
|
|
# Arrange
|
|
|
|
files = get_sample_files_data()
|
|
|
|
|
|
|
|
# Act
|
|
|
|
response = client.post("/api/v1/index/update", files=files)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 403
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
|
|
|
def test_index_update_with_invalid_auth_key(client):
|
|
|
|
# Arrange
|
|
|
|
files = get_sample_files_data()
|
|
|
|
headers = {"Authorization": "Bearer kk-invalid-token"}
|
|
|
|
|
|
|
|
# Act
|
|
|
|
response = client.post("/api/v1/index/update", files=files, headers=headers)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 403
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-03-01 01:27:12 +00:00
|
|
|
def test_update_with_invalid_content_type(client):
|
2023-10-26 19:33:03 +00:00
|
|
|
# Arrange
|
|
|
|
headers = {"Authorization": "Bearer kk-secret"}
|
|
|
|
|
2022-06-29 19:09:48 +00:00
|
|
|
# Act
|
2023-10-26 19:33:03 +00:00
|
|
|
response = client.get(f"/api/update?t=invalid_content_type", headers=headers)
|
2022-06-29 19:09:48 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 422
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 19:33:03 +00:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-08-31 19:55:17 +00:00
|
|
|
def test_regenerate_with_invalid_content_type(client):
|
2023-10-26 19:33:03 +00:00
|
|
|
# Arrange
|
|
|
|
headers = {"Authorization": "Bearer kk-secret"}
|
|
|
|
|
2023-07-29 01:47:56 +00:00
|
|
|
# Act
|
2023-10-26 19:33:03 +00:00
|
|
|
response = client.get(f"/api/update?force=true&t=invalid_content_type", headers=headers)
|
2023-07-29 01:47:56 +00:00
|
|
|
|
|
|
|
# Assert
|
2023-08-31 19:55:17 +00:00
|
|
|
assert response.status_code == 422
|
2023-07-29 01:47:56 +00:00
|
|
|
|
|
|
|
|
2023-11-26 04:28:04 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
|
|
|
def test_index_update_big_files(client):
|
|
|
|
# Arrange
|
2023-11-26 04:48:32 +00:00
|
|
|
state.billing_enabled = True
|
2023-11-26 04:28:04 +00:00
|
|
|
files = get_big_size_sample_files_data()
|
|
|
|
headers = {"Authorization": "Bearer kk-secret"}
|
|
|
|
|
|
|
|
# Act
|
|
|
|
response = client.post("/api/v1/index/update", files=files, headers=headers)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 429
|
|
|
|
|
|
|
|
|
2023-11-26 21:09:00 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
|
|
|
def test_index_update_medium_file_unsubscribed(client, api_user4: KhojApiUser):
|
|
|
|
# Arrange
|
|
|
|
api_token = api_user4.token
|
|
|
|
state.billing_enabled = True
|
|
|
|
files = get_medium_size_sample_files_data()
|
|
|
|
headers = {"Authorization": f"Bearer {api_token}"}
|
|
|
|
|
|
|
|
# Act
|
|
|
|
response = client.post("/api/v1/index/update", files=files, headers=headers)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 429
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
|
|
|
def test_index_update_normal_file_unsubscribed(client, api_user4: KhojApiUser):
|
|
|
|
# Arrange
|
|
|
|
api_token = api_user4.token
|
|
|
|
state.billing_enabled = True
|
|
|
|
files = get_sample_files_data()
|
|
|
|
headers = {"Authorization": f"Bearer {api_token}"}
|
|
|
|
|
|
|
|
# Act
|
|
|
|
response = client.post("/api/v1/index/update", files=files, headers=headers)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
|
2023-11-26 04:28:04 +00:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
|
|
|
def test_index_update_big_files_no_billing(client):
|
|
|
|
# Arrange
|
2023-11-26 04:48:32 +00:00
|
|
|
state.billing_enabled = False
|
2023-11-26 04:28:04 +00:00
|
|
|
files = get_big_size_sample_files_data()
|
|
|
|
headers = {"Authorization": "Bearer kk-secret"}
|
|
|
|
|
|
|
|
# Act
|
|
|
|
response = client.post("/api/v1/index/update", files=files, headers=headers)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
|
2021-09-30 03:47:58 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 19:33:03 +00:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-10-17 11:30:27 +00:00
|
|
|
def test_index_update(client):
|
2023-08-31 19:55:17 +00:00
|
|
|
# Arrange
|
2023-10-12 23:16:51 +00:00
|
|
|
files = get_sample_files_data()
|
2023-10-26 19:33:03 +00:00
|
|
|
headers = {"Authorization": "Bearer kk-secret"}
|
2023-08-31 19:55:17 +00:00
|
|
|
|
2021-09-30 02:02:55 +00:00
|
|
|
# Act
|
2023-10-17 11:30:27 +00:00
|
|
|
response = client.post("/api/v1/index/update", files=files, headers=headers)
|
2021-09-30 02:02:55 +00:00
|
|
|
|
|
|
|
# Assert
|
2023-08-31 19:55:17 +00:00
|
|
|
assert response.status_code == 200
|
2021-09-30 02:02:55 +00:00
|
|
|
|
|
|
|
|
2021-09-30 03:47:58 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 19:33:03 +00:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-03-01 01:27:12 +00:00
|
|
|
def test_regenerate_with_valid_content_type(client):
|
2023-10-26 16:42:29 +00:00
|
|
|
for content_type in ["all", "org", "markdown", "image", "pdf", "notion"]:
|
2023-08-31 19:55:17 +00:00
|
|
|
# Arrange
|
2023-10-12 23:16:51 +00:00
|
|
|
files = get_sample_files_data()
|
2023-10-26 19:33:03 +00:00
|
|
|
headers = {"Authorization": "Bearer kk-secret"}
|
2023-08-31 19:55:17 +00:00
|
|
|
|
2021-09-30 02:02:55 +00:00
|
|
|
# Act
|
2023-10-17 11:42:04 +00:00
|
|
|
response = client.post(f"/api/v1/index/update?t={content_type}", files=files, headers=headers)
|
2023-10-26 19:33:03 +00:00
|
|
|
|
2021-09-30 02:02:55 +00:00
|
|
|
# Assert
|
2023-07-19 02:34:09 +00:00
|
|
|
assert response.status_code == 200, f"Returned status: {response.status_code} for content type: {content_type}"
|
2021-09-30 02:02:55 +00:00
|
|
|
|
|
|
|
|
2023-07-29 01:47:56 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 19:33:03 +00:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-07-29 01:47:56 +00:00
|
|
|
def test_regenerate_with_github_fails_without_pat(client):
|
|
|
|
# Act
|
2023-10-26 19:33:03 +00:00
|
|
|
headers = {"Authorization": "Bearer kk-secret"}
|
|
|
|
response = client.get(f"/api/update?force=true&t=github", headers=headers)
|
2023-07-29 01:47:56 +00:00
|
|
|
|
2023-08-31 19:55:17 +00:00
|
|
|
# Arrange
|
2023-10-12 23:16:51 +00:00
|
|
|
files = get_sample_files_data()
|
2023-08-31 19:55:17 +00:00
|
|
|
|
|
|
|
# Act
|
2023-10-17 11:42:04 +00:00
|
|
|
response = client.post(f"/api/v1/index/update?t=github", files=files, headers=headers)
|
2023-10-26 19:33:03 +00:00
|
|
|
|
2023-07-29 01:47:56 +00:00
|
|
|
# Assert
|
2023-08-31 19:55:17 +00:00
|
|
|
assert response.status_code == 200, f"Returned status: {response.status_code} for content type: github"
|
2023-07-29 01:47:56 +00:00
|
|
|
|
|
|
|
|
2023-03-01 04:08:26 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 16:42:29 +00:00
|
|
|
@pytest.mark.django_db
|
|
|
|
def test_get_configured_types_via_api(client, sample_org_data):
|
2023-03-01 04:08:26 +00:00
|
|
|
# Act
|
2023-11-01 21:51:33 +00:00
|
|
|
text_search.setup(OrgToEntries, sample_org_data, regenerate=False)
|
2023-03-01 06:35:21 +00:00
|
|
|
|
2023-11-01 01:50:54 +00:00
|
|
|
enabled_types = EntryAdapters.get_unique_file_types(user=None).all().values_list("file_type", flat=True)
|
2023-03-01 06:35:21 +00:00
|
|
|
|
|
|
|
# Assert
|
2023-10-26 16:42:29 +00:00
|
|
|
assert list(enabled_types) == ["org"]
|
2023-03-01 06:35:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 16:42:29 +00:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-10-26 19:33:03 +00:00
|
|
|
def test_get_api_config_types(client, sample_org_data, default_user: KhojUser):
|
2023-03-01 06:35:21 +00:00
|
|
|
# Arrange
|
2023-10-26 19:33:03 +00:00
|
|
|
headers = {"Authorization": "Bearer kk-secret"}
|
2023-11-01 21:51:33 +00:00
|
|
|
text_search.setup(OrgToEntries, sample_org_data, regenerate=False, user=default_user)
|
2023-10-26 18:37:41 +00:00
|
|
|
|
|
|
|
# Act
|
2023-10-26 19:33:03 +00:00
|
|
|
response = client.get(f"/api/config/types", headers=headers)
|
2023-10-26 18:37:41 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
2023-10-26 19:33:03 +00:00
|
|
|
assert response.json() == ["all", "org", "image", "plaintext"]
|
2023-03-01 06:35:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 16:42:29 +00:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
|
|
|
def test_get_configured_types_with_no_content_config(fastapi_app: FastAPI):
|
2023-03-01 06:35:21 +00:00
|
|
|
# Arrange
|
2023-10-26 19:33:03 +00:00
|
|
|
state.anonymous_mode = True
|
2023-11-15 08:17:44 +00:00
|
|
|
if state.config and state.config.content_type:
|
|
|
|
state.config.content_type = None
|
|
|
|
state.search_models = configure_search_types()
|
2023-03-01 06:35:21 +00:00
|
|
|
|
2023-10-26 16:42:29 +00:00
|
|
|
configure_routes(fastapi_app)
|
|
|
|
client = TestClient(fastapi_app)
|
2023-03-01 06:35:21 +00:00
|
|
|
|
|
|
|
# Act
|
|
|
|
response = client.get(f"/api/config/types")
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
2023-06-29 03:10:57 +00:00
|
|
|
assert response.json() == ["all"]
|
2023-03-01 06:35:21 +00:00
|
|
|
|
|
|
|
|
2021-10-03 03:28:33 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 17:17:29 +00:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-03-01 01:27:12 +00:00
|
|
|
def test_image_search(client, content_config: ContentConfig, search_config: SearchConfig):
|
2021-10-03 03:28:33 +00:00
|
|
|
# Arrange
|
2023-10-26 19:33:03 +00:00
|
|
|
headers = {"Authorization": "Bearer kk-secret"}
|
2023-07-14 08:19:38 +00:00
|
|
|
search_models.image_search = image_search.initialize_model(search_config.image)
|
|
|
|
content_index.image = image_search.setup(
|
|
|
|
content_config.image, search_models.image_search.image_encoder, regenerate=False
|
|
|
|
)
|
2023-02-17 16:04:26 +00:00
|
|
|
query_expected_image_pairs = [
|
|
|
|
("kitten", "kitten_park.jpg"),
|
|
|
|
("a horse and dog on a leash", "horse_dog.jpg"),
|
|
|
|
("A guinea pig eating grass", "guineapig_grass.jpg"),
|
|
|
|
]
|
2021-10-03 03:28:33 +00:00
|
|
|
|
|
|
|
for query, expected_image_name in query_expected_image_pairs:
|
2022-07-19 23:01:56 +00:00
|
|
|
# Act
|
2023-10-26 19:33:03 +00:00
|
|
|
response = client.get(f"/api/search?q={query}&n=1&t=image", headers=headers)
|
2021-10-03 03:28:33 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
2022-07-19 23:01:56 +00:00
|
|
|
actual_image = Image.open(BytesIO(client.get(response.json()[0]["entry"]).content))
|
|
|
|
expected_image = Image.open(content_config.image.input_directories[0].joinpath(expected_image_name))
|
2021-10-03 03:28:33 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert expected_image == actual_image
|
|
|
|
|
|
|
|
|
2021-08-17 10:27:16 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 16:42:29 +00:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-10-26 19:33:03 +00:00
|
|
|
def test_notes_search(client, search_config: SearchConfig, sample_org_data, default_user: KhojUser):
|
2021-09-30 03:24:27 +00:00
|
|
|
# Arrange
|
2023-10-26 19:33:03 +00:00
|
|
|
headers = {"Authorization": "Bearer kk-secret"}
|
2023-11-01 21:51:33 +00:00
|
|
|
text_search.setup(OrgToEntries, sample_org_data, regenerate=False, user=default_user)
|
2022-09-12 13:42:41 +00:00
|
|
|
user_query = quote("How to git install application?")
|
2021-09-30 03:24:27 +00:00
|
|
|
|
|
|
|
# Act
|
2023-11-16 04:22:59 +00:00
|
|
|
response = client.get(f"/api/search?q={user_query}&n=1&t=org&r=true&max_distance=0.18", headers=headers)
|
2021-09-30 03:24:27 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
2023-11-16 04:22:59 +00:00
|
|
|
|
|
|
|
assert len(response.json()) == 1, "Expected only 1 result"
|
2022-07-20 16:33:27 +00:00
|
|
|
search_result = response.json()[0]["entry"]
|
2023-11-16 04:22:59 +00:00
|
|
|
assert "git clone https://github.com/khoj-ai/khoj" in search_result, "Expected 'git clone' in search result"
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
|
|
|
def test_notes_search_no_results(client, search_config: SearchConfig, sample_org_data, default_user: KhojUser):
|
|
|
|
# Arrange
|
|
|
|
headers = {"Authorization": "Bearer kk-secret"}
|
|
|
|
text_search.setup(OrgToEntries, sample_org_data, regenerate=False, user=default_user)
|
|
|
|
user_query = quote("How to find my goat?")
|
|
|
|
|
|
|
|
# Act
|
|
|
|
response = client.get(f"/api/search?q={user_query}&n=1&t=org&r=true&max_distance=0.18", headers=headers)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.json() == [], "Expected no results"
|
2021-09-30 11:13:40 +00:00
|
|
|
|
|
|
|
|
2022-09-12 13:42:41 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 16:42:29 +00:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-08-31 19:55:17 +00:00
|
|
|
def test_notes_search_with_only_filters(
|
2023-10-26 19:33:03 +00:00
|
|
|
client, content_config: ContentConfig, search_config: SearchConfig, sample_org_data, default_user: KhojUser
|
2023-08-31 19:55:17 +00:00
|
|
|
):
|
2022-09-12 13:42:41 +00:00
|
|
|
# Arrange
|
2023-10-26 19:33:03 +00:00
|
|
|
headers = {"Authorization": "Bearer kk-secret"}
|
2023-10-26 16:42:29 +00:00
|
|
|
text_search.setup(
|
2023-11-01 21:51:33 +00:00
|
|
|
OrgToEntries,
|
2023-08-31 19:55:17 +00:00
|
|
|
sample_org_data,
|
|
|
|
regenerate=False,
|
2023-10-26 19:33:03 +00:00
|
|
|
user=default_user,
|
2023-02-17 16:04:26 +00:00
|
|
|
)
|
2022-09-12 13:42:41 +00:00
|
|
|
user_query = quote('+"Emacs" file:"*.org"')
|
|
|
|
|
|
|
|
# Act
|
2023-10-26 19:33:03 +00:00
|
|
|
response = client.get(f"/api/search?q={user_query}&n=1&t=org", headers=headers)
|
2022-09-12 13:42:41 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
# assert actual_data contains word "Emacs"
|
|
|
|
search_result = response.json()[0]["entry"]
|
|
|
|
assert "Emacs" in search_result
|
|
|
|
|
|
|
|
|
2021-09-30 11:13:40 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 16:42:29 +00:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-10-26 19:33:03 +00:00
|
|
|
def test_notes_search_with_include_filter(client, sample_org_data, default_user: KhojUser):
|
2021-09-30 11:13:40 +00:00
|
|
|
# Arrange
|
2023-10-26 19:33:03 +00:00
|
|
|
headers = {"Authorization": "Bearer kk-secret"}
|
2023-11-01 21:51:33 +00:00
|
|
|
text_search.setup(OrgToEntries, sample_org_data, regenerate=False, user=default_user)
|
2022-09-12 13:42:41 +00:00
|
|
|
user_query = quote('How to git install application? +"Emacs"')
|
2021-09-30 11:13:40 +00:00
|
|
|
|
|
|
|
# Act
|
2023-10-26 19:33:03 +00:00
|
|
|
response = client.get(f"/api/search?q={user_query}&n=1&t=org", headers=headers)
|
2021-09-30 11:13:40 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
2022-09-04 14:18:47 +00:00
|
|
|
# assert actual_data contains word "Emacs"
|
2022-07-20 16:33:27 +00:00
|
|
|
search_result = response.json()[0]["entry"]
|
2023-11-16 10:47:58 +00:00
|
|
|
assert "emacs" in search_result
|
2021-09-30 11:13:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-10-26 16:42:29 +00:00
|
|
|
@pytest.mark.django_db(transaction=True)
|
2023-10-26 19:33:03 +00:00
|
|
|
def test_notes_search_with_exclude_filter(client, sample_org_data, default_user: KhojUser):
|
2021-09-30 11:13:40 +00:00
|
|
|
# Arrange
|
2023-10-26 19:33:03 +00:00
|
|
|
headers = {"Authorization": "Bearer kk-secret"}
|
2023-10-26 16:42:29 +00:00
|
|
|
text_search.setup(
|
2023-11-01 21:51:33 +00:00
|
|
|
OrgToEntries,
|
2023-08-31 19:55:17 +00:00
|
|
|
sample_org_data,
|
|
|
|
regenerate=False,
|
2023-10-26 19:33:03 +00:00
|
|
|
user=default_user,
|
2023-02-17 16:04:26 +00:00
|
|
|
)
|
2022-09-12 13:42:41 +00:00
|
|
|
user_query = quote('How to git install application? -"clone"')
|
2021-09-30 11:13:40 +00:00
|
|
|
|
|
|
|
# Act
|
2023-10-26 19:33:03 +00:00
|
|
|
response = client.get(f"/api/search?q={user_query}&n=1&t=org", headers=headers)
|
2021-09-30 11:13:40 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
2023-08-27 01:11:18 +00:00
|
|
|
# assert actual_data does not contains word "clone"
|
2022-07-20 16:33:27 +00:00
|
|
|
search_result = response.json()[0]["entry"]
|
2021-09-30 11:13:40 +00:00
|
|
|
assert "clone" not in search_result
|
2023-08-31 19:55:17 +00:00
|
|
|
|
|
|
|
|
2023-11-16 10:47:58 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
|
|
|
def test_notes_search_requires_parent_context(
|
|
|
|
client, search_config: SearchConfig, sample_org_data, default_user: KhojUser
|
|
|
|
):
|
|
|
|
# Arrange
|
|
|
|
headers = {"Authorization": "Bearer kk-secret"}
|
|
|
|
text_search.setup(OrgToEntries, sample_org_data, regenerate=False, user=default_user)
|
|
|
|
user_query = quote("Install Khoj on Emacs")
|
|
|
|
|
|
|
|
# Act
|
|
|
|
response = client.get(f"/api/search?q={user_query}&n=1&t=org&r=true&max_distance=0.18", headers=headers)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
assert len(response.json()) == 1, "Expected only 1 result"
|
|
|
|
search_result = response.json()[0]["entry"]
|
|
|
|
assert "Emacs load path" in search_result, "Expected 'Emacs load path' in search result"
|
|
|
|
|
|
|
|
|
2023-10-26 16:42:29 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
|
|
|
def test_different_user_data_not_accessed(client, sample_org_data, default_user: KhojUser):
|
|
|
|
# Arrange
|
2023-10-26 19:33:03 +00:00
|
|
|
headers = {"Authorization": "Bearer kk-token"} # Token for default_user2
|
2023-11-01 21:51:33 +00:00
|
|
|
text_search.setup(OrgToEntries, sample_org_data, regenerate=False, user=default_user)
|
2023-10-26 16:42:29 +00:00
|
|
|
user_query = quote("How to git install application?")
|
|
|
|
|
|
|
|
# Act
|
2023-10-26 19:33:03 +00:00
|
|
|
response = client.get(f"/api/search?q={user_query}&n=1&t=org", headers=headers)
|
2023-10-26 16:42:29 +00:00
|
|
|
|
|
|
|
# Assert
|
2023-10-26 19:33:03 +00:00
|
|
|
assert response.status_code == 403
|
2023-10-26 16:42:29 +00:00
|
|
|
# assert actual response has no data as the default_user is different from the user making the query (anonymous)
|
2023-10-26 19:33:03 +00:00
|
|
|
assert len(response.json()) == 1 and response.json()["detail"] == "Forbidden"
|
2023-10-26 16:42:29 +00:00
|
|
|
|
|
|
|
|
2023-11-10 22:00:58 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
|
|
|
def test_user_no_data_returns_empty(client, sample_org_data, api_user3: KhojApiUser):
|
|
|
|
# Arrange
|
|
|
|
token = api_user3.token
|
|
|
|
headers = {"Authorization": "Bearer " + token}
|
|
|
|
user_query = quote("How to git install application?")
|
|
|
|
|
|
|
|
# Act
|
|
|
|
response = client.get(f"/api/search?q={user_query}&n=1&t=org", headers=headers)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
# assert actual response has no data as the default_user3, though other users have data
|
|
|
|
assert len(response.json()) == 0
|
|
|
|
assert response.json() == []
|
|
|
|
|
|
|
|
|
2023-08-31 19:55:17 +00:00
|
|
|
def get_sample_files_data():
|
2023-11-04 21:29:30 +00:00
|
|
|
return [
|
|
|
|
("files", ("path/to/filename.org", "* practicing piano", "text/org")),
|
|
|
|
("files", ("path/to/filename1.org", "** top 3 reasons why I moved to SF", "text/org")),
|
|
|
|
("files", ("path/to/filename2.org", "* how to build a search engine", "text/org")),
|
|
|
|
("files", ("path/to/filename.pdf", "Moore's law does not apply to consumer hardware", "application/pdf")),
|
|
|
|
("files", ("path/to/filename1.pdf", "The sun is a ball of helium", "application/pdf")),
|
|
|
|
("files", ("path/to/filename2.pdf", "Effect of sunshine on baseline human happiness", "application/pdf")),
|
|
|
|
("files", ("path/to/filename.txt", "data,column,value", "text/plain")),
|
|
|
|
("files", ("path/to/filename1.txt", "<html>my first web page</html>", "text/plain")),
|
|
|
|
("files", ("path/to/filename2.txt", "2021-02-02 Journal Entry", "text/plain")),
|
|
|
|
("files", ("path/to/filename.md", "# Notes from client call", "text/markdown")),
|
|
|
|
(
|
|
|
|
"files",
|
|
|
|
("path/to/filename1.md", "## Studying anthropological records from the Fatimid caliphate", "text/markdown"),
|
2023-10-12 23:16:51 +00:00
|
|
|
),
|
2023-11-04 21:29:30 +00:00
|
|
|
("files", ("path/to/filename2.md", "**Understanding science through the lens of art**", "text/markdown")),
|
|
|
|
]
|
2023-11-26 04:28:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_big_size_sample_files_data():
|
|
|
|
big_text = "a" * (25 * 1024 * 1024) # a string of approximately 25 MB
|
|
|
|
return [
|
|
|
|
(
|
|
|
|
"files",
|
|
|
|
("path/to/filename.org", big_text, "text/org"),
|
|
|
|
)
|
|
|
|
]
|
2023-11-26 21:09:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_medium_size_sample_files_data():
|
|
|
|
big_text = "a" * (10 * 1024 * 1024) # a string of approximately 10 MB
|
|
|
|
return [
|
|
|
|
(
|
|
|
|
"files",
|
|
|
|
("path/to/filename.org", big_text, "text/org"),
|
|
|
|
)
|
|
|
|
]
|