#!/bin/sh
# Jetable. Les deux routes que la doc du plugin username signale.
set -eu

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

echo "=== 1. is-username-available, sans aucune session ==="
curl -s -o /tmp/a.json -w 'HTTP %{http_code}  ' \
  -H 'Content-Type: application/json' -d '{"username":"narin"}' \
  "$BASE/api/auth/is-username-available"
cat /tmp/a.json; echo
curl -s -o /tmp/b.json -w 'HTTP %{http_code}  ' \
  -H 'Content-Type: application/json' -d '{"username":"personne.inexistante"}' \
  "$BASE/api/auth/is-username-available"
cat /tmp/b.json; echo

echo
echo "=== 2. sign-up/email, apres la fermeture ==="
curl -s -o /tmp/c.json -w 'HTTP %{http_code}  ' \
  -H 'Content-Type: application/json' \
  -d '{"email":"x@advmfr.local","password":"NotMyFactory1","name":"X","username":"xxtest"}' \
  "$BASE/api/auth/sign-up/email"
head -c 120 /tmp/c.json; echo

echo
echo "=== 3. update-user : un employe peut-il changer son propre identifiant ? ==="
curl -s -c "$JAR" -o /dev/null -H 'Content-Type: application/json' \
  -d '{"username":"nattikan","password":"staff1"}' \
  "$BASE/api/auth/sign-in/username"
curl -s -b "$JAR" -o /tmp/d.json -w 'HTTP %{http_code}  ' \
  -H 'Content-Type: application/json' -d '{"username":"nattikan2"}' \
  "$BASE/api/auth/update-user"
head -c 200 /tmp/d.json; echo
