Skip to content

Commit

Permalink
CBG-4383 handle no revpos in attachment block (#7210)
Browse files Browse the repository at this point in the history
  • Loading branch information
torcolvin authored and bbrks committed Dec 12, 2024
1 parent 4aa3a46 commit 02e63b4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion db/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func (c *DatabaseCollection) ForEachStubAttachment(body Body, minRevpos int, doc
return base.HTTPErrorf(http.StatusBadRequest, "Invalid attachment")
}
if meta["data"] == nil {
if revpos, ok := base.ToInt64(meta["revpos"]); revpos < int64(minRevpos) || !ok {
if revpos, ok := base.ToInt64(meta["revpos"]); revpos < int64(minRevpos) || (!ok && minRevpos > 0) {
continue
}
digest, ok := meta["digest"].(string)
Expand Down
52 changes: 52 additions & 0 deletions rest/attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2976,3 +2976,55 @@ func TestAttachmentMigrationToGlobalXattrOnUpdate(t *testing.T) {
attMeta := globalXattr.GlobalAttachments["camera.txt"].(map[string]interface{})
assert.Equal(t, float64(20), attMeta["length"])
}

func TestBlipPushRevWithAttachment(t *testing.T) {
btcRunner := NewBlipTesterClientRunner(t)

btcRunner.Run(func(t *testing.T, SupportedBLIPProtocols []string) {
// Setup
rt := NewRestTesterPersistentConfig(t)
defer rt.Close()
const username = "bernard"

opts := &BlipTesterClientOpts{Username: username, SupportedBLIPProtocols: SupportedBLIPProtocols}
btc := btcRunner.NewBlipTesterClientOptsWithRT(rt, opts)
docID := "doc1"
attachmentName := "attachment1"
attachmentData := "attachmentContents"

contentType := "text/plain"

length, digest, err := btcRunner.saveAttachment(btc.id, contentType, base64.StdEncoding.EncodeToString([]byte(attachmentData)))
require.NoError(t, err)

blipBody := db.Body{
"key": "val",
"_attachments": db.Body{
"attachment1": db.Body{
"digest": digest,
"stub": true,
"length": length,
},
},
}
version1, err := btcRunner.PushRev(btc.id, docID, EmptyDocVersion(), base.MustJSONMarshal(t, blipBody))
require.NoError(t, err)
body := rt.GetDocBody(docID)
require.Equal(t, db.Body{
"key": "val",
"_attachments": map[string]any{
"attachment1": map[string]any{
"digest": digest,
"stub": true,
"revpos": float64(1),
"length": float64(length),
},
},
"_id": docID,
"_rev": version1.RevTreeID,
}, body)
response := rt.SendAdminRequest(http.MethodGet, fmt.Sprintf("/{{.keyspace}}/%s/%s", docID, attachmentName), "")
RequireStatus(t, response, http.StatusOK)
require.Equal(t, attachmentData, string(response.BodyBytes()))
})
}

0 comments on commit 02e63b4

Please sign in to comment.