1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
// Copyright 2018 Canonical Ltd.
// Licensed under the LGPL, see LICENCE file for details.
package aclstore_test
import (
"context"
"sort"
"testing"
qt "github.com/frankban/quicktest"
"github.com/juju/simplekv/memsimplekv"
"gopkg.in/errgo.v1"
aclstore "github.com/juju/aclstore/v2"
)
func TestCreateACL(t *testing.T) {
ctx := context.Background()
c := qt.New(t)
store := aclstore.NewACLStore(memsimplekv.NewStore())
err := store.CreateACL(ctx, "foo", []string{"x", "y"})
c.Assert(err, qt.Equals, nil)
acl, err := store.Get(ctx, "foo")
c.Assert(err, qt.Equals, nil)
c.Assert(acl, qt.DeepEquals, []string{"x", "y"})
}
func TestNewACLOnExistingACL(t *testing.T) {
ctx := context.Background()
c := qt.New(t)
store := aclstore.NewACLStore(memsimplekv.NewStore())
err := store.CreateACL(ctx, "foo", []string{"x", "y"})
c.Assert(err, qt.Equals, nil)
err = store.CreateACL(ctx, "foo", []string{"z", "w"})
c.Assert(err, qt.Equals, nil)
acl, err := store.Get(ctx, "foo")
c.Assert(err, qt.Equals, nil)
c.Assert(acl, qt.DeepEquals, []string{"x", "y"})
}
func TestAddToNonExistentACL(t *testing.T) {
ctx := context.Background()
c := qt.New(t)
store := aclstore.NewACLStore(memsimplekv.NewStore())
err := store.Add(ctx, "foo", []string{"x", "y"})
c.Assert(err, qt.ErrorMatches, `ACL not found`)
c.Assert(errgo.Cause(err), qt.Equals, aclstore.ErrACLNotFound)
}
func TestAdd(t *testing.T) {
ctx := context.Background()
c := qt.New(t)
store := aclstore.NewACLStore(memsimplekv.NewStore())
err := store.CreateACL(ctx, "foo", []string{"e", "c"})
c.Assert(err, qt.Equals, nil)
err = store.Add(ctx, "foo", []string{"a", "d", "f", "e", "a"})
c.Assert(err, qt.Equals, nil)
acl, err := store.Get(ctx, "foo")
c.Assert(err, qt.Equals, nil)
c.Assert(acl, qt.DeepEquals, []string{"a", "c", "d", "e", "f"})
}
func TestRemoveNonExistentACL(t *testing.T) {
ctx := context.Background()
c := qt.New(t)
store := aclstore.NewACLStore(memsimplekv.NewStore())
err := store.Remove(ctx, "foo", []string{"x", "y"})
c.Assert(err, qt.ErrorMatches, `ACL not found`)
c.Assert(errgo.Cause(err), qt.Equals, aclstore.ErrACLNotFound)
}
func TestRemove(t *testing.T) {
ctx := context.Background()
c := qt.New(t)
store := aclstore.NewACLStore(memsimplekv.NewStore())
err := store.CreateACL(ctx, "foo", []string{"a", "b", "c", "d"})
c.Assert(err, qt.Equals, nil)
err = store.Remove(ctx, "foo", []string{"b", "c", "e"})
c.Assert(err, qt.Equals, nil)
acl, err := store.Get(ctx, "foo")
c.Assert(err, qt.Equals, nil)
c.Assert(acl, qt.DeepEquals, []string{"a", "d"})
}
func TestSetNonExistingACL(t *testing.T) {
ctx := context.Background()
c := qt.New(t)
store := aclstore.NewACLStore(memsimplekv.NewStore())
err := store.Set(ctx, "foo", []string{"x", "y"})
c.Assert(err, qt.ErrorMatches, `ACL not found`)
c.Assert(errgo.Cause(err), qt.Equals, aclstore.ErrACLNotFound)
}
func TestSet(t *testing.T) {
ctx := context.Background()
c := qt.New(t)
store := aclstore.NewACLStore(memsimplekv.NewStore())
err := store.CreateACL(ctx, "foo", []string{"a", "b", "c", "d"})
c.Assert(err, qt.Equals, nil)
err = store.Set(ctx, "foo", []string{"b", "c", "e", "e"})
c.Assert(err, qt.Equals, nil)
acl, err := store.Get(ctx, "foo")
c.Assert(err, qt.Equals, nil)
c.Assert(acl, qt.DeepEquals, []string{"b", "c", "e"})
}
func TestGetNonExistent(t *testing.T) {
ctx := context.Background()
c := qt.New(t)
store := aclstore.NewACLStore(memsimplekv.NewStore())
acl, err := store.Get(ctx, "foo")
c.Assert(err, qt.ErrorMatches, `ACL not found`)
c.Assert(errgo.Cause(err), qt.Equals, aclstore.ErrACLNotFound)
c.Assert(acl, qt.IsNil)
}
func TestGetEmpty(t *testing.T) {
ctx := context.Background()
c := qt.New(t)
store := aclstore.NewACLStore(memsimplekv.NewStore())
err := store.CreateACL(ctx, "foo", nil)
c.Assert(err, qt.Equals, nil)
acl, err := store.Get(ctx, "foo")
c.Assert(err, qt.Equals, nil)
c.Assert(acl, qt.HasLen, 0)
}
func TestACLLister(t *testing.T) {
ctx := context.Background()
c := qt.New(t)
store := aclstore.NewACLStore(memsimplekv.NewStore())
lister, ok := store.(aclstore.ACLLister)
c.Assert(ok, qt.Equals, true)
acls, err := lister.ACLs(ctx)
c.Assert(err, qt.Equals, nil)
c.Assert(acls, qt.HasLen, 0)
err = store.CreateACL(ctx, "foo", []string{"x", "y"})
c.Assert(err, qt.Equals, nil)
err = store.CreateACL(ctx, "bar", []string{"x", "y"})
c.Assert(err, qt.Equals, nil)
err = store.CreateACL(ctx, "choo", []string{"x", "y"})
c.Assert(err, qt.Equals, nil)
acls, err = lister.ACLs(ctx)
c.Assert(err, qt.Equals, nil)
sort.Strings(acls)
c.Assert(acls, qt.DeepEquals, []string{"bar", "choo", "foo"})
}
|