test_auth_flow.ps1 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # JWT Authentication System Test Script
  2. # Tests the complete authentication flow
  3. Write-Host "`n=== JWT Authentication System Test ===" -ForegroundColor Cyan
  4. # Test 1: Register a new user
  5. Write-Host "`n[Test 1] User Registration..." -ForegroundColor Yellow
  6. $registerBody = @{
  7. username = "testuser2"
  8. email = "test2@example.com"
  9. password = "password123"
  10. } | ConvertTo-Json
  11. try {
  12. $registerResponse = Invoke-RestMethod -Uri "http://localhost:8000/api/auth/register" `
  13. -Method Post -ContentType "application/json" -Body $registerBody
  14. Write-Host "✓ Registration successful!" -ForegroundColor Green
  15. Write-Host " User ID: $($registerResponse.user.id)"
  16. Write-Host " Username: $($registerResponse.user.username)"
  17. Write-Host " Role: $($registerResponse.user.role)"
  18. } catch {
  19. Write-Host "✗ Registration failed: $($_.Exception.Message)" -ForegroundColor Red
  20. }
  21. # Test 2: Login with credentials
  22. Write-Host "`n[Test 2] User Login..." -ForegroundColor Yellow
  23. $loginBody = @{
  24. username = "testuser2"
  25. password = "password123"
  26. } | ConvertTo-Json
  27. try {
  28. $loginResponse = Invoke-RestMethod -Uri "http://localhost:8000/api/auth/login" `
  29. -Method Post -ContentType "application/json" -Body $loginBody
  30. Write-Host "✓ Login successful!" -ForegroundColor Green
  31. Write-Host " Access Token: $($loginResponse.access_token.Substring(0,50))..."
  32. $token = $loginResponse.access_token
  33. $refreshToken = $loginResponse.refresh_token
  34. } catch {
  35. Write-Host "✗ Login failed: $($_.Exception.Message)" -ForegroundColor Red
  36. exit 1
  37. }
  38. # Test 3: Access protected endpoint with token
  39. Write-Host "`n[Test 3] Access Protected Endpoint (with token)..." -ForegroundColor Yellow
  40. $headers = @{
  41. Authorization = "Bearer $token"
  42. }
  43. try {
  44. $meResponse = Invoke-RestMethod -Uri "http://localhost:8000/api/auth/me" `
  45. -Method Get -Headers $headers
  46. Write-Host "✓ Successfully accessed protected endpoint!" -ForegroundColor Green
  47. Write-Host " User: $($meResponse.username)"
  48. Write-Host " Email: $($meResponse.email)"
  49. } catch {
  50. Write-Host "✗ Failed to access protected endpoint: $($_.Exception.Message)" -ForegroundColor Red
  51. }
  52. # Test 4: Access protected endpoint without token
  53. Write-Host "`n[Test 4] Access Protected Endpoint (without token)..." -ForegroundColor Yellow
  54. try {
  55. $response = Invoke-WebRequest -Uri "http://localhost:8000/api/projects" `
  56. -Method Get -ErrorAction Stop
  57. Write-Host "✗ Should have been rejected!" -ForegroundColor Red
  58. } catch {
  59. if ($_.Exception.Response.StatusCode.value__ -eq 401) {
  60. Write-Host "✓ Correctly rejected with 401 Unauthorized!" -ForegroundColor Green
  61. } else {
  62. Write-Host "✗ Wrong status code: $($_.Exception.Response.StatusCode.value__)" -ForegroundColor Red
  63. }
  64. }
  65. # Test 5: Create a project (authenticated)
  66. Write-Host "`n[Test 5] Create Project (authenticated)..." -ForegroundColor Yellow
  67. $projectBody = @{
  68. name = "Test Project"
  69. description = "A test project"
  70. config = '{"type":"image"}'
  71. } | ConvertTo-Json
  72. try {
  73. $projectResponse = Invoke-RestMethod -Uri "http://localhost:8000/api/projects" `
  74. -Method Post -Headers $headers -ContentType "application/json" -Body $projectBody
  75. Write-Host "✓ Project created successfully!" -ForegroundColor Green
  76. Write-Host " Project ID: $($projectResponse.id)"
  77. Write-Host " Project Name: $($projectResponse.name)"
  78. $projectId = $projectResponse.id
  79. } catch {
  80. Write-Host "✗ Failed to create project: $($_.Exception.Message)" -ForegroundColor Red
  81. }
  82. # Test 6: Create a task (authenticated, auto-assigned to current user)
  83. Write-Host "`n[Test 6] Create Task (auto-assigned to current user)..." -ForegroundColor Yellow
  84. $taskBody = @{
  85. project_id = $projectId
  86. name = "Test Task"
  87. data = @{
  88. items = @(
  89. @{ id = 1; text = "Sample text" }
  90. )
  91. }
  92. } | ConvertTo-Json -Depth 5
  93. try {
  94. $taskResponse = Invoke-RestMethod -Uri "http://localhost:8000/api/tasks" `
  95. -Method Post -Headers $headers -ContentType "application/json" -Body $taskBody
  96. Write-Host "✓ Task created successfully!" -ForegroundColor Green
  97. Write-Host " Task ID: $($taskResponse.id)"
  98. Write-Host " Assigned to: $($taskResponse.assigned_to)"
  99. $taskId = $taskResponse.id
  100. } catch {
  101. Write-Host "✗ Failed to create task: $($_.Exception.Message)" -ForegroundColor Red
  102. }
  103. # Test 7: Create an annotation (authenticated, auto-assigned to current user)
  104. Write-Host "`n[Test 7] Create Annotation (auto-assigned to current user)..." -ForegroundColor Yellow
  105. $annotationBody = @{
  106. task_id = $taskId
  107. user_id = "ignored" # This should be ignored and use authenticated user
  108. result = @{
  109. label = "positive"
  110. confidence = 0.95
  111. }
  112. } | ConvertTo-Json -Depth 5
  113. try {
  114. $annotationResponse = Invoke-RestMethod -Uri "http://localhost:8000/api/annotations" `
  115. -Method Post -Headers $headers -ContentType "application/json" -Body $annotationBody
  116. Write-Host "✓ Annotation created successfully!" -ForegroundColor Green
  117. Write-Host " Annotation ID: $($annotationResponse.id)"
  118. Write-Host " User ID: $($annotationResponse.user_id)"
  119. } catch {
  120. Write-Host "✗ Failed to create annotation: $($_.Exception.Message)" -ForegroundColor Red
  121. }
  122. # Test 8: Token refresh
  123. Write-Host "`n[Test 8] Token Refresh..." -ForegroundColor Yellow
  124. $refreshBody = @{
  125. refresh_token = $refreshToken
  126. } | ConvertTo-Json
  127. try {
  128. $refreshResponse = Invoke-RestMethod -Uri "http://localhost:8000/api/auth/refresh" `
  129. -Method Post -ContentType "application/json" -Body $refreshBody
  130. Write-Host "✓ Token refreshed successfully!" -ForegroundColor Green
  131. Write-Host " New Access Token: $($refreshResponse.access_token.Substring(0,50))..."
  132. } catch {
  133. Write-Host "✗ Failed to refresh token: $($_.Exception.Message)" -ForegroundColor Red
  134. }
  135. # Test 9: Try to delete project as non-admin (should fail)
  136. Write-Host "`n[Test 9] Delete Project (as non-admin, should fail)..." -ForegroundColor Yellow
  137. try {
  138. $response = Invoke-WebRequest -Uri "http://localhost:8000/api/projects/$projectId" `
  139. -Method Delete -Headers $headers -ErrorAction Stop
  140. Write-Host "✗ Should have been rejected!" -ForegroundColor Red
  141. } catch {
  142. if ($_.Exception.Response.StatusCode.value__ -eq 403) {
  143. Write-Host "✓ Correctly rejected with 403 Forbidden!" -ForegroundColor Green
  144. } else {
  145. Write-Host "✗ Wrong status code: $($_.Exception.Response.StatusCode.value__)" -ForegroundColor Red
  146. }
  147. }
  148. Write-Host "`n=== All Tests Completed ===" -ForegroundColor Cyan