80 lines
2.4 KiB
Python
80 lines
2.4 KiB
Python
import pytest
|
|
|
|
from accounts.models import User
|
|
from toolbox.models import BoardSession
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_calculator_api_公开访问并返回精确值(client):
|
|
response = client.post(
|
|
"/api/v1/toolbox/calculate/",
|
|
{"operation": "factor", "expression": "x^2 - 1"},
|
|
content_type="application/json",
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
assert response.json()["result"]["exact"] == "(x - 1)*(x + 1)"
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_calculator_api_危险表达式返回四百(client):
|
|
response = client.post(
|
|
"/api/v1/toolbox/calculate/",
|
|
{"operation": "calculate", "expression": "__import__('os').system('id')"},
|
|
content_type="application/json",
|
|
)
|
|
|
|
assert response.status_code == 400
|
|
assert "error" in response.json()
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_board_api_联机码加入你画我猜并服务端计分(client):
|
|
host = User.objects.create_user(
|
|
username="board_host",
|
|
password="StrongPass_2026",
|
|
nickname="画板房主",
|
|
)
|
|
guest = User.objects.create_user(
|
|
username="board_guest",
|
|
password="StrongPass_2026",
|
|
nickname="猜题玩家",
|
|
)
|
|
client.force_login(host)
|
|
created = client.post(
|
|
"/api/v1/toolbox/boards/",
|
|
{"mode": "draw_guess"},
|
|
content_type="application/json",
|
|
)
|
|
target = created.json()["target"]
|
|
code = created.json()["code"]
|
|
session_id = created.json()["session_id"]
|
|
|
|
client.force_login(guest)
|
|
joined = client.post(
|
|
"/api/v1/toolbox/boards/join/",
|
|
{"code": code.lower()},
|
|
content_type="application/json",
|
|
)
|
|
wrong = client.post(
|
|
f"/api/v1/toolbox/boards/{session_id}/guess/",
|
|
{"guess": "不是答案"},
|
|
content_type="application/json",
|
|
)
|
|
correct = client.post(
|
|
f"/api/v1/toolbox/boards/{session_id}/guess/",
|
|
{"guess": target},
|
|
content_type="application/json",
|
|
)
|
|
|
|
assert created.status_code == 201
|
|
assert created.json()["role"] == "host"
|
|
assert target
|
|
assert joined.status_code == 200
|
|
assert joined.json()["target"] is None
|
|
assert joined.json()["status"] == BoardSession.Status.ACTIVE
|
|
assert wrong.json()["correct"] is False
|
|
assert correct.json()["correct"] is True
|
|
assert correct.json()["guest_score"] == 1
|
|
assert correct.json()["status"] == BoardSession.Status.COMPLETED
|