forked from mtusi2026/mtusi2026lab7
на оценку 4
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
package student_tests
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"mtusi2026lab7/internal/domain"
|
||||
)
|
||||
|
||||
func TestMessageAttachmentContract(t *testing.T) {
|
||||
memoryStore, serverURL, user, chatID := createMessageFixture(t)
|
||||
|
||||
readyID := "11111111-1111-4111-8111-111111111111"
|
||||
if err := memoryStore.PutAttachment(domain.Attachment{
|
||||
ID: readyID,
|
||||
ChatID: chatID,
|
||||
OwnerID: user.ID,
|
||||
Status: domain.AttachmentStatusReady,
|
||||
}); err != nil {
|
||||
t.Fatalf("seed ready attachment: %v", err)
|
||||
}
|
||||
|
||||
status, payload := sendMessageRequest(
|
||||
t,
|
||||
serverURL,
|
||||
chatID,
|
||||
user.ID,
|
||||
"attachment-only",
|
||||
fmt.Sprintf(`{"text":"","attachment_ids":[%q]}`, readyID),
|
||||
)
|
||||
if status != http.StatusCreated {
|
||||
t.Fatalf("attachment-only message: expected 201, got %d: %s", status, payload)
|
||||
}
|
||||
|
||||
message := decodeMessageResponse(t, payload)
|
||||
if message.Text != "" {
|
||||
t.Fatalf("attachment-only message changed text: %q", message.Text)
|
||||
}
|
||||
if !reflect.DeepEqual(message.AttachmentIDs, []string{readyID}) {
|
||||
t.Fatalf("attachment_ids changed: %#v", message.AttachmentIDs)
|
||||
}
|
||||
|
||||
items, _, err := memoryStore.GetMessages(chatID, user.ID, 10, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("read stored messages: %v", err)
|
||||
}
|
||||
if len(items) != 1 || !reflect.DeepEqual(items[0].AttachmentIDs, []string{readyID}) {
|
||||
t.Fatalf("stored attachment relation differs: %#v", items)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessageAttachmentValidationAndAccess(t *testing.T) {
|
||||
memoryStore, serverURL, user, chatID := createMessageFixture(t)
|
||||
otherChat, err := memoryStore.CreateChat("Other chat", []string{user.ID})
|
||||
if err != nil {
|
||||
t.Fatalf("create other chat: %v", err)
|
||||
}
|
||||
|
||||
pendingID := "22222222-2222-4222-8222-222222222222"
|
||||
if err = memoryStore.PutAttachment(domain.Attachment{
|
||||
ID: pendingID,
|
||||
ChatID: chatID,
|
||||
OwnerID: user.ID,
|
||||
Status: domain.AttachmentStatusPending,
|
||||
}); err != nil {
|
||||
t.Fatalf("seed pending attachment: %v", err)
|
||||
}
|
||||
|
||||
foreignID := "33333333-3333-4333-8333-333333333333"
|
||||
if err = memoryStore.PutAttachment(domain.Attachment{
|
||||
ID: foreignID,
|
||||
ChatID: otherChat.ID,
|
||||
OwnerID: user.ID,
|
||||
Status: domain.AttachmentStatusReady,
|
||||
}); err != nil {
|
||||
t.Fatalf("seed foreign attachment: %v", err)
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
body string
|
||||
wantStatus int
|
||||
}{
|
||||
{
|
||||
name: "empty without attachments",
|
||||
body: `{"text":"","attachment_ids":[]}`,
|
||||
wantStatus: http.StatusBadRequest,
|
||||
},
|
||||
{
|
||||
name: "invalid attachment uuid",
|
||||
body: `{"text":"hello","attachment_ids":["not-a-uuid"]}`,
|
||||
wantStatus: http.StatusBadRequest,
|
||||
},
|
||||
{
|
||||
name: "duplicate attachment",
|
||||
body: fmt.Sprintf(`{"text":"hello","attachment_ids":[%q,%q]}`, pendingID, pendingID),
|
||||
wantStatus: http.StatusBadRequest,
|
||||
},
|
||||
{
|
||||
name: "unknown attachment",
|
||||
body: `{"text":"hello","attachment_ids":["44444444-4444-4444-8444-444444444444"]}`,
|
||||
wantStatus: http.StatusNotFound,
|
||||
},
|
||||
{
|
||||
name: "attachment from another chat",
|
||||
body: fmt.Sprintf(`{"text":"hello","attachment_ids":[%q]}`, foreignID),
|
||||
wantStatus: http.StatusNotFound,
|
||||
},
|
||||
{
|
||||
name: "attachment not ready",
|
||||
body: fmt.Sprintf(`{"text":"hello","attachment_ids":[%q]}`, pendingID),
|
||||
wantStatus: http.StatusConflict,
|
||||
},
|
||||
}
|
||||
|
||||
for index, testCase := range testCases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
status, payload := sendMessageRequest(
|
||||
t,
|
||||
serverURL,
|
||||
chatID,
|
||||
user.ID,
|
||||
fmt.Sprintf("attachment-validation-%d", index),
|
||||
testCase.body,
|
||||
)
|
||||
if status != testCase.wantStatus {
|
||||
t.Fatalf("expected %d, got %d: %s", testCase.wantStatus, status, payload)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user