|
| 1 | +import logging |
| 2 | +import os |
| 3 | + |
1 | 4 | import pytest |
| 5 | +import weaviate |
| 6 | +from _pytest.logging import LogCaptureFixture |
2 | 7 | from fastapi.testclient import TestClient |
| 8 | +from loguru import logger |
3 | 9 | from weaviate import Client |
4 | | -import weaviate |
5 | | -import os |
6 | | -from models.models import DocumentMetadataFilter, Source |
7 | | -from server.main import app |
| 10 | + |
8 | 11 | from datastore.providers.weaviate_datastore import ( |
9 | 12 | SCHEMA, |
10 | 13 | WeaviateDataStore, |
11 | 14 | extract_schema_properties, |
12 | 15 | ) |
13 | | -import logging |
14 | | -from loguru import logger |
15 | | -from _pytest.logging import LogCaptureFixture |
| 16 | +from models.models import DocumentMetadataFilter, Source |
| 17 | +from server.main import app |
16 | 18 |
|
17 | 19 | BEARER_TOKEN = os.getenv("BEARER_TOKEN") |
18 | 20 |
|
@@ -99,30 +101,6 @@ def documents(): |
99 | 101 | yield documents |
100 | 102 |
|
101 | 103 |
|
102 | | -@pytest.fixture |
103 | | -def mock_env_public_access(monkeypatch): |
104 | | - monkeypatch.setattr( |
105 | | - "datastore.providers.weaviate_datastore.WEAVIATE_USERNAME", None |
106 | | - ) |
107 | | - monkeypatch.setattr( |
108 | | - "datastore.providers.weaviate_datastore.WEAVIATE_PASSWORD", None |
109 | | - ) |
110 | | - |
111 | | - |
112 | | -@pytest.fixture |
113 | | -def mock_env_resource_owner_password_flow(monkeypatch): |
114 | | - monkeypatch.setattr( |
115 | | - "datastore.providers.weaviate_datastore.WEAVIATE_SCOPES", |
116 | | - ["schema:read", "schema:write"], |
117 | | - ) |
118 | | - monkeypatch.setattr( |
119 | | - "datastore.providers.weaviate_datastore.WEAVIATE_USERNAME", "admin" |
120 | | - ) |
121 | | - monkeypatch.setattr( |
122 | | - "datastore.providers.weaviate_datastore.WEAVIATE_PASSWORD", "abc123" |
123 | | - ) |
124 | | - |
125 | | - |
126 | 104 | @pytest.fixture |
127 | 105 | def caplog(caplog: LogCaptureFixture): |
128 | 106 | handler_id = logger.add(caplog.handler, format="{message}") |
@@ -337,16 +315,38 @@ def test_delete(test_db, weaviate_client, caplog): |
337 | 315 | assert not weaviate_client.data_object.get()["objects"] |
338 | 316 |
|
339 | 317 |
|
340 | | -def test_access_with_username_password(mock_env_resource_owner_password_flow): |
341 | | - auth_credentials = WeaviateDataStore._build_auth_credentials() |
342 | | - |
343 | | - assert isinstance(auth_credentials, weaviate.auth.AuthClientPassword) |
344 | | - |
345 | | - |
346 | | -def test_public_access(mock_env_public_access): |
347 | | - auth_credentials = WeaviateDataStore._build_auth_credentials() |
348 | | - |
349 | | - assert auth_credentials is None |
| 318 | +def test_build_auth_credentials(monkeypatch): |
| 319 | + # Test when WEAVIATE_URL ends with weaviate.network and WEAVIATE_API_KEY is set |
| 320 | + with monkeypatch.context() as m: |
| 321 | + m.setenv("WEAVIATE_URL", "https://example.weaviate.network") |
| 322 | + m.setenv("WEAVIATE_API_KEY", "your_api_key") |
| 323 | + auth_credentials = WeaviateDataStore._build_auth_credentials() |
| 324 | + assert auth_credentials is not None |
| 325 | + assert isinstance(auth_credentials, weaviate.auth.AuthApiKey) |
| 326 | + assert auth_credentials.api_key == "your_api_key" |
| 327 | + |
| 328 | + # Test when WEAVIATE_URL ends with weaviate.network and WEAVIATE_API_KEY is not set |
| 329 | + with monkeypatch.context() as m: |
| 330 | + m.setenv("WEAVIATE_URL", "https://example.weaviate.network") |
| 331 | + m.delenv("WEAVIATE_API_KEY", raising=False) |
| 332 | + with pytest.raises( |
| 333 | + ValueError, match="WEAVIATE_API_KEY environment variable is not set" |
| 334 | + ): |
| 335 | + WeaviateDataStore._build_auth_credentials() |
| 336 | + |
| 337 | + # Test when WEAVIATE_URL does not end with weaviate.network |
| 338 | + with monkeypatch.context() as m: |
| 339 | + m.setenv("WEAVIATE_URL", "https://example.notweaviate.network") |
| 340 | + m.setenv("WEAVIATE_API_KEY", "your_api_key") |
| 341 | + auth_credentials = WeaviateDataStore._build_auth_credentials() |
| 342 | + assert auth_credentials is None |
| 343 | + |
| 344 | + # Test when WEAVIATE_URL is not set |
| 345 | + with monkeypatch.context() as m: |
| 346 | + m.delenv("WEAVIATE_URL", raising=False) |
| 347 | + m.setenv("WEAVIATE_API_KEY", "your_api_key") |
| 348 | + auth_credentials = WeaviateDataStore._build_auth_credentials() |
| 349 | + assert auth_credentials is None |
350 | 350 |
|
351 | 351 |
|
352 | 352 | def test_extract_schema_properties(): |
@@ -519,3 +519,20 @@ def build_upsert_payload(document): |
519 | 519 | # but it is None right now because an |
520 | 520 | # update function is out of scope |
521 | 521 | assert weaviate_doc[0]["source"] is None |
| 522 | + |
| 523 | + |
| 524 | +@pytest.mark.parametrize( |
| 525 | + "url, expected_result", |
| 526 | + [ |
| 527 | + ("https://example.weaviate.network", True), |
| 528 | + ("https://example.weaviate.network/", True), |
| 529 | + ("https://example.weaviate.cloud", True), |
| 530 | + ("https://example.weaviate.cloud/", True), |
| 531 | + ("https://example.notweaviate.network", False), |
| 532 | + ("https://weaviate.network.example.com", False), |
| 533 | + ("https://example.weaviate.network/somepage", False), |
| 534 | + ("", False), |
| 535 | + ], |
| 536 | +) |
| 537 | +def test_is_wcs_domain(url, expected_result): |
| 538 | + assert WeaviateDataStore._is_wcs_domain(url) == expected_result |
0 commit comments