...

Source file src/github.com/go-kivik/kivik/v4/kiviktest/db/getattachmentmeta.go

Documentation: github.com/go-kivik/kivik/v4/kiviktest/db

     1  // Licensed under the Apache License, Version 2.0 (the "License"); you may not
     2  // use this file except in compliance with the License. You may obtain a copy of
     3  // the License at
     4  //
     5  //  http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     9  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    10  // License for the specific language governing permissions and limitations under
    11  // the License.
    12  
    13  package db
    14  
    15  import (
    16  	"context"
    17  
    18  	"github.com/go-kivik/kivik/v4"
    19  	"github.com/go-kivik/kivik/v4/kiviktest/kt"
    20  )
    21  
    22  func init() {
    23  	kt.Register("GetAttachmentMeta", getAttachmentMeta)
    24  }
    25  
    26  func getAttachmentMeta(ctx *kt.Context) {
    27  	ctx.RunRW(func(ctx *kt.Context) {
    28  		dbname := ctx.TestDB()
    29  		adb := ctx.Admin.DB(dbname, ctx.Options("db"))
    30  		if err := adb.Err(); err != nil {
    31  			ctx.Fatalf("Failed to open db: %s", err)
    32  		}
    33  
    34  		doc := map[string]interface{}{
    35  			"_id": "foo",
    36  			"_attachments": map[string]interface{}{
    37  				"foo.txt": map[string]interface{}{
    38  					"content_type": "text/plain",
    39  					"data":         "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ=",
    40  				},
    41  			},
    42  		}
    43  		if _, err := adb.Put(context.Background(), "foo", doc); err != nil {
    44  			ctx.Fatalf("Failed to create doc: %s", err)
    45  		}
    46  
    47  		ddoc := map[string]interface{}{
    48  			"_id": "_design/foo",
    49  			"_attachments": map[string]interface{}{
    50  				"foo.txt": map[string]interface{}{
    51  					"content_type": "text/plain",
    52  					"data":         "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ=",
    53  				},
    54  			},
    55  		}
    56  		if _, err := adb.Put(context.Background(), "_design/foo", ddoc); err != nil {
    57  			ctx.Fatalf("Failed to create design doc: %s", err)
    58  		}
    59  
    60  		ctx.Run("group", func(ctx *kt.Context) {
    61  			ctx.RunAdmin(func(ctx *kt.Context) {
    62  				ctx.Parallel()
    63  				testGetAttachmentMeta(ctx, ctx.Admin, dbname, "foo", "foo.txt")
    64  				testGetAttachmentMeta(ctx, ctx.Admin, dbname, "foo", "NotFound")
    65  				testGetAttachmentMeta(ctx, ctx.Admin, dbname, "_design/foo", "foo.txt")
    66  			})
    67  			ctx.RunNoAuth(func(ctx *kt.Context) {
    68  				ctx.Parallel()
    69  				testGetAttachmentMeta(ctx, ctx.NoAuth, dbname, "foo", "foo.txt")
    70  				testGetAttachmentMeta(ctx, ctx.NoAuth, dbname, "foo", "NotFound")
    71  				testGetAttachmentMeta(ctx, ctx.NoAuth, dbname, "_design/foo", "foo.txt")
    72  			})
    73  		})
    74  	})
    75  }
    76  
    77  func testGetAttachmentMeta(ctx *kt.Context, client *kivik.Client, dbname, docID, filename string) {
    78  	ctx.Run(docID+"/"+filename, func(ctx *kt.Context) {
    79  		ctx.Parallel()
    80  		db := client.DB(dbname, ctx.Options("db"))
    81  		if err := db.Err(); err != nil {
    82  			ctx.Fatalf("Failed to connect to db")
    83  		}
    84  		att, err := db.GetAttachmentMeta(context.Background(), docID, filename)
    85  		if !ctx.IsExpectedSuccess(err) {
    86  			return
    87  		}
    88  		if client.Driver() != "pouch" {
    89  			if att.ContentType != "text/plain" {
    90  				ctx.Errorf("Content-Type: Expected %s, Actual %s", "text/plain", att.ContentType)
    91  			}
    92  		}
    93  	})
    94  }
    95  

View as plain text