lab4: исправления по ревью

This commit is contained in:
Vladislav Kunetskiy
2026-10-21 23:25:11 +03:00
parent 3389c6c900
commit e9f0fde6f2
32 changed files with 451 additions and 131 deletions
+45 -2
View File
@@ -112,7 +112,7 @@ func (f *fakeStore) List(_ context.Context, prefix string) ([]objectstore.Object
return objects, nil
}
func (f *fakeStore) PresignPut(_ context.Context, key string, ttl time.Duration) (string, time.Time, error) {
func (f *fakeStore) PresignPut(_ context.Context, key string, _ int64, ttl time.Duration) (string, time.Time, error) {
return "http://s3.example/" + key, time.Now().UTC().Add(ttl), nil
}
@@ -156,7 +156,7 @@ func TestValidateFilename(t *testing.T) {
require.NoError(t, attachment.ValidateFilename(name), name)
}
invalid := []string{"", strings.Repeat("x", 256), "a/b.png", `a\b.png`, "a\x00b.png", "a\nb.png"}
invalid := []string{"", " ", "\t\n", strings.Repeat("x", 256), "a/b.png", `a\b.png`, "a\x00b.png", "a\nb.png"}
for _, name := range invalid {
require.ErrorIs(t, attachment.ValidateFilename(name), domain.ErrValidation, name)
}
@@ -316,3 +316,46 @@ func TestCompleteRejectsChecksumMismatch(t *testing.T) {
require.ErrorIs(t, err, domain.ErrValidation)
require.NotContains(t, store.objects, stagingKey)
}
func TestCompleteKeepsObjectOnWrongRequestChecksum(t *testing.T) {
repo := mocks.NewMockRepository(t)
store := newFakeStore()
service := attachment.NewService(repo, store, serviceConfig(), slog.Default())
id := uuid.New()
chatID := uuid.New()
ownerID := uuid.New()
content := pngBytes(t, 10, 10)
sum := sha256.Sum256(content)
digest := hex.EncodeToString(sum[:])
stagingKey := "staging/presign/" + chatID.String() + "/" + id.String()
store.objects[stagingKey] = storedObject{
data: content,
contentType: "image/png",
metadata: map[string]string{"sha256": digest},
}
pending := domain.Attachment{
ID: id, ChatID: chatID, OwnerID: ownerID, ObjectKey: stagingKey,
ContentType: "image/png", SizeBytes: int64(len(content)), SHA256: digest,
Status: domain.AttachmentPending,
}
repo.EXPECT().GetForOwner(mock.Anything, ownerID, id).Return(pending, nil).Once()
_, err := service.Complete(t.Context(), ownerID, id, strings.Repeat("0", 64))
require.ErrorIs(t, err, domain.ErrConflict)
require.Contains(t, store.objects, stagingKey)
}
func TestPresignRejectsNonPositiveSize(t *testing.T) {
repo := mocks.NewMockRepository(t)
service := attachment.NewService(repo, newFakeStore(), serviceConfig(), slog.Default())
_, err := service.Presign(t.Context(), domain.PresignRequest{
ChatID: uuid.New(), OwnerID: uuid.New(),
Filename: "photo.png", ContentType: "image/png",
SizeBytes: 0, SHA256: strings.Repeat("a", 64),
})
require.ErrorIs(t, err, domain.ErrValidation)
}