-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_link_blocks.sh
More file actions
executable file
·185 lines (155 loc) · 5.46 KB
/
test_link_blocks.sh
File metadata and controls
executable file
·185 lines (155 loc) · 5.46 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
#!/bin/bash
# Link Blocks API Test Script
# Tests link block CRUD operations, redirect, and analytics
set -e
BASE_URL="http://localhost:3000"
PROFILE_ID=""
OWNER_TOKEN=""
LINK_ID=""
echo "======================================"
echo "Link Blocks API Test Script"
echo "======================================"
echo ""
# Step 1: Create a test profile
echo "1. Creating test profile..."
CREATE_PROFILE_RESPONSE=$(curl -s -X POST "${BASE_URL}/api/profiles" \
-H "Content-Type: application/json" \
-d '{
"slug": "test-link-blocks-'$(date +%s)'",
"name": "Test User for Link Blocks",
"type": "PERSON",
"bio": "Testing link blocks feature"
}')
echo "Response: $CREATE_PROFILE_RESPONSE"
echo ""
# Extract owner token and slug
OWNER_TOKEN=$(echo $CREATE_PROFILE_RESPONSE | grep -oP '"ownerToken":"[^"]*"' | cut -d'"' -f4)
SLUG=$(echo $CREATE_PROFILE_RESPONSE | grep -oP '"slug":"[^"]*"' | cut -d'"' -f4)
if [ -z "$OWNER_TOKEN" ] || [ -z "$SLUG" ]; then
echo "❌ Failed to create profile"
exit 1
fi
# Fetch the profile by slug to get the ID
PROFILE_RESPONSE=$(curl -s "http://localhost:3000/api/profiles" | grep -A20 "\"slug\":\"$SLUG\"")
PROFILE_ID=$(echo $PROFILE_RESPONSE | grep -oP '"id":"[^"]*"' | cut -d'"' -f4 | head -1)
if [ -z "$PROFILE_ID" ]; then
echo "❌ Failed to get profile ID"
exit 1
fi
echo "✅ Profile created: $PROFILE_ID"
echo " Owner token: ${OWNER_TOKEN:0:20}..."
echo ""
# Step 2: Create a link block
echo "2. Creating link block..."
CREATE_LINK_RESPONSE=$(curl -s -X POST "${BASE_URL}/api/profiles/${PROFILE_ID}/links" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${OWNER_TOKEN}" \
-d '{
"url": "https://github.com/test-user",
"title": "My GitHub Profile",
"description": "Check out my open source projects!",
"privacy": "PUBLIC",
"sortOrder": 0,
"tags": ["social", "code"]
}')
echo "Response: $CREATE_LINK_RESPONSE"
echo ""
# Extract link ID
LINK_ID=$(echo $CREATE_LINK_RESPONSE | grep -o '"id":"[^"]*"' | cut -d'"' -f4 | head -1)
if [ -z "$LINK_ID" ]; then
echo "❌ Failed to create link"
exit 1
fi
echo "✅ Link created: $LINK_ID"
echo ""
# Step 3: List all links for profile
echo "3. Listing all links..."
LIST_LINKS_RESPONSE=$(curl -s -X GET "${BASE_URL}/api/profiles/${PROFILE_ID}/links")
echo "Response: $LIST_LINKS_RESPONSE"
echo ""
# Step 4: Get specific link
echo "4. Getting link by ID..."
GET_LINK_RESPONSE=$(curl -s -X GET "${BASE_URL}/api/profiles/${PROFILE_ID}/links/${LINK_ID}")
echo "Response: $GET_LINK_RESPONSE"
echo ""
# Step 5: Test link redirect
echo "5. Testing link redirect..."
REDIRECT_RESPONSE=$(curl -s -w "\nHTTP Status: %{http_code}\nRedirect URL: %{redirect_url}\n" \
-o /dev/null \
-H "Referer: https://hatef.ir/test" \
"${BASE_URL}/l/${LINK_ID}")
echo "$REDIRECT_RESPONSE"
echo "✅ Redirect tested (should be 302 to https://github.com/test-user)"
echo ""
# Step 6: Simulate multiple clicks for analytics
echo "6. Simulating clicks for analytics..."
for i in {1..5}; do
curl -s -o /dev/null -w "Click $i: HTTP %{http_code}\n" \
-H "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36" \
"${BASE_URL}/l/${LINK_ID}"
sleep 0.5
done
echo ""
# Step 7: Get analytics
echo "7. Getting link analytics..."
ANALYTICS_RESPONSE=$(curl -s -X GET "${BASE_URL}/api/profiles/${PROFILE_ID}/links/analytics" \
-H "Authorization: Bearer ${OWNER_TOKEN}")
echo "Response: $ANALYTICS_RESPONSE"
echo ""
# Step 8: Update link
echo "8. Updating link..."
UPDATE_LINK_RESPONSE=$(curl -s -X PUT "${BASE_URL}/api/profiles/${PROFILE_ID}/links/${LINK_ID}" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${OWNER_TOKEN}" \
-d '{
"title": "Updated GitHub Profile",
"description": "New description!",
"sortOrder": 5
}')
echo "Response: $UPDATE_LINK_RESPONSE"
echo ""
# Step 9: Verify update
echo "9. Verifying link update..."
VERIFY_UPDATE=$(curl -s -X GET "${BASE_URL}/api/profiles/${PROFILE_ID}/links/${LINK_ID}")
echo "Response: $VERIFY_UPDATE"
echo ""
# Step 10: Delete link
echo "10. Deleting link..."
DELETE_LINK_RESPONSE=$(curl -s -X DELETE "${BASE_URL}/api/profiles/${PROFILE_ID}/links/${LINK_ID}" \
-H "Authorization: Bearer ${OWNER_TOKEN}")
echo "Response: $DELETE_LINK_RESPONSE"
echo ""
# Step 11: Verify deletion (should 404)
echo "11. Verifying link deletion..."
VERIFY_DELETE=$(curl -s -w "\nHTTP Status: %{http_code}\n" \
"${BASE_URL}/api/profiles/${PROFILE_ID}/links/${LINK_ID}")
echo "Response: $VERIFY_DELETE"
echo "✅ Link deleted (should return 404)"
echo ""
# Step 12: Test redirect after deletion (should 404)
echo "12. Testing redirect after deletion..."
REDIRECT_AFTER_DELETE=$(curl -s -w "\nHTTP Status: %{http_code}\n" \
-o /dev/null \
"${BASE_URL}/l/${LINK_ID}")
echo "$REDIRECT_AFTER_DELETE"
echo "✅ Redirect after delete (should return 404)"
echo ""
# Cleanup: Delete test profile
echo "13. Cleaning up test profile..."
DELETE_PROFILE_RESPONSE=$(curl -s -X DELETE "${BASE_URL}/api/profiles/${PROFILE_ID}" \
-H "Authorization: Bearer ${OWNER_TOKEN}")
echo "Response: $DELETE_PROFILE_RESPONSE"
echo ""
echo "======================================"
echo "✅ All tests completed!"
echo "======================================"
echo ""
echo "Summary:"
echo "- Profile ID: $PROFILE_ID"
echo "- Link ID: $LINK_ID"
echo "- All CRUD operations tested"
echo "- Redirect functionality tested"
echo "- Analytics recorded (5 test clicks)"
echo "- Cleanup completed"
echo ""
echo "Note: Run 'docker logs core' to see server-side logs"