144 lines
5.7 KiB
PowerShell
144 lines
5.7 KiB
PowerShell
|
|
# Test Authentication APIs
|
||
|
|
|
||
|
|
Write-Host "========================================" -ForegroundColor Cyan
|
||
|
|
Write-Host "Testing User Authentication System" -ForegroundColor Cyan
|
||
|
|
Write-Host "========================================" -ForegroundColor Cyan
|
||
|
|
Write-Host ""
|
||
|
|
|
||
|
|
$baseUrl = "http://localhost:5000/api"
|
||
|
|
|
||
|
|
# Test 1: Register a new user
|
||
|
|
Write-Host "1. Testing User Registration..." -ForegroundColor Yellow
|
||
|
|
try {
|
||
|
|
$registerData = @{
|
||
|
|
name = "Test User"
|
||
|
|
email = "testuser@example.com"
|
||
|
|
password = "TestPassword123!"
|
||
|
|
role = "User"
|
||
|
|
} | ConvertTo-Json
|
||
|
|
|
||
|
|
$response = Invoke-RestMethod -Uri "$baseUrl/auth/register" -Method Post -Body $registerData -ContentType "application/json"
|
||
|
|
Write-Host "✓ User registered successfully!" -ForegroundColor Green
|
||
|
|
Write-Host "User ID: $($response.data.id), Name: $($response.data.name), Email: $($response.data.email)" -ForegroundColor Green
|
||
|
|
$newUserId = $response.data.id
|
||
|
|
} catch {
|
||
|
|
$errorDetails = $_.ErrorDetails.Message | ConvertFrom-Json
|
||
|
|
Write-Host "✗ Registration failed: $($errorDetails.message)" -ForegroundColor Red
|
||
|
|
}
|
||
|
|
Write-Host ""
|
||
|
|
|
||
|
|
# Test 2: Login with existing user (from seed data)
|
||
|
|
Write-Host "2. Testing User Login (seed data user)..." -ForegroundColor Yellow
|
||
|
|
try {
|
||
|
|
$loginData = @{
|
||
|
|
email = "john@example.com"
|
||
|
|
password = "Password123!"
|
||
|
|
} | ConvertTo-Json
|
||
|
|
|
||
|
|
$response = Invoke-RestMethod -Uri "$baseUrl/auth/login" -Method Post -Body $loginData -ContentType "application/json"
|
||
|
|
Write-Host "✓ Login successful!" -ForegroundColor Green
|
||
|
|
Write-Host "Token: $($response.data.token.Substring(0, 50))..." -ForegroundColor Green
|
||
|
|
Write-Host "User: $($response.data.user.name) ($($response.data.user.role))" -ForegroundColor Green
|
||
|
|
$token = $response.data.token
|
||
|
|
$userId = $response.data.user.id
|
||
|
|
} catch {
|
||
|
|
$errorDetails = $_.ErrorDetails.Message | ConvertFrom-Json
|
||
|
|
Write-Host "✗ Login failed: $($errorDetails.message)" -ForegroundColor Red
|
||
|
|
}
|
||
|
|
Write-Host ""
|
||
|
|
|
||
|
|
# Test 3: Login with new user
|
||
|
|
Write-Host "3. Testing Login with newly registered user..." -ForegroundColor Yellow
|
||
|
|
try {
|
||
|
|
$loginData = @{
|
||
|
|
email = "testuser@example.com"
|
||
|
|
password = "TestPassword123!"
|
||
|
|
} | ConvertTo-Json
|
||
|
|
|
||
|
|
$response = Invoke-RestMethod -Uri "$baseUrl/auth/login" -Method Post -Body $loginData -ContentType "application/json"
|
||
|
|
Write-Host "✓ Login successful!" -ForegroundColor Green
|
||
|
|
Write-Host "User: $($response.data.user.name), Last Login: $($response.data.user.lastLoginAt)" -ForegroundColor Green
|
||
|
|
} catch {
|
||
|
|
$errorDetails = $_.ErrorDetails.Message | ConvertFrom-Json
|
||
|
|
Write-Host "✗ Login failed: $($errorDetails.message)" -ForegroundColor Red
|
||
|
|
}
|
||
|
|
Write-Host ""
|
||
|
|
|
||
|
|
# Test 4: Get user profile
|
||
|
|
Write-Host "4. Testing Get User Profile..." -ForegroundColor Yellow
|
||
|
|
try {
|
||
|
|
$response = Invoke-RestMethod -Uri "$baseUrl/auth/profile/$userId" -Method Get
|
||
|
|
Write-Host "✓ Profile retrieved!" -ForegroundColor Green
|
||
|
|
$response.data | Format-List
|
||
|
|
} catch {
|
||
|
|
Write-Host "✗ Error: $_" -ForegroundColor Red
|
||
|
|
}
|
||
|
|
Write-Host ""
|
||
|
|
|
||
|
|
# Test 5: Get all users
|
||
|
|
Write-Host "5. Testing Get All Users..." -ForegroundColor Yellow
|
||
|
|
try {
|
||
|
|
$response = Invoke-RestMethod -Uri "$baseUrl/users" -Method Get
|
||
|
|
Write-Host "✓ Users retrieved (${response.data.Count} users):" -ForegroundColor Green
|
||
|
|
$response.data | Select-Object id, name, email, role, status | Format-Table -AutoSize
|
||
|
|
} catch {
|
||
|
|
Write-Host "✗ Error: $_" -ForegroundColor Red
|
||
|
|
}
|
||
|
|
Write-Host ""
|
||
|
|
|
||
|
|
# Test 6: Update user
|
||
|
|
Write-Host "6. Testing Update User..." -ForegroundColor Yellow
|
||
|
|
try {
|
||
|
|
$updateData = @{
|
||
|
|
name = "Updated Test User"
|
||
|
|
status = "Active"
|
||
|
|
} | ConvertTo-Json
|
||
|
|
|
||
|
|
$response = Invoke-RestMethod -Uri "$baseUrl/users/$newUserId" -Method Put -Body $updateData -ContentType "application/json"
|
||
|
|
Write-Host "✓ User updated successfully!" -ForegroundColor Green
|
||
|
|
Write-Host "New name: $($response.data.name), Status: $($response.data.status)" -ForegroundColor Green
|
||
|
|
} catch {
|
||
|
|
$errorDetails = $_.ErrorDetails.Message | ConvertFrom-Json
|
||
|
|
Write-Host "✗ Update failed: $($errorDetails.message)" -ForegroundColor Red
|
||
|
|
}
|
||
|
|
Write-Host ""
|
||
|
|
|
||
|
|
# Test 7: Change password
|
||
|
|
Write-Host "7. Testing Change Password..." -ForegroundColor Yellow
|
||
|
|
try {
|
||
|
|
$changePasswordData = @{
|
||
|
|
currentPassword = "TestPassword123!"
|
||
|
|
newPassword = "NewPassword123!"
|
||
|
|
} | ConvertTo-Json
|
||
|
|
|
||
|
|
$response = Invoke-RestMethod -Uri "$baseUrl/auth/change-password/$newUserId" -Method Post -Body $changePasswordData -ContentType "application/json"
|
||
|
|
Write-Host "✓ Password changed successfully!" -ForegroundColor Green
|
||
|
|
} catch {
|
||
|
|
$errorDetails = $_.ErrorDetails.Message | ConvertFrom-Json
|
||
|
|
Write-Host "✗ Password change failed: $($errorDetails.message)" -ForegroundColor Red
|
||
|
|
}
|
||
|
|
Write-Host ""
|
||
|
|
|
||
|
|
# Test 8: Login with wrong password
|
||
|
|
Write-Host "8. Testing Login with wrong password..." -ForegroundColor Yellow
|
||
|
|
try {
|
||
|
|
$loginData = @{
|
||
|
|
email = "john@example.com"
|
||
|
|
password = "WrongPassword"
|
||
|
|
} | ConvertTo-Json
|
||
|
|
|
||
|
|
$response = Invoke-RestMethod -Uri "$baseUrl/auth/login" -Method Post -Body $loginData -ContentType "application/json"
|
||
|
|
Write-Host "✗ Should have failed!" -ForegroundColor Red
|
||
|
|
} catch {
|
||
|
|
$errorDetails = $_.ErrorDetails.Message | ConvertFrom-Json
|
||
|
|
Write-Host "✓ Correctly rejected: $($errorDetails.message)" -ForegroundColor Green
|
||
|
|
}
|
||
|
|
Write-Host ""
|
||
|
|
|
||
|
|
Write-Host "========================================" -ForegroundColor Cyan
|
||
|
|
Write-Host "Authentication Tests Complete!" -ForegroundColor Cyan
|
||
|
|
Write-Host "========================================" -ForegroundColor Cyan
|
||
|
|
Write-Host ""
|
||
|
|
Write-Host "📝 Note: Seed data users have password: Password123!" -ForegroundColor Yellow
|
||
|
|
Write-Host " Example: john@example.com / Password123!" -ForegroundColor Yellow
|