docs: add evidence for grade 3 requirements

This commit is contained in:
kk10
2026-11-04 17:26:36 +03:00
parent 94afb53e05
commit b266d634c0
6 changed files with 136 additions and 0 deletions
+61
View File
@@ -5,6 +5,9 @@ package studenttests_test
import (
"bytes"
"encoding/json"
"image/jpeg"
"image/png"
"io"
"net/http"
"net/url"
"testing"
@@ -105,6 +108,64 @@ func requireErrorCode(t *testing.T, resp *http.Response, code dto.ErrorCode) {
}
}
func TestAttachmentJPEG(t *testing.T) {
user := createUser(t)
chat := createChat(t, user, user.ID)
img, err := png.Decode(bytes.NewReader(makePNG(t, 32, 32)))
if err != nil {
t.Fatalf("decode png: %v", err)
}
var body bytes.Buffer
if err := jpeg.Encode(&body, img, nil); err != nil {
t.Fatalf("encode jpeg: %v", err)
}
resp := uploadAttachment(t, user, chat.ID, "sample.jpg", "image/jpeg", body.Bytes())
requireStatus(t, resp, http.StatusCreated)
var att dto.AttachmentResponse
mustDecode(t, resp, &att)
if att.Status != "ready" {
t.Fatalf("expected ready attachment, got %q", att.Status)
}
resp = doRequest(t, http.MethodGet, "/api/v1/attachments/"+att.ID.String()+"/content", user.Token, nil)
requireStatus(t, resp, http.StatusOK)
defer resp.Body.Close()
original, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("read original: %v", err)
}
if !bytes.Equal(original, body.Bytes()) {
t.Error("downloaded JPEG differs from uploaded original")
}
}
func TestAttachmentTooManyPixels(t *testing.T) {
user := createUser(t)
chat := createChat(t, user, user.ID)
body := makePNG(t, 8000, 5001)
if len(body) > maxUploadBytes {
t.Fatal("pixel-limit fixture exceeds upload byte limit")
}
resp := uploadAttachment(t, user, chat.ID, "large.png", "image/png", body)
requireStatus(t, resp, http.StatusRequestEntityTooLarge)
requireErrorCode(t, resp, dto.ErrorCodePayloadTooLarge)
}
func TestAttachmentUnsafeFilename(t *testing.T) {
user := createUser(t)
chat := createChat(t, user, user.ID)
resp := uploadAttachment(t, user, chat.ID, "../image.png", "image/png", makePNG(t, 32, 32))
requireStatus(t, resp, http.StatusBadRequest)
requireErrorCode(t, resp, dto.ErrorCodeInvalidRequest)
}
func TestAttachmentTooLarge(t *testing.T) {
user := createUser(t)
chat := createChat(t, user, user.ID)