Files
mtusi2026lab4/student-tests/attachment_test.go
T

172 lines
4.3 KiB
Go

//go:build contract
package studenttests_test
import (
"bytes"
"encoding/json"
"net/http"
"net/url"
"testing"
"github.com/google/uuid"
"mtusi2026-messenger/internal/dto"
)
const maxUploadBytes = 10 * 1024 * 1024
func uploadAttachment(
t *testing.T,
actor testUser,
chatID uuid.UUID,
filename string,
contentType string,
body []byte,
) *http.Response {
t.Helper()
req, err := http.NewRequest(
http.MethodPost,
*baseURL+"/api/v1/chats/"+chatID.String()+"/attachments?filename="+url.QueryEscape(filename),
bytes.NewReader(body),
)
if err != nil {
t.Fatalf("build upload request: %v", err)
}
req.Header.Set("Content-Type", contentType)
req.Header.Set("Authorization", "Bearer "+actor.Token)
resp, err := httpClient.Do(req)
if err != nil {
t.Fatalf("upload request failed: %v", err)
}
return resp
}
func uploadPNG(t *testing.T, actor testUser, chatID uuid.UUID) dto.AttachmentResponse {
t.Helper()
resp := uploadAttachment(t, actor, chatID, "sample.png", "image/png", makePNG(t, 32, 32))
requireStatus(t, resp, http.StatusCreated)
var att dto.AttachmentResponse
mustDecode(t, resp, &att)
return att
}
func postMessage(
t *testing.T,
actor testUser,
chatID uuid.UUID,
text string,
attachmentIDs []uuid.UUID,
) *http.Response {
t.Helper()
body, err := json.Marshal(map[string]any{
"text": text,
"attachment_ids": attachmentIDs,
})
if err != nil {
t.Fatalf("marshal message: %v", err)
}
req, err := http.NewRequest(
http.MethodPost,
*baseURL+"/api/v1/chats/"+chatID.String()+"/messages",
bytes.NewReader(body),
)
if err != nil {
t.Fatalf("build message request: %v", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+actor.Token)
req.Header.Set("Idempotency-Key", uuid.NewString())
resp, err := httpClient.Do(req)
if err != nil {
t.Fatalf("message request failed: %v", err)
}
return resp
}
func requireErrorCode(t *testing.T, resp *http.Response, code dto.ErrorCode) {
t.Helper()
var apiErr dto.ErrorResponse
mustDecode(t, resp, &apiErr)
if apiErr.Error.Code != code {
t.Errorf("expected error code %q, got %q", code, apiErr.Error.Code)
}
}
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)
}