#!/bin/bash
# Get groups for both users
source "$(dirname "$0")/config.sh"

TOKEN1=$(load_state "TOKEN1")
TOKEN2=$(load_state "TOKEN2")

if [[ -z "$TOKEN1" || -z "$TOKEN2" ]]; then
    echo "ERROR: Missing tokens. Run 02_login.sh first."
    exit 1
fi

echo "=== Getting groups for user1 ==="
RESP=$(curl -s -w "\n%{http_code}" -X POST "$BASE_URL/get/groups" \
    -d "token=$TOKEN1")
BODY=$(echo "$RESP" | head -1)
CODE=$(echo "$RESP" | tail -1)
echo "Response: $BODY (HTTP $CODE)"

if [[ "$CODE" != "202" ]]; then
    echo "FAIL: Expected HTTP 202, got $CODE"
    exit 1
fi

echo ""
echo "=== Getting groups for user2 ==="
RESP=$(curl -s -w "\n%{http_code}" -X POST "$BASE_URL/get/groups" \
    -d "token=$TOKEN2")
BODY=$(echo "$RESP" | head -1)
CODE=$(echo "$RESP" | tail -1)
echo "Response: $BODY (HTTP $CODE)"

if [[ "$CODE" != "202" ]]; then
    echo "FAIL: Expected HTTP 202, got $CODE"
    exit 1
fi

echo ""
echo "=== Getting groups with invalid token ==="
RESP=$(curl -s -w "\n%{http_code}" -X POST "$BASE_URL/get/groups" \
    -d "token=invalid_token")
BODY=$(echo "$RESP" | head -1)
CODE=$(echo "$RESP" | tail -1)
echo "Response: $BODY (HTTP $CODE)"

if [[ "$CODE" != "401" ]]; then
    echo "FAIL: Expected HTTP 401, got $CODE"
    exit 1
fi

echo ""
echo "ALL PASSED"
