Spaces:
Running
Running
Commit ·
438ac1c
1
Parent(s): d165104
Fix template rendering with Starlette 1.x
Browse files- server.py +12 -10
- tests/test_security.py +7 -0
server.py
CHANGED
|
@@ -570,8 +570,9 @@ async def metrics():
|
|
| 570 |
@app.get("/", response_class=HTMLResponse)
|
| 571 |
async def read_index(request: Request):
|
| 572 |
return templates.TemplateResponse(
|
| 573 |
-
|
| 574 |
-
|
|
|
|
| 575 |
)
|
| 576 |
|
| 577 |
|
|
@@ -581,8 +582,9 @@ async def read_class(request: Request, class_name: str):
|
|
| 581 |
if not real_class:
|
| 582 |
return HTMLResponse("Class not found", status_code=404)
|
| 583 |
return templates.TemplateResponse(
|
| 584 |
-
|
| 585 |
-
|
|
|
|
| 586 |
)
|
| 587 |
|
| 588 |
|
|
@@ -592,9 +594,9 @@ async def read_student(request: Request, student_token: str):
|
|
| 592 |
if not real_class or not real_student:
|
| 593 |
return HTMLResponse("Student not found", status_code=404)
|
| 594 |
return templates.TemplateResponse(
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
| 598 |
"restrict_class": real_class,
|
| 599 |
"restrict_student": real_student,
|
| 600 |
},
|
|
@@ -693,9 +695,9 @@ async def run_project_page(request: Request, student_token: str, project_name: s
|
|
| 693 |
pass
|
| 694 |
|
| 695 |
return templates.TemplateResponse(
|
| 696 |
-
|
| 697 |
-
|
| 698 |
-
|
| 699 |
"class_name": real_class,
|
| 700 |
"student_name": real_student,
|
| 701 |
"student_token": student_token,
|
|
|
|
| 570 |
@app.get("/", response_class=HTMLResponse)
|
| 571 |
async def read_index(request: Request):
|
| 572 |
return templates.TemplateResponse(
|
| 573 |
+
request=request,
|
| 574 |
+
name="index.html",
|
| 575 |
+
context={"restrict_class": "", "restrict_student": ""},
|
| 576 |
)
|
| 577 |
|
| 578 |
|
|
|
|
| 582 |
if not real_class:
|
| 583 |
return HTMLResponse("Class not found", status_code=404)
|
| 584 |
return templates.TemplateResponse(
|
| 585 |
+
request=request,
|
| 586 |
+
name="index.html",
|
| 587 |
+
context={"restrict_class": real_class, "restrict_student": ""},
|
| 588 |
)
|
| 589 |
|
| 590 |
|
|
|
|
| 594 |
if not real_class or not real_student:
|
| 595 |
return HTMLResponse("Student not found", status_code=404)
|
| 596 |
return templates.TemplateResponse(
|
| 597 |
+
request=request,
|
| 598 |
+
name="index.html",
|
| 599 |
+
context={
|
| 600 |
"restrict_class": real_class,
|
| 601 |
"restrict_student": real_student,
|
| 602 |
},
|
|
|
|
| 695 |
pass
|
| 696 |
|
| 697 |
return templates.TemplateResponse(
|
| 698 |
+
request=request,
|
| 699 |
+
name="player.html",
|
| 700 |
+
context={
|
| 701 |
"class_name": real_class,
|
| 702 |
"student_name": real_student,
|
| 703 |
"student_token": student_token,
|
tests/test_security.py
CHANGED
|
@@ -4,6 +4,7 @@ from pathlib import Path
|
|
| 4 |
from unittest.mock import patch
|
| 5 |
|
| 6 |
import server
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
class ServerSecurityTests(unittest.TestCase):
|
|
@@ -53,6 +54,12 @@ class ServerSecurityTests(unittest.TestCase):
|
|
| 53 |
with self.assertRaisesRegex(RuntimeError, "拒絕未隔離執行"):
|
| 54 |
server._runner_sandbox_command("/tmp/project", "/tmp/session/runner.sock")
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
if __name__ == "__main__":
|
| 58 |
unittest.main()
|
|
|
|
| 4 |
from unittest.mock import patch
|
| 5 |
|
| 6 |
import server
|
| 7 |
+
from fastapi.testclient import TestClient
|
| 8 |
|
| 9 |
|
| 10 |
class ServerSecurityTests(unittest.TestCase):
|
|
|
|
| 54 |
with self.assertRaisesRegex(RuntimeError, "拒絕未隔離執行"):
|
| 55 |
server._runner_sandbox_command("/tmp/project", "/tmp/session/runner.sock")
|
| 56 |
|
| 57 |
+
def test_homepage_renders_template(self):
|
| 58 |
+
with TestClient(server.app) as client:
|
| 59 |
+
response = client.get("/")
|
| 60 |
+
self.assertEqual(response.status_code, 200)
|
| 61 |
+
self.assertIn("text/html", response.headers["content-type"])
|
| 62 |
+
|
| 63 |
|
| 64 |
if __name__ == "__main__":
|
| 65 |
unittest.main()
|