feat: edit quiz questions over MCP

- quizzes_create_question, update_question, delete_question and
  reorder_questions on /api/v1/quizzes/{slug}/questions
- update_question replaces answers by id (kept, created, deleted), so
  answer ids stored in past attempts stay valid; foreign ids are refused
- classes.CheckQuestion holds the importer's per-question rules, shared
  with the manage routes; attempt scores are never touched (D-16)
This commit is contained in:
Jakub Zych
2026-11-09 14:33:56 +01:00
parent 4a60eda425
commit c6d8d1a973
11 changed files with 792 additions and 44 deletions

View File

@@ -18,16 +18,20 @@ func TestManageHandlersWithoutDatabase(t *testing.T) {
target string
body string
}{
"list": {h.ManageListQuizzes, "GET", "/", ""},
"show": {h.ManageShowQuiz, "GET", "/", ""},
"create": {h.ManageCreateQuiz, "POST", "/", `{"title":"Pojęcia"}`},
"update": {h.ManageUpdateQuiz, "PATCH", "/", `{"max_attempts":0}`},
"delete": {h.ManageDeleteQuiz, "DELETE", "/?confirm=x", ""},
"publish": {h.ManagePublish, "POST", "/", ""},
"unpublish": {h.ManageUnpublish, "POST", "/", ""},
"stats": {h.ManageQuizStats, "GET", "/", ""},
"list": {h.ManageListQuizzes, "GET", "/", ""},
"show": {h.ManageShowQuiz, "GET", "/", ""},
"create": {h.ManageCreateQuiz, "POST", "/", `{"title":"Pojęcia"}`},
"update": {h.ManageUpdateQuiz, "PATCH", "/", `{"max_attempts":0}`},
"delete": {h.ManageDeleteQuiz, "DELETE", "/?confirm=x", ""},
"publish": {h.ManagePublish, "POST", "/", ""},
"unpublish": {h.ManageUnpublish, "POST", "/", ""},
"stats": {h.ManageQuizStats, "GET", "/", ""},
"create question": {h.ManageCreateQuestion, "POST", "/", `{"text":"Q","type":"self_check"}`},
"update question": {h.ManageUpdateQuestion, "PATCH", "/", `{"text":"Q"}`},
"delete question": {h.ManageDeleteQuestion, "DELETE", "/", ""},
"reorder": {h.ManageReorderQuestions, "PUT", "/", `{"question_ids":[2,1]}`},
} {
code, _ := call(t, tc.fn, tc.method, tc.target, tc.body, 7, map[string]string{"slug": "x"})
code, _ := call(t, tc.fn, tc.method, tc.target, tc.body, 7, map[string]string{"slug": "x", "id": "3"})
if code != http.StatusServiceUnavailable {
t.Errorf("%s without a database = %d, want 503", name, code)
}
@@ -56,10 +60,24 @@ func TestManageWritesRefuseBadInputBeforeDatabase(t *testing.T) {
"delete without confirm": {h.ManageDeleteQuiz, "DELETE", "/", "", "confirm must repeat the quiz slug"},
"delete wrong confirm": {h.ManageDeleteQuiz, "DELETE", "/?confirm=y", "", "confirm must repeat the quiz slug"},
} {
code, out := call(t, tc.fn, tc.method, tc.target, tc.body, 7, map[string]string{"slug": "x"})
code, out := call(t, tc.fn, tc.method, tc.target, tc.body, 7, map[string]string{"slug": "x", "id": "3"})
msg, _ := out["message"].(string)
if code != http.StatusUnprocessableEntity || !strings.Contains(msg, tc.want) {
t.Errorf("%s = %d %v, want 422 with %q", name, code, out, tc.want)
}
}
}
// TestManageQuestionIDs: a question id that is not a positive integer is
// the 404 of a missing question, before the database.
func TestManageQuestionIDs(t *testing.T) {
h := New(backpack.New(nil))
for _, id := range []string{"", "0", "abc", "-1"} {
for name, fn := range map[string]http.HandlerFunc{"update": h.ManageUpdateQuestion, "delete": h.ManageDeleteQuestion} {
code, out := call(t, fn, "PATCH", "/", `{}`, 7, map[string]string{"slug": "x", "id": id})
if code != http.StatusNotFound || out["message"] != "Question not found." {
t.Errorf("%s id %q = %d %v, want 404 Question not found.", name, id, code, out)
}
}
}
}