github.com/go-kivik/kivik/v4@v4.3.2/kiviktest/db/security.go (about)

     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  	"gitlab.com/flimzy/testy"
    19  
    20  	"github.com/go-kivik/kivik/v4"
    21  	"github.com/go-kivik/kivik/v4/kiviktest/kt"
    22  )
    23  
    24  func init() {
    25  	kt.Register("Security", security)
    26  	kt.Register("SetSecurity", setSecurity)
    27  }
    28  
    29  var sec = &kivik.Security{
    30  	Admins: kivik.Members{
    31  		Names: []string{"bob", "alice"},
    32  		Roles: []string{"hipsters"},
    33  	},
    34  	Members: kivik.Members{
    35  		Names: []string{"fred"},
    36  		Roles: []string{"beatniks"},
    37  	},
    38  }
    39  
    40  func security(ctx *kt.Context) {
    41  	ctx.RunAdmin(func(ctx *kt.Context) {
    42  		for _, dbname := range ctx.MustStringSlice("databases") {
    43  			func(dbname string) {
    44  				ctx.Run(dbname, func(ctx *kt.Context) {
    45  					ctx.Parallel()
    46  					testGetSecurity(ctx, ctx.Admin, dbname, nil)
    47  				})
    48  			}(dbname)
    49  		}
    50  	})
    51  	ctx.RunNoAuth(func(ctx *kt.Context) {
    52  		for _, dbname := range ctx.MustStringSlice("databases") {
    53  			func(dbname string) {
    54  				ctx.Run(dbname, func(ctx *kt.Context) {
    55  					ctx.Parallel()
    56  					testGetSecurity(ctx, ctx.NoAuth, dbname, nil)
    57  				})
    58  			}(dbname)
    59  		}
    60  	})
    61  	ctx.RunRW(func(ctx *kt.Context) {
    62  		dbname := ctx.TestDB()
    63  		db := ctx.Admin.DB(dbname, ctx.Options("db"))
    64  		if err := db.Err(); err != nil {
    65  			ctx.Fatalf("Failed to open db: %s", err)
    66  		}
    67  		err := kt.Retry(func() error {
    68  			return db.SetSecurity(context.Background(), sec)
    69  		})
    70  		if err != nil {
    71  			ctx.Fatalf("Failed to set security: %s", err)
    72  		}
    73  		ctx.Run("group", func(ctx *kt.Context) {
    74  			ctx.RunAdmin(func(ctx *kt.Context) {
    75  				ctx.Parallel()
    76  				testGetSecurity(ctx, ctx.Admin, dbname, sec)
    77  			})
    78  			ctx.RunNoAuth(func(ctx *kt.Context) {
    79  				ctx.Parallel()
    80  				testGetSecurity(ctx, ctx.NoAuth, dbname, sec)
    81  			})
    82  		})
    83  	})
    84  }
    85  
    86  func setSecurity(ctx *kt.Context) {
    87  	ctx.RunRW(func(ctx *kt.Context) {
    88  		ctx.RunAdmin(func(ctx *kt.Context) {
    89  			testSetSecurityTests(ctx, ctx.Admin)
    90  		})
    91  		ctx.RunNoAuth(func(ctx *kt.Context) {
    92  			testSetSecurityTests(ctx, ctx.NoAuth)
    93  		})
    94  	})
    95  }
    96  
    97  func testSetSecurityTests(ctx *kt.Context, client *kivik.Client) {
    98  	ctx.Run("Exists", func(ctx *kt.Context) {
    99  		ctx.Parallel()
   100  		dbname := ctx.TestDB()
   101  		testSetSecurity(ctx, client, dbname)
   102  	})
   103  	ctx.Run("NotExists", func(ctx *kt.Context) {
   104  		ctx.Parallel()
   105  		dbname := ctx.TestDBName()
   106  		testSetSecurity(ctx, client, dbname)
   107  	})
   108  }
   109  
   110  func testSetSecurity(ctx *kt.Context, client *kivik.Client, dbname string) {
   111  	db := client.DB(dbname, ctx.Options("db"))
   112  	if err := db.Err(); err != nil {
   113  		ctx.Fatalf("Failed to open db: %s", err)
   114  	}
   115  	err := kt.Retry(func() error {
   116  		return db.SetSecurity(context.Background(), sec)
   117  	})
   118  	ctx.CheckError(err)
   119  }
   120  
   121  func testGetSecurity(ctx *kt.Context, client *kivik.Client, dbname string, expected *kivik.Security) {
   122  	sec, err := func() (*kivik.Security, error) {
   123  		db := client.DB(dbname, ctx.Options("db"))
   124  		if err := db.Err(); err != nil {
   125  			return nil, err
   126  		}
   127  		return db.Security(context.Background())
   128  	}()
   129  	if !ctx.IsExpectedSuccess(err) {
   130  		return
   131  	}
   132  	if expected != nil {
   133  		if d := testy.DiffAsJSON(expected, sec); d != nil {
   134  			ctx.Errorf("Security document differs from expected:\n%s\n", d)
   135  		}
   136  	}
   137  }