79 lines
4.3 KiB
Python
79 lines
4.3 KiB
Python
import io
|
|
import json
|
|
import sqlite3
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
from studio.api import create_app
|
|
|
|
|
|
@pytest.fixture
|
|
def client(tmp_path):
|
|
with TestClient(create_app(tmp_path), base_url="http://127.0.0.1:8765") as client:
|
|
yield client
|
|
|
|
|
|
def csv_upload(client, year=2026):
|
|
text = 'school_name,school_code,major_code,major_name,department_name,nzsrsstr,subject1,subject2,subject3,subject4\n北京大学,10001,081200,计算机科学与技术,信息学院,专业:6(不含推免),101思想政治理论,201英语一,301数学一,408计算机学科专业基础\n'
|
|
return client.post("/api/import", files={"file": ("yzw.csv", text.encode("utf-8-sig"))}, data={"year": year})
|
|
|
|
|
|
def test_csv_upload_filter_favorite_and_export(client):
|
|
assert csv_upload(client).json()["records"] == 1
|
|
rows = client.get("/api/catalog", params={"year": 2026, "subject": "408"}).json()
|
|
assert rows["total"] == 1
|
|
record = rows["items"][0]
|
|
assert record["quota"] == 6
|
|
assert client.put(f'/api/catalog/{record["id"]}/favorite', json={"enabled": True}).status_code == 200
|
|
assert client.get("/api/catalog", params={"favorites": True}).json()["total"] == 1
|
|
exported = client.get("/api/export", params={"format": "json", "year": 2026}).json()
|
|
assert exported["catalog"][0]["year"] == 2026
|
|
assert client.get("/api/export", params={"format": "xlsx"}).content.startswith(b"PK")
|
|
|
|
|
|
def test_import_does_not_invent_year_when_csv_has_no_year(client):
|
|
content = b"school_name,major_code\nTest,081200\n"
|
|
response = client.post("/api/import", files={"file": ("data.csv", content)})
|
|
assert response.status_code == 400
|
|
assert "年份" in response.json()["detail"]
|
|
|
|
|
|
def test_cross_origin_write_is_blocked_and_secrets_are_not_in_settings(client):
|
|
response = client.put("/api/settings", json={"cookie": "secret", "network_mode": "direct"}, headers={"Origin": "https://untrusted.example"})
|
|
assert response.status_code == 403
|
|
assert "cookie" not in client.get("/api/settings").json()
|
|
|
|
|
|
def test_export_prevents_spreadsheet_formula_execution(client):
|
|
data = {"catalog": [{"year": 2026, "school_name": "=1+1", "major_code": "081200", "major_name": "test"}]}
|
|
response = client.post("/api/import", files={"file": ("studio.json", json.dumps(data).encode())})
|
|
assert response.status_code == 200
|
|
response = client.get("/api/export", params={"format": "csv"})
|
|
assert "'=1+1" in response.content.decode("utf-8-sig")
|
|
|
|
|
|
def test_import_yam_sqlite_keeps_year_and_score_scope(client, tmp_path):
|
|
path = tmp_path / "yam.db"
|
|
conn = sqlite3.connect(path)
|
|
conn.executescript('CREATE TABLE schools(school_id TEXT,major_code TEXT,name TEXT,province TEXT); CREATE TABLE departments(department_id TEXT,school_id TEXT,major_code TEXT,name TEXT,research_direction TEXT,exam_subjects TEXT,enrollment_text TEXT); CREATE TABLE score_lines(school_id TEXT,department_id TEXT,major_code TEXT,year INTEGER,total INTEGER,match_scope TEXT,raw_code TEXT);')
|
|
conn.execute("INSERT INTO schools VALUES('10001','081200','北京大学','北京')")
|
|
conn.execute("INSERT INTO departments VALUES('001','10001','081200','信息学院','人工智能','[\"201英语一\"]','专业:6')")
|
|
conn.execute("INSERT INTO score_lines VALUES('10001','001','081200',2025,310,'discipline','0812')")
|
|
conn.commit()
|
|
conn.close()
|
|
response = client.post("/api/import", files={"file": ("yam.db", path.read_bytes())}, data={"year": 2026})
|
|
assert response.status_code == 200, response.text
|
|
rows = client.get("/api/catalog").json()["items"]
|
|
assert rows[0]["year"] == 2026
|
|
assert rows[0]["score_year"] == 2025
|
|
assert rows[0]["score_scope"] == "discipline"
|
|
|
|
|
|
def test_yam_seed_json_imports_school_index_without_inventing_admissions(client):
|
|
payload = [{'dwdm': '10001', 'dwmc': '北京大学', 'zydm': '081200', 'zymc': '计算机科学与技术', 'szss': '北京', 'data': [], 'sign': 'token-must-not-copy'}]
|
|
response = client.post('/api/import', files={'file': ('yam-seed.json', json.dumps(payload).encode())})
|
|
assert response.status_code == 200, response.text
|
|
assert response.json()['records'] == 0
|
|
assert response.json()['indexed_schools'] == 1
|
|
assert client.get('/api/catalog').json()['total'] == 0
|
|
assert 'token-must-not-copy' not in client.get('/api/schools').text
|