133 lines
6.9 KiB
Batchfile
133 lines
6.9 KiB
Batchfile
@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 |