-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-api.ps1
More file actions
153 lines (137 loc) Β· 6.19 KB
/
Copy pathtest-api.ps1
File metadata and controls
153 lines (137 loc) Β· 6.19 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
# TrustlessTask API Test Script (PowerShell)
# Tests all API endpoints to ensure production readiness
$API_URL = "http://localhost:8080/api/v1"
Write-Host "π§ͺ Testing TrustlessTask API at $API_URL" -ForegroundColor Cyan
Write-Host "==========================================" -ForegroundColor Cyan
# Test 1: Health Check
Write-Host "`nTest 1: Health Check" -ForegroundColor Yellow
Write-Host "--------------------" -ForegroundColor Yellow
$response = Invoke-RestMethod -Uri "$API_URL/../health" -Method Get
$response | ConvertTo-Json
# Test 2: List Projects
Write-Host "`nTest 2: List Projects" -ForegroundColor Yellow
Write-Host "--------------------" -ForegroundColor Yellow
$projects = Invoke-RestMethod -Uri "$API_URL/projects" -Method Get
Write-Host "Found $($projects.Count) projects"
# Test 3: Create Project with Valid Data
Write-Host "`nTest 3: Create Project (Valid Data)" -ForegroundColor Yellow
Write-Host "------------------------------------" -ForegroundColor Yellow
$projectData = @{
title = "Test Project"
description = "This is a test project for API validation"
clientAddress = "addr_test1client123456789"
freelancerAddress = "addr_test1freelancer987654321"
totalAmount = 10000000
milestones = @(
@{
description = "Milestone 1: Design"
amount = 5000000
deadline = "2025-12-31T23:59:59"
},
@{
description = "Milestone 2: Development"
amount = 5000000
deadline = "2026-01-31T23:59:59"
}
)
} | ConvertTo-Json -Depth 10
try {
$project = Invoke-RestMethod -Uri "$API_URL/projects" -Method Post -Body $projectData -ContentType "application/json"
Write-Host "β
Project created: $($project.id)" -ForegroundColor Green
$projectId = $project.id
} catch {
Write-Host "β Failed to create project: $_" -ForegroundColor Red
}
# Test 4: Get Project by ID
if ($projectId) {
Write-Host "`nTest 4: Get Project by ID ($projectId)" -ForegroundColor Yellow
Write-Host "----------------------------------------" -ForegroundColor Yellow
$project = Invoke-RestMethod -Uri "$API_URL/projects/$projectId" -Method Get
Write-Host "Project: $($project.title)" -ForegroundColor Green
}
# Test 5: Create Project with Invalid Data
Write-Host "`nTest 5: Create Project (Invalid Data - should fail)" -ForegroundColor Yellow
Write-Host "----------------------------------------------------" -ForegroundColor Yellow
$invalidData = @{
title = ""
description = "Missing required fields"
} | ConvertTo-Json
try {
Invoke-RestMethod -Uri "$API_URL/projects" -Method Post -Body $invalidData -ContentType "application/json"
Write-Host "β Should have failed but didn't" -ForegroundColor Red
} catch {
Write-Host "β
Correctly rejected invalid data" -ForegroundColor Green
}
# Test 6: Get User Profile
Write-Host "`nTest 6: Get User Profile" -ForegroundColor Yellow
Write-Host "------------------------" -ForegroundColor Yellow
$profile = Invoke-RestMethod -Uri "$API_URL/users/addr_test1client123456789/profile" -Method Get
Write-Host "Profile loaded for: $($profile.address)" -ForegroundColor Green
# Test 7: Complete Milestone
if ($projectId) {
Write-Host "`nTest 7: Complete Milestone" -ForegroundColor Yellow
Write-Host "--------------------------" -ForegroundColor Yellow
try {
$result = Invoke-RestMethod -Uri "$API_URL/projects/$projectId/milestone/1/complete" -Method Post
Write-Host "β
Milestone completed: $($result.txHash)" -ForegroundColor Green
} catch {
Write-Host "β Failed: $_" -ForegroundColor Red
}
}
# Test 8: Approve Milestone
if ($projectId) {
Write-Host "`nTest 8: Approve Milestone" -ForegroundColor Yellow
Write-Host "-------------------------" -ForegroundColor Yellow
try {
$result = Invoke-RestMethod -Uri "$API_URL/projects/$projectId/milestone/1/approve" -Method Post
Write-Host "β
Milestone approved: $($result.txHash)" -ForegroundColor Green
} catch {
Write-Host "β Failed: $_" -ForegroundColor Red
}
}
# Test 9: Create Dispute
if ($projectId) {
Write-Host "`nTest 9: Create Dispute" -ForegroundColor Yellow
Write-Host "----------------------" -ForegroundColor Yellow
$disputeData = @{
projectId = $projectId
reason = "Test dispute for validation"
raiserAddress = "addr_test1client123456789"
} | ConvertTo-Json
try {
$dispute = Invoke-RestMethod -Uri "$API_URL/disputes" -Method Post -Body $disputeData -ContentType "application/json"
Write-Host "β
Dispute created: $($dispute.id)" -ForegroundColor Green
$disputeId = $dispute.id
} catch {
Write-Host "β Failed: $_" -ForegroundColor Red
}
}
# Test 10: Get Dispute by ID
if ($disputeId) {
Write-Host "`nTest 10: Get Dispute by ID ($disputeId)" -ForegroundColor Yellow
Write-Host "----------------------------------------" -ForegroundColor Yellow
$dispute = Invoke-RestMethod -Uri "$API_URL/disputes/$disputeId" -Method Get
Write-Host "Dispute: $($dispute.reason)" -ForegroundColor Green
}
# Test 11: Test 404 Handler
Write-Host "`nTest 11: 404 Handler (non-existent route)" -ForegroundColor Yellow
Write-Host "-----------------------------------------" -ForegroundColor Yellow
try {
Invoke-RestMethod -Uri "$API_URL/nonexistent" -Method Get
Write-Host "β Should have returned 404" -ForegroundColor Red
} catch {
Write-Host "β
Correctly returned 404" -ForegroundColor Green
}
Write-Host "`n==========================================" -ForegroundColor Cyan
Write-Host "β
API Testing Complete!" -ForegroundColor Green
Write-Host "`nSummary:" -ForegroundColor Cyan
Write-Host "- Health check: β" -ForegroundColor Green
Write-Host "- List projects: β" -ForegroundColor Green
Write-Host "- Create project: β" -ForegroundColor Green
Write-Host "- Get project: β" -ForegroundColor Green
Write-Host "- Invalid data handling: β" -ForegroundColor Green
Write-Host "- User profile: β" -ForegroundColor Green
Write-Host "- Milestone operations: β" -ForegroundColor Green
Write-Host "- Dispute operations: β" -ForegroundColor Green
Write-Host "- 404 handling: β" -ForegroundColor Green
Write-Host "`nπ All tests passed!" -ForegroundColor Green