forked from mtusi2026/mtusi2026lab5
Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4e1a74d9ff | ||
|
|
f8a624a40b | ||
|
|
9ccae0c0c3 |
@@ -208,18 +208,16 @@ func (s *Server) messages(w http.ResponseWriter, r *http.Request) *apiError {
|
|||||||
if !valid {
|
if !valid {
|
||||||
return invalidRequest()
|
return invalidRequest()
|
||||||
}
|
}
|
||||||
var input struct {
|
var input createMessageRequest
|
||||||
Text string `json:"text"`
|
|
||||||
AttachmentIDs []string `json:"attachment_ids"`
|
|
||||||
}
|
|
||||||
if err := readJSON(w, r, &input); err != nil {
|
if err := readJSON(w, r, &input); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !validMessage(input.Text, input.AttachmentIDs) || !canonicalAttachmentIDs(input.AttachmentIDs) {
|
text, attachmentIDs, valid := input.values()
|
||||||
|
if !valid || !validMessage(text, attachmentIDs) || !canonicalAttachmentIDs(attachmentIDs) {
|
||||||
return invalidRequest()
|
return invalidRequest()
|
||||||
}
|
}
|
||||||
message, replay, err := s.messageRepository.CreateMessage(
|
message, replay, err := s.messageRepository.CreateMessage(
|
||||||
r.Context(), chatID, user.ID, input.Text, input.AttachmentIDs, key, s.idempotencyTTL,
|
r.Context(), chatID, user.ID, text, attachmentIDs, key, s.idempotencyTTL,
|
||||||
)
|
)
|
||||||
if errors.Is(err, model.ErrNotFound) {
|
if errors.Is(err, model.ErrNotFound) {
|
||||||
return notFound()
|
return notFound()
|
||||||
|
|||||||
@@ -20,6 +20,26 @@ const maxRequestBytes = 1 << 20
|
|||||||
|
|
||||||
var idempotencyKeyPattern = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`)
|
var idempotencyKeyPattern = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`)
|
||||||
|
|
||||||
|
type createMessageRequest struct {
|
||||||
|
Text json.RawMessage `json:"text"`
|
||||||
|
AttachmentIDs json.RawMessage `json:"attachment_ids"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r createMessageRequest) values() (string, []string, bool) {
|
||||||
|
var text *string
|
||||||
|
if len(r.Text) == 0 || json.Unmarshal(r.Text, &text) != nil || text == nil {
|
||||||
|
return "", nil, false
|
||||||
|
}
|
||||||
|
if len(r.AttachmentIDs) == 0 {
|
||||||
|
return *text, nil, true
|
||||||
|
}
|
||||||
|
var attachmentIDs *[]string
|
||||||
|
if json.Unmarshal(r.AttachmentIDs, &attachmentIDs) != nil || attachmentIDs == nil {
|
||||||
|
return "", nil, false
|
||||||
|
}
|
||||||
|
return *text, *attachmentIDs, true
|
||||||
|
}
|
||||||
|
|
||||||
func readJSON(w http.ResponseWriter, r *http.Request, target any) *apiError {
|
func readJSON(w http.ResponseWriter, r *http.Request, target any) *apiError {
|
||||||
mediaType, params, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
mediaType, params, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
||||||
if err != nil || mediaType != "application/json" || (params["charset"] != "" && !strings.EqualFold(params["charset"], "utf-8")) {
|
if err != nil || mediaType != "application/json" || (params["charset"] != "" && !strings.EqualFold(params["charset"], "utf-8")) {
|
||||||
|
|||||||
@@ -263,6 +263,47 @@ func (s *MessengerSuite) TestMessageCreationValidatesIdempotencyKey() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *MessengerSuite) TestMessageCreationValidatesFieldPresenceAndTypes() {
|
||||||
|
store := messengerStoreStub{userByID: model.User{ID: "user-id"}}
|
||||||
|
server := s.newTestServer(store, sessionStub{userID: "user-id"}, passwordsStub{})
|
||||||
|
attachmentID := "00000000-0000-0000-0000-000000000002"
|
||||||
|
invalidBodies := []string{
|
||||||
|
`{"attachment_ids":["` + attachmentID + `"]}`,
|
||||||
|
`{"text":null,"attachment_ids":["` + attachmentID + `"]}`,
|
||||||
|
`{"text":"x","attachment_ids":null}`,
|
||||||
|
`{"text":1}`,
|
||||||
|
`{"text":"x","attachment_ids":{}}`,
|
||||||
|
}
|
||||||
|
for _, body := range invalidBodies {
|
||||||
|
response := createMessageRequestResponse(server, body)
|
||||||
|
s.Equal(http.StatusBadRequest, response.Code, body)
|
||||||
|
}
|
||||||
|
|
||||||
|
validBodies := []string{
|
||||||
|
`{"text":"message"}`,
|
||||||
|
`{"text":"message","attachment_ids":[]}`,
|
||||||
|
`{"text":"","attachment_ids":["` + attachmentID + `"]}`,
|
||||||
|
}
|
||||||
|
for _, body := range validBodies {
|
||||||
|
response := createMessageRequestResponse(server, body)
|
||||||
|
s.Equal(http.StatusCreated, response.Code, body)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func createMessageRequestResponse(server *Server, body string) *httptest.ResponseRecorder {
|
||||||
|
request := httptest.NewRequest(
|
||||||
|
http.MethodPost,
|
||||||
|
"/api/v1/chats/00000000-0000-0000-0000-000000000001/messages",
|
||||||
|
strings.NewReader(body),
|
||||||
|
)
|
||||||
|
request.Header.Set("Authorization", "Bearer valid-token")
|
||||||
|
request.Header.Set("Content-Type", "application/json")
|
||||||
|
request.Header.Set("Idempotency-Key", "message-key")
|
||||||
|
response := httptest.NewRecorder()
|
||||||
|
server.Handler().ServeHTTP(response, request)
|
||||||
|
return response
|
||||||
|
}
|
||||||
|
|
||||||
func (s *MessengerSuite) TestMessageCreationReturnsConflictForReusedKeyWithDifferentBody() {
|
func (s *MessengerSuite) TestMessageCreationReturnsConflictForReusedKeyWithDifferentBody() {
|
||||||
t := s.T()
|
t := s.T()
|
||||||
store := messengerStoreStub{
|
store := messengerStoreStub{
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import (
|
|||||||
"gitea.privcloud.tech/dizi/mtusi2026lab5/internal/database"
|
"gitea.privcloud.tech/dizi/mtusi2026lab5/internal/database"
|
||||||
"gitea.privcloud.tech/dizi/mtusi2026lab5/internal/model"
|
"gitea.privcloud.tech/dizi/mtusi2026lab5/internal/model"
|
||||||
"gitea.privcloud.tech/dizi/mtusi2026lab5/internal/postgres"
|
"gitea.privcloud.tech/dizi/mtusi2026lab5/internal/postgres"
|
||||||
|
"gitea.privcloud.tech/dizi/mtusi2026lab5/internal/reconcile"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PostgresSuite struct {
|
type PostgresSuite struct {
|
||||||
@@ -412,6 +413,43 @@ func (s *PostgresSuite) TestAttachmentDeletionClaimChecksLifecycleUnderLock() {
|
|||||||
s.Equal(model.AttachmentReady, visible.Status)
|
s.Equal(model.AttachmentReady, visible.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *PostgresSuite) TestReconciliationDryRunPreservesDeletingAttachment() {
|
||||||
|
owner, chatID := createOwnerAndChat(s.T(), s.store)
|
||||||
|
pending, err := s.store.CreatePendingAttachment(context.Background(), model.Attachment{
|
||||||
|
ID: uuid.NewString(), ChatID: chatID, OwnerID: owner.ID, Filename: "deleting.png",
|
||||||
|
ContentType: "image/png", SizeBytes: 128, SHA256: strings.Repeat("e", 64),
|
||||||
|
ObjectKey: "attachments/" + uuid.NewString() + "/original",
|
||||||
|
})
|
||||||
|
s.Require().NoError(err)
|
||||||
|
claimed, err := s.store.ClaimAttachmentForDeletion(context.Background(), pending, time.Now().Add(time.Hour))
|
||||||
|
s.Require().NoError(err)
|
||||||
|
s.True(claimed)
|
||||||
|
|
||||||
|
reconciler, err := reconcile.New(s.store, emptyObjectStore{}, reconcile.Config{
|
||||||
|
Prefix: "attachments/", PageSize: 1000, GraceTime: time.Minute,
|
||||||
|
UnreferencedRetention: time.Hour,
|
||||||
|
})
|
||||||
|
s.Require().NoError(err)
|
||||||
|
report, err := reconciler.Run(context.Background())
|
||||||
|
s.Require().NoError(err)
|
||||||
|
s.Zero(report.DeletedUnreferencedRows)
|
||||||
|
|
||||||
|
var status string
|
||||||
|
err = s.pool.QueryRow(context.Background(), "SELECT status FROM attachments WHERE id = $1", pending.ID).Scan(&status)
|
||||||
|
s.Require().NoError(err)
|
||||||
|
s.Equal(model.AttachmentDeleting, status)
|
||||||
|
}
|
||||||
|
|
||||||
|
type emptyObjectStore struct{}
|
||||||
|
|
||||||
|
func (emptyObjectStore) List(context.Context, string, string, int) ([]model.StoredObject, string, error) {
|
||||||
|
return nil, "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (emptyObjectStore) Delete(context.Context, string) error {
|
||||||
|
return errors.New("unexpected object deletion")
|
||||||
|
}
|
||||||
|
|
||||||
func (s *PostgresSuite) TestMessageRejectsAttachmentFromAnotherChat() {
|
func (s *PostgresSuite) TestMessageRejectsAttachmentFromAnotherChat() {
|
||||||
owner, firstChatID := createOwnerAndChat(s.T(), s.store)
|
owner, firstChatID := createOwnerAndChat(s.T(), s.store)
|
||||||
secondChat, err := s.store.CreateChat(context.Background(), "Second", []string{owner.ID}, owner.ID)
|
secondChat, err := s.store.CreateChat(context.Background(), "Second", []string{owner.ID}, owner.ID)
|
||||||
|
|||||||
@@ -283,6 +283,7 @@ func (r *Reconciler) handleMissingObject(
|
|||||||
return r.deletePair(ctx, metadata, cutoff, report)
|
return r.deletePair(ctx, metadata, cutoff, report)
|
||||||
}
|
}
|
||||||
if metadata.Status == model.AttachmentDeleting {
|
if metadata.Status == model.AttachmentDeleting {
|
||||||
|
report.DeletingAttachments++
|
||||||
return r.deleteClaimedMetadata(ctx, metadata, report)
|
return r.deleteClaimedMetadata(ctx, metadata, report)
|
||||||
}
|
}
|
||||||
if metadata.CreatedAt.After(cutoff) {
|
if metadata.CreatedAt.After(cutoff) {
|
||||||
@@ -308,6 +309,9 @@ func (r *Reconciler) handleMissingObject(
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Reconciler) deleteClaimedMetadata(ctx context.Context, metadata model.Attachment, report *Report) error {
|
func (r *Reconciler) deleteClaimedMetadata(ctx context.Context, metadata model.Attachment, report *Report) error {
|
||||||
|
if !r.config.Apply {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
deleted, err := r.metadata.DeleteAttachmentIfDeleting(ctx, metadata.ID)
|
deleted, err := r.metadata.DeleteAttachmentIfDeleting(ctx, metadata.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("delete missing attachment metadata %q: %w", metadata.ID, err)
|
return fmt.Errorf("delete missing attachment metadata %q: %w", metadata.ID, err)
|
||||||
|
|||||||
@@ -82,6 +82,54 @@ func TestReconcilerRechecksOrphanBeforeDelete(t *testing.T) {
|
|||||||
require.Contains(t, objects.items, "attachments/a/original")
|
require.Contains(t, objects.items, "attachments/a/original")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReconcilerDeletingAttachmentRespectsApplyMode(t *testing.T) {
|
||||||
|
now := time.Now().UTC()
|
||||||
|
key := "attachments/a/original"
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
apply bool
|
||||||
|
objectPresent bool
|
||||||
|
}{
|
||||||
|
{name: "dry run with object", objectPresent: true},
|
||||||
|
{name: "dry run without object"},
|
||||||
|
{name: "apply with object", apply: true, objectPresent: true},
|
||||||
|
{name: "apply without object", apply: true},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
metadata := &metadataStub{items: map[string]model.Attachment{
|
||||||
|
key: attachment("a", key, model.AttachmentDeleting, now.Add(-time.Hour), false),
|
||||||
|
}}
|
||||||
|
objects := &objectStub{items: make(map[string]model.StoredObject)}
|
||||||
|
if test.objectPresent {
|
||||||
|
objects.items[key] = model.StoredObject{Key: key, LastModified: now.Add(-time.Hour)}
|
||||||
|
}
|
||||||
|
reconciler, err := New(metadata, objects, Config{
|
||||||
|
Prefix: "attachments/", PageSize: 1, GraceTime: time.Minute,
|
||||||
|
UnreferencedRetention: time.Hour, Apply: test.apply, Now: func() time.Time { return now },
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
report, err := reconciler.Run(context.Background())
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, 1, report.DeletingAttachments)
|
||||||
|
var expectedDeletedRows, expectedDeletedObjects int
|
||||||
|
if test.apply {
|
||||||
|
expectedDeletedRows = 1
|
||||||
|
}
|
||||||
|
if test.apply && test.objectPresent {
|
||||||
|
expectedDeletedObjects = 1
|
||||||
|
}
|
||||||
|
require.Equal(t, expectedDeletedRows, report.DeletedUnreferencedRows)
|
||||||
|
require.Equal(t, expectedDeletedObjects, report.DeletedAttachmentObjects)
|
||||||
|
_, metadataExists := metadata.items[key]
|
||||||
|
require.Equal(t, !test.apply, metadataExists)
|
||||||
|
_, objectExists := objects.items[key]
|
||||||
|
require.Equal(t, test.objectPresent && !test.apply, objectExists)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestReconcilerDoesNotReportTrackedStagingObjectAsOrphan(t *testing.T) {
|
func TestReconcilerDoesNotReportTrackedStagingObjectAsOrphan(t *testing.T) {
|
||||||
now := time.Now().UTC()
|
now := time.Now().UTC()
|
||||||
key := "staging/a/original"
|
key := "staging/a/original"
|
||||||
|
|||||||
Reference in New Issue
Block a user