forked from mtusi2026/mtusi2026lab4
feat: attachments over S3 (grade 3)
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -15,6 +16,7 @@ import (
|
||||
"time"
|
||||
"uuid"
|
||||
|
||||
"messenger/internal/domain"
|
||||
"messenger/internal/handler"
|
||||
"messenger/internal/repository/postgres"
|
||||
"messenger/internal/repository/redis"
|
||||
@@ -22,6 +24,38 @@ import (
|
||||
"messenger/internal/service"
|
||||
)
|
||||
|
||||
type stubObjectStore struct {
|
||||
objects map[string][]byte
|
||||
}
|
||||
|
||||
func newStubObjectStore() *stubObjectStore {
|
||||
return &stubObjectStore{objects: map[string][]byte{}}
|
||||
}
|
||||
|
||||
func (s *stubObjectStore) Put(_ context.Context, key string, r io.Reader, _ int64, _ string) error {
|
||||
data, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.objects[key] = data
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *stubObjectStore) Get(_ context.Context, key string) (io.ReadCloser, error) {
|
||||
data, ok := s.objects[key]
|
||||
if !ok {
|
||||
return nil, domain.ErrNotFound
|
||||
}
|
||||
return io.NopCloser(strings.NewReader(string(data))), nil
|
||||
}
|
||||
|
||||
func (s *stubObjectStore) Delete(_ context.Context, key string) error {
|
||||
delete(s.objects, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *stubObjectStore) Ping(context.Context) error { return nil }
|
||||
|
||||
func testURLs(t *testing.T) (string, string) {
|
||||
t.Helper()
|
||||
dbURL, redisURL := os.Getenv("TEST_DATABASE_URL"), os.Getenv("TEST_REDIS_URL")
|
||||
@@ -56,7 +90,8 @@ func newStack(t *testing.T, dbURL, redisURL string, ttl time.Duration) *httptest
|
||||
auth := service.NewAuthService(store, sessions, ttl)
|
||||
chats := service.NewChatService(store)
|
||||
messages := service.NewMessageService(chats, store)
|
||||
h := handler.New(users, auth, chats, messages, []handler.Pinger{store.Ping, sessions.Ping}, slog.New(slog.DiscardHandler))
|
||||
attachments := service.NewAttachmentService(chats, store, newStubObjectStore(), 10485760, 40000000)
|
||||
h := handler.New(users, auth, chats, messages, attachments, []handler.Pinger{store.Ping, sessions.Ping}, slog.New(slog.DiscardHandler))
|
||||
srv := httptest.NewServer(router.New(h, "test"))
|
||||
t.Cleanup(srv.Close)
|
||||
return srv
|
||||
|
||||
Reference in New Issue
Block a user