-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_api.sh
More file actions
91 lines (73 loc) · 2.46 KB
/
test_api.sh
File metadata and controls
91 lines (73 loc) · 2.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
#!/bin/bash
# Setup
API_URL="http://localhost:3000/api"
ORG="Org1"
USER_ID="apiUser_$(date +%s)"
ADMIN_ID="admin"
ASSET_ID="asset_$(date +%s)"
COLLECTION="AgriCollection"
echo "------------------------------------------------"
echo "Starting VeriChain API Verification"
echo "------------------------------------------------"
# 1. Register User (Public endpoint)
echo "Step 1: Registering User $USER_ID..."
RESPONSE=$(curl -s -X POST "$API_URL/register" \
-H "Content-Type: application/json" \
-d "{\"orgName\": \"$ORG\", \"userId\": \"$USER_ID\", \"adminId\": \"$ADMIN_ID\", \"role\": \"client\"}")
echo "Response: $RESPONSE"
if [[ $RESPONSE == *"Successfully registered"* ]]; then
echo "✅ Registration Successful"
else
echo "❌ Registration Failed"
exit 1
fi
echo "------------------------------------------------"
# 1.5 Login as OrgAdmin to get Token
echo "Step 1.5: Logging in as OrgAdmin..."
LOGIN_RESPONSE=$(curl -s -X POST "$API_URL/login" \
-H "Content-Type: application/json" \
-d "{\"orgName\": \"$ORG\", \"userId\": \"OrgAdmin\"}")
TOKEN=$(echo $LOGIN_RESPONSE | jq -r '.token')
if [[ "$TOKEN" != "null" && -n "$TOKEN" ]]; then
echo "✅ Login Successful. Token received."
else
echo "❌ Login Failed"
echo "Response: $LOGIN_RESPONSE"
exit 1
fi
echo "------------------------------------------------"
# 2. Create Asset (Protected)
echo "Step 2: Creating Asset $ASSET_ID in $COLLECTION..."
# Use OrgAdmin token
RESPONSE=$(curl -s -X POST "$API_URL/assets" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d "{
\"assetId\": \"$ASSET_ID\",
\"cropType\": \"Sweet Corn\",
\"variety\": \"Golden Jubilee\",
\"harvestDate\": \"2024-10-15\",
\"farmLocation\": \"Nashik\",
\"farmerName\": \"FarmerJohn\",
\"quantity\": 5000,
\"organicCertified\": true,
\"status\": \"HARVESTED\"
}")
echo "Response: $RESPONSE"
if [[ $RESPONSE == *"created successfully"* ]]; then
echo "✅ Asset Creation Successful"
else
echo "❌ Asset Creation Failed"
fi
echo "------------------------------------------------"
# 3. Read Asset (Protected)
echo "Step 3: Reading Asset $ASSET_ID..."
RESPONSE=$(curl -s -H "Authorization: Bearer $TOKEN" "$API_URL/assets/$ASSET_ID?collection=$COLLECTION")
echo "Response: $RESPONSE"
if [[ $RESPONSE == *"FarmerJohn"* ]]; then
echo "✅ Read Asset Successful"
else
echo "❌ Read Asset Failed"
fi
echo "------------------------------------------------"
echo "Verification Complete"