-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_user_auth_service.sh
executable file
·155 lines (125 loc) · 4.68 KB
/
test_user_auth_service.sh
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
#!/bin/bash
BASE_URL="http://localhost:8000"
TOTAL_TESTS=0
PASSED_TESTS=0
# Function to make API requests
make_request() {
local method=$1
local endpoint=$2
local data=$3
local token=$4
local expected_status=$5
TOTAL_TESTS=$((TOTAL_TESTS + 1))
headers=(-H "Accept: application/json")
if [ -n "$data" ]; then
headers+=(-H "Content-Type: application/json")
fi
if [ -n "$token" ]; then
headers+=(-H "Authorization: Bearer $token")
fi
echo -e "Request details:"
echo "Method: $method"
echo "Endpoint: $endpoint"
echo "Data: $data"
echo "Token: $token"
echo "Expected status: $expected_status"
if [ -n "$data" ]; then
response=$(curl -v -s -w "\n%{http_code}" -X $method "${headers[@]}" -d "$data" "$BASE_URL$endpoint" 2>&1)
else
response=$(curl -v -s -w "\n%{http_code}" -X $method "${headers[@]}" "$BASE_URL$endpoint" 2>&1)
fi
status_code=$(echo "$response" | tail -n 1)
body=$(echo "$response" | sed '$d' | sed -n '/^{/,/^}/p' | tr -d '\n')
echo "Response details:"
echo "Status code: $status_code"
echo "Body: $body"
if [ "$status_code" = "$expected_status" ]; then
PASSED_TESTS=$((PASSED_TESTS + 1))
print_result "PASS" "$method" "$endpoint" "$expected_status" "$status_code"
else
print_result "FAIL" "$method" "$endpoint" "$expected_status" "$status_code"
fi
#echo "$body"
}
# Function to print test results
print_result() {
local result=$1
local method=$2
local endpoint=$3
local expected=$4
local actual=$5
if [ "$result" = "PASS" ]; then
echo -e "${GREEN}✓ PASS${NC} $method $endpoint (Expected: $expected, Got: $actual)\n"
else
echo -e "${RED}✗ FAIL${NC} $method $endpoint (Expected: $expected, Got: $actual)\n"
fi
}
# Create a new user
echo "Creating a new user"
response=$(make_request POST "/users" '{"username": "testuser", "email": "[email protected]", "password": "testpass"}' "" "201")
user_id=$(echo "$response" | grep -o '"id":"[^"]*' | cut -d'"' -f4 | head -n 1)
echo "User ID: $user_id"
# Login with the new user
echo "Logging in with the new user"
login_response=$(make_request POST "/users/login" '{"username": "testuser", "password": "testpass"}' "" "200")
token=$(echo "$login_response" | grep -o '"token":"[^"]*' | cut -d'"' -f4 | head -n 1)
echo "Token: $token"
# Validate token
echo "Validating token"
echo "Token being sent: $token"
make_request GET "/users/validate-token" "" "$token" "200"
# List users
echo "Listing users"
make_request GET "/users" "" "$token" "200"
# Get user details
echo "Get user details"
make_request GET "/users/$user_id" "" "$token" "200"
# Update user details
echo "Update user details"
make_request PUT "/users/$user_id" '{"email": "[email protected]"}' "$token" "200"
# Verify the update
echo "Verify user update"
make_request GET "/users/$user_id" "" "$token" "200"
# Attempt to create a user with the same email
echo "Attempt creation of duplicate user"
make_request POST "/users" '{"username": "testuser2", "email": "[email protected]", "password": "testpass2"}' "" "400"
# Refresh token
echo "Refresh token"
refresh_response=$(make_request POST "/users/refresh" "" "$token" "200")
echo "Refresh response: $refresh_response"
new_token=$(echo "$refresh_response" | grep -o '"token":"[^"]*' | cut -d'"' -f4 | head -n 1)
echo "New token: $new_token"
echo "Old token: $token"
echo "New token: $new_token"
if [ "$token" = "$new_token" ]; then
echo "Error: New token is the same as the old token"
exit 1
fi
# Use the new token to get user details
echo "Get user with new token"
make_request GET "/users/$user_id" "" "$new_token" "200"
# Validate new token
echo "Validating new token"
echo "New token: $new_token"
make_request GET "/users/validate-token" "" "$new_token" "200"
# Try to use the old token (should fail)
echo "Trying to use old token"
echo "Old token: $token"
echo "New token: $new_token"
make_request GET "/users/$user_id" "" "$token" "404"
# Delete user
#echo "Delete user"
#make_request DELETE "/users/$user_id" "" "$new_token" "204"
# Verify the deletion
#echo "Verify user deletion"
#make_request GET "/users/$user_id" "" "$new_token" "500"
# Try to use the token after user deletion (should fail)
echo "Trying to use token after user deletion"
make_request GET "/users" "" "$new_token" "401"
# Print summary
echo -e "\n----- Test Summary -----"
echo "Total tests: $TOTAL_TESTS"
echo -e "Passed tests: ${GREEN}$PASSED_TESTS${NC}"
echo -e "Failed tests: ${RED}$((TOTAL_TESTS - PASSED_TESTS))${NC}"
echo -e "Success rate: ${GREEN}$(( (PASSED_TESTS * 100) / TOTAL_TESTS ))%${NC}"
echo -e "\nUser authentication testing completed."