forked from mtusi2026/mtusi2026lab4
lab4
This commit is contained in:
Executable
+100
@@ -0,0 +1,100 @@
|
||||
#!/bin/sh
|
||||
# Shared helpers for student-tests/*.sh (curl + jq only).
|
||||
# Usage: . "$(dirname "$0")/lib.sh"
|
||||
|
||||
set -eu
|
||||
|
||||
BASE_URL="${BASE_URL:-http://127.0.0.1:8080}"
|
||||
PASS="${STUDENT_TEST_PASSWORD:-Student-test-password-1}"
|
||||
ROOT="$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)"
|
||||
FIXTURES="$ROOT/tools/fixtures"
|
||||
|
||||
die() {
|
||||
printf '%s\n' "$*" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
need() {
|
||||
command -v "$1" >/dev/null 2>&1 || die "missing dependency: $1"
|
||||
}
|
||||
|
||||
need curl
|
||||
need jq
|
||||
need sha256sum
|
||||
need python3
|
||||
|
||||
uniq_name() {
|
||||
printf '%s_%s' "$1" "$(python3 -c 'import uuid; print(uuid.uuid4().hex[:12])')"
|
||||
}
|
||||
|
||||
# http_json METHOD PATH [curl args...]
|
||||
# Sets: HTTP_CODE, HTTP_BODY, HTTP_HDRS
|
||||
http_json() {
|
||||
method=$1
|
||||
path=$2
|
||||
shift 2
|
||||
hdr=$(mktemp)
|
||||
body=$(mktemp)
|
||||
code=$(curl -sS -m 60 -o "$body" -D "$hdr" -w '%{http_code}' \
|
||||
-X "$method" "$BASE_URL$path" "$@") || {
|
||||
rm -f "$hdr" "$body"
|
||||
die "curl failed: $method $path"
|
||||
}
|
||||
HTTP_CODE=$code
|
||||
HTTP_BODY=$(cat "$body")
|
||||
HTTP_HDRS=$(cat "$hdr")
|
||||
rm -f "$hdr" "$body"
|
||||
}
|
||||
|
||||
expect_code() {
|
||||
want=$1
|
||||
[ "$HTTP_CODE" = "$want" ] || die "expected HTTP $want got $HTTP_CODE body=$HTTP_BODY"
|
||||
}
|
||||
|
||||
register_user() {
|
||||
label=$1
|
||||
user=$(uniq_name "$label")
|
||||
http_json POST /api/v1/users \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d "{\"username\":\"$user\",\"display_name\":\"$label\",\"password\":\"$PASS\"}"
|
||||
expect_code 201
|
||||
USER_ID=$(printf '%s' "$HTTP_BODY" | jq -r .id)
|
||||
USERNAME=$user
|
||||
cred=$(printf '%s' "$USERNAME:$PASS" | python3 -c 'import sys,base64; print(base64.b64encode(sys.stdin.buffer.read()).decode())')
|
||||
http_json POST /api/v1/auth/sessions -H "Authorization: Basic $cred"
|
||||
expect_code 201
|
||||
TOKEN=$(printf '%s' "$HTTP_BODY" | jq -r .token)
|
||||
[ -n "$TOKEN" ] && [ "$TOKEN" != null ] || die "empty token"
|
||||
}
|
||||
|
||||
create_chat() {
|
||||
title=${1:-test}
|
||||
member_json=$2
|
||||
http_json POST /api/v1/chats \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d "{\"title\":\"$title\",\"member_ids\":$member_json}"
|
||||
expect_code 201
|
||||
CHAT_ID=$(printf '%s' "$HTTP_BODY" | jq -r .id)
|
||||
}
|
||||
|
||||
upload_file() {
|
||||
chat_id=$1
|
||||
file=$2
|
||||
filename=$3
|
||||
ctype=$4
|
||||
http_json POST "/api/v1/chats/$chat_id/attachments?filename=$filename" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Content-Type: $ctype" \
|
||||
--data-binary @"$file"
|
||||
}
|
||||
|
||||
sha256_file() {
|
||||
sha256sum "$1" | awk '{print $1}'
|
||||
}
|
||||
|
||||
assert_eq() {
|
||||
[ "$1" = "$2" ] || die "assert_eq failed: '$1' != '$2'${3:+ ($3)}"
|
||||
}
|
||||
|
||||
printf 'lib.sh: BASE_URL=%s\n' "$BASE_URL" >&2
|
||||
Reference in New Issue
Block a user