feat: port lab4 implementation
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
//go:build contract
|
||||
|
||||
package studenttests_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"io"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"mtusi2026-messenger/internal/dto"
|
||||
)
|
||||
|
||||
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)
|
||||
|
||||
body := bytes.Repeat([]byte{0x00}, maxUploadBytes+1)
|
||||
|
||||
resp := uploadAttachment(t, user, chat.ID, "big.png", "image/png", body)
|
||||
requireStatus(t, resp, http.StatusRequestEntityTooLarge)
|
||||
requireErrorCode(t, resp, dto.ErrorCodePayloadTooLarge)
|
||||
}
|
||||
|
||||
func TestAttachmentCorruptedImage(t *testing.T) {
|
||||
user := createUser(t)
|
||||
chat := createChat(t, user, user.ID)
|
||||
|
||||
body := []byte("this is definitely not a png or jpeg image")
|
||||
|
||||
resp := uploadAttachment(t, user, chat.ID, "broken.png", "image/png", body)
|
||||
requireStatus(t, resp, http.StatusUnsupportedMediaType)
|
||||
requireErrorCode(t, resp, dto.ErrorCodeUnsupportedMediaType)
|
||||
}
|
||||
|
||||
func TestAttachmentMimeSubstitution(t *testing.T) {
|
||||
user := createUser(t)
|
||||
chat := createChat(t, user, user.ID)
|
||||
|
||||
resp := uploadAttachment(t, user, chat.ID, "actually-png.jpg", "image/jpeg", makePNG(t, 32, 32))
|
||||
requireStatus(t, resp, http.StatusUnsupportedMediaType)
|
||||
requireErrorCode(t, resp, dto.ErrorCodeUnsupportedMediaType)
|
||||
}
|
||||
|
||||
func TestAttachmentForeignID(t *testing.T) {
|
||||
user := createUser(t)
|
||||
chat := createChat(t, user, user.ID)
|
||||
|
||||
foreign := uuid.New()
|
||||
|
||||
resp := doRequest(t, http.MethodGet, "/api/v1/attachments/"+foreign.String(), user.Token, nil)
|
||||
requireStatus(t, resp, http.StatusNotFound)
|
||||
requireErrorCode(t, resp, dto.ErrorCodeNotFound)
|
||||
|
||||
resp = doRequest(t, http.MethodGet, "/api/v1/attachments/"+foreign.String()+"/content", user.Token, nil)
|
||||
requireStatus(t, resp, http.StatusNotFound)
|
||||
requireErrorCode(t, resp, dto.ErrorCodeNotFound)
|
||||
|
||||
resp = postMessage(t, user, chat.ID, "", []uuid.UUID{foreign})
|
||||
requireStatus(t, resp, http.StatusNotFound)
|
||||
requireErrorCode(t, resp, dto.ErrorCodeNotFound)
|
||||
}
|
||||
|
||||
func TestAttachmentFromAnotherChat(t *testing.T) {
|
||||
user := createUser(t)
|
||||
member := createUser(t)
|
||||
|
||||
chatA := createChat(t, user, user.ID, member.ID)
|
||||
chatB := createChat(t, user, user.ID)
|
||||
|
||||
att := uploadPNG(t, user, chatA.ID)
|
||||
|
||||
// The attachment belongs to chatA; linking it to a chatB message must fail.
|
||||
resp := postMessage(t, user, chatB.ID, "", []uuid.UUID{att.ID})
|
||||
requireStatus(t, resp, http.StatusNotFound)
|
||||
requireErrorCode(t, resp, dto.ErrorCodeNotFound)
|
||||
}
|
||||
Reference in New Issue
Block a user