#!/bin/sh
# Jetable. Les routes fermees, l'ecran ouvert, et la session encore vivante.
set -eu

BASE=http://127.0.0.1:4214
JAR=$(mktemp -d)

signin() {
  curl -s -c "$JAR/$1.txt" -H 'Content-Type: application/json' \
    -d "{\"username\":\"$1\",\"password\":\"$2\"}" \
    "$BASE/api/auth/sign-in/username" > /dev/null
}
signin narin admin1
signin malai manager1
signin nattikan staff1

echo "=== routes d'auth ==="
for r in /sign-up/email /update-user /is-username-available /change-password; do
  printf '%-26s %s\n' "$r" \
    "$(curl -s -o /dev/null -w '%{http_code}' -H 'Content-Type: application/json' -d '{}' "$BASE/api/auth$r")"
done
printf '%-26s %s\n' "/get-session (doit vivre)" \
  "$(curl -s -o /dev/null -w '%{http_code}' -b "$JAR/narin.txt" "$BASE/api/auth/get-session")"

echo
echo "=== /settings, les trois niveaux et un inconnu ==="
for who in narin malai nattikan; do
  printf '%-10s %s\n' "$who" \
    "$(curl -s -o /dev/null -w '%{http_code}' -b "$JAR/$who.txt" "$BASE/settings")"
done
printf '%-10s %s\n' "anonyme" \
  "$(curl -s -o /dev/null -w '%{http_code} %{redirect_url}' "$BASE/settings")"

echo
echo "=== le contenu de l'ecran, en admin ==="
curl -s -b "$JAR/narin.txt" "$BASE/settings" \
  | sed -n 's/.*<main[^>]*>\(.*\)<\/main>.*/\1/p' \
  | sed 's/<[^>]*>/\n/g' | grep -v '^[[:space:]]*$' | head -20
