forked from mtusi2026/mtusi2026lab5
103 lines
4.1 KiB
Go
103 lines
4.1 KiB
Go
package fiber
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab5/internal/model/dto"
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab5/internal/service"
|
|
)
|
|
|
|
type attachmentFixture struct {
|
|
attachment dto.Attachment
|
|
body []byte
|
|
err error
|
|
}
|
|
|
|
// Upload returns the configured application outcome for an HTTP mapping scenario.
|
|
func (f attachmentFixture) Upload(context.Context, string, string, string, string, []byte) (dto.Attachment, error) {
|
|
return f.attachment, f.err
|
|
}
|
|
|
|
// Get returns the configured metadata outcome for an HTTP mapping scenario.
|
|
func (f attachmentFixture) Get(context.Context, string, string) (dto.Attachment, error) {
|
|
return f.attachment, f.err
|
|
}
|
|
|
|
// Download returns a stream that fails if the request context is canceled before transmission.
|
|
func (f attachmentFixture) Download(ctx context.Context, _, _, _ string) (dto.Attachment, io.ReadCloser, error) {
|
|
return f.attachment, &contextReader{Reader: bytes.NewReader(f.body), ctx: ctx}, f.err
|
|
}
|
|
|
|
type contextReader struct {
|
|
*bytes.Reader
|
|
ctx context.Context
|
|
}
|
|
|
|
// Read verifies the admission context survives until the response is actually read.
|
|
func (r *contextReader) Read(p []byte) (int, error) {
|
|
if err := r.ctx.Err(); err != nil {
|
|
return 0, err
|
|
}
|
|
return r.Reader.Read(p)
|
|
}
|
|
|
|
// Close makes the synthetic response stream an owned closer.
|
|
func (r *contextReader) Close() error {
|
|
return nil
|
|
}
|
|
|
|
// TestAttachmentHTTPMapping exercises early errors, successful metadata and byte-exact streaming.
|
|
func TestAttachmentHTTPMapping(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
method string
|
|
path string
|
|
fixture *attachmentFixture
|
|
status int
|
|
}{
|
|
{"upload unavailable", "POST", "/api/v1/chats/test/attachments", nil, http.StatusServiceUnavailable},
|
|
{"metadata unavailable", "GET", "/api/v1/attachments/test", nil, http.StatusServiceUnavailable},
|
|
{"download unavailable", "GET", "/api/v1/attachments/test/content", nil, http.StatusServiceUnavailable},
|
|
{"upload too large", "POST", "/api/v1/chats/test/attachments", &attachmentFixture{err: service.ErrTooLarge}, http.StatusRequestEntityTooLarge},
|
|
{"upload unsupported", "POST", "/api/v1/chats/test/attachments", &attachmentFixture{err: service.ErrMedia}, http.StatusUnsupportedMediaType},
|
|
{"metadata missing", "GET", "/api/v1/attachments/test", &attachmentFixture{err: service.ErrNotFound}, http.StatusNotFound},
|
|
{"download missing", "GET", "/api/v1/attachments/test/content", &attachmentFixture{err: service.ErrNotFound}, http.StatusNotFound},
|
|
{"upload queued", "POST", "/api/v1/chats/test/attachments", &attachmentFixture{attachment: dto.Attachment{ID: uuid.New(), Status: "queued"}}, http.StatusAccepted},
|
|
{"metadata ready", "GET", "/api/v1/attachments/test", &attachmentFixture{attachment: dto.Attachment{ID: uuid.New(), Status: "ready"}}, http.StatusOK},
|
|
{"download stream", "GET", "/api/v1/attachments/test/content", &attachmentFixture{attachment: dto.Attachment{SizeBytes: 4, ContentType: "image/png"}, body: []byte{0, 1, 2, 3}}, http.StatusOK},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
ctrl := testController(t, nil)
|
|
user := call(t, ctrl, http.MethodPost, "/api/v1/users", "", map[string]any{"username": "test_user", "display_name": "Test", "password": "synthetic-password"}, http.StatusCreated)
|
|
if tc.fixture != nil {
|
|
ctrl.WithAttachments(*tc.fixture)
|
|
}
|
|
req := httptest.NewRequest(tc.method, tc.path, bytes.NewReader([]byte("fixture")))
|
|
req.Header.Set("Authorization", "Bearer "+user["id"].(string))
|
|
req.Header.Set("Content-Type", "image/png")
|
|
response, err := ctrl.server.Test(req, 10000)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body, err := io.ReadAll(response.Body)
|
|
if closeErr := response.Body.Close(); closeErr != nil {
|
|
t.Fatal(closeErr)
|
|
}
|
|
if err != nil || response.StatusCode != tc.status {
|
|
t.Fatalf("response status=%d err=%v", response.StatusCode, err)
|
|
}
|
|
if tc.fixture != nil && tc.fixture.body != nil && !bytes.Equal(body, tc.fixture.body) {
|
|
t.Fatalf("stream changed: %v", body)
|
|
}
|
|
})
|
|
}
|
|
}
|