Skip to content

Commit d0c91d3

Browse files
authored
Fixtures: Adding API key model (#294)
* reshuffling fixtures * updating types * updating types
1 parent f1d651b commit d0c91d3

8 files changed

Lines changed: 55 additions & 48 deletions

File tree

backend/app/tests/api/routes/collections/test_collection_info.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ def create_collection(
3737

3838

3939
def test_collection_info_processing(
40-
db: Session, client: TestClient, normal_user_api_key_headers
40+
db: Session, client: TestClient, user_api_key_header
4141
):
42-
headers = normal_user_api_key_headers
42+
headers = user_api_key_header
4343
user = get_user_from_api_key(db, headers)
4444
collection = create_collection(db, user, status=CollectionStatus.processing)
4545

@@ -58,9 +58,9 @@ def test_collection_info_processing(
5858

5959

6060
def test_collection_info_successful(
61-
db: Session, client: TestClient, normal_user_api_key_headers
61+
db: Session, client: TestClient, user_api_key_header
6262
):
63-
headers = normal_user_api_key_headers
63+
headers = user_api_key_header
6464
user = get_user_from_api_key(db, headers)
6565
collection = create_collection(
6666
db, user, status=CollectionStatus.successful, with_llm=True
@@ -80,10 +80,8 @@ def test_collection_info_successful(
8080
assert data["llm_service_name"] == "gpt-4o"
8181

8282

83-
def test_collection_info_failed(
84-
db: Session, client: TestClient, normal_user_api_key_headers
85-
):
86-
headers = normal_user_api_key_headers
83+
def test_collection_info_failed(db: Session, client: TestClient, user_api_key_header):
84+
headers = user_api_key_header
8785
user = get_user_from_api_key(db, headers)
8886
collection = create_collection(db, user, status=CollectionStatus.failed)
8987

backend/app/tests/api/routes/collections/test_create_collections.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class TestCollectionRouteCreate:
4747

4848
@openai_responses.mock()
4949
def test_create_collection_success(
50-
self, client: TestClient, db: Session, normal_user_api_key_headers
50+
self, client: TestClient, db: Session, user_api_key_header
5151
):
5252
store = DocumentStore(db)
5353
documents = store.fill(self._n_documents)
@@ -60,7 +60,7 @@ def test_create_collection_success(
6060
"instructions": "Test collection assistant.",
6161
"temperature": 0.1,
6262
}
63-
headers = normal_user_api_key_headers
63+
headers = user_api_key_header
6464

6565
response = client.post(
6666
f"{settings.API_V1_STR}/collections/create", json=body, headers=headers

backend/app/tests/api/routes/documents/test_route_document_upload.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def put(self, route: Route, scratch: Path):
2525
with scratch.open("rb") as fp:
2626
return self.client.post(
2727
str(route),
28-
headers=self.normal_user_api_key_headers,
28+
headers=self.user_api_key_header,
2929
files={
3030
"src": (str(scratch), fp, mtype),
3131
},
@@ -45,8 +45,8 @@ def route():
4545

4646

4747
@pytest.fixture
48-
def uploader(client: TestClient, normal_user_api_key_headers: dict[str, str]):
49-
return WebUploader(client, normal_user_api_key_headers)
48+
def uploader(client: TestClient, user_api_key_header: dict[str, str]):
49+
return WebUploader(client, user_api_key_header)
5050

5151

5252
@pytest.fixture(scope="class")

backend/app/tests/api/routes/test_assistants.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def assistant_id():
3030
def test_ingest_assistant_success(
3131
mock_fetch_assistant,
3232
client: TestClient,
33-
normal_user_api_key_headers: dict[str, str],
33+
user_api_key_header: dict[str, str],
3434
):
3535
"""Test successful assistant ingestion from OpenAI."""
3636
mock_assistant = mock_openai_assistant()
@@ -39,7 +39,7 @@ def test_ingest_assistant_success(
3939

4040
response = client.post(
4141
f"/api/v1/assistant/{mock_assistant.id}/ingest",
42-
headers=normal_user_api_key_headers,
42+
headers=user_api_key_header,
4343
)
4444

4545
assert response.status_code == 201
@@ -53,7 +53,7 @@ def test_create_assistant_success(
5353
mock_verify_vector_ids,
5454
client: TestClient,
5555
assistant_create_payload: dict,
56-
normal_user_api_key_headers: dict,
56+
user_api_key_header: dict,
5757
):
5858
"""Test successful assistant creation with OpenAI vector store ID verification."""
5959

@@ -62,7 +62,7 @@ def test_create_assistant_success(
6262
response = client.post(
6363
"/api/v1/assistant",
6464
json=assistant_create_payload,
65-
headers=normal_user_api_key_headers,
65+
headers=user_api_key_header,
6666
)
6767

6868
assert response.status_code == 201
@@ -92,7 +92,7 @@ def test_create_assistant_invalid_vector_store(
9292
mock_verify_vector_ids,
9393
client: TestClient,
9494
assistant_create_payload: dict,
95-
normal_user_api_key_headers: dict,
95+
user_api_key_header: dict,
9696
):
9797
"""Test failure when one or more vector store IDs are invalid."""
9898

@@ -106,7 +106,7 @@ def test_create_assistant_invalid_vector_store(
106106
response = client.post(
107107
"/api/v1/assistant",
108108
json=payload,
109-
headers=normal_user_api_key_headers,
109+
headers=user_api_key_header,
110110
)
111111

112112
assert response.status_code == 400
@@ -175,7 +175,7 @@ def test_update_assistant_invalid_vector_store(
175175

176176
def test_update_assistant_not_found(
177177
client: TestClient,
178-
normal_user_api_key_headers: dict,
178+
user_api_key_header: dict,
179179
):
180180
"""Test failure when updating a non-existent assistant."""
181181
update_payload = {"name": "Updated Assistant"}
@@ -185,7 +185,7 @@ def test_update_assistant_not_found(
185185
response = client.patch(
186186
f"/api/v1/assistant/{non_existent_id}",
187187
json=update_payload,
188-
headers=normal_user_api_key_headers,
188+
headers=user_api_key_header,
189189
)
190190

191191
assert response.status_code == 404
@@ -217,14 +217,14 @@ def test_get_assistant_success(
217217

218218
def test_get_assistant_not_found(
219219
client: TestClient,
220-
normal_user_api_key_headers: dict,
220+
user_api_key_header: dict,
221221
):
222222
"""Test failure when fetching a non-existent assistant."""
223223
non_existent_id = str(uuid4())
224224

225225
response = client.get(
226226
f"/api/v1/assistant/{non_existent_id}",
227-
headers=normal_user_api_key_headers,
227+
headers=user_api_key_header,
228228
)
229229

230230
assert response.status_code == 404
@@ -258,27 +258,27 @@ def test_list_assistants_success(
258258

259259
def test_list_assistants_invalid_pagination(
260260
client: TestClient,
261-
normal_user_api_key_headers: dict,
261+
user_api_key_header: dict,
262262
):
263263
"""Test assistants list with invalid pagination parameters."""
264264
# Test negative skip
265265
response = client.get(
266266
"/api/v1/assistant/?skip=-1&limit=10",
267-
headers=normal_user_api_key_headers,
267+
headers=user_api_key_header,
268268
)
269269
assert response.status_code == 422
270270

271271
# Test limit too high
272272
response = client.get(
273273
"/api/v1/assistant/?skip=0&limit=101",
274-
headers=normal_user_api_key_headers,
274+
headers=user_api_key_header,
275275
)
276276
assert response.status_code == 422
277277

278278
# Test limit too low
279279
response = client.get(
280280
"/api/v1/assistant/?skip=0&limit=0",
281-
headers=normal_user_api_key_headers,
281+
headers=user_api_key_header,
282282
)
283283
assert response.status_code == 422
284284

@@ -304,14 +304,14 @@ def test_delete_assistant_success(
304304

305305
def test_delete_assistant_not_found(
306306
client: TestClient,
307-
normal_user_api_key_headers: dict,
307+
user_api_key_header: dict,
308308
):
309309
"""Test failure when deleting a non-existent assistant."""
310310
non_existent_id = str(uuid4())
311311

312312
response = client.delete(
313313
f"/api/v1/assistant/{non_existent_id}",
314-
headers=normal_user_api_key_headers,
314+
headers=user_api_key_header,
315315
)
316316

317317
assert response.status_code == 404

backend/app/tests/api/routes/test_responses.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
@patch("app.api.routes.responses.OpenAI")
1717
@patch("app.api.routes.responses.get_provider_credential")
1818
def test_responses_endpoint_success(
19-
mock_get_credential, mock_openai, db, normal_user_api_key_headers: dict[str, str]
19+
mock_get_credential, mock_openai, db, user_api_key_header: dict[str, str]
2020
):
2121
"""Test the /responses endpoint for successful response creation."""
2222
# Setup mock credentials
@@ -48,9 +48,7 @@ def test_responses_endpoint_success(
4848
"callback_url": "http://example.com/callback",
4949
}
5050

51-
response = client.post(
52-
"/responses", json=request_data, headers=normal_user_api_key_headers
53-
)
51+
response = client.post("/responses", json=request_data, headers=user_api_key_header)
5452

5553
assert response.status_code == 200
5654
response_json = response.json()
@@ -67,7 +65,7 @@ def test_responses_endpoint_without_vector_store(
6765
mock_get_credential,
6866
mock_openai,
6967
db,
70-
normal_user_api_key_headers,
68+
user_api_key_header,
7169
):
7270
"""Test the /responses endpoint when assistant has no vector store configured."""
7371
# Setup mock credentials
@@ -107,9 +105,7 @@ def test_responses_endpoint_without_vector_store(
107105
"callback_url": "http://example.com/callback",
108106
}
109107

110-
response = client.post(
111-
"/responses", json=request_data, headers=normal_user_api_key_headers
112-
)
108+
response = client.post("/responses", json=request_data, headers=user_api_key_header)
113109
assert response.status_code == 200
114110
response_json = response.json()
115111
assert response_json["success"] is True

backend/app/tests/conftest.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from app.core.db import engine
1212
from app.api.deps import get_db
1313
from app.main import app
14+
from app.models import APIKeyPublic
1415
from app.tests.utils.user import authentication_token_from_email
1516
from app.tests.utils.utils import get_superuser_token_headers, get_api_key_by_email
1617
from app.seed_data.seed_data import seed_database
@@ -64,12 +65,24 @@ def normal_user_token_headers(client: TestClient, db: Session) -> dict[str, str]
6465

6566

6667
@pytest.fixture(scope="function")
67-
def superuser_api_key_headers(db: Session) -> dict[str, str]:
68+
def superuser_api_key_header(db: Session) -> dict[str, str]:
6869
api_key = get_api_key_by_email(db, settings.FIRST_SUPERUSER)
69-
return {"X-API-KEY": api_key}
70+
return {"X-API-KEY": api_key.key}
7071

7172

7273
@pytest.fixture(scope="function")
73-
def normal_user_api_key_headers(db: Session) -> dict[str, str]:
74+
def user_api_key_header(db: Session) -> dict[str, str]:
7475
api_key = get_api_key_by_email(db, settings.EMAIL_TEST_USER)
75-
return {"X-API-KEY": api_key}
76+
return {"X-API-KEY": api_key.key}
77+
78+
79+
@pytest.fixture(scope="function")
80+
def superuser_api_key(db: Session) -> APIKeyPublic:
81+
api_key = get_api_key_by_email(db, settings.FIRST_SUPERUSER)
82+
return api_key
83+
84+
85+
@pytest.fixture(scope="function")
86+
def user_api_key(db: Session) -> APIKeyPublic:
87+
api_key = get_api_key_by_email(db, settings.EMAIL_TEST_USER)
88+
return api_key

backend/app/tests/utils/document.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,18 @@ def append(self, doc: Document, suffix: str = None):
113113
@dataclass
114114
class WebCrawler:
115115
client: TestClient
116-
normal_user_api_key_headers: dict[str, str]
116+
user_api_key_header: dict[str, str]
117117

118118
def get(self, route: Route):
119119
return self.client.get(
120120
str(route),
121-
headers=self.normal_user_api_key_headers,
121+
headers=self.user_api_key_header,
122122
)
123123

124124
def delete(self, route: Route):
125125
return self.client.delete(
126126
str(route),
127-
headers=self.normal_user_api_key_headers,
127+
headers=self.user_api_key_header,
128128
)
129129

130130

@@ -158,5 +158,5 @@ def to_dict(self):
158158

159159

160160
@pytest.fixture
161-
def crawler(client: TestClient, normal_user_api_key_headers: dict[str, str]):
162-
return WebCrawler(client, normal_user_api_key_headers)
161+
def crawler(client: TestClient, user_api_key_header: dict[str, str]):
162+
return WebCrawler(client, user_api_key_header)

backend/app/tests/utils/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ def get_superuser_token_headers(client: TestClient) -> dict[str, str]:
4646
return headers
4747

4848

49-
def get_api_key_by_email(db: Session, email: EmailStr) -> str:
49+
def get_api_key_by_email(db: Session, email: EmailStr) -> APIKeyPublic:
5050
user = get_user_by_email(session=db, email=email)
5151
api_key = get_api_key_by_user_id(db, user_id=user.id)
5252

53-
return api_key.key
53+
return api_key
5454

5555

5656
def get_user_id_by_email(db: Session) -> int:

0 commit comments

Comments
 (0)