-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-auth.sh
More file actions
executable file
·159 lines (136 loc) · 4.26 KB
/
test-auth.sh
File metadata and controls
executable file
·159 lines (136 loc) · 4.26 KB
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
#!/usr/bin/env bash
# Quick test script for the new authentication endpoints
# Run this after starting the API server
API_URL="${API_URL:-http://localhost:4001}"
EMAIL="test-$(date +%s)@example.com"
PASSWORD="testpassword123"
echo "🧪 Testing Mixtape Auth API"
echo "=============================="
echo ""
# Test 1: Health check
echo "1️⃣ Health check..."
HEALTH=$(curl -s "${API_URL}/health")
if echo "$HEALTH" | grep -q "ok"; then
echo "✅ Health check passed"
else
echo "❌ Health check failed"
exit 1
fi
echo ""
# Test 2: Register new user
echo "2️⃣ Registering new user (${EMAIL})..."
REGISTER_RESPONSE=$(curl -s -c cookies.txt -w "\n%{http_code}" \
-X POST "${API_URL}/api/auth/register" \
-H "Content-Type: application/json" \
-d "{
\"email\": \"${EMAIL}\",
\"password\": \"${PASSWORD}\",
\"firstName\": \"Test\",
\"lastName\": \"User\"
}")
HTTP_CODE=$(echo "$REGISTER_RESPONSE" | tail -n1)
BODY=$(echo "$REGISTER_RESPONSE" | sed '$d')
if [ "$HTTP_CODE" = "201" ]; then
echo "✅ Registration successful"
echo " Response: $BODY"
else
echo "❌ Registration failed (HTTP $HTTP_CODE)"
echo " Response: $BODY"
exit 1
fi
echo ""
# Test 3: Get current user (should work with cookie from registration)
echo "3️⃣ Fetching current user (/me)..."
ME_RESPONSE=$(curl -s -b cookies.txt -w "\n%{http_code}" "${API_URL}/api/auth/me")
HTTP_CODE=$(echo "$ME_RESPONSE" | tail -n1)
BODY=$(echo "$ME_RESPONSE" | sed '$d')
if [ "$HTTP_CODE" = "200" ]; then
echo "✅ Fetched current user"
echo " Response: $BODY"
else
echo "❌ Failed to fetch current user (HTTP $HTTP_CODE)"
echo " Response: $BODY"
fi
echo ""
# Test 4: Logout
echo "4️⃣ Logging out..."
LOGOUT_RESPONSE=$(curl -s -b cookies.txt -c cookies.txt -w "\n%{http_code}" \
-X POST "${API_URL}/api/auth/logout")
HTTP_CODE=$(echo "$LOGOUT_RESPONSE" | tail -n1)
BODY=$(echo "$LOGOUT_RESPONSE" | sed '$d')
if [ "$HTTP_CODE" = "200" ]; then
echo "✅ Logout successful"
echo " Response: $BODY"
else
echo "❌ Logout failed (HTTP $HTTP_CODE)"
echo " Response: $BODY"
fi
echo ""
# Test 5: Try to access /me after logout (should fail)
echo "5️⃣ Testing /me after logout (should fail)..."
ME_AFTER_LOGOUT=$(curl -s -b cookies.txt -w "\n%{http_code}" "${API_URL}/api/auth/me")
HTTP_CODE=$(echo "$ME_AFTER_LOGOUT" | tail -n1)
if [ "$HTTP_CODE" = "401" ]; then
echo "✅ Correctly blocked after logout"
else
echo "⚠️ Unexpected status after logout (HTTP $HTTP_CODE)"
fi
echo ""
# Test 6: Login with credentials
echo "6️⃣ Logging in with credentials..."
LOGIN_RESPONSE=$(curl -s -c cookies.txt -w "\n%{http_code}" \
-X POST "${API_URL}/api/auth/login" \
-H "Content-Type: application/json" \
-d "{
\"email\": \"${EMAIL}\",
\"password\": \"${PASSWORD}\"
}")
HTTP_CODE=$(echo "$LOGIN_RESPONSE" | tail -n1)
BODY=$(echo "$LOGIN_RESPONSE" | sed '$d')
if [ "$HTTP_CODE" = "200" ]; then
echo "✅ Login successful"
echo " Response: $BODY"
else
echo "❌ Login failed (HTTP $HTTP_CODE)"
echo " Response: $BODY"
fi
echo ""
# Test 7: Get current user after login
echo "7️⃣ Fetching current user after login..."
ME_AFTER_LOGIN=$(curl -s -b cookies.txt -w "\n%{http_code}" "${API_URL}/api/auth/me")
HTTP_CODE=$(echo "$ME_AFTER_LOGIN" | tail -n1)
BODY=$(echo "$ME_AFTER_LOGIN" | sed '$d')
if [ "$HTTP_CODE" = "200" ]; then
echo "✅ Fetched current user after login"
echo " Response: $BODY"
else
echo "❌ Failed to fetch current user (HTTP $HTTP_CODE)"
echo " Response: $BODY"
fi
echo ""
# Test 8: Test duplicate email registration
echo "8️⃣ Testing duplicate email (should fail)..."
DUP_RESPONSE=$(curl -s -w "\n%{http_code}" \
-X POST "${API_URL}/api/auth/register" \
-H "Content-Type: application/json" \
-d "{
\"email\": \"${EMAIL}\",
\"password\": \"${PASSWORD}\",
\"firstName\": \"Test\",
\"lastName\": \"User\"
}")
HTTP_CODE=$(echo "$DUP_RESPONSE" | tail -n1)
if [ "$HTTP_CODE" = "409" ]; then
echo "✅ Correctly rejected duplicate email"
else
echo "⚠️ Unexpected status for duplicate email (HTTP $HTTP_CODE)"
fi
echo ""
# Cleanup
rm -f cookies.txt
echo "=============================="
echo "✨ All tests completed!"
echo ""
echo "Test user created:"
echo " Email: ${EMAIL}"
echo " Password: ${PASSWORD}"