mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-26 03:04:13 +00:00
(test) /key/generate - valid, invalid models
This commit is contained in:
parent
39d5af5c85
commit
cc51651b49
1 changed files with 64 additions and 11 deletions
|
@ -33,6 +33,7 @@ from litellm.proxy._types import NewUserRequest, DynamoDBArgs
|
||||||
from litellm.proxy.utils import DBClient
|
from litellm.proxy.utils import DBClient
|
||||||
from starlette.datastructures import URL
|
from starlette.datastructures import URL
|
||||||
|
|
||||||
|
|
||||||
db_args = {
|
db_args = {
|
||||||
"ssl_verify": False,
|
"ssl_verify": False,
|
||||||
"billing_mode": "PAY_PER_REQUEST",
|
"billing_mode": "PAY_PER_REQUEST",
|
||||||
|
@ -87,7 +88,7 @@ def test_call_with_invalid_key():
|
||||||
generated_key = "bad-key"
|
generated_key = "bad-key"
|
||||||
bearer_token = "Bearer " + generated_key
|
bearer_token = "Bearer " + generated_key
|
||||||
|
|
||||||
request = Request(scope={"type": "http"})
|
request = Request(scope={"type": "http"}, receive=None)
|
||||||
request._url = URL(url="/chat/completions")
|
request._url = URL(url="/chat/completions")
|
||||||
|
|
||||||
# use generated key to auth in
|
# use generated key to auth in
|
||||||
|
@ -102,18 +103,70 @@ def test_call_with_invalid_key():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
# def test_call_with_invalid_model():
|
def test_call_with_invalid_model():
|
||||||
# # 3. Make a call to a key with an invalid model - expect to fail
|
# 3. Make a call to a key with an invalid model - expect to fail
|
||||||
# key = new_user(ValidNewUserRequest())
|
setattr(litellm.proxy.proxy_server, "custom_db_client", custom_db_client)
|
||||||
# result = user_auth(InvalidModelRequest(key))
|
setattr(litellm.proxy.proxy_server, "master_key", "sk-1234")
|
||||||
# assert result is False
|
try:
|
||||||
|
|
||||||
|
async def test():
|
||||||
|
request = NewUserRequest(models=["mistral"])
|
||||||
|
key = await new_user(request)
|
||||||
|
print(key)
|
||||||
|
|
||||||
|
generated_key = key.key
|
||||||
|
bearer_token = "Bearer " + generated_key
|
||||||
|
|
||||||
|
request = Request(scope={"type": "http"})
|
||||||
|
request._url = URL(url="/chat/completions")
|
||||||
|
|
||||||
|
async def return_body():
|
||||||
|
return b'{"model": "gemini-pro-vision"}'
|
||||||
|
|
||||||
|
request.body = return_body
|
||||||
|
|
||||||
|
# use generated key to auth in
|
||||||
|
result = await user_api_key_auth(request=request, api_key=bearer_token)
|
||||||
|
pytest.fail(f"This should have failed!. IT's an invalid model")
|
||||||
|
|
||||||
|
asyncio.run(test())
|
||||||
|
except Exception as e:
|
||||||
|
assert (
|
||||||
|
e.detail
|
||||||
|
== "Authentication Error, API Key not allowed to access model. This token can only access models=['mistral']. Tried to access gemini-pro-vision"
|
||||||
|
)
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
# def test_call_with_valid_model():
|
def test_call_with_valid_model():
|
||||||
# # 4. Make a call to a key with a valid model - expect to pass
|
# 4. Make a call to a key with a valid model - expect to pass
|
||||||
# key = new_user(ValidNewUserRequest())
|
setattr(litellm.proxy.proxy_server, "custom_db_client", custom_db_client)
|
||||||
# result = user_auth(ValidModelRequest(key))
|
setattr(litellm.proxy.proxy_server, "master_key", "sk-1234")
|
||||||
# assert result is True
|
try:
|
||||||
|
|
||||||
|
async def test():
|
||||||
|
request = NewUserRequest(models=["mistral"])
|
||||||
|
key = await new_user(request)
|
||||||
|
print(key)
|
||||||
|
|
||||||
|
generated_key = key.key
|
||||||
|
bearer_token = "Bearer " + generated_key
|
||||||
|
|
||||||
|
request = Request(scope={"type": "http"})
|
||||||
|
request._url = URL(url="/chat/completions")
|
||||||
|
|
||||||
|
async def return_body():
|
||||||
|
return b'{"model": "mistral"}'
|
||||||
|
|
||||||
|
request.body = return_body
|
||||||
|
|
||||||
|
# use generated key to auth in
|
||||||
|
result = await user_api_key_auth(request=request, api_key=bearer_token)
|
||||||
|
print("result from user auth with new key", result)
|
||||||
|
|
||||||
|
asyncio.run(test())
|
||||||
|
except Exception as e:
|
||||||
|
pytest.fail(f"An exception occurred - {str(e)}")
|
||||||
|
|
||||||
|
|
||||||
# def test_call_with_expired_key():
|
# def test_call_with_expired_key():
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue