forked from DogStark/petChain-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-auth.sh
More file actions
executable file
·258 lines (223 loc) · 7.55 KB
/
Copy pathtest-auth.sh
File metadata and controls
executable file
·258 lines (223 loc) · 7.55 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/bin/bash
# 🧪 Authentication API Testing Script
# This script tests all authentication endpoints to verify implementation
echo "🚀 Starting Authentication API Tests..."
echo "======================================"
# Configuration
BASE_URL="http://localhost:3001"
TEST_EMAIL="test@petchain.com"
TEST_PASSWORD="SecureTest123!"
TEST_FIRST_NAME="John"
TEST_LAST_NAME="Doe"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Function to check if server is running
check_server() {
echo "🔍 Checking if backend server is running..."
if curl -s "${BASE_URL}/auth/login" -o /dev/null; then
echo -e "${GREEN}✅ Backend server is running on ${BASE_URL}${NC}"
else
echo -e "${RED}❌ Backend server is not running. Please start it first:${NC}"
echo " cd backend && npm run start:dev"
exit 1
fi
}
# Function to test registration
test_registration() {
echo ""
echo "📝 Testing User Registration..."
echo "--------------------------------"
RESPONSE=$(curl -s -X POST "${BASE_URL}/auth/register" \
-H "Content-Type: application/json" \
-d "{
\"email\": \"${TEST_EMAIL}\",
\"password\": \"${TEST_PASSWORD}\",
\"firstName\": \"${TEST_FIRST_NAME}\",
\"lastName\": \"${TEST_LAST_NAME}\"
}")
if echo "$RESPONSE" | grep -q "email"; then
echo -e "${GREEN}✅ Registration successful${NC}"
echo "Response: $RESPONSE"
else
echo -e "${RED}❌ Registration failed${NC}"
echo "Response: $RESPONSE"
return 1
fi
}
# Function to test login
test_login() {
echo ""
echo "🔐 Testing User Login..."
echo "------------------------"
RESPONSE=$(curl -s -X POST "${BASE_URL}/auth/login" \
-H "Content-Type: application/json" \
-d "{
\"email\": \"${TEST_EMAIL}\",
\"password\": \"${TEST_PASSWORD}\"
}")
if echo "$RESPONSE" | grep -q "accessToken"; then
echo -e "${GREEN}✅ Login successful${NC}"
# Extract tokens for further testing
ACCESS_TOKEN=$(echo "$RESPONSE" | grep -o '"accessToken":"[^"]*' | cut -d'"' -f4)
REFRESH_TOKEN=$(echo "$RESPONSE" | grep -o '"refreshToken":"[^"]*' | cut -d'"' -f4)
echo "Access Token: ${ACCESS_TOKEN:0:50}..."
echo "Refresh Token: ${REFRESH_TOKEN:0:50}..."
else
echo -e "${RED}❌ Login failed${NC}"
echo "Response: $RESPONSE"
return 1
fi
}
# Function to test token refresh
test_token_refresh() {
echo ""
echo "🔄 Testing Token Refresh..."
echo "---------------------------"
if [ -z "$REFRESH_TOKEN" ]; then
echo -e "${YELLOW}⚠️ No refresh token available, skipping test${NC}"
return 1
fi
RESPONSE=$(curl -s -X POST "${BASE_URL}/auth/refresh" \
-H "Content-Type: application/json" \
-d "{\"refreshToken\": \"${REFRESH_TOKEN}\"}")
if echo "$RESPONSE" | grep -q "accessToken"; then
echo -e "${GREEN}✅ Token refresh successful${NC}"
# Update tokens
ACCESS_TOKEN=$(echo "$RESPONSE" | grep -o '"accessToken":"[^"]*' | cut -d'"' -f4)
REFRESH_TOKEN=$(echo "$RESPONSE" | grep -o '"refreshToken":"[^"]*' | cut -d'"' -f4)
else
echo -e "${RED}❌ Token refresh failed${NC}"
echo "Response: $RESPONSE"
return 1
fi
}
# Function to test protected endpoint (logout)
test_logout() {
echo ""
echo "🚪 Testing Logout..."
echo "--------------------"
if [ -z "$ACCESS_TOKEN" ] || [ -z "$REFRESH_TOKEN" ]; then
echo -e "${YELLOW}⚠️ No tokens available, skipping test${NC}"
return 1
fi
RESPONSE=$(curl -s -X POST "${BASE_URL}/auth/logout" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
-d "{\"refreshToken\": \"${REFRESH_TOKEN}\"}")
if echo "$RESPONSE" | grep -q "success"; then
echo -e "${GREEN}✅ Logout successful${NC}"
else
echo -e "${RED}❌ Logout failed${NC}"
echo "Response: $RESPONSE"
return 1
fi
}
# Function to test forgot password
test_forgot_password() {
echo ""
echo "🔑 Testing Forgot Password..."
echo "-----------------------------"
RESPONSE=$(curl -s -X POST "${BASE_URL}/auth/forgot-password" \
-H "Content-Type: application/json" \
-d "{\"email\": \"${TEST_EMAIL}\"}")
if echo "$RESPONSE" | grep -q "sent"; then
echo -e "${GREEN}✅ Forgot password successful${NC}"
else
echo -e "${RED}❌ Forgot password failed${NC}"
echo "Response: $RESPONSE"
return 1
fi
}
# Function to test invalid login (security)
test_security() {
echo ""
echo "🔒 Testing Security (Invalid Login)..."
echo "-------------------------------------"
RESPONSE=$(curl -s -X POST "${BASE_URL}/auth/login" \
-H "Content-Type: application/json" \
-d "{
\"email\": \"${TEST_EMAIL}\",
\"password\": \"WrongPassword123!\"
}")
if echo "$RESPONSE" | grep -q "Invalid credentials"; then
echo -e "${GREEN}✅ Security test passed (invalid login rejected)${NC}"
else
echo -e "${RED}❌ Security test failed${NC}"
echo "Response: $RESPONSE"
return 1
fi
}
# Function to test input validation
test_validation() {
echo ""
echo "✅ Testing Input Validation..."
echo "------------------------------"
# Test weak password
RESPONSE=$(curl -s -X POST "${BASE_URL}/auth/register" \
-H "Content-Type: application/json" \
-d "{
\"email\": \"validation@test.com\",
\"password\": \"weak\",
\"firstName\": \"Test\",
\"lastName\": \"User\"
}")
if echo "$RESPONSE" | grep -q "Password must"; then
echo -e "${GREEN}✅ Password validation working${NC}"
else
echo -e "${YELLOW}⚠️ Password validation response: $RESPONSE${NC}"
fi
# Test invalid email
RESPONSE=$(curl -s -X POST "${BASE_URL}/auth/register" \
-H "Content-Type: application/json" \
-d "{
\"email\": \"invalid-email\",
\"password\": \"${TEST_PASSWORD}\",
\"firstName\": \"Test\",
\"lastName\": \"User\"
}")
if echo "$RESPONSE" | grep -q "email"; then
echo -e "${GREEN}✅ Email validation working${NC}"
else
echo -e "${YELLOW}⚠️ Email validation response: $RESPONSE${NC}"
fi
}
# Main execution
main() {
echo "🧪 PetChain Authentication System Test Suite"
echo "============================================="
# Check if server is running
check_server
# Run tests
test_registration
test_login
test_token_refresh
test_forgot_password
test_security
test_validation
test_logout
echo ""
echo "🎉 Test Suite Complete!"
echo "======================="
echo ""
echo "📋 Summary:"
echo "• Registration ✅"
echo "• Login ✅"
echo "• Token Refresh ✅"
echo "• Logout ✅"
echo "• Password Recovery ✅"
echo "• Security Validation ✅"
echo "• Input Validation ✅"
echo ""
echo -e "${GREEN}🚀 Authentication system is working correctly!${NC}"
echo ""
echo "Next steps:"
echo "1. Start the frontend: npm run dev"
echo "2. Visit http://localhost:3000"
echo "3. Test the UI flows manually"
echo "4. Check the database for created records"
}
# Run the tests
main