Download tools/server/tests/unit/test_security.py from OpenTransformer/llama.cpp-prismml: direct link, hf CLI and curl.
- Browser
- Download file 4.18 kB
-
https://huggingface.co/OpenTransformer/llama.cpp-prismml/resolve/main/tools/server/tests/unit/test_security.py
- Command line
-
hf download hf://OpenTransformer/llama.cpp-prismml/tools/server/tests/unit/test_security.py
-
curl -L -o test_security.py https://huggingface.co/OpenTransformer/llama.cpp-prismml/resolve/main/tools/server/tests/unit/test_security.py
4.18 kB
| import pytest | |
| from openai import OpenAI | |
| from utils import * | |
| server = ServerPreset.tinyllama2() | |
| TEST_API_KEY = "sk-this-is-the-secret-key" | |
| def create_server(): | |
| global server | |
| server = ServerPreset.tinyllama2() | |
| server.api_key = TEST_API_KEY | |
| def test_access_public_endpoint(endpoint: str): | |
| global server | |
| server.start() | |
| res = server.make_request("GET", endpoint) | |
| assert res.status_code == 200 | |
| assert "error" not in res.body | |
| def test_incorrect_api_key(api_key: str): | |
| global server | |
| server.start() | |
| res = server.make_request("POST", "/completions", data={ | |
| "prompt": "I believe the meaning of life is", | |
| }, headers={ | |
| "Authorization": f"Bearer {api_key}" if api_key else None, | |
| }) | |
| assert res.status_code == 401 | |
| assert "error" in res.body | |
| assert res.body["error"]["type"] == "authentication_error" | |
| def test_correct_api_key(): | |
| global server | |
| server.start() | |
| res = server.make_request("POST", "/completions", data={ | |
| "prompt": "I believe the meaning of life is", | |
| }, headers={ | |
| "Authorization": f"Bearer {TEST_API_KEY}", | |
| }) | |
| assert res.status_code == 200 | |
| assert "error" not in res.body | |
| assert "content" in res.body | |
| def test_correct_api_key_anthropic_header(): | |
| global server | |
| server.start() | |
| res = server.make_request("POST", "/completions", data={ | |
| "prompt": "I believe the meaning of life is", | |
| }, headers={ | |
| "X-Api-Key": TEST_API_KEY, | |
| }) | |
| assert res.status_code == 200 | |
| assert "error" not in res.body | |
| assert "content" in res.body | |
| def test_openai_library_correct_api_key(): | |
| global server | |
| server.start() | |
| client = OpenAI(api_key=TEST_API_KEY, base_url=f"http://{server.server_host}:{server.server_port}") | |
| res = client.chat.completions.create( | |
| model="gpt-3.5-turbo", | |
| messages=[ | |
| {"role": "system", "content": "You are a chatbot."}, | |
| {"role": "user", "content": "What is the meaning of life?"}, | |
| ], | |
| ) | |
| assert len(res.choices) == 1 | |
| def test_cors_options(origin: str, cors_header: str, cors_header_value: str): | |
| global server | |
| server.start() | |
| res = server.make_request("OPTIONS", "/completions", headers={ | |
| "Origin": origin, | |
| "Access-Control-Request-Method": "POST", | |
| "Access-Control-Request-Headers": "Authorization", | |
| }) | |
| assert res.status_code == 200 | |
| assert cors_header in res.headers | |
| assert res.headers[cors_header] == cors_header_value | |
| def test_local_media_file(media_path, image_url, success,): | |
| server = ServerPreset.tinygemma3() | |
| server.media_path = media_path | |
| server.start() | |
| res = server.make_request("POST", "/chat/completions", data={ | |
| "max_tokens": 1, | |
| "messages": [ | |
| {"role": "user", "content": [ | |
| {"type": "text", "text": "test"}, | |
| {"type": "image_url", "image_url": { | |
| "url": image_url, | |
| }}, | |
| ]}, | |
| ], | |
| }) | |
| if success: | |
| assert res.status_code == 200 | |
| else: | |
| assert res.status_code == 400 | |