Management API: record routes for courses, meetings, class sessions, readings and deadlines (Fillable + Rules + TemporalErrors, PATCH, filters)
This commit is contained in:
@@ -346,3 +346,159 @@ RETURNING id`).Scan(&groupID).Error; err != nil {
|
||||
t.Errorf("a blob is left in the bucket: %v %v", obj, err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestManagementRecords drives the record routes: create, partial update,
|
||||
// validation (unknown column, rules, temporal order, a missing reference),
|
||||
// list filters and delete, for courses, meetings, sessions, readings and
|
||||
// deadlines.
|
||||
func TestManagementRecords(t *testing.T) {
|
||||
app, plugins, db := migrated(t)
|
||||
var userID uint
|
||||
if err := db.Raw(`
|
||||
INSERT INTO users (email, password, must_change_password)
|
||||
VALUES ('bm-records-token@example.test', '', false)
|
||||
RETURNING id`).Scan(&userID).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var groupID uint
|
||||
if err := db.Raw(`
|
||||
INSERT INTO user_groups (name, code, permissions, created_at, updated_at)
|
||||
VALUES ('BM records', 'bm-records', '{"jz.bm.access_courses":1,"jz.bm.access_schedule":1}', NOW(), NOW())
|
||||
RETURNING id`).Scan(&groupID).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Exec(`INSERT INTO users_groups (user_id, user_group_id) VALUES (?, ?)`, userID, groupID).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
secret, token, err := userclasses.MintAPIToken("usr_", userID, "BM records", []string{"read", "write"}, nil, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(token).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
handler, err := surf.Assemble(app, plugins)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
send := func(method, path string, payload any) (int, map[string]any) {
|
||||
t.Helper()
|
||||
var rd io.Reader
|
||||
if payload != nil {
|
||||
raw, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rd = bytes.NewReader(raw)
|
||||
}
|
||||
req := httptest.NewRequest(method, "/api/v1/bm"+path, rd)
|
||||
req.Header.Set("Authorization", "Bearer "+secret)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rec := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rec, req)
|
||||
var out map[string]any
|
||||
if rec.Body.Len() > 0 {
|
||||
_ = json.Unmarshal(rec.Body.Bytes(), &out)
|
||||
}
|
||||
return rec.Code, out
|
||||
}
|
||||
data := func(out map[string]any) map[string]any {
|
||||
t.Helper()
|
||||
d, _ := out["data"].(map[string]any)
|
||||
if d == nil {
|
||||
t.Fatalf("no data object in %v", out)
|
||||
}
|
||||
return d
|
||||
}
|
||||
id := func(d map[string]any) string { return strconv.Itoa(int(d["id"].(float64))) }
|
||||
errorsOf := func(out map[string]any) map[string]any {
|
||||
e, _ := out["errors"].(map[string]any)
|
||||
return e
|
||||
}
|
||||
|
||||
code, out := send(http.MethodPost, "/courses", map[string]any{"name": "Teoria", "form": "wyklad", "semester": 1, "code": "26-X"})
|
||||
if code != http.StatusCreated {
|
||||
t.Fatalf("create course = %d %v", code, out)
|
||||
}
|
||||
course := data(out)
|
||||
code, out = send(http.MethodPatch, "/courses/"+id(course), map[string]any{"description": "Plan wykładów: 1. Wprowadzenie."})
|
||||
if code != http.StatusOK || data(out)["description"] != "Plan wykładów: 1. Wprowadzenie." || data(out)["name"] != "Teoria" {
|
||||
t.Fatalf("patch course = %d %v", code, out)
|
||||
}
|
||||
if code, out = send(http.MethodPatch, "/courses/"+id(course), map[string]any{"owner": 1}); code != http.StatusUnprocessableEntity || errorsOf(out)["owner"] == nil {
|
||||
t.Errorf("course patch with an unknown column = %d %v", code, out)
|
||||
}
|
||||
if code, out = send(http.MethodPatch, "/courses/"+id(course), map[string]any{"form": "warsztat", "semester": 9}); code != http.StatusUnprocessableEntity || errorsOf(out)["form"] == nil || errorsOf(out)["semester"] == nil {
|
||||
t.Errorf("course patch breaking the rules = %d %v", code, out)
|
||||
}
|
||||
if code, _ = send(http.MethodDelete, "/courses/"+id(course), nil); code != http.StatusMethodNotAllowed && code != http.StatusNotFound {
|
||||
t.Errorf("course delete = %d, want no route", code)
|
||||
}
|
||||
|
||||
code, out = send(http.MethodPost, "/meetings", map[string]any{"number": 1, "starts_on": "2026-10-03", "ends_on": "2026-10-04"})
|
||||
if code != http.StatusCreated || data(out)["starts_on"] != "2026-10-03" {
|
||||
t.Fatalf("create meeting = %d %v", code, out)
|
||||
}
|
||||
meeting := data(out)
|
||||
if code, out = send(http.MethodPost, "/meetings", map[string]any{"number": 1, "starts_on": "2026-10-17", "ends_on": "2026-10-18"}); code != http.StatusUnprocessableEntity || errorsOf(out)["number"] == nil {
|
||||
t.Errorf("duplicate meeting number = %d %v", code, out)
|
||||
}
|
||||
if code, out = send(http.MethodPatch, "/meetings/"+id(meeting), map[string]any{"notes": "online"}); code != http.StatusOK || data(out)["number"] != float64(1) {
|
||||
t.Errorf("patch own meeting (unique on itself) = %d %v", code, out)
|
||||
}
|
||||
|
||||
session := map[string]any{"course_id": course["id"], "meeting_id": meeting["id"], "starts_at": "2026-10-03 09:00", "ends_at": "2026-10-03 10:30", "room": "107"}
|
||||
code, out = send(http.MethodPost, "/class-sessions", session)
|
||||
if code != http.StatusCreated || data(out)["starts_at"] != "2026-10-03T09:00:00+02:00" {
|
||||
t.Fatalf("create session = %d %v", code, out)
|
||||
}
|
||||
sess := data(out)
|
||||
if code, out = send(http.MethodPatch, "/class-sessions/"+id(sess), map[string]any{"ends_at": "2026-10-03 08:00"}); code != http.StatusUnprocessableEntity || errorsOf(out)["ends_at"] == nil {
|
||||
t.Errorf("session ending before it starts = %d %v", code, out)
|
||||
}
|
||||
if code, out = send(http.MethodPatch, "/class-sessions/"+id(sess), map[string]any{"room": "", "lecturer": "dr X", "is_online": true}); code != http.StatusOK || data(out)["room"] != nil || data(out)["is_online"] != true {
|
||||
t.Errorf("patch session = %d %v", code, out)
|
||||
}
|
||||
if code, out = send(http.MethodPatch, "/class-sessions/"+id(sess), map[string]any{"room": 102}); code != http.StatusOK || data(out)["room"] != "102" {
|
||||
t.Errorf("room sent as a number = %d %v", code, out)
|
||||
}
|
||||
if code, out = send(http.MethodPost, "/class-sessions", map[string]any{"course_id": 999999, "starts_at": "2026-10-03 09:00", "ends_at": "2026-10-03 10:00"}); code != http.StatusUnprocessableEntity || errorsOf(out)["course_id"] == nil {
|
||||
t.Errorf("session of an unknown course = %d %v", code, out)
|
||||
}
|
||||
|
||||
code, out = send(http.MethodPost, "/readings", map[string]any{"course_id": course["id"], "title": "Kuhn, Struktura rewolucji naukowych", "is_required": true})
|
||||
if code != http.StatusCreated {
|
||||
t.Fatalf("create reading = %d %v", code, out)
|
||||
}
|
||||
reading := data(out)
|
||||
if code, out = send(http.MethodPost, "/readings", map[string]any{"course_id": course["id"]}); code != http.StatusUnprocessableEntity || errorsOf(out)["title"] == nil {
|
||||
t.Errorf("reading without a title = %d %v", code, out)
|
||||
}
|
||||
if code, out = send(http.MethodGet, "/readings?course_id="+id(course), nil); code != http.StatusOK || len(out["data"].([]any)) != 1 {
|
||||
t.Errorf("readings of the course = %d %v", code, out)
|
||||
}
|
||||
if code, out = send(http.MethodGet, "/readings?course_id=999999", nil); code != http.StatusOK || len(out["data"].([]any)) != 0 {
|
||||
t.Errorf("readings of another course = %d %v", code, out)
|
||||
}
|
||||
|
||||
code, out = send(http.MethodPost, "/deadlines", map[string]any{"title": "Egzamin", "due_at": "2027-01-30T12:00:00+01:00", "kind": "egzamin", "course_id": course["id"]})
|
||||
if code != http.StatusCreated {
|
||||
t.Fatalf("create deadline = %d %v", code, out)
|
||||
}
|
||||
deadline := data(out)
|
||||
if code, out = send(http.MethodPatch, "/deadlines/"+id(deadline), map[string]any{"due_at": "jutro"}); code != http.StatusUnprocessableEntity || errorsOf(out)["due_at"] == nil {
|
||||
t.Errorf("deadline with a bad time = %d %v", code, out)
|
||||
}
|
||||
if code, out = send(http.MethodPatch, "/deadlines/"+id(deadline), map[string]any{"course_id": nil}); code != http.StatusOK || data(out)["course_id"] != nil {
|
||||
t.Errorf("deadline detached from the course = %d %v", code, out)
|
||||
}
|
||||
|
||||
for _, path := range []string{"/readings/" + id(reading), "/deadlines/" + id(deadline), "/class-sessions/" + id(sess)} {
|
||||
if code, _ := send(http.MethodDelete, path, nil); code != http.StatusNoContent {
|
||||
t.Errorf("delete %s = %d", path, code)
|
||||
}
|
||||
if code, _ := send(http.MethodGet, path, nil); code != http.StatusNotFound {
|
||||
t.Errorf("get %s after delete = %d", path, code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user