Add a test to verify that a user without data sucessfully returns a respones to the /search endpoint

This commit is contained in:
sabaimran 2023-11-10 14:00:58 -08:00
parent e62788ad79
commit 262a8574d1
2 changed files with 48 additions and 1 deletions

View file

@ -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.mark.django_db
@pytest.fixture @pytest.fixture
def api_user(default_user): 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") @pytest.fixture(scope="session")
def search_models(search_config: SearchConfig): def search_models(search_config: SearchConfig):
search_models = SearchModels() search_models = SearchModels()

View file

@ -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.search_type import text_search, image_search
from khoj.utils.rawconfig import ContentConfig, SearchConfig from khoj.utils.rawconfig import ContentConfig, SearchConfig
from khoj.processor.org_mode.org_to_entries import OrgToEntries 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 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" 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(): def get_sample_files_data():
return [ return [
("files", ("path/to/filename.org", "* practicing piano", "text/org")), ("files", ("path/to/filename.org", "* practicing piano", "text/org")),