feat: port lab4 implementation
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
//go:build contract || reconcile || failure
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user