Skip to content

Commit 440f46e

Browse files
committed
test: add test for encryption
1 parent 34e17f6 commit 440f46e

File tree

3 files changed

+29
-52
lines changed

3 files changed

+29
-52
lines changed

crab/server/api.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@
2626
from .logger import crab_logger as logger
2727

2828
api_router = APIRouter()
29-
ENC_KEY = generate_key_from_env()
3029

3130

3231
@api_router.post("/raw_action")
3332
async def raw_action(request: Request):
3433
"""Perform the specified action with given parameters."""
34+
enc_key = generate_key_from_env()
3535
# Extract query parameters as a dictionary
3636
request_content = await request.body()
3737
request_content = request_content.decode("utf-8")
38-
if ENC_KEY is not None:
39-
request_content = decrypt_message(request_content, ENC_KEY)
38+
if enc_key is not None:
39+
request_content = decrypt_message(request_content, enc_key)
4040
request_json = json.loads(request_content)
4141

4242
action = request_json["action"]
@@ -47,8 +47,8 @@ async def raw_action(request: Request):
4747
parameters["env"] = request.app.environment
4848

4949
resp_data = {"action_returns": entry(**parameters)}
50-
if ENC_KEY is None:
50+
if enc_key is None:
5151
return JSONResponse(content=resp_data)
5252
else:
53-
encrypted = encrypt_message(json.dumps(resp_data), ENC_KEY)
53+
encrypted = encrypt_message(json.dumps(resp_data), enc_key)
5454
return PlainTextResponse(content=encrypted)

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ certifi = "^2024.2.2"
122122
lint.select = ["E501", "E4", "E7", "E9", "F", "I"]
123123
lint.ignore = ["E731"]
124124
lint.ignore-init-module-imports = true
125-
exclude = ["docs/"]
125+
exclude = ["docs/", "crab-benchmark-v0/thirdparty/"]
126126

127127
[[tool.mypy.overrides]]
128128
module = ["dill", "easyocr"]

test/server/test_api.py

+23-46
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
# See the License for the specific language governing permissions and
1212
# limitations under the License.
1313
# =========== Copyright 2024 @ CAMEL-AI.org. All Rights Reserved. ===========
14+
15+
import pytest
1416
from fastapi.testclient import TestClient
1517

18+
from crab import create_environment
1619
from crab.environments.template import (
1720
current_state,
1821
set_state,
@@ -21,53 +24,27 @@
2124
from crab.server.main import init
2225

2326

24-
def test_raw_action():
25-
app = init(template_environment_config)
26-
client = TestClient(app)
27-
response = client.post(
28-
"/raw_action",
29-
json={
30-
"action": set_state.to_raw_action(),
31-
"parameters": {"value": True},
32-
},
33-
)
34-
assert response.json()["action_returns"] is None
27+
@pytest.fixture
28+
def mock_env():
29+
mock_app = init(template_environment_config)
30+
mock_cli = TestClient(mock_app)
31+
mock_env = create_environment(template_environment_config)
32+
mock_env._client = mock_cli
33+
return mock_env
3534

36-
response = client.post(
37-
"/raw_action",
38-
json={
39-
"action": current_state.to_raw_action(),
40-
"parameters": {},
41-
},
42-
)
43-
assert response.json()["action_returns"] is True
4435

45-
action = set_state(True)
46-
response = client.post(
47-
"/raw_action",
48-
json={
49-
"action": action.to_raw_action(),
50-
"parameters": {},
51-
},
52-
)
53-
assert response.json()["action_returns"] is None
36+
def test_raw_action_unencrypted(mock_env):
37+
assert mock_env._action_endpoint(set_state, {"value": True}) is None
38+
assert mock_env._action_endpoint(current_state, {}) is True
39+
assert mock_env._action_endpoint(set_state(True), {}) is None
40+
assert mock_env._action_endpoint(current_state >> set_state, {}) is None
41+
assert mock_env._action_endpoint(set_state(True) + current_state, {}) is True
5442

55-
action = current_state >> set_state
56-
response = client.post(
57-
"/raw_action",
58-
json={
59-
"action": action.to_raw_action(),
60-
"parameters": {},
61-
},
62-
)
63-
assert response.json()["action_returns"] is None
6443

65-
action = set_state(True) + current_state
66-
response = client.post(
67-
"/raw_action",
68-
json={
69-
"action": action.to_raw_action(),
70-
"parameters": {},
71-
},
72-
)
73-
assert response.json()["action_returns"] is True
44+
def test_raw_action_encrypted(mock_env, monkeypatch):
45+
monkeypatch.setenv("ENCRYPTION_KEY", "the-cake-is-a-lie")
46+
assert mock_env._action_endpoint(set_state, {"value": True}) is None
47+
assert mock_env._action_endpoint(current_state, {}) is True
48+
assert mock_env._action_endpoint(set_state(True), {}) is None
49+
assert mock_env._action_endpoint(current_state >> set_state, {}) is None
50+
assert mock_env._action_endpoint(set_state(True) + current_state, {}) is True

0 commit comments

Comments
 (0)