Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 0 additions & 75 deletions mock_tests/test_collection.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import datetime
import json
import time
from typing import Any, Dict, Literal

import grpc
import pytest
from pytest_httpserver import HTTPServer
from werkzeug import Request, Response

import weaviate
import weaviate.classes as wvc
from mock_tests.conftest import (
MOCK_PORT,
MOCK_IP,
MOCK_PORT_GRPC,
CLIENT_ID,
MockRetriesWeaviateService,
)
from weaviate.backup.backup import BackupStorage
Expand Down Expand Up @@ -76,77 +72,6 @@ def test_old_version(ready_mock: HTTPServer, start_grpc_server: grpc.Server) ->
ready_mock.check_assertions()


@pytest.mark.parametrize("header_name", ["Authorization", "authorization"])
def test_auth_header_priority(
weaviate_auth_mock: HTTPServer, start_grpc_server: grpc.Server, header_name: str
) -> None:
"""Test that auth_client_secret has priority over the auth header."""
bearer_token = "OTHER TOKEN"

weaviate_auth_mock.expect_request("/auth").respond_with_json(
{"access_token": ACCESS_TOKEN, "expires_in": 500, "refresh_token": REFRESH_TOKEN}
)

def handler(request: Request) -> Response:
assert request.headers["Authorization"] == "Bearer " + ACCESS_TOKEN
return Response(json.dumps({}))

weaviate_auth_mock.expect_request("/v1/schema").respond_with_handler(handler)

with pytest.warns(UserWarning) as recwarn:
with weaviate.connect_to_local(
port=MOCK_PORT,
host=MOCK_IP,
grpc_port=MOCK_PORT_GRPC,
headers={header_name: "Bearer " + bearer_token},
auth_credentials=wvc.init.Auth.api_key("key"),
) as _:
assert str(recwarn[0].message).startswith("Auth004")


def test_auth_header_with_catchall_proxy(
weaviate_mock: HTTPServer, start_grpc_server: grpc.Server
) -> None:
"""Test that the client can handle situations in which a proxy returns a catchall page for all requests."""
weaviate_mock.expect_request("/v1/schema").respond_with_json({})
weaviate_mock.expect_request("/v1/.well-known/openid-configuration").respond_with_data(
"JsonCannotParseThis"
)

with pytest.warns(UserWarning) as recwarn:
weaviate.connect_to_local(
port=MOCK_PORT,
grpc_port=MOCK_PORT_GRPC,
host=MOCK_IP,
auth_credentials=wvc.init.Auth.bearer_token("token"),
)
assert str(recwarn[0].message).startswith("Auth005")


def test_refresh(weaviate_auth_mock: HTTPServer, start_grpc_server: grpc.Server) -> None:
"""Test that refresh tokens are used to get a new access token."""
weaviate_auth_mock.expect_request(
"/v1/schema", headers={"Authorization": "Bearer " + ACCESS_TOKEN}
).respond_with_json({"classes": {}})

weaviate_auth_mock.expect_request(
"/auth",
data=f"grant_type=refresh_token&refresh_token={REFRESH_TOKEN}&client_id={CLIENT_ID}",
).respond_with_json(
{"access_token": ACCESS_TOKEN, "expires_in": 1, "refresh_token": REFRESH_TOKEN}
)
with weaviate.connect_to_local(
port=MOCK_PORT,
grpc_port=MOCK_PORT_GRPC,
host=MOCK_IP,
auth_credentials=wvc.init.Auth.bearer_token(
ACCESS_TOKEN, refresh_token=REFRESH_TOKEN, expires_in=1
),
) as client:
time.sleep(1) # client gets a new token 1s before expiration
client.collections.list_all()


def test_closed_connection(weaviate_auth_mock: HTTPServer, start_grpc_server: grpc.Server) -> None:
client = weaviate.WeaviateClient(
ConnectionParams(
Expand Down
Loading