(t *testing.T)
| 667 | } |
| 668 | |
| 669 | func TestStoreAttachments(t *testing.T) { |
| 670 | db, ctx := setupTestDB(t) |
| 671 | defer db.Close(ctx) |
| 672 | collection, ctx := GetSingleDatabaseCollectionWithUser(ctx, t, db) |
| 673 | |
| 674 | var revBody Body |
| 675 | // Simulate Invalid _attachments scenario; try to put a document with bad |
| 676 | // attachment metadata. It should throw "Invalid _attachments" error. |
| 677 | revText := `{"key1": "value1", "_attachments": {"att1.txt": "YXR0MS50eHQ="}}` |
| 678 | assert.NoError(t, base.JSONUnmarshal([]byte(revText), &revBody)) |
| 679 | revId, doc, err := collection.Put(ctx, "doc1", revBody) |
| 680 | assert.Empty(t, revId, "The revId should be empty since the request has attachment") |
| 681 | assert.Empty(t, doc, "The doc should be empty since the request has attachment") |
| 682 | assert.Error(t, err, "It should throw 400 Invalid _attachments") |
| 683 | assert.Contains(t, err.Error(), strconv.Itoa(http.StatusBadRequest)) |
| 684 | |
| 685 | // Simulate illegal base64 data error while storing attachments in Couchbase database. |
| 686 | revText = `{"key1": "value1", "_attachments": {"att1.txt": {"data": "%$^&**"}}}` |
| 687 | assert.NoError(t, base.JSONUnmarshal([]byte(revText), &revBody)) |
| 688 | revId, doc, err = collection.Put(ctx, "doc1", revBody) |
| 689 | assert.Empty(t, revId, "The revId should be empty since illegal base64 data in attachment") |
| 690 | assert.Empty(t, doc, "The doc should be empty since illegal base64 data in attachment") |
| 691 | assert.Error(t, err, "It should throw illegal base64 data at input byte 0 error") |
| 692 | assert.Contains(t, err.Error(), "illegal base64 data at input byte 0") |
| 693 | |
| 694 | // Simulate a valid scenario; attachment contains data, so store it in the database. |
| 695 | // Include content type, encoding, attachment length in attachment metadata. |
| 696 | revText = `{"key1": "value1", "_attachments": {"att1.txt": {"data": "YXR0MS50eHQ=", "content_type": "text/plain", "encoding": "utf-8", "length": 12}}}` |
| 697 | assert.NoError(t, base.JSONUnmarshal([]byte(revText), &revBody)) |
| 698 | revId, doc, err = collection.Put(ctx, "doc1", revBody) |
| 699 | assert.NoError(t, err, "Couldn't update document") |
| 700 | assert.NotEmpty(t, revId, "Document revision id should be generated") |
| 701 | require.NotNil(t, doc) |
| 702 | assert.NotEmpty(t, doc.Attachments(), "Attachment metadata should be populated") |
| 703 | attachment := doc.Attachments()["att1.txt"].(map[string]interface{}) |
| 704 | assert.Equal(t, "text/plain", attachment["content_type"]) |
| 705 | assert.Equal(t, "sha1-crv3IVNxp3JXbP6bizTHt3GB3O0=", attachment["digest"]) |
| 706 | assert.Equal(t, 8, attachment["encoded_length"]) |
| 707 | assert.Equal(t, "utf-8", attachment["encoding"]) |
| 708 | assert.Equal(t, float64(12), attachment["length"]) |
| 709 | assert.NotEmpty(t, attachment["revpos"]) |
| 710 | assert.True(t, attachment["stub"].(bool)) |
| 711 | |
| 712 | // Simulate a valid scenario; attachment contains data, so store it in the database. |
| 713 | // Include content type, encoding in attachment metadata but no attachment length. |
| 714 | revText = `{"key1": "value1", "_attachments": {"att1.txt": {"data": "YXR0MS50eHQ=", "content_type": "text/plain", "encoding": "utf-8"}}}` |
| 715 | assert.NoError(t, base.JSONUnmarshal([]byte(revText), &revBody)) |
| 716 | revId, doc, err = collection.Put(ctx, "doc2", revBody) |
| 717 | assert.NoError(t, err, "Couldn't update document") |
| 718 | assert.NotEmpty(t, revId, "Document revision id should be generated") |
| 719 | require.NotNil(t, doc) |
| 720 | assert.NotEmpty(t, doc.Attachments(), "Attachment metadata should be populated") |
| 721 | attachment = doc.Attachments()["att1.txt"].(map[string]interface{}) |
| 722 | assert.Equal(t, "text/plain", attachment["content_type"]) |
| 723 | assert.Equal(t, "sha1-crv3IVNxp3JXbP6bizTHt3GB3O0=", attachment["digest"]) |
| 724 | assert.Equal(t, 8, attachment["encoded_length"]) |
| 725 | assert.Equal(t, "utf-8", attachment["encoding"]) |
| 726 | assert.Empty(t, attachment["length"], "Attachment length should be empty") |
nothing calls this directly
no test coverage detected