Lab5 - оценка 4

This commit is contained in:
2026-11-10 13:55:56 +03:00
parent 276dbdad8a
commit e138dfb13d
53 changed files with 7477 additions and 24 deletions
+132
View File
@@ -0,0 +1,132 @@
@echo off
setlocal EnableDelayedExpansion
cd /d "%~dp0.."
set BASE=http://localhost:8080
set PASS=Example-only-not-a-real-secret-123
set FIXTURE=tests\fixtures\sample.png
set TMP=%TEMP%\lab5-failover
if not exist "%TMP%" mkdir "%TMP%"
call :guid
set RND=%GUIDVAL%
set U=fo%RND%
call :register "%U%" "Failover" u
if errorlevel 1 exit /b 1
set UID=!u_ID!
call :login "%U%" u
if errorlevel 1 exit /b 1
set TOKEN=!u_TOKEN!
curl.exe -s -X POST "%BASE%/api/v1/chats" ^
-H "Authorization: Bearer %TOKEN%" -H "Content-Type: application/json" ^
-d "{\"title\":\"Failover chat\",\"member_ids\":[\"%UID%\"]}" ^
-o "%TMP%\chat.json" -w "%%{http_code}" > "%TMP%\chat.code"
call :check_code chat.code 201 "create chat"
if errorlevel 1 exit /b 1
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\chat.json' -Raw | ConvertFrom-Json).id"') do set CHAT_ID=%%a
echo === Upload, capture replica via X-Instance-Id header ===
curl.exe -s -X POST "%BASE%/api/v1/chats/%CHAT_ID%/attachments?filename=failover.png" ^
-H "Authorization: Bearer %TOKEN%" -H "Content-Type: image/png" ^
--data-binary "@%FIXTURE%" ^
-D "%TMP%\upload.headers" ^
-o "%TMP%\att.json" ^
-w "%%{http_code}" > "%TMP%\att.code"
call :check_code att.code 202 "upload"
if errorlevel 1 exit /b 1
for /f "tokens=1,* delims=:" %%a in ('findstr /i "X-Instance-Id" "%TMP%\upload.headers"') do set UPLOAD_INSTANCE=%%b
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\att.json' -Raw | ConvertFrom-Json).id"') do set ATT_ID=%%a
echo Uploaded by:%UPLOAD_INSTANCE%
if "%ATT_ID%"=="" (
echo FAIL: attachment id is empty. Response body:
type "%TMP%\att.json"
echo.
echo Response headers:
type "%TMP%\upload.headers"
exit /b 1
)
echo attachment=%ATT_ID%
echo === Wait for ready ===
powershell -NoProfile -Command "for($i=0;$i -lt 60;$i++){ $m=Invoke-RestMethod -Uri '%BASE%/api/v1/attachments/%ATT_ID%' -Headers @{Authorization='Bearer %TOKEN%'}; if($m.status -eq 'ready'){ $m | ConvertTo-Json -Depth 6 | Set-Content '%TMP%\meta.json'; exit 0 }; if($m.status -eq 'failed'){ exit 2 }; Start-Sleep -Seconds 1 }; exit 1"
if errorlevel 1 ( echo FAIL: not ready in 60s & exit /b 1 )
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\meta.json' -Raw | ConvertFrom-Json).sha256"') do set EXPECTED_SHA=%%a
echo expected sha256=%EXPECTED_SHA%
echo === Stop one API replica ===
set STOP_ID=
for /f "delims=" %%i in ('docker compose ps -q api') do if not defined STOP_ID set STOP_ID=%%i
if "%STOP_ID%"=="" ( echo FAIL: no api containers & exit /b 1 )
echo Stopping %STOP_ID%
docker stop --time 10 %STOP_ID% >nul
timeout /t 5 /nobreak >nul
echo === Download via remaining replica (with retries) ===
set TRY=0
:dl_retry
set /a TRY+=1
if %TRY% GTR 15 (
echo FAIL: download did not recover after replica stop
docker compose --compatibility up -d api >nul
exit /b 1
)
curl.exe -s -X GET "%BASE%/api/v1/attachments/%ATT_ID%/content" ^
-H "Authorization: Bearer %TOKEN%" ^
-D "%TMP%\dl.headers" ^
-o "%TMP%\dl.bin" ^
-w "%%{http_code}" > "%TMP%\dl.code"
set /p RC=<"%TMP%\dl.code"
if "%RC%"=="200" goto dl_ok
echo Download attempt %TRY% got HTTP %RC%, retrying...
timeout /t 1 /nobreak >nul
goto dl_retry
:dl_ok
for /f "tokens=1,* delims=:" %%a in ('findstr /i "X-Instance-Id" "%TMP%\dl.headers"') do set DL_INSTANCE=%%b
echo Downloaded by:%DL_INSTANCE%
powershell -NoProfile -Command "$h=[Security.Cryptography.SHA256]::Create(); $bytes=[IO.File]::ReadAllBytes('%TMP%\dl.bin'); $sha=[BitConverter]::ToString($h.ComputeHash($bytes)).Replace('-','').ToLower(); if($sha -ne '%EXPECTED_SHA%'){ exit 1 } else { exit 0 }"
if errorlevel 1 (
echo FAIL: downloaded sha256 mismatch after failover
docker compose --compatibility up -d api >nul
exit /b 1
)
echo PASS: original bytes available after stopping one replica
echo === Restore API replicas ===
docker compose --compatibility up -d api
timeout /t 15 /nobreak >nul
docker compose ps
exit /b 0
:guid
for /f "delims=" %%a in ('powershell -NoProfile -Command "[guid]::NewGuid().ToString('N').Substring(0,8)"') do set GUIDVAL=%%a
exit /b 0
:register
curl.exe -s -X POST "%BASE%/api/v1/users" ^
-H "Content-Type: application/json" ^
-d "{\"username\":\"%~1\",\"display_name\":\"%~2\",\"password\":\"%PASS%\"}" ^
-o "%TMP%\%~3.json" -w "%%{http_code}" > "%TMP%\%~3.code"
set /p RC=<"%TMP%\%~3.code"
if not "%RC%"=="201" ( echo FAIL: register %~1, HTTP %RC% & type "%TMP%\%~3.json" & exit /b 1 )
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~3.json' -Raw | ConvertFrom-Json).id"') do set "%~3_ID=%%a"
exit /b 0
:login
curl.exe -s -X POST "%BASE%/api/v1/auth/sessions" ^
-u "%~1:%PASS%" ^
-o "%TMP%\%~2.json" -w "%%{http_code}" > "%TMP%\%~2.code"
set /p RC=<"%TMP%\%~2.code"
if not "%RC%"=="201" ( echo FAIL: login %~1, HTTP %RC% & type "%TMP%\%~2.json" & exit /b 1 )
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~2.json' -Raw | ConvertFrom-Json).token"') do set "%~2_TOKEN=%%a"
exit /b 0
:check_code
set /p RC=<"%TMP%\%~1"
if not "%RC%"=="%~2" ( echo FAIL: %~3 expected %~2, got %RC% & exit /b 1 )
exit /b 0
+133
View File
@@ -0,0 +1,133 @@
@echo off
setlocal EnableDelayedExpansion
cd /d "%~dp0.."
set BASE=http://localhost:8080
set PASS=Example-only-not-a-real-secret-123
set FIXTURE=tests\fixtures\sample.png
set TMP=%TEMP%\lab5-manual
if not exist "%TMP%" mkdir "%TMP%"
call :guid
set RND=%GUIDVAL%
set ALICE=alice%RND%
set BOB=bob%RND%
set EVE=eve%RND%
echo === Register + login ===
call :register "%ALICE%" "Alice" alice
if errorlevel 1 exit /b 1
set ALICE_ID=!alice_ID!
call :register "%BOB%" "Bob" bob
if errorlevel 1 exit /b 1
set BOB_ID=!bob_ID!
call :register "%EVE%" "Eve" eve
if errorlevel 1 exit /b 1
call :login "%ALICE%" alice
if errorlevel 1 exit /b 1
set ALICE_TOKEN=!alice_TOKEN!
call :login "%BOB%" bob
if errorlevel 1 exit /b 1
set BOB_TOKEN=!bob_TOKEN!
call :login "%EVE%" eve
if errorlevel 1 exit /b 1
set EVE_TOKEN=!eve_TOKEN!
echo === Create chat ===
curl.exe -s -X POST "%BASE%/api/v1/chats" ^
-H "Authorization: Bearer %ALICE_TOKEN%" ^
-H "Content-Type: application/json" ^
-d "{\"title\":\"Lab5 manual\",\"member_ids\":[\"%ALICE_ID%\",\"%BOB_ID%\"]}" ^
-o "%TMP%\chat.json" -w "%%{http_code}" > "%TMP%\chat.code"
call :check_code chat.code 201 "create chat"
if errorlevel 1 exit /b 1
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\chat.json' -Raw | ConvertFrom-Json).id"') do set CHAT_ID=%%a
echo === Upload: expect 202 queued ===
curl.exe -s -X POST "%BASE%/api/v1/chats/%CHAT_ID%/attachments?filename=sample.png" ^
-H "Authorization: Bearer %ALICE_TOKEN%" ^
-H "Content-Type: image/png" ^
--data-binary "@%FIXTURE%" ^
-o "%TMP%\att.json" -w "%%{http_code}" > "%TMP%\att.code"
call :check_code att.code 202 "upload returns 202"
if errorlevel 1 exit /b 1
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\att.json' -Raw | ConvertFrom-Json).id"') do set ATT_ID=%%a
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\att.json' -Raw | ConvertFrom-Json).status"') do set UP_STATUS=%%a
if not "%UP_STATUS%"=="queued" (
echo FAIL: upload status must be queued, got %UP_STATUS%
exit /b 1
)
echo PASS: upload returned 202 queued, attachment=%ATT_ID%
echo === Poll until ready ===
powershell -NoProfile -Command "for($i=0;$i -lt 60;$i++){ $m=Invoke-RestMethod -Uri '%BASE%/api/v1/attachments/%ATT_ID%' -Headers @{Authorization='Bearer %BOB_TOKEN%'}; if($m.status -eq 'ready' -or $m.status -eq 'failed'){ $m | ConvertTo-Json -Depth 6 | Set-Content '%TMP%\meta.json'; exit 0 }; Start-Sleep -Seconds 1 }; exit 1"
if errorlevel 1 (
echo FAIL: attachment did not reach terminal state in 60s
exit /b 1
)
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\meta.json' -Raw | ConvertFrom-Json).status"') do set RD_STATUS=%%a
if not "%RD_STATUS%"=="ready" (
echo FAIL: attachment status is %RD_STATUS%, expected ready
exit /b 1
)
echo PASS: attachment became ready
echo === Validate variants (thumbnail 256x160 jpeg, crop 128x128 jpeg, optimized webp) ===
powershell -NoProfile -Command "$m=Get-Content '%TMP%\meta.json' -Raw | ConvertFrom-Json; $t=$m.variants | Where-Object name -eq 'thumbnail'; $c=$m.variants | Where-Object name -eq 'crop'; $o=$m.variants | Where-Object name -eq 'optimized'; if(($m.variants | Measure-Object).Count -ne 3){ Write-Host 'FAIL: expected 3 variants'; exit 1 }; if(-not $t -or $t.width -ne 256 -or $t.height -ne 160 -or $t.content_type -ne 'image/jpeg'){ Write-Host 'FAIL: thumbnail wrong'; exit 1 }; if(-not $c -or $c.width -ne 128 -or $c.height -ne 128 -or $c.content_type -ne 'image/jpeg'){ Write-Host 'FAIL: crop wrong'; exit 1 }; if(-not $o -or $o.content_type -ne 'image/webp'){ Write-Host 'FAIL: optimized wrong'; exit 1 }; Write-Host 'PASS: variants metadata correct'"
if errorlevel 1 exit /b 1
echo === Download each variant and compare sha256 ===
powershell -NoProfile -Command "$m=Get-Content '%TMP%\meta.json' -Raw | ConvertFrom-Json; $h=@{Authorization='Bearer %BOB_TOKEN%'}; foreach($v in $m.variants){ $r=Invoke-WebRequest -Uri ('%BASE%/api/v1/attachments/%ATT_ID%/content?variant='+$v.name) -Headers $h -UseBasicParsing; $sha=[BitConverter]::ToString([Security.Cryptography.SHA256]::Create().ComputeHash($r.Content)).Replace('-','').ToLower(); if($sha -ne $v.sha256){ Write-Host ('FAIL: sha mismatch for '+$v.name); exit 1 }; if($r.Headers['Content-Type'] -ne $v.content_type){ Write-Host ('FAIL: content-type mismatch for '+$v.name); exit 1 }; Write-Host ('PASS: variant '+$v.name+' bytes and content-type match') }; $r=Invoke-WebRequest -Uri '%BASE%/api/v1/attachments/%ATT_ID%/content' -Headers $h -UseBasicParsing; $sha=[BitConverter]::ToString([Security.Cryptography.SHA256]::Create().ComputeHash($r.Content)).Replace('-','').ToLower(); if($sha -ne $m.sha256){ Write-Host 'FAIL: original sha mismatch'; exit 1 }; Write-Host 'PASS: original bytes match'"
if errorlevel 1 exit /b 1
echo === Eve gets 404 on metadata and content ===
curl.exe -s -o nul -H "Authorization: Bearer %EVE_TOKEN%" -w "%%{http_code}" "%BASE%/api/v1/attachments/%ATT_ID%" > "%TMP%\eve_meta.code"
call :check_code eve_meta.code 404 "eve metadata"
if errorlevel 1 exit /b 1
curl.exe -s -o nul -H "Authorization: Bearer %EVE_TOKEN%" -w "%%{http_code}" "%BASE%/api/v1/attachments/%ATT_ID%/content" > "%TMP%\eve_content.code"
call :check_code eve_content.code 404 "eve content"
if errorlevel 1 exit /b 1
echo === Message with attachment_ids ===
call :guid
set KEY=%GUIDVAL%
curl.exe -s -X POST "%BASE%/api/v1/chats/%CHAT_ID%/messages" ^
-H "Authorization: Bearer %ALICE_TOKEN%" ^
-H "Idempotency-Key: %KEY%" ^
-H "Content-Type: application/json" ^
-d "{\"text\":\"\",\"attachment_ids\":[\"%ATT_ID%\"]}" ^
-o "%TMP%\msg.json" -w "%%{http_code}" > "%TMP%\msg.code"
call :check_code msg.code 201 "create message with attachment"
if errorlevel 1 exit /b 1
echo.
echo ALL MANUAL LAB5 CHECKS PASSED
exit /b 0
:guid
for /f "delims=" %%a in ('powershell -NoProfile -Command "[guid]::NewGuid().ToString('N').Substring(0,8)"') do set GUIDVAL=%%a
exit /b 0
:register
curl.exe -s -X POST "%BASE%/api/v1/users" ^
-H "Content-Type: application/json" ^
-d "{\"username\":\"%~1\",\"display_name\":\"%~2\",\"password\":\"%PASS%\"}" ^
-o "%TMP%\%~3.json" -w "%%{http_code}" > "%TMP%\%~3.code"
set /p RC=<"%TMP%\%~3.code"
if not "%RC%"=="201" ( echo FAIL: register %~1, HTTP %RC% & type "%TMP%\%~3.json" & exit /b 1 )
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~3.json' -Raw | ConvertFrom-Json).id"') do set "%~3_ID=%%a"
exit /b 0
:login
curl.exe -s -X POST "%BASE%/api/v1/auth/sessions" ^
-u "%~1:%PASS%" ^
-o "%TMP%\%~2.json" -w "%%{http_code}" > "%TMP%\%~2.code"
set /p RC=<"%TMP%\%~2.code"
if not "%RC%"=="201" ( echo FAIL: login %~1, HTTP %RC% & type "%TMP%\%~2.json" & exit /b 1 )
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~2.json' -Raw | ConvertFrom-Json).token"') do set "%~2_TOKEN=%%a"
exit /b 0
:check_code
set /p RC=<"%TMP%\%~1"
if not "%RC%"=="%~2" ( echo FAIL: %~3 expected %~2, got %RC% & exit /b 1 )
exit /b 0
+147
View File
@@ -0,0 +1,147 @@
@echo off
setlocal EnableDelayedExpansion
cd /d "%~dp0.."
set BASE=http://localhost:8080
set PASS=Example-only-not-a-real-secret-123
set FIXTURE=tests\fixtures\sample.png
set TMP=%TEMP%\lab5-negative
if not exist "%TMP%" mkdir "%TMP%"
call :guid
set RND=%GUIDVAL%
set ALICE=alice%RND%
set BOB=bob%RND%
set EVE=eve%RND%
call :register "%ALICE%" "Alice" alice
if errorlevel 1 exit /b 1
set ALICE_ID=!alice_ID!
call :register "%BOB%" "Bob" bob
if errorlevel 1 exit /b 1
set BOB_ID=!bob_ID!
call :register "%EVE%" "Eve" eve
if errorlevel 1 exit /b 1
call :login "%ALICE%" alice
if errorlevel 1 exit /b 1
set ALICE_TOKEN=!alice_TOKEN!
call :login "%EVE%" eve
if errorlevel 1 exit /b 1
set EVE_TOKEN=!eve_TOKEN!
curl.exe -s -X POST "%BASE%/api/v1/chats" ^
-H "Authorization: Bearer %ALICE_TOKEN%" -H "Content-Type: application/json" ^
-d "{\"title\":\"Neg chat1\",\"member_ids\":[\"%ALICE_ID%\",\"%BOB_ID%\"]}" ^
-o "%TMP%\chat1.json" -w "%%{http_code}" > "%TMP%\chat1.code"
call :check_code chat1.code 201 "create chat1"
if errorlevel 1 exit /b 1
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\chat1.json' -Raw | ConvertFrom-Json).id"') do set CHAT1_ID=%%a
curl.exe -s -X POST "%BASE%/api/v1/chats" ^
-H "Authorization: Bearer %ALICE_TOKEN%" -H "Content-Type: application/json" ^
-d "{\"title\":\"Neg chat2\",\"member_ids\":[\"%ALICE_ID%\",\"%EVE_ID%\"]}" ^
-o "%TMP%\chat2.json" -w "%%{http_code}" > "%TMP%\chat2.code"
call :check_code chat2.code 201 "create chat2"
if errorlevel 1 exit /b 1
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\chat2.json' -Raw | ConvertFrom-Json).id"') do set CHAT2_ID=%%a
echo === 1. Too large file 413 ===
powershell -NoProfile -Command "$sig=[byte[]](0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A,0x0A); $pad=New-Object byte[] (11534336-8); [IO.File]::WriteAllBytes('%TMP%\large.png', $sig+$pad)"
curl.exe -s -o nul -X POST "%BASE%/api/v1/chats/%CHAT1_ID%/attachments?filename=large.png" ^
-H "Authorization: Bearer %ALICE_TOKEN%" -H "Content-Type: image/png" ^
--data-binary "@%TMP%\large.png" -w "%%{http_code}" > "%TMP%\large.code"
call :check_code large.code 413 "too large upload"
if errorlevel 1 exit /b 1
echo === 2. Broken image 415 ===
powershell -NoProfile -Command "[IO.File]::WriteAllBytes('%TMP%\broken.png',[byte[]](0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A,0x0A,0xFF,0xFF,0xFF,0xFF))"
curl.exe -s -o nul -X POST "%BASE%/api/v1/chats/%CHAT1_ID%/attachments?filename=broken.png" ^
-H "Authorization: Bearer %ALICE_TOKEN%" -H "Content-Type: image/png" ^
--data-binary "@%TMP%\broken.png" -w "%%{http_code}" > "%TMP%\broken.code"
call :check_code broken.code 415 "broken png"
if errorlevel 1 exit /b 1
echo === 3. MIME mismatch 415 ===
curl.exe -s -o nul -X POST "%BASE%/api/v1/chats/%CHAT1_ID%/attachments?filename=fake.png" ^
-H "Authorization: Bearer %ALICE_TOKEN%" -H "Content-Type: image/jpeg" ^
--data-binary "@%FIXTURE%" -w "%%{http_code}" > "%TMP%\mime.code"
call :check_code mime.code 415 "jpeg disguised as png"
if errorlevel 1 exit /b 1
echo === 4. Dangerous filename 400 ===
curl.exe -s -o nul -X POST "%BASE%/api/v1/chats/%CHAT1_ID%/attachments?filename=..%%2Fevil.png" ^
-H "Authorization: Bearer %ALICE_TOKEN%" -H "Content-Type: image/png" ^
--data-binary "@%FIXTURE%" -w "%%{http_code}" > "%TMP%\filename.code"
call :check_code filename.code 400 "path traversal filename"
if errorlevel 1 exit /b 1
echo === 5. Valid upload, wait ready ===
curl.exe -s -X POST "%BASE%/api/v1/chats/%CHAT1_ID%/attachments?filename=sample.png" ^
-H "Authorization: Bearer %ALICE_TOKEN%" -H "Content-Type: image/png" ^
--data-binary "@%FIXTURE%" -o "%TMP%\att.json" -w "%%{http_code}" > "%TMP%\att.code"
call :check_code att.code 202 "valid upload"
if errorlevel 1 exit /b 1
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\att.json' -Raw | ConvertFrom-Json).id"') do set ATT_ID=%%a
powershell -NoProfile -Command "for($i=0;$i -lt 60;$i++){ $m=Invoke-RestMethod -Uri '%BASE%/api/v1/attachments/%ATT_ID%' -Headers @{Authorization='Bearer %ALICE_TOKEN%'}; if($m.status -eq 'ready' -or $m.status -eq 'failed'){ exit 0 }; Start-Sleep -Seconds 1 }; exit 1"
if errorlevel 1 ( echo FAIL: not ready in 60s & exit /b 1 )
echo === 6. Unknown variant 404 ===
curl.exe -s -o nul -H "Authorization: Bearer %ALICE_TOKEN%" -w "%%{http_code}" "%BASE%/api/v1/attachments/%ATT_ID%/content?variant=nope" > "%TMP%\variant.code"
call :check_code variant.code 404 "unknown variant"
if errorlevel 1 exit /b 1
echo === 7. Cross-chat reference 404 ===
call :guid
set KEY1=%GUIDVAL%
curl.exe -s -o nul -X POST "%BASE%/api/v1/chats/%CHAT2_ID%/messages" ^
-H "Authorization: Bearer %ALICE_TOKEN%" -H "Idempotency-Key: %KEY1%" -H "Content-Type: application/json" ^
-d "{\"text\":\"\",\"attachment_ids\":[\"%ATT_ID%\"]}" -w "%%{http_code}" > "%TMP%\crosschat.code"
call :check_code crosschat.code 404 "cross-chat attachment reference"
if errorlevel 1 exit /b 1
echo === 8. Unknown attachment id 404 ===
for /f "delims=" %%a in ('powershell -NoProfile -Command "[guid]::NewGuid().ToString()"') do set UNKNOWN=%%a
call :guid
set KEY2=%GUIDVAL%
curl.exe -s -o nul -X POST "%BASE%/api/v1/chats/%CHAT1_ID%/messages" ^
-H "Authorization: Bearer %ALICE_TOKEN%" -H "Idempotency-Key: %KEY2%" -H "Content-Type: application/json" ^
-d "{\"text\":\"\",\"attachment_ids\":[\"%UNKNOWN%\"]}" -w "%%{http_code}" > "%TMP%\unknown.code"
call :check_code unknown.code 404 "unknown attachment id"
if errorlevel 1 exit /b 1
echo === 9. Eve download 404 ===
curl.exe -s -o nul -H "Authorization: Bearer %EVE_TOKEN%" -w "%%{http_code}" "%BASE%/api/v1/attachments/%ATT_ID%/content" > "%TMP%\eve_dl.code"
call :check_code eve_dl.code 404 "eve download"
if errorlevel 1 exit /b 1
echo.
echo ALL NEGATIVE CHECKS PASSED
exit /b 0
:guid
for /f "delims=" %%a in ('powershell -NoProfile -Command "[guid]::NewGuid().ToString('N').Substring(0,8)"') do set GUIDVAL=%%a
exit /b 0
:register
curl.exe -s -X POST "%BASE%/api/v1/users" ^
-H "Content-Type: application/json" ^
-d "{\"username\":\"%~1\",\"display_name\":\"%~2\",\"password\":\"%PASS%\"}" ^
-o "%TMP%\%~3.json" -w "%%{http_code}" > "%TMP%\%~3.code"
set /p RC=<"%TMP%\%~3.code"
if not "%RC%"=="201" ( echo FAIL: register %~1, HTTP %RC% & type "%TMP%\%~3.json" & exit /b 1 )
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~3.json' -Raw | ConvertFrom-Json).id"') do set "%~3_ID=%%a"
exit /b 0
:login
curl.exe -s -X POST "%BASE%/api/v1/auth/sessions" ^
-u "%~1:%PASS%" ^
-o "%TMP%\%~2.json" -w "%%{http_code}" > "%TMP%\%~2.code"
set /p RC=<"%TMP%\%~2.code"
if not "%RC%"=="201" ( echo FAIL: login %~1, HTTP %RC% & type "%TMP%\%~2.json" & exit /b 1 )
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~2.json' -Raw | ConvertFrom-Json).token"') do set "%~2_TOKEN=%%a"
exit /b 0
:check_code
set /p RC=<"%TMP%\%~1"
if not "%RC%"=="%~2" ( echo FAIL: %~3 expected %~2, got %RC% & exit /b 1 )
exit /b 0
+87
View File
@@ -0,0 +1,87 @@
@echo off
setlocal EnableDelayedExpansion
cd /d "%~dp0.."
set BASE=http://localhost:8080
set PASS=Example-only-not-a-real-secret-123
set FIXTURE=tests\fixtures\sample.png
set TMP=%TEMP%\lab5-persistence
if not exist "%TMP%" mkdir "%TMP%"
call :guid
set RND=%GUIDVAL%
set U=pers%RND%
call :register "%U%" "Persistence" u
if errorlevel 1 exit /b 1
set UID=!u_ID!
call :login "%U%" u
if errorlevel 1 exit /b 1
set TOKEN=!u_TOKEN!
curl.exe -s -X POST "%BASE%/api/v1/chats" ^
-H "Authorization: Bearer %TOKEN%" -H "Content-Type: application/json" ^
-d "{\"title\":\"Persistence chat\",\"member_ids\":[\"%UID%\"]}" ^
-o "%TMP%\chat.json" -w "%%{http_code}" > "%TMP%\chat.code"
call :check_code chat.code 201 "create chat"
if errorlevel 1 exit /b 1
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\chat.json' -Raw | ConvertFrom-Json).id"') do set CHAT_ID=%%a
curl.exe -s -X POST "%BASE%/api/v1/chats/%CHAT_ID%/attachments?filename=sample.png" ^
-H "Authorization: Bearer %TOKEN%" -H "Content-Type: image/png" ^
--data-binary "@%FIXTURE%" -o "%TMP%\att.json" -w "%%{http_code}" > "%TMP%\att.code"
call :check_code att.code 202 "upload"
if errorlevel 1 exit /b 1
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\att.json' -Raw | ConvertFrom-Json).id"') do set ATT_ID=%%a
echo === Waiting for ready ===
powershell -NoProfile -Command "for($i=0;$i -lt 60;$i++){ $m=Invoke-RestMethod -Uri '%BASE%/api/v1/attachments/%ATT_ID%' -Headers @{Authorization='Bearer %TOKEN%'}; if($m.status -eq 'ready'){ $m | ConvertTo-Json -Depth 6 | Set-Content '%TMP%\meta.json'; exit 0 }; if($m.status -eq 'failed'){ exit 2 }; Start-Sleep -Seconds 1 }; exit 1"
if errorlevel 1 ( echo FAIL: not ready in 60s & exit /b 1 )
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\meta.json' -Raw | ConvertFrom-Json).sha256"') do set SHA256=%%a
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\meta.json' -Raw | ConvertFrom-Json).variants | ForEach-Object { $_.name + ':' + $_.sha256 }"') do (
echo %%a>> "%TMP%\variants.txt"
)
>"%TMP%\token.txt" echo %TOKEN%
>"%TMP%\chat_id.txt" echo %CHAT_ID%
>"%TMP%\att_id.txt" echo %ATT_ID%
>"%TMP%\sha256.txt" echo %SHA256%
echo.
echo State saved to %TMP%
echo attachment=%ATT_ID%
echo original sha256=%SHA256%
type "%TMP%\variants.txt"
echo.
echo Now run:
echo scripts\restart-stack.bat
echo scripts\persistence-verify.bat
exit /b 0
:guid
for /f "delims=" %%a in ('powershell -NoProfile -Command "[guid]::NewGuid().ToString('N').Substring(0,8)"') do set GUIDVAL=%%a
exit /b 0
:register
curl.exe -s -X POST "%BASE%/api/v1/users" ^
-H "Content-Type: application/json" ^
-d "{\"username\":\"%~1\",\"display_name\":\"%~2\",\"password\":\"%PASS%\"}" ^
-o "%TMP%\%~3.json" -w "%%{http_code}" > "%TMP%\%~3.code"
set /p RC=<"%TMP%\%~3.code"
if not "%RC%"=="201" ( echo FAIL: register %~1, HTTP %RC% & type "%TMP%\%~3.json" & exit /b 1 )
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~3.json' -Raw | ConvertFrom-Json).id"') do set "%~3_ID=%%a"
exit /b 0
:login
curl.exe -s -X POST "%BASE%/api/v1/auth/sessions" ^
-u "%~1:%PASS%" ^
-o "%TMP%\%~2.json" -w "%%{http_code}" > "%TMP%\%~2.code"
set /p RC=<"%TMP%\%~2.code"
if not "%RC%"=="201" ( echo FAIL: login %~1, HTTP %RC% & type "%TMP%\%~2.json" & exit /b 1 )
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~2.json' -Raw | ConvertFrom-Json).token"') do set "%~2_TOKEN=%%a"
exit /b 0
:check_code
set /p RC=<"%TMP%\%~1"
if not "%RC%"=="%~2" ( echo FAIL: %~3 expected %~2, got %RC% & exit /b 1 )
exit /b 0
+59
View File
@@ -0,0 +1,59 @@
@echo off
setlocal EnableDelayedExpansion
cd /d "%~dp0.."
set BASE=http://localhost:8080
set TMP=%TEMP%\lab5-persistence
if not exist "%TMP%\token.txt" (
echo FAIL: state not found. Run scripts\persistence-attachment.bat first.
exit /b 1
)
set /p TOKEN=<"%TMP%\token.txt"
set /p CHAT_ID=<"%TMP%\chat_id.txt"
set /p ATT_ID=<"%TMP%\att_id.txt"
set /p EXPECTED_SHA=<"%TMP%\sha256.txt"
echo === Persistence verify after restart ===
echo chat=%CHAT_ID%
echo attachment=%ATT_ID%
echo expected original sha256=%EXPECTED_SHA%
echo === Check metadata ===
curl.exe -s -X GET "%BASE%/api/v1/attachments/%ATT_ID%" ^
-H "Authorization: Bearer %TOKEN%" ^
-o "%TMP%\meta.json" -w "%%{http_code}" > "%TMP%\meta.code"
set /p RC=<"%TMP%\meta.code"
if not "%RC%"=="200" ( echo FAIL: metadata HTTP %RC% & type "%TMP%\meta.json" & exit /b 1 )
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\meta.json' -Raw | ConvertFrom-Json).sha256"') do set META_SHA=%%a
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\meta.json' -Raw | ConvertFrom-Json).status"') do set ST=%%a
if not "%ST%"=="ready" ( echo FAIL: status is %ST%, expected ready & exit /b 1 )
if not "%META_SHA%"=="%EXPECTED_SHA%" ( echo FAIL: metadata sha256 mismatch & exit /b 1 )
echo PASS: attachment metadata survived restart (status=ready, sha256 matches)
echo === Check original bytes ===
curl.exe -s -X GET "%BASE%/api/v1/attachments/%ATT_ID%/content" ^
-H "Authorization: Bearer %TOKEN%" ^
-o "%TMP%\original.bin" -w "%%{http_code}" > "%TMP%\original.code"
set /p RC=<"%TMP%\original.code"
if not "%RC%"=="200" ( echo FAIL: original HTTP %RC% & exit /b 1 )
powershell -NoProfile -Command "$h=[Security.Cryptography.SHA256]::Create(); $bytes=[IO.File]::ReadAllBytes('%TMP%\original.bin'); $sha=[BitConverter]::ToString($h.ComputeHash($bytes)).Replace('-','').ToLower(); if($sha -ne '%EXPECTED_SHA%'){ exit 1 } else { exit 0 }"
if errorlevel 1 ( echo FAIL: original sha256 mismatch & exit /b 1 )
echo PASS: original bytes survived restart
echo === Check each variant bytes ===
powershell -NoProfile -Command "$m=Get-Content '%TMP%\meta.json' -Raw | ConvertFrom-Json; $h=@{Authorization='Bearer %TOKEN%'}; foreach($v in $m.variants){ $r=Invoke-WebRequest -Uri ('%BASE%/api/v1/attachments/%ATT_ID%/content?variant='+$v.name) -Headers $h -UseBasicParsing; $sha=[BitConverter]::ToString([Security.Cryptography.SHA256]::Create().ComputeHash($r.Content)).Replace('-','').ToLower(); if($sha -ne $v.sha256){ Write-Host ('FAIL: variant '+$v.name+' sha256 mismatch'); exit 1 }; Write-Host ('PASS: variant '+$v.name+' bytes survived restart') }"
if errorlevel 1 exit /b 1
echo === Check old session token still works ===
curl.exe -s -o nul -H "Authorization: Bearer %TOKEN%" -w "%%{http_code}" "%BASE%/api/v1/chats/%CHAT_ID%/messages?limit=1" > "%TMP%\msg.code"
set /p RC=<"%TMP%\msg.code"
if not "%RC%"=="200" ( echo FAIL: messages HTTP %RC%, token may have expired & exit /b 1 )
echo PASS: old session token still works
echo.
echo ALL PERSISTENCE CHECKS PASSED
exit /b 0
+35
View File
@@ -0,0 +1,35 @@
@echo off
setlocal EnableDelayedExpansion
cd /d "%~dp0.."
set TMP=%TEMP%\lab5-private
if not exist "%TMP%" mkdir "%TMP%"
echo === Private bucket check (original + thumbnail) ===
for /f "usebackq delims=" %%a in (`docker compose exec -T postgres psql -U messenger -d messenger -tAc "SELECT s3_key FROM attachments WHERE status='ready' ORDER BY created_at DESC LIMIT 1"`) do set KEY=%%a
if "%KEY%"=="" (
echo FAIL: no ready attachments found. Run scripts\manual-attachments.bat first.
exit /b 1
)
echo Latest original key: %KEY%
docker compose exec -T api sh -lc "wget -S -O /dev/null http://rustfs:9000/messenger/%KEY% 2>&1" > "%TMP%\out1.txt" 2>&1
findstr /c:"HTTP/1.1 200" "%TMP%\out1.txt" >nul
if not errorlevel 1 ( echo FAIL: original is publicly readable & type "%TMP%\out1.txt" & exit /b 1 )
echo PASS: original not publicly readable
for /f "usebackq delims=" %%a in (`docker compose exec -T postgres psql -U messenger -d messenger -tAc "SELECT chat_id || '/' || id || '/thumbnail' FROM attachments WHERE status='ready' ORDER BY created_at DESC LIMIT 1"`) do set TKEY=%%a
if "%TKEY%"=="" (
echo FAIL: no ready attachment for thumbnail check
exit /b 1
)
echo Latest thumbnail key: %TKEY%
docker compose exec -T api sh -lc "wget -S -O /dev/null http://rustfs:9000/messenger/%TKEY% 2>&1" > "%TMP%\out2.txt" 2>&1
findstr /c:"HTTP/1.1 200" "%TMP%\out2.txt" >nul
if not errorlevel 1 ( echo FAIL: thumbnail is publicly readable & type "%TMP%\out2.txt" & exit /b 1 )
echo PASS: thumbnail not publicly readable
echo.
echo PRIVATE BUCKET CHECKS PASSED
exit /b 0
+102
View File
@@ -0,0 +1,102 @@
@echo off
setlocal EnableDelayedExpansion
cd /d "%~dp0.."
set BASE=http://localhost:8080
set PASS=Example-only-not-a-real-secret-123
set FIXTURE=tests\fixtures\sample.png
set TMP=%TEMP%\lab5-reconcile
if not exist "%TMP%" mkdir "%TMP%"
call :guid
set RND=%GUIDVAL%
set U=rec%RND%
call :register "%U%" "Reconcile" u
if errorlevel 1 exit /b 1
set UID=!u_ID!
call :login "%U%" u
if errorlevel 1 exit /b 1
set TOKEN=!u_TOKEN!
curl.exe -s -X POST "%BASE%/api/v1/chats" ^
-H "Authorization: Bearer %TOKEN%" -H "Content-Type: application/json" ^
-d "{\"title\":\"Reconcile demo\",\"member_ids\":[\"%UID%\"]}" ^
-o "%TMP%\chat.json" -w "%%{http_code}" > "%TMP%\chat.code"
call :check_code chat.code 201 "create chat"
if errorlevel 1 exit /b 1
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\chat.json' -Raw | ConvertFrom-Json).id"') do set CHAT_ID=%%a
echo === Upload and wait ready (creates S3 objects + DB metadata) ===
curl.exe -s -X POST "%BASE%/api/v1/chats/%CHAT_ID%/attachments?filename=sample.png" ^
-H "Authorization: Bearer %TOKEN%" -H "Content-Type: image/png" ^
--data-binary "@%FIXTURE%" -o "%TMP%\att.json" -w "%%{http_code}" > "%TMP%\att.code"
call :check_code att.code 202 "upload"
if errorlevel 1 exit /b 1
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\att.json' -Raw | ConvertFrom-Json).id"') do set ATT_ID=%%a
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\att.json' -Raw | ConvertFrom-Json).chat_id"') do set CHAT_ID=%%a
powershell -NoProfile -Command "for($i=0;$i -lt 60;$i++){ $m=Invoke-RestMethod -Uri '%BASE%/api/v1/attachments/%ATT_ID%' -Headers @{Authorization='Bearer %TOKEN%'}; if($m.status -eq 'ready'){ exit 0 }; if($m.status -eq 'failed'){ exit 2 }; Start-Sleep -Seconds 1 }; exit 1"
if errorlevel 1 ( echo FAIL: not ready in 60s & exit /b 1 )
echo attachment=%ATT_ID% reached ready
echo === Count S3 objects for this attachment before deletion ===
for /f "usebackq delims=" %%a in (`docker compose exec -T postgres psql -U messenger -d messenger -tAc "SELECT count(*) FROM attachments WHERE id='%ATT_ID%'"`) do set BEFORE_DB=%%a
echo DB rows before: %BEFORE_DB%
echo === Simulate crash: delete DB rows for this attachment, leave S3 objects orphaned ===
docker compose exec -T postgres psql -U messenger -d messenger -c "DELETE FROM attachments WHERE id='%ATT_ID%';"
docker compose exec -T postgres psql -U messenger -d messenger -c "DELETE FROM jobs WHERE attachment_id='%ATT_ID%';"
docker compose exec -T postgres psql -U messenger -d messenger -c "DELETE FROM job_failures WHERE attachment_id='%ATT_ID%';"
echo === List objects that became orphaned ===
docker compose exec -T rustfs sh -lc "find /data/messenger/staging/%CHAT_ID%/%ATT_ID% /data/messenger/%CHAT_ID%/%ATT_ID% -type f 2>/dev/null" || echo "(rustfs find skipped)"
echo === Run reconcile with grace=0 (must delete orphaned objects) ===
docker compose --compatibility --profile tools run -e RECONCILE_GRACE_PERIOD_SECONDS=0 --rm reconcile
if errorlevel 1 (
echo FAIL: reconcile exited non-zero
exit /b 1
)
echo === Run reconcile again (idempotent: 0 orphans deleted) ===
docker compose --compatibility --profile tools run -e RECONCILE_GRACE_PERIOD_SECONDS=0 --rm reconcile
if errorlevel 1 exit /b 1
echo.
echo RECONCILE DEMO COMPLETE.
echo In the first run log look for "reconcile deleted orphan" with 4-5 keys:
echo staging/%CHAT_ID%/%ATT_ID%
echo %CHAT_ID%/%ATT_ID%/original
echo %CHAT_ID%/%ATT_ID%/thumbnail
echo %CHAT_ID%/%ATT_ID%/crop
echo %CHAT_ID%/%ATT_ID%/optimized
echo In the second run log look for "orphans_deleted 0" (or "deleted 0").
exit /b 0
:guid
for /f "delims=" %%a in ('powershell -NoProfile -Command "[guid]::NewGuid().ToString('N').Substring(0,8)"') do set GUIDVAL=%%a
exit /b 0
:register
curl.exe -s -X POST "%BASE%/api/v1/users" ^
-H "Content-Type: application/json" ^
-d "{\"username\":\"%~1\",\"display_name\":\"%~2\",\"password\":\"%PASS%\"}" ^
-o "%TMP%\%~3.json" -w "%%{http_code}" > "%TMP%\%~3.code"
set /p RC=<"%TMP%\%~3.code"
if not "%RC%"=="201" ( echo FAIL: register %~1, HTTP %RC% & type "%TMP%\%~3.json" & exit /b 1 )
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~3.json' -Raw | ConvertFrom-Json).id"') do set "%~3_ID=%%a"
exit /b 0
:login
curl.exe -s -X POST "%BASE%/api/v1/auth/sessions" ^
-u "%~1:%PASS%" ^
-o "%TMP%\%~2.json" -w "%%{http_code}" > "%TMP%\%~2.code"
set /p RC=<"%TMP%\%~2.code"
if not "%RC%"=="201" ( echo FAIL: login %~1, HTTP %RC% & type "%TMP%\%~2.json" & exit /b 1 )
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~2.json' -Raw | ConvertFrom-Json).token"') do set "%~2_TOKEN=%%a"
exit /b 0
:check_code
set /p RC=<"%TMP%\%~1"
if not "%RC%"=="%~2" ( echo FAIL: %~3 expected %~2, got %RC% & exit /b 1 )
exit /b 0
+27
View File
@@ -0,0 +1,27 @@
@echo off
setlocal EnableDelayedExpansion
cd /d "%~dp0.."
set TMP=%TEMP%\lab5-restart
if not exist "%TMP%" mkdir "%TMP%"
echo === Restarting stack without removing volumes ===
docker compose --compatibility restart
echo === Waiting for /health/ready ===
set COUNT=0
:wait
set /a COUNT+=1
if %COUNT% GTR 90 (
echo FAIL: stack did not become ready in 90s
exit /b 1
)
curl.exe -s -o nul -w "%%{http_code}" http://localhost:8080/health/ready > "%TMP%\code.txt"
set /p C=<"%TMP%\code.txt"
if "%C%"=="200" (
echo Stack is ready.
exit /b 0
)
timeout /t 1 /nobreak >nul
goto wait
+111
View File
@@ -0,0 +1,111 @@
@echo off
setlocal EnableDelayedExpansion
cd /d "%~dp0.."
set BASE=http://localhost:8080
set PASS=Example-only-not-a-real-secret-123
set FIXTURE=tests\fixtures\sample.png
set TMP=%TEMP%\lab5-retryfail
if not exist "%TMP%" mkdir "%TMP%"
call :guid
set RND=%GUIDVAL%
set U=user%RND%
call :register "%U%" "RetryFail" u
if errorlevel 1 exit /b 1
set UID=!u_ID!
call :login "%U%" u
if errorlevel 1 exit /b 1
set TOKEN=!u_TOKEN!
curl.exe -s -X POST "%BASE%/api/v1/chats" ^
-H "Authorization: Bearer %TOKEN%" -H "Content-Type: application/json" ^
-d "{\"title\":\"Retry fail\",\"member_ids\":[\"%UID%\"]}" ^
-o "%TMP%\chat.json" -w "%%{http_code}" > "%TMP%\chat.code"
call :check_code chat.code 201 "create chat"
if errorlevel 1 exit /b 1
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\chat.json' -Raw | ConvertFrom-Json).id"') do set CHAT_ID=%%a
echo === Stop worker so the job cannot complete ===
docker compose --compatibility stop worker
echo === Upload while worker down: job stays queued ===
curl.exe -s -X POST "%BASE%/api/v1/chats/%CHAT_ID%/attachments?filename=sample.png" ^
-H "Authorization: Bearer %TOKEN%" -H "Content-Type: image/png" ^
--data-binary "@%FIXTURE%" -o "%TMP%\att.json" -w "%%{http_code}" > "%TMP%\att.code"
call :check_code att.code 202 "upload"
if errorlevel 1 ( docker compose --compatibility up -d & exit /b 1 )
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\att.json' -Raw | ConvertFrom-Json).id"') do set ATT_ID=%%a
echo === Stop rustfs, then start ONLY the worker container ===
docker compose --compatibility stop rustfs
set WID=
for /f "delims=" %%i in ('docker ps -a --filter "name=worker" --format "{{.ID}}"') do if not defined WID set WID=%%i
if "%WID%"=="" (
echo FAIL: worker container not found
docker compose --compatibility up -d
exit /b 1
)
docker start %WID% >nul
echo === Wait for terminal failed (retry exhaustion, up to 180s) ===
powershell -NoProfile -Command "for($i=0;$i -lt 180;$i++){ $m=Invoke-RestMethod -Uri '%BASE%/api/v1/attachments/%ATT_ID%' -Headers @{Authorization='Bearer %TOKEN%'}; if($m.status -eq 'failed'){ $m | ConvertTo-Json -Depth 6 | Set-Content '%TMP%\meta.json'; exit 0 }; Start-Sleep -Seconds 1 }; exit 1"
if errorlevel 1 (
echo FAIL: attachment did not reach failed in 180s
docker compose --compatibility up -d
exit /b 1
)
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\meta.json' -Raw | ConvertFrom-Json).error_code"') do set ECODE=%%a
echo PASS: attachment reached failed with error_code=%ECODE%
echo === job_failures registry has a row ===
for /f "usebackq delims=" %%a in (`docker compose exec -T postgres psql -U messenger -d messenger -tAc "SELECT count(*) FROM job_failures WHERE attachment_id='%ATT_ID%'"`) do set FCOUNT=%%a
if "%FCOUNT%"=="0" (
echo FAIL: no job_failures row
docker compose --compatibility up -d
exit /b 1
)
echo PASS: job_failures contains %FCOUNT% row(s)
echo === Restore stack; status stays terminal failed ===
docker compose --compatibility up -d
timeout /t 15 /nobreak >nul
curl.exe -s -H "Authorization: Bearer %TOKEN%" "%BASE%/api/v1/attachments/%ATT_ID%" -o "%TMP%\meta2.json"
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\meta2.json' -Raw | ConvertFrom-Json).status"') do set ST2=%%a
if not "%ST2%"=="failed" (
echo FAIL: expected terminal failed after restore, got %ST2%
exit /b 1
)
echo PASS: failed is terminal, no silent reprocessing
echo.
echo RETRY-FAIL CHECKS PASSED
exit /b 0
:guid
for /f "delims=" %%a in ('powershell -NoProfile -Command "[guid]::NewGuid().ToString('N').Substring(0,8)"') do set GUIDVAL=%%a
exit /b 0
:register
curl.exe -s -X POST "%BASE%/api/v1/users" ^
-H "Content-Type: application/json" ^
-d "{\"username\":\"%~1\",\"display_name\":\"%~2\",\"password\":\"%PASS%\"}" ^
-o "%TMP%\%~3.json" -w "%%{http_code}" > "%TMP%\%~3.code"
set /p RC=<"%TMP%\%~3.code"
if not "%RC%"=="201" ( echo FAIL: register %~1, HTTP %RC% & type "%TMP%\%~3.json" & exit /b 1 )
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~3.json' -Raw | ConvertFrom-Json).id"') do set "%~3_ID=%%a"
exit /b 0
:login
curl.exe -s -X POST "%BASE%/api/v1/auth/sessions" ^
-u "%~1:%PASS%" ^
-o "%TMP%\%~2.json" -w "%%{http_code}" > "%TMP%\%~2.code"
set /p RC=<"%TMP%\%~2.code"
if not "%RC%"=="201" ( echo FAIL: login %~1, HTTP %RC% & type "%TMP%\%~2.json" & exit /b 1 )
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~2.json' -Raw | ConvertFrom-Json).token"') do set "%~2_TOKEN=%%a"
exit /b 0
:check_code
set /p RC=<"%TMP%\%~1"
if not "%RC%"=="%~2" ( echo FAIL: %~3 expected %~2, got %RC% & exit /b 1 )
exit /b 0
+6
View File
@@ -0,0 +1,6 @@
@echo off
setlocal
cd /d "%~dp0.."
set PYTHONUTF8=1
python -X utf8 tests\smoke.py --base-url http://localhost:8080
exit /b %ERRORLEVEL%
+101
View File
@@ -0,0 +1,101 @@
@echo off
setlocal EnableDelayedExpansion
cd /d "%~dp0.."
set BASE=http://localhost:8080
set PASS=Example-only-not-a-real-secret-123
set FIXTURE=tests\fixtures\sample.png
set TMP=%TEMP%\lab5-workerdown
if not exist "%TMP%" mkdir "%TMP%"
call :guid
set RND=%GUIDVAL%
set U=user%RND%
call :register "%U%" "WorkerDown" u
if errorlevel 1 exit /b 1
set UID=!u_ID!
call :login "%U%" u
if errorlevel 1 exit /b 1
set TOKEN=!u_TOKEN!
curl.exe -s -X POST "%BASE%/api/v1/chats" ^
-H "Authorization: Bearer %TOKEN%" -H "Content-Type: application/json" ^
-d "{\"title\":\"Worker down\",\"member_ids\":[\"%UID%\"]}" ^
-o "%TMP%\chat.json" -w "%%{http_code}" > "%TMP%\chat.code"
call :check_code chat.code 201 "create chat"
if errorlevel 1 exit /b 1
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\chat.json' -Raw | ConvertFrom-Json).id"') do set CHAT_ID=%%a
echo === Stop worker ===
docker compose --compatibility stop worker
timeout /t 3 /nobreak >nul
echo === Upload while worker down: expect 202 queued ===
curl.exe -s -X POST "%BASE%/api/v1/chats/%CHAT_ID%/attachments?filename=sample.png" ^
-H "Authorization: Bearer %TOKEN%" -H "Content-Type: image/png" ^
--data-binary "@%FIXTURE%" -o "%TMP%\att.json" -w "%%{http_code}" > "%TMP%\att.code"
call :check_code att.code 202 "upload with worker down"
if errorlevel 1 ( docker compose --compatibility start worker & exit /b 1 )
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\att.json' -Raw | ConvertFrom-Json).id"') do set ATT_ID=%%a
echo === Status must stay queued for ~5s (API does not process synchronously) ===
timeout /t 5 /nobreak >nul
curl.exe -s -H "Authorization: Bearer %TOKEN%" "%BASE%/api/v1/attachments/%ATT_ID%" -o "%TMP%\meta.json"
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\meta.json' -Raw | ConvertFrom-Json).status"') do set ST=%%a
if not "%ST%"=="queued" (
echo FAIL: expected queued while worker down, got %ST%
docker compose --compatibility start worker
exit /b 1
)
echo PASS: status stayed queued with worker down
echo === Download before ready must be 409 ===
curl.exe -s -o nul -H "Authorization: Bearer %TOKEN%" -w "%%{http_code}" "%BASE%/api/v1/attachments/%ATT_ID%/content" > "%TMP%\dl.code"
call :check_code dl.code 409 "download before ready"
if errorlevel 1 ( docker compose --compatibility start worker & exit /b 1 )
echo === Start worker and wait ready ===
docker compose --compatibility start worker
powershell -NoProfile -Command "for($i=0;$i -lt 60;$i++){ $m=Invoke-RestMethod -Uri '%BASE%/api/v1/attachments/%ATT_ID%' -Headers @{Authorization='Bearer %TOKEN%'}; if($m.status -eq 'ready'){ exit 0 }; if($m.status -eq 'failed'){ exit 2 }; Start-Sleep -Seconds 1 }; exit 1"
if errorlevel 1 (
echo FAIL: attachment did not become ready after worker restart
exit /b 1
)
echo PASS: job survived worker restart and reached ready
echo === Download now 200 ===
curl.exe -s -o "%TMP%\dl.png" -H "Authorization: Bearer %TOKEN%" -w "%%{http_code}" "%BASE%/api/v1/attachments/%ATT_ID%/content" > "%TMP%\dl2.code"
call :check_code dl2.code 200 "download after ready"
if errorlevel 1 exit /b 1
echo.
echo WORKER-DOWN CHECKS PASSED
exit /b 0
:guid
for /f "delims=" %%a in ('powershell -NoProfile -Command "[guid]::NewGuid().ToString('N').Substring(0,8)"') do set GUIDVAL=%%a
exit /b 0
:register
curl.exe -s -X POST "%BASE%/api/v1/users" ^
-H "Content-Type: application/json" ^
-d "{\"username\":\"%~1\",\"display_name\":\"%~2\",\"password\":\"%PASS%\"}" ^
-o "%TMP%\%~3.json" -w "%%{http_code}" > "%TMP%\%~3.code"
set /p RC=<"%TMP%\%~3.code"
if not "%RC%"=="201" ( echo FAIL: register %~1, HTTP %RC% & type "%TMP%\%~3.json" & exit /b 1 )
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~3.json' -Raw | ConvertFrom-Json).id"') do set "%~3_ID=%%a"
exit /b 0
:login
curl.exe -s -X POST "%BASE%/api/v1/auth/sessions" ^
-u "%~1:%PASS%" ^
-o "%TMP%\%~2.json" -w "%%{http_code}" > "%TMP%\%~2.code"
set /p RC=<"%TMP%\%~2.code"
if not "%RC%"=="201" ( echo FAIL: login %~1, HTTP %RC% & type "%TMP%\%~2.json" & exit /b 1 )
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~2.json' -Raw | ConvertFrom-Json).token"') do set "%~2_TOKEN=%%a"
exit /b 0
:check_code
set /p RC=<"%TMP%\%~1"
if not "%RC%"=="%~2" ( echo FAIL: %~3 expected %~2, got %RC% & exit /b 1 )
exit /b 0