From 262a8574d1af4b36bcc906c90bdfab231772186a Mon Sep 17 00:00:00 2001 From: sabaimran Date: Fri, 10 Nov 2023 14:00:58 -0800 Subject: [PATCH] Add a test to verify that a user without data sucessfully returns a respones to the /search endpoint --- tests/conftest.py | 29 +++++++++++++++++++++++++++++ tests/test_client.py | 20 +++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index 59104123..08ff7033 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -85,6 +85,22 @@ def default_user2(): ) +@pytest.mark.django_db +@pytest.fixture +def default_user3(): + """ + This user should not have any data associated with it + """ + if KhojUser.objects.filter(username="default3").exists(): + return KhojUser.objects.get(username="default3") + + return KhojUser.objects.create( + username="default3", + email="default3@example.com", + password="default3", + ) + + @pytest.mark.django_db @pytest.fixture def api_user(default_user): @@ -111,6 +127,19 @@ def api_user2(default_user2): ) +@pytest.mark.django_db +@pytest.fixture +def api_user3(default_user3): + if KhojApiUser.objects.filter(user=default_user3).exists(): + return KhojApiUser.objects.get(user=default_user3) + + return KhojApiUser.objects.create( + user=default_user3, + name="api-key", + token="kk-diff-secret-3", + ) + + @pytest.fixture(scope="session") def search_models(search_config: SearchConfig): search_models = SearchModels() diff --git a/tests/test_client.py b/tests/test_client.py index c105c605..1894577c 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -16,7 +16,7 @@ from khoj.utils.state import search_models, content_index, config from khoj.search_type import text_search, image_search from khoj.utils.rawconfig import ContentConfig, SearchConfig from khoj.processor.org_mode.org_to_entries import OrgToEntries -from database.models import KhojUser +from database.models import KhojUser, KhojApiUser from database.adapters import EntryAdapters @@ -351,6 +351,24 @@ def test_different_user_data_not_accessed(client, sample_org_data, default_user: assert len(response.json()) == 1 and response.json()["detail"] == "Forbidden" +# ---------------------------------------------------------------------------------------------------- +@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() == [] + + def get_sample_files_data(): return [ ("files", ("path/to/filename.org", "* practicing piano", "text/org")),