134 lines
6.0 KiB
Bash
Executable File
134 lines
6.0 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# dth_lms.test.sh — e2e verification for S1..S4 of chg_0006 (LMS bootstrap).
|
|
# Exit 0 only if all 4 scenarios pass. Each assertion is a hard-fail.
|
|
#
|
|
# Usage:
|
|
# DTH_BASE=http://127.0.0.1:30000 bash tests/e2e/dth_lms.test.sh
|
|
#
|
|
# Pre-req: backend is running and admin + 2 LMS students / 2 LMS courses seeded.
|
|
|
|
set -uo pipefail
|
|
|
|
BASE="${DTH_BASE:-http://127.0.0.1:${PORT:-30000}}"
|
|
LOGIN_URL="$BASE/api/ssw/dth/auth/login"
|
|
STUDENTS_URL="$BASE/api/ssw/dth/lms/students"
|
|
COURSES_URL="$BASE/api/ssw/dth/lms/courses"
|
|
|
|
# Fixed seed UUIDs (from pglite/init.ts chg_0006 LMS seed)
|
|
SEED_STUDENT_ID="11111111-1111-1111-1111-111111111111"
|
|
SEED_COURSE_ID="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
|
|
|
|
pass=0; fail=0; fails=()
|
|
|
|
json_get() {
|
|
local field="$1"; local body="$2"
|
|
python3 -c "import json,sys; d=json.loads(sys.argv[1]); print(d${field})" "$body" 2>/dev/null
|
|
}
|
|
|
|
login() {
|
|
curl -s -X POST "$LOGIN_URL" -H 'Content-Type: application/json' \
|
|
-d "{\"username\":\"$1\",\"password\":\"$2\"}"
|
|
}
|
|
|
|
assert() {
|
|
local id="$1"; local cond="$2"; local detail="$3"
|
|
if eval "$cond"; then
|
|
pass=$((pass+1))
|
|
printf " ✓ %s\n" "$id"
|
|
else
|
|
fail=$((fail+1))
|
|
fails+=("$id: $detail")
|
|
printf " ✗ %s — %s\n" "$id" "$detail"
|
|
fi
|
|
}
|
|
|
|
# ----- pre: login admin -----
|
|
ADMIN_BODY="$(login admin 123456)"
|
|
ADMIN_TOKEN="$(json_get "['data']['token']" "$ADMIN_BODY")"
|
|
assert "pre_admin_login" "[ -n '$ADMIN_TOKEN' ]" "admin token empty; login body=$ADMIN_BODY"
|
|
|
|
# ----- S1: list ≥2 students with correct schema -----
|
|
LIST_STUDENTS_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$STUDENTS_URL")"
|
|
LIST_STUDENTS_LEN="$(python3 -c "import json,sys; print(len(json.loads(sys.argv[1])['data']))" "$LIST_STUDENTS_BODY" 2>/dev/null)"
|
|
STUDENT_HAS_FIELDS="$(python3 -c "
|
|
import json,sys
|
|
d=json.loads(sys.argv[1])
|
|
need={'id','student_no','name','email','created_at'}
|
|
print(all(need.issubset(set(s.keys())) for s in d['data']))
|
|
" "$LIST_STUDENTS_BODY")"
|
|
HAS_SEED_STUDENT="$(python3 -c "
|
|
import json,sys
|
|
d=json.loads(sys.argv[1])
|
|
ids={s['id'] for s in d['data']}
|
|
print('$SEED_STUDENT_ID' in ids)
|
|
" "$LIST_STUDENTS_BODY")"
|
|
assert "S1_students_ge_2" "[ '$LIST_STUDENTS_LEN' -ge 2 ]" "got $LIST_STUDENTS_LEN"
|
|
assert "S1_students_schema_ok" "[ '$STUDENT_HAS_FIELDS' = 'True' ]" "schema missing fields"
|
|
assert "S1_students_has_seed_id" "[ '$HAS_SEED_STUDENT' = 'True' ]" "seed student id missing"
|
|
|
|
# ----- S2: POST /students creates new, then GET /students/:id returns it -----
|
|
CREATE_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
|
|
-d '{"student_no":"S2026001","name":"张三","email":"[email protected]"}' \
|
|
"$STUDENTS_URL")"
|
|
NEW_STUDENT_ID="$(json_get "['data']['id']" "$CREATE_BODY")"
|
|
NEW_STUDENT_NAME="$(json_get "['data']['name']" "$CREATE_BODY")"
|
|
assert "S2_create_id_uuid" "echo '$NEW_STUDENT_ID' | grep -Eq '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'" "id=$NEW_STUDENT_ID"
|
|
assert "S2_create_name_zhangsan" "[ '$NEW_STUDENT_NAME' = '张三' ]" "name=$NEW_STUDENT_NAME"
|
|
|
|
GET_NEW_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$STUDENTS_URL/$NEW_STUDENT_ID")"
|
|
GET_NEW_NAME="$(json_get "['data']['name']" "$GET_NEW_BODY")"
|
|
GET_NEW_EMAIL="$(json_get "['data']['email']" "$GET_NEW_BODY")"
|
|
GET_NEW_NO="$(json_get "['data']['student_no']" "$GET_NEW_BODY")"
|
|
assert "S2_get_name_zhangsan" "[ '$GET_NEW_NAME' = '张三' ]" "name=$GET_NEW_NAME"
|
|
assert "S2_get_email_correct" "[ '$GET_NEW_EMAIL' = '[email protected]' ]" "email=$GET_NEW_EMAIL"
|
|
assert "S2_get_no_correct" "[ '$GET_NEW_NO' = 'S2026001' ]" "student_no=$GET_NEW_NO"
|
|
|
|
# ----- S3: list ≥2 courses with correct schema -----
|
|
LIST_COURSES_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$COURSES_URL")"
|
|
LIST_COURSES_LEN="$(python3 -c "import json,sys; print(len(json.loads(sys.argv[1])['data']))" "$LIST_COURSES_BODY" 2>/dev/null)"
|
|
COURSE_HAS_FIELDS="$(python3 -c "
|
|
import json,sys
|
|
d=json.loads(sys.argv[1])
|
|
need={'id','code','name','credits','instructor','created_at'}
|
|
print(all(need.issubset(set(c.keys())) for c in d['data']))
|
|
" "$LIST_COURSES_BODY")"
|
|
HAS_SEED_COURSE="$(python3 -c "
|
|
import json,sys
|
|
d=json.loads(sys.argv[1])
|
|
ids={c['id'] for c in d['data']}
|
|
print('$SEED_COURSE_ID' in ids)
|
|
" "$LIST_COURSES_BODY")"
|
|
assert "S3_courses_ge_2" "[ '$LIST_COURSES_LEN' -ge 2 ]" "got $LIST_COURSES_LEN"
|
|
assert "S3_courses_schema_ok" "[ '$COURSE_HAS_FIELDS' = 'True' ]" "schema missing fields"
|
|
assert "S3_courses_has_seed_id" "[ '$HAS_SEED_COURSE' = 'True' ]" "seed course id missing"
|
|
|
|
# ----- S4: enroll student → course, then list enrollments shows it -----
|
|
ENROLL_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
|
|
-d "{\"student_id\":\"$SEED_STUDENT_ID\"}" \
|
|
"$COURSES_URL/$SEED_COURSE_ID/enroll")"
|
|
ENROLL_CODE="$(json_get "['code']" "$ENROLL_BODY")"
|
|
ENROLL_STUDENT_ID="$(json_get "['data']['student_id']" "$ENROLL_BODY")"
|
|
ENROLL_COURSE_ID="$(json_get "['data']['course_id']" "$ENROLL_BODY")"
|
|
assert "S4_enroll_code_200" "[ '$ENROLL_CODE' = '200' ]" "code=$ENROLL_CODE"
|
|
assert "S4_enroll_student_id" "[ '$ENROLL_STUDENT_ID' = '$SEED_STUDENT_ID' ]" "student_id=$ENROLL_STUDENT_ID"
|
|
assert "S4_enroll_course_id" "[ '$ENROLL_COURSE_ID' = '$SEED_COURSE_ID' ]" "course_id=$ENROLL_COURSE_ID"
|
|
|
|
LIST_ENR_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" \
|
|
"$STUDENTS_URL/$SEED_STUDENT_ID/enrollments")"
|
|
LIST_ENR_LEN="$(python3 -c "import json,sys; print(len(json.loads(sys.argv[1])['data']))" "$LIST_ENR_BODY" 2>/dev/null)"
|
|
HAS_COURSE_IN_LIST="$(python3 -c "
|
|
import json,sys
|
|
d=json.loads(sys.argv[1])
|
|
ids={e['course_id'] for e in d['data']}
|
|
print('$SEED_COURSE_ID' in ids)
|
|
" "$LIST_ENR_BODY")"
|
|
assert "S4_list_enrollments_ge_1" "[ '$LIST_ENR_LEN' -ge 1 ]" "got $LIST_ENR_LEN"
|
|
assert "S4_list_enrollments_has_course" "[ '$HAS_COURSE_IN_LIST' = 'True' ]" "course_id not in list"
|
|
|
|
echo "---"
|
|
echo "e2e dth_lms: pass=$pass fail=$fail"
|
|
if [ $fail -gt 0 ]; then
|
|
printf 'FAILED:\n'; printf ' - %s\n' "${fails[@]}"
|
|
exit 1
|
|
fi
|
|
exit 0 |