|
11 | 11 | # See the License for the specific language governing permissions and
|
12 | 12 | # limitations under the License.
|
13 | 13 | # =========== Copyright 2024 @ CAMEL-AI.org. All Rights Reserved. ===========
|
| 14 | + |
| 15 | +import pytest |
14 | 16 | from fastapi.testclient import TestClient
|
15 | 17 |
|
| 18 | +from crab import create_environment |
16 | 19 | from crab.environments.template import (
|
17 | 20 | current_state,
|
18 | 21 | set_state,
|
|
21 | 24 | from crab.server.main import init
|
22 | 25 |
|
23 | 26 |
|
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 |
35 | 34 |
|
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 |
44 | 35 |
|
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 |
54 | 42 |
|
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 |
64 | 43 |
|
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