-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-tests.sh
More file actions
executable file
Β·174 lines (150 loc) Β· 5.32 KB
/
api-tests.sh
File metadata and controls
executable file
Β·174 lines (150 loc) Β· 5.32 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
#!/bin/bash
# Task Manager API - Example curl commands
# Use port 80 (nginx) for all API requests
BASE_URL="http://localhost"
API_URL="${BASE_URL}/api/v1"
echo "π§ Task Manager API Examples"
echo "============================"
echo ""
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
# Generate unique email for testing
TIMESTAMP=$(date +%s)
TEST_EMAIL="test.user.${TIMESTAMP}@example.com"
TEST_USERNAME="test_user_${TIMESTAMP}"
echo "1οΈβ£ Register User"
echo -e "${YELLOW}curl -X POST ${API_URL}/auth/register \\${NC}"
REGISTER_RESPONSE=$(curl -s -X POST "${API_URL}/auth/register" \
-H "Content-Type: application/json" \
-d "{
\"username\": \"${TEST_USERNAME}\",
\"email\": \"${TEST_EMAIL}\",
\"password\": \"Test@123456\",
\"first_name\": \"Test\",
\"last_name\": \"User\"
}")
echo "$REGISTER_RESPONSE" | jq .
# Check if registration was successful
if echo "$REGISTER_RESPONSE" | jq -e '.user.id' > /dev/null 2>&1; then
echo -e "${GREEN}β
Registration successful${NC}\n"
else
echo -e "${RED}β Registration failed${NC}\n"
fi
echo "2οΈβ£ Login with New User"
echo -e "${YELLOW}curl -X POST ${API_URL}/auth/login \\${NC}"
LOGIN_RESPONSE=$(curl -s -X POST "${API_URL}/auth/login" \
-H "Content-Type: application/json" \
-d "{
\"email\": \"${TEST_EMAIL}\",
\"password\": \"Test@123456\"
}")
echo "$LOGIN_RESPONSE" | jq .
TOKEN=$(echo "$LOGIN_RESPONSE" | jq -r '.access_token')
# Check if login was successful
if [ "$TOKEN" != "null" ] && [ -n "$TOKEN" ]; then
echo -e "\n${GREEN}β
Login successful - Token saved${NC}\n"
else
echo -e "\n${RED}β Login failed - No token received${NC}"
echo -e "${YELLOW}π‘ Trying with existing user (jane@example.com)${NC}\n"
# Fallback to existing user
LOGIN_RESPONSE=$(curl -s -X POST "${API_URL}/auth/login" \
-H "Content-Type: application/json" \
-d '{
"email": "jane@example.com",
"password": "SecurePass@456"
}')
echo "$LOGIN_RESPONSE" | jq .
TOKEN=$(echo "$LOGIN_RESPONSE" | jq -r '.access_token')
if [ "$TOKEN" != "null" ] && [ -n "$TOKEN" ]; then
echo -e "\n${GREEN}β
Login successful with existing user${NC}\n"
else
echo -e "\n${RED}β Login failed - Cannot continue with authenticated requests${NC}\n"
TOKEN=""
fi
fi
if [ -n "$TOKEN" ] && [ "$TOKEN" != "null" ]; then
echo "3οΈβ£ Create Task"
echo -e "${YELLOW}curl -X POST ${API_URL}/tasks \\${NC}"
TASK_RESPONSE=$(curl -s -X POST "${API_URL}/tasks" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{
"title": "Test Task",
"description": "Testing API via nginx",
"status": "pending",
"priority": "medium"
}')
echo "$TASK_RESPONSE" | jq .
if echo "$TASK_RESPONSE" | jq -e '.id' > /dev/null 2>&1; then
echo -e "${GREEN}β
Task created successfully${NC}\n"
TASK_ID=$(echo "$TASK_RESPONSE" | jq -r '.id')
else
echo -e "${RED}β Failed to create task${NC}\n"
fi
echo "4οΈβ£ Get All Tasks"
echo -e "${YELLOW}curl -X GET ${API_URL}/tasks \\${NC}"
TASKS_RESPONSE=$(curl -s -X GET "${API_URL}/tasks" \
-H "Authorization: Bearer $TOKEN")
echo "$TASKS_RESPONSE" | jq .
if echo "$TASKS_RESPONSE" | jq -e 'length' > /dev/null 2>&1; then
TASK_COUNT=$(echo "$TASKS_RESPONSE" | jq 'length')
echo -e "${GREEN}β
Retrieved ${TASK_COUNT} tasks${NC}\n"
else
echo -e "${RED}β Failed to retrieve tasks${NC}\n"
fi
# Bonus: Update task if we created one
if [ -n "$TASK_ID" ] && [ "$TASK_ID" != "null" ]; then
echo "5οΈβ£ Update Task"
echo -e "${YELLOW}curl -X PUT ${API_URL}/tasks/${TASK_ID} \\${NC}"
UPDATE_RESPONSE=$(curl -s -X PUT "${API_URL}/tasks/${TASK_ID}" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{
"title": "Updated Test Task",
"status": "in_progress"
}')
echo "$UPDATE_RESPONSE" | jq .
if echo "$UPDATE_RESPONSE" | jq -e '.message' > /dev/null 2>&1; then
echo -e "${GREEN}β
Task updated successfully${NC}\n"
else
echo -e "${RED}β Failed to update task${NC}\n"
fi
fi
else
echo -e "${RED}β οΈ Skipping authenticated requests (no valid token)${NC}\n"
fi
echo "6οΈβ£ Health Check"
echo -e "${YELLOW}curl -X GET ${BASE_URL}/health \\${NC}"
HEALTH_RESPONSE=$(curl -s -X GET "${BASE_URL}/health")
echo "$HEALTH_RESPONSE" | jq .
if echo "$HEALTH_RESPONSE" | jq -e '.status == "healthy"' > /dev/null 2>&1; then
echo -e "${GREEN}β
System is healthy${NC}\n"
else
echo -e "${RED}β System health check failed${NC}\n"
fi
echo "7οΈβ£ Nginx Health"
echo -e "${YELLOW}curl -X GET ${BASE_URL}/nginx-health \\${NC}"
NGINX_RESPONSE=$(curl -s -X GET "${BASE_URL}/nginx-health")
echo "$NGINX_RESPONSE"
echo -e "\n"
echo "============================"
echo -e "${GREEN}β
All API tests completed!${NC}"
echo ""
echo "π Test Summary:"
echo " - Registration: ${TEST_EMAIL}"
if [ -n "$TOKEN" ] && [ "$TOKEN" != "null" ]; then
echo " - Authentication: β
Success"
echo " - Token received: ${TOKEN:0:20}..."
else
echo " - Authentication: β Failed"
fi
if [ -n "$TASK_ID" ] && [ "$TASK_ID" != "null" ]; then
echo " - Task created: ${TASK_ID}"
fi
echo ""
echo "π‘ Important:"
echo " - Use port 80 (or omit port)"
echo " - Backend port 8081 is no longer exposed"
echo " - All traffic goes through nginx"