forked from mtusi2026/mtusi2026lab6
ЛР 6 — БПИ2302 — Нурлыгаянова — на 3
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/domain"
|
||||
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/janitor"
|
||||
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/objectstore"
|
||||
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/store"
|
||||
)
|
||||
|
||||
type signedMemory struct{ *objectstore.Memory }
|
||||
|
||||
func (s signedMemory) UploadMAC(b []byte) []byte {
|
||||
h := sha256.Sum256(append([]byte("test-key"), b...))
|
||||
return h[:]
|
||||
}
|
||||
func (s signedMemory) PresignUpload(_ context.Context, key, _ string, _ int64) (string, error) {
|
||||
return key, nil
|
||||
}
|
||||
|
||||
func TestDirectSizeExpiryAndCleanup(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
repo := store.New()
|
||||
alice, err := repo.CreateUser(ctx, "alice", "Alice", "hash")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
chat, err := repo.CreateChat(ctx, alice.ID, "chat", []string{alice.ID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
objects := signedMemory{objectstore.NewMemory()}
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
svc := NewAttachments(repo, objects, AttachmentLimits{MaxUploadBytes: 1024, MaxImagePixels: 100, MaxConcurrentUploads: 2}, logger)
|
||||
data := []byte("1234")
|
||||
sum := sha256.Sum256(data)
|
||||
u, err := svc.InitDirect(ctx, alice.ID, chat.ID, DirectInput{Filename: "x.png", ContentType: "image/png", SizeBytes: 4, SHA256: hex.EncodeToString(sum[:])})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
objects.PutAt(u.URL, data[:3], time.Now())
|
||||
if _, err = svc.CompleteDirect(ctx, alice.ID, chat.ID, u.Token); !errors.Is(err, domain.ErrInvalidArgument) {
|
||||
t.Fatalf("wrong size: %v", err)
|
||||
}
|
||||
// Expire a correctly authenticated ticket without sleeping in a unit test.
|
||||
encoded := bytes.Split([]byte(u.Token), []byte("."))[0]
|
||||
raw, _ := base64.RawURLEncoding.DecodeString(string(encoded))
|
||||
var ticket directTicket
|
||||
if err = json.Unmarshal(raw, &ticket); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ticket.Expires = time.Now().Add(-time.Second).Unix()
|
||||
raw, _ = json.Marshal(ticket)
|
||||
token := base64.RawURLEncoding.EncodeToString(raw) + "." + base64.RawURLEncoding.EncodeToString(objects.UploadMAC(raw))
|
||||
if _, err = svc.CompleteDirect(ctx, alice.ID, chat.ID, token); !errors.Is(err, domain.ErrConflict) {
|
||||
t.Fatalf("expired: %v", err)
|
||||
}
|
||||
future := time.Now().Add(6 * time.Minute)
|
||||
report, err := janitor.Run(ctx, objects, noMetadata{}, janitor.Options{Prefix: ObjectKeyPrefix, Grace: 5 * time.Minute, MaxDeletes: 100, Now: func() time.Time { return future }, Logger: logger})
|
||||
if err != nil || report.Deleted != 1 {
|
||||
t.Fatalf("cleanup: %+v %v", report, err)
|
||||
}
|
||||
if _, _, err = objects.Get(ctx, u.URL); !errors.Is(err, objectstore.ErrNotFound) {
|
||||
t.Fatalf("abandoned staging remains: %v", err)
|
||||
}
|
||||
report, err = janitor.Run(ctx, objects, noMetadata{}, janitor.Options{Prefix: ObjectKeyPrefix, Grace: 5 * time.Minute, MaxDeletes: 100, Now: func() time.Time { return future }, Logger: logger})
|
||||
if err != nil || report.Deleted != 0 {
|
||||
t.Fatalf("repeat cleanup: %+v %v", report, err)
|
||||
}
|
||||
}
|
||||
|
||||
type noMetadata struct{}
|
||||
|
||||
func (noMetadata) ExistingObjectKeys(context.Context, []string) (map[string]struct{}, error) {
|
||||
return map[string]struct{}{}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user