Files
mtusi2026lab4/scripts/manual-attachments.bat
2026-10-26 16:10:22 +03:00

184 lines
5.5 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%\lab4-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 users ===
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
echo === Login users ===
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 alice+bob ===
curl.exe -s -X POST "%BASE%/api/v1/chats" ^
-H "Authorization: Bearer %ALICE_TOKEN%" ^
-H "Content-Type: application/json" ^
-d "{\"title\":\"Lab4 manual bat\",\"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 chat=%CHAT_ID%
echo === Upload attachment as alice ===
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-FileHash -Algorithm SHA256 -Path '%FIXTURE%').Hash.ToLower()"') do set ORIG_SHA=%%a
echo original sha256=%ORIG_SHA%
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 201 "upload attachment"
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).sha256"') do set ATT_SHA=%%a
echo attachment=%ATT_ID%
echo attachment sha256=%ATT_SHA%
if not "%ATT_SHA%"=="%ORIG_SHA%" (
echo FAIL: attachment sha256 mismatch
exit /b 1
)
echo === Create message with empty text and 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"
if errorlevel 1 exit /b 1
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\msg.json' -Raw | ConvertFrom-Json).id"') do set MSG_ID=%%a
echo message=%MSG_ID%
echo === Bob downloads attachment content ===
curl.exe -s -X GET "%BASE%/api/v1/attachments/%ATT_ID%/content" ^
-H "Authorization: Bearer %BOB_TOKEN%" ^
-o "%TMP%\download.png" ^
-w "%%{http_code}" > "%TMP%\dl.code"
call :check_code dl.code 200 "download content"
if errorlevel 1 exit /b 1
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-FileHash -Algorithm SHA256 -Path '%TMP%\download.png').Hash.ToLower()"') do set DL_SHA=%%a
echo downloaded sha256=%DL_SHA%
if not "%DL_SHA%"=="%ORIG_SHA%" (
echo FAIL: downloaded sha256 mismatch
exit /b 1
)
echo === Eve must get 404 on metadata ===
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
echo === Eve must get 404 on content ===
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.
echo ALL MANUAL ATTACHMENT 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
rem %1 username, %2 display_name, %3 prefix
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
rem %1 username, %2 prefix
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"
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~2.json' -Raw | ConvertFrom-Json).user.id"') do set "%~2_UID=%%a"
exit /b 0
:check_code
rem %1 code file, %2 expected status, %3 label
set /p RC=<"%TMP%\%~1"
if not "%RC%"=="%~2" (
echo FAIL: %~3 expected %~2, got %RC%
exit /b 1
)
exit /b 0