-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_all_apis.sh
More file actions
358 lines (290 loc) Β· 13.4 KB
/
test_all_apis.sh
File metadata and controls
358 lines (290 loc) Β· 13.4 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/bin/bash
# Task Challenge API - Comprehensive Test Script
# This script tests both REST and GraphQL APIs to ensure they work correctly
set -e # Exit on any error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# API Base URL
BASE_URL="http://127.0.0.1:8000"
# Your JWT Token (will be updated after login)
TOKEN=""
# Function to print section headers
print_header() {
echo -e "\n${BLUE}================================${NC}"
echo -e "${BLUE}$1${NC}"
echo -e "${BLUE}================================${NC}\n"
}
# Function to print test results
print_result() {
if [ $? -eq 0 ]; then
echo -e "${GREEN}β
$1 - SUCCESS${NC}\n"
else
echo -e "${RED}β $1 - FAILED${NC}\n"
fi
}
# Function to make REST API calls
api_call() {
local method=$1
local endpoint=$2
local data=$3
local description=$4
echo -e "${YELLOW}Testing REST: $description${NC}"
echo -e "${YELLOW}$method $endpoint${NC}"
if [ -n "$data" ]; then
if [ -n "$TOKEN" ]; then
curl -s -X $method "$BASE_URL$endpoint" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d "$data" | jq '.' 2>/dev/null || echo "Response received"
else
curl -s -X $method "$BASE_URL$endpoint" \
-H "Content-Type: application/json" \
-d "$data" | jq '.' 2>/dev/null || echo "Response received"
fi
else
if [ -n "$TOKEN" ]; then
curl -s -X $method "$BASE_URL$endpoint" \
-H "Authorization: Bearer $TOKEN" | jq '.' 2>/dev/null || echo "Response received"
else
curl -s -X $method "$BASE_URL$endpoint" | jq '.' 2>/dev/null || echo "Response received"
fi
fi
print_result "$description"
}
# Function to make GraphQL API calls
graphql_call() {
local query=$1
local description=$2
local variables=$3
echo -e "${YELLOW}Testing GraphQL: $description${NC}"
echo -e "${YELLOW}POST /graphql${NC}"
local payload
if [ -n "$variables" ]; then
payload="{\"query\": \"$query\", \"variables\": $variables}"
else
payload="{\"query\": \"$query\"}"
fi
if [ -n "$TOKEN" ]; then
curl -s -X POST "$BASE_URL/graphql" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d "$payload" | jq '.' 2>/dev/null || echo "Response received"
else
curl -s -X POST "$BASE_URL/graphql" \
-H "Content-Type: application/json" \
-d "$payload" | jq '.' 2>/dev/null || echo "Response received"
fi
print_result "$description"
}
echo -e "${GREEN}π TASK CHALLENGE API - COMPREHENSIVE TEST (REST + GraphQL)${NC}"
echo -e "${GREEN}============================================================${NC}"
# 1. HEALTH CHECK
print_header "1. HEALTH CHECK"
echo -e "${YELLOW}Testing: Health Check${NC}"
echo -e "${YELLOW}GET /ping${NC}"
curl -s -X GET "$BASE_URL/ping" | jq '.'
print_result "Health Check"
echo -e "${YELLOW}Testing: Root Endpoint${NC}"
echo -e "${YELLOW}GET /${NC}"
curl -s -X GET "$BASE_URL/" | jq '.'
print_result "Root Endpoint"
# 2. GRAPHQL INTROSPECTION
print_header "2. GRAPHQL INTROSPECTION"
graphql_call "{ __schema { types { name } } }" "GraphQL Schema Introspection"
# 3. AUTHENTICATION TESTS (REST)
print_header "3. AUTHENTICATION TESTS (REST)"
# Register a new user via REST
api_call "POST" "/api/auth/register" \
'{"email": "resttest@example.com", "full_name": "REST Test User", "password": "testpass123"}' \
"REST User Registration"
# Login with the new user via REST
echo -e "${YELLOW}Testing REST: User Login${NC}"
echo -e "${YELLOW}POST /api/auth/login${NC}"
LOGIN_RESPONSE=$(curl -s -X POST "$BASE_URL/api/auth/login" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=resttest@example.com&password=testpass123")
echo "$LOGIN_RESPONSE" | jq '.'
print_result "REST User Login"
# Extract token if login successful
TOKEN=$(echo "$LOGIN_RESPONSE" | jq -r '.access_token // empty')
if [ -n "$TOKEN" ] && [ "$TOKEN" != "null" ]; then
echo -e "${GREEN}β
Using token for subsequent tests: ${TOKEN:0:20}...${NC}"
fi
# Get current user via REST
api_call "GET" "/api/auth/me" "" "Get Current User (REST)"
# 4. AUTHENTICATION TESTS (GraphQL)
print_header "4. AUTHENTICATION TESTS (GraphQL)"
# Register a new user via GraphQL
graphql_call "mutation { register(userInput: { email: \"graphqltest@example.com\", fullName: \"GraphQL Test User\", password: \"testpass123\" }) { id email fullName } }" "GraphQL User Registration"
# Login via GraphQL
echo -e "${YELLOW}Testing GraphQL: User Login${NC}"
GRAPHQL_LOGIN_RESPONSE=$(curl -s -X POST "$BASE_URL/graphql" \
-H "Content-Type: application/json" \
-d '{"query": "mutation { login(loginInput: { email: \"graphqltest@example.com\", password: \"testpass123\" }) { accessToken tokenType user { id email fullName } } }"}')
echo "$GRAPHQL_LOGIN_RESPONSE" | jq '.'
print_result "GraphQL User Login"
# 5. TASK LIST TESTS (REST)
print_header "5. TASK LIST TESTS (REST)"
# Create Task List via REST
echo -e "${YELLOW}Testing REST: Create Task List${NC}"
echo -e "${YELLOW}POST /api/task-lists/${NC}"
TASK_LIST_RESPONSE=$(curl -s -X POST "$BASE_URL/api/task-lists/" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{"name": "REST Test List", "description": "Testing REST API"}')
echo "$TASK_LIST_RESPONSE" | jq '.'
print_result "Create Task List (REST)"
# Extract task list ID
TASK_LIST_ID=$(echo "$TASK_LIST_RESPONSE" | jq -r '.id // empty')
echo -e "${GREEN}π Task List ID: $TASK_LIST_ID${NC}"
# List Task Lists via REST
api_call "GET" "/api/task-lists/" "" "List Task Lists (REST)"
# Get Specific Task List via REST
if [ -n "$TASK_LIST_ID" ] && [ "$TASK_LIST_ID" != "null" ]; then
api_call "GET" "/api/task-lists/$TASK_LIST_ID" "" "Get Task List by ID (REST)"
# Update Task List via REST
api_call "PUT" "/api/task-lists/$TASK_LIST_ID" \
'{"name": "Updated REST Test List", "description": "Updated via REST"}' \
"Update Task List (REST)"
fi
# 6. TASK LIST TESTS (GraphQL)
print_header "6. TASK LIST TESTS (GraphQL)"
# Create Task List via GraphQL
echo -e "${YELLOW}Testing GraphQL: Create Task List${NC}"
GRAPHQL_TASK_LIST_RESPONSE=$(curl -s -X POST "$BASE_URL/graphql" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{"query": "mutation { createTaskList(input: { name: \"GraphQL Test List\", description: \"Testing GraphQL API\" }) { id name description } }"}')
echo "$GRAPHQL_TASK_LIST_RESPONSE" | jq '.'
print_result "Create Task List (GraphQL)"
# Extract GraphQL task list ID
GRAPHQL_TASK_LIST_ID=$(echo "$GRAPHQL_TASK_LIST_RESPONSE" | jq -r '.data.createTaskList.id // empty')
# List Task Lists via GraphQL
graphql_call "{ taskLists { id name description } }" "List Task Lists (GraphQL)"
# 7. TASK TESTS (REST)
print_header "7. TASK TESTS (REST)"
if [ -n "$TASK_LIST_ID" ] && [ "$TASK_LIST_ID" != "null" ]; then
# Create Task via REST
echo -e "${YELLOW}Testing REST: Create Task${NC}"
echo -e "${YELLOW}POST /api/tasks/${NC}"
TASK_RESPONSE=$(curl -s -X POST "$BASE_URL/api/tasks/" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d "{\"title\": \"REST Test Task\", \"description\": \"Testing REST task creation\", \"task_list_id\": $TASK_LIST_ID, \"priority\": \"high\"}")
echo "$TASK_RESPONSE" | jq '.'
print_result "Create Task (REST)"
# Extract task ID
TASK_ID=$(echo "$TASK_RESPONSE" | jq -r '.id // empty')
echo -e "${GREEN}π Task ID: $TASK_ID${NC}"
# List Tasks via REST
api_call "GET" "/api/tasks/?task_list_id=$TASK_LIST_ID" "" "List Tasks (REST)"
if [ -n "$TASK_ID" ] && [ "$TASK_ID" != "null" ]; then
# Get Specific Task via REST
api_call "GET" "/api/tasks/$TASK_ID" "" "Get Task by ID (REST)"
# Update Task via REST
api_call "PUT" "/api/tasks/$TASK_ID" \
'{"title": "Updated REST Task", "description": "Updated via REST", "priority": "medium"}' \
"Update Task (REST)"
# Update Task Status via REST
api_call "PATCH" "/api/tasks/$TASK_ID/status" \
'{"status": "in_progress"}' \
"Update Task Status (REST)"
fi
fi
# 8. TASK TESTS (GraphQL)
print_header "8. TASK TESTS (GraphQL)"
if [ -n "$GRAPHQL_TASK_LIST_ID" ] && [ "$GRAPHQL_TASK_LIST_ID" != "null" ]; then
# Create Task via GraphQL
echo -e "${YELLOW}Testing GraphQL: Create Task${NC}"
GRAPHQL_TASK_RESPONSE=$(curl -s -X POST "$BASE_URL/graphql" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d "{\"query\": \"mutation { createTask(input: { title: \\\"GraphQL Test Task\\\", description: \\\"Testing GraphQL task creation\\\", taskListId: $GRAPHQL_TASK_LIST_ID, priority: HIGH }) { id title description status priority } }\"}")
echo "$GRAPHQL_TASK_RESPONSE" | jq '.'
print_result "Create Task (GraphQL)"
# Extract GraphQL task ID
GRAPHQL_TASK_ID=$(echo "$GRAPHQL_TASK_RESPONSE" | jq -r '.data.createTask.id // empty')
# List Tasks via GraphQL
graphql_call "{ tasks(filter: { taskListId: $GRAPHQL_TASK_LIST_ID }) { id title description status priority } }" "List Tasks (GraphQL)"
fi
# 9. FILTERING TESTS
print_header "9. FILTERING AND COMPLETION TESTS"
# Create multiple tasks for filtering tests
if [ -n "$TASK_LIST_ID" ] && [ "$TASK_LIST_ID" != "null" ]; then
echo -e "${YELLOW}Creating multiple tasks for filtering tests${NC}"
# Create tasks with different priorities and statuses
curl -s -X POST "$BASE_URL/api/tasks/" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d "{\"title\": \"Low Priority Task\", \"description\": \"Low priority test\", \"task_list_id\": $TASK_LIST_ID, \"priority\": \"low\"}" | jq '.'
curl -s -X POST "$BASE_URL/api/tasks/" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d "{\"title\": \"Medium Priority Task\", \"description\": \"Medium priority test\", \"task_list_id\": $TASK_LIST_ID, \"priority\": \"medium\"}" | jq '.'
# Test filtering by priority
api_call "GET" "/api/tasks/?task_list_id=$TASK_LIST_ID&priority=high" "" "Filter Tasks by High Priority"
api_call "GET" "/api/tasks/?task_list_id=$TASK_LIST_ID&status=pending" "" "Filter Tasks by Pending Status"
# Get completion percentage
api_call "GET" "/api/task-lists/$TASK_LIST_ID" "" "Get Task List with Completion Percentage"
fi
# 10. ERROR HANDLING TESTS
print_header "10. ERROR HANDLING TESTS"
# Test invalid endpoints
echo -e "${YELLOW}Testing: Invalid Task List ID (REST)${NC}"
curl -s -X GET "$BASE_URL/api/task-lists/99999" \
-H "Authorization: Bearer $TOKEN" | jq '.'
print_result "Invalid Task List ID (should return 404)"
echo -e "${YELLOW}Testing: Invalid Task ID (REST)${NC}"
curl -s -X GET "$BASE_URL/api/tasks/99999" \
-H "Authorization: Bearer $TOKEN" | jq '.'
print_result "Invalid Task ID (should return 404)"
echo -e "${YELLOW}Testing: Invalid Token (REST)${NC}"
curl -s -X GET "$BASE_URL/api/task-lists/" \
-H "Authorization: Bearer invalid_token" | jq '.'
print_result "Invalid Token (should return 401)"
# Test GraphQL errors
echo -e "${YELLOW}Testing: Invalid GraphQL Query${NC}"
curl -s -X POST "$BASE_URL/graphql" \
-H "Content-Type: application/json" \
-d '{"query": "{ invalidField }"}' | jq '.'
print_result "Invalid GraphQL Query (should return error)"
# 11. CLEANUP TESTS
print_header "11. CLEANUP TESTS"
if [ -n "$TASK_ID" ] && [ "$TASK_ID" != "null" ]; then
# Delete Task via REST
api_call "DELETE" "/api/tasks/$TASK_ID" "" "Delete Task (REST)"
fi
if [ -n "$GRAPHQL_TASK_ID" ] && [ "$GRAPHQL_TASK_ID" != "null" ]; then
# Delete Task via GraphQL
graphql_call "mutation { deleteTask(id: $GRAPHQL_TASK_ID) }" "Delete Task (GraphQL)"
fi
if [ -n "$TASK_LIST_ID" ] && [ "$TASK_LIST_ID" != "null" ]; then
# Delete Task List via REST
api_call "DELETE" "/api/task-lists/$TASK_LIST_ID" "" "Delete Task List (REST)"
fi
if [ -n "$GRAPHQL_TASK_LIST_ID" ] && [ "$GRAPHQL_TASK_LIST_ID" != "null" ]; then
# Delete Task List via GraphQL
graphql_call "mutation { deleteTaskList(id: $GRAPHQL_TASK_LIST_ID) }" "Delete Task List (GraphQL)"
fi
# 12. FINAL SUMMARY
print_header "12. TEST SUMMARY"
echo -e "${GREEN}π COMPREHENSIVE API TEST COMPLETED!${NC}"
echo -e "${GREEN}=====================================${NC}"
echo -e "${GREEN}β
Health Check${NC}"
echo -e "${GREEN}β
GraphQL Introspection${NC}"
echo -e "${GREEN}β
REST Authentication (Register/Login)${NC}"
echo -e "${GREEN}β
GraphQL Authentication (Register/Login)${NC}"
echo -e "${GREEN}β
REST Task Lists (CRUD Operations)${NC}"
echo -e "${GREEN}β
GraphQL Task Lists (CRUD Operations)${NC}"
echo -e "${GREEN}β
REST Tasks (CRUD Operations)${NC}"
echo -e "${GREEN}β
GraphQL Tasks (CRUD Operations)${NC}"
echo -e "${GREEN}β
Task Status Updates${NC}"
echo -e "${GREEN}β
Filtering and Completion Percentage${NC}"
echo -e "${GREEN}β
Error Handling (REST & GraphQL)${NC}"
echo -e "${GREEN}β
Cleanup Operations${NC}"
echo -e "\n${BLUE}π Both REST and GraphQL APIs have been tested!${NC}"
echo -e "${BLUE}The Task Challenge API is ready for evaluation! π${NC}\n"