aboutsummaryrefslogtreecommitdiff
path: root/test_auth.sh
blob: d12c91c2d0af8cb708b0fcc1e0e8f69e25f368ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/bash

# JChat Authentication Test Suite
# Tests registration, login, and JMAP API access

set -e

BASE_URL="http://api.jchat.localhost"
TEST_EMAIL="test@example.com"
TEST_PASSWORD="testpass123"
TEST_DISPLAY_NAME="Test User"

echo "๐Ÿงช Starting JChat Authentication Tests..."
echo "==============================================="

# Test 1: Register a new user
echo "1. Testing user registration..."
REGISTER_RESPONSE=$(curl -s -X POST "${BASE_URL}/auth/register" \
  -H "Content-Type: application/json" \
  -d '{
    "email": "'${TEST_EMAIL}'",
    "password": "'${TEST_PASSWORD}'",
    "displayName": "'${TEST_DISPLAY_NAME}'"
  }' -w "\n%{http_code}")

HTTP_CODE=$(echo "$REGISTER_RESPONSE" | tail -n1)
REGISTER_BODY=$(echo "$REGISTER_RESPONSE" | head -n -1)

echo "   HTTP Status: $HTTP_CODE"
if [ "$HTTP_CODE" -eq 201 ]; then
    echo "   โœ… Registration successful"
    echo "   Response: $REGISTER_BODY"
    TOKEN=$(echo "$REGISTER_BODY" | jq -r '.token // empty')
    echo "   Token: ${TOKEN:0:20}..."
else
    echo "   โŒ Registration failed"
    echo "   Response: $REGISTER_BODY"
    exit 1
fi

echo ""

# Test 2: Login with the registered user
echo "2. Testing user login..."
LOGIN_RESPONSE=$(curl -s -X POST "${BASE_URL}/auth/login" \
  -H "Content-Type: application/json" \
  -d '{
    "email": "'${TEST_EMAIL}'",
    "password": "'${TEST_PASSWORD}'"
  }' -w "\n%{http_code}")

HTTP_CODE=$(echo "$LOGIN_RESPONSE" | tail -n1)
LOGIN_BODY=$(echo "$LOGIN_RESPONSE" | head -n -1)

echo "   HTTP Status: $HTTP_CODE"
if [ "$HTTP_CODE" -eq 200 ]; then
    echo "   โœ… Login successful"
    echo "   Response: $LOGIN_BODY"
    TOKEN=$(echo "$LOGIN_BODY" | jq -r '.token // empty')
    echo "   Token: ${TOKEN:0:20}..."
else
    echo "   โŒ Login failed"
    echo "   Response: $LOGIN_BODY"
    exit 1
fi

echo ""

# Test 3: Verify token with /auth/me
echo "3. Testing token verification..."
ME_RESPONSE=$(curl -s -X GET "${BASE_URL}/auth/me" \
  -H "Authorization: Bearer ${TOKEN}" \
  -w "\n%{http_code}")

HTTP_CODE=$(echo "$ME_RESPONSE" | tail -n1)
ME_BODY=$(echo "$ME_RESPONSE" | head -n -1)

echo "   HTTP Status: $HTTP_CODE"
if [ "$HTTP_CODE" -eq 200 ]; then
    echo "   โœ… Token verification successful"
    echo "   User info: $ME_BODY"
else
    echo "   โŒ Token verification failed"
    echo "   Response: $ME_BODY"
    exit 1
fi

echo ""

# Test 4: Test JMAP API with authentication
echo "4. Testing JMAP API access..."
JMAP_REQUEST='{
    "using": ["urn:ietf:params:jmap:core", "https://jmap.io/jchat/"],
    "methodCalls": [
        ["Conversation/query", {
            "accountId": "default",
            "filter": {},
            "sort": [{"property": "lastMessageAt", "isAscending": false}]
        }, "c1"]
    ]
}'

JMAP_RESPONSE=$(curl -s -X POST "${BASE_URL}/jmap/api" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${TOKEN}" \
  -d "$JMAP_REQUEST" \
  -w "\n%{http_code}")

HTTP_CODE=$(echo "$JMAP_RESPONSE" | tail -n1)
JMAP_BODY=$(echo "$JMAP_RESPONSE" | head -n -1)

echo "   HTTP Status: $HTTP_CODE"
if [ "$HTTP_CODE" -eq 200 ]; then
    echo "   โœ… JMAP API access successful"
    echo "   Response: $JMAP_BODY"
else
    echo "   โŒ JMAP API access failed"
    echo "   Response: $JMAP_BODY"
    exit 1
fi

echo ""

# Test 5: Test JMAP API without authentication
echo "5. Testing JMAP API without authentication..."
JMAP_UNAUTH_RESPONSE=$(curl -s -X POST "${BASE_URL}/jmap/api" \
  -H "Content-Type: application/json" \
  -d "$JMAP_REQUEST" \
  -w "\n%{http_code}")

HTTP_CODE=$(echo "$JMAP_UNAUTH_RESPONSE" | tail -n1)
JMAP_UNAUTH_BODY=$(echo "$JMAP_UNAUTH_RESPONSE" | head -n -1)

echo "   HTTP Status: $HTTP_CODE"
if [ "$HTTP_CODE" -eq 401 ]; then
    echo "   โœ… Unauthenticated access properly rejected"
    echo "   Response: $JMAP_UNAUTH_BODY"
else
    echo "   โŒ Unauthenticated access should be rejected"
    echo "   Response: $JMAP_UNAUTH_BODY"
fi

echo ""

# Test 6: Test logout
echo "6. Testing logout..."
LOGOUT_RESPONSE=$(curl -s -X POST "${BASE_URL}/auth/logout" \
  -H "Authorization: Bearer ${TOKEN}" \
  -w "\n%{http_code}")

HTTP_CODE=$(echo "$LOGOUT_RESPONSE" | tail -n1)
LOGOUT_BODY=$(echo "$LOGOUT_RESPONSE" | head -n -1)

echo "   HTTP Status: $HTTP_CODE"
if [ "$HTTP_CODE" -eq 200 ]; then
    echo "   โœ… Logout successful"
    echo "   Response: $LOGOUT_BODY"
else
    echo "   โŒ Logout failed"
    echo "   Response: $LOGOUT_BODY"
fi

echo ""
echo "๐ŸŽ‰ All tests completed!"
echo "==============================================="