yearning
18 小时以前 e3e6db2d9974588f91ac1567a58fa4846655ce1b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import urllib.request
import json
 
API = "http://127.0.0.1:8010"
 
def post(path, body, token=None):
    headers = {"Content-Type": "application/json"}
    if token:
        headers["Authorization"] = f"Bearer {token}"
    data = json.dumps(body).encode()
    req = urllib.request.Request(f"{API}{path}", data=data, headers=headers)
    return json.loads(urllib.request.urlopen(req).read())
 
def get(path, token=None):
    headers = {}
    if token:
        headers["Authorization"] = f"Bearer {token}"
    req = urllib.request.Request(f"{API}{path}", headers=headers)
    return json.loads(urllib.request.urlopen(req).read())
 
def put(path, body, token=None):
    headers = {"Content-Type": "application/json"}
    if token:
        headers["Authorization"] = f"Bearer {token}"
    data = json.dumps(body).encode()
    req = urllib.request.Request(f"{API}{path}", data=data, headers=headers, method="PUT")
    return json.loads(urllib.request.urlopen(req).read())
 
# 1. Admin login
r = post("/api/auth/login", {"username": "admin", "password": "admin123"})
admin_token = r["access_token"]
print("=== Admin login OK ===")
 
# 2. Assign subject to grader
subs = get("/api/subjects", admin_token)
first_sub = subs["items"][0]
put("/api/admin/graders/2", {
    "username": "grader", "display_name": "", "is_active": True, "subjects": [first_sub]
}, admin_token)
print(f"Grader assigned subject: {first_sub}")
 
# 3. Grader login
r = post("/api/auth/login", {"username": "grader", "password": "grader123"})
grader_token = r["access_token"]
print("=== Grader login OK ===")
 
# 4. Check settings (privacy OFF)
settings = get("/api/settings")
print(f"Settings: grader_can_see_privacy={settings['grader_can_see_privacy']}")
 
# 5. Grader scores (privacy OFF, name/id_card should be null, no search)
scores = get("/api/scores?page=1&page_size=5", grader_token)
print(f"\n=== Grader scores (privacy OFF) ===")
print(f"Total: {scores['total']}")
for item in scores["items"]:
    print(f"  student_no={item['student_no']}, name={item['name']}, id_card={item['id_card']}, score={item['score']}")
 
# 6. Grader search by name (should be ignored)
scores2 = get("/api/scores?page=1&page_size=5&name=test", grader_token)
print(f"\n=== Grader search name=test (should be ignored, same total) ===")
print(f"Total: {scores2['total']} (expected: {scores['total']})")
 
# 7. Admin turns ON privacy
put("/api/admin/settings", {
    "system_name": settings["system_name"], "grader_can_see_privacy": True
}, admin_token)
print("\n=== Admin turned ON privacy ===")
 
# 8. Grader scores (privacy ON, should show full)
scores3 = get("/api/scores?page=1&page_size=5", grader_token)
print(f"\n=== Grader scores (privacy ON) ===")
print(f"Total: {scores3['total']}")
for item in scores3["items"]:
    print(f"  student_no={item['student_no']}, name={item['name']}, id_card={item['id_card']}, score={item['score']}")
 
# 9. Grader search by name (should work now)
scores4 = get("/api/scores?page=1&page_size=5&name=a", grader_token)
print(f"\n=== Grader search name=a (should work now) ===")
print(f"Total: {scores4['total']}")
 
# 10. Reset to OFF
put("/api/admin/settings", {
    "system_name": settings["system_name"], "grader_can_see_privacy": False
}, admin_token)
print("\n=== Reset privacy to OFF ===")
 
# 11. Admin scores (always full)
scores5 = get("/api/scores?page=1&page_size=5", admin_token)
print(f"\n=== Admin scores (always full) ===")
print(f"Total: {scores5['total']}")
for item in scores5["items"]:
    print(f"  student_no={item['student_no']}, name={item['name']}, id_card={item['id_card']}, score={item['score']}")