1
2
3
4
5
6
7
8
9
10
11
12
13 package memorydb
14
15 import (
16 "context"
17 "testing"
18
19 "gitlab.com/flimzy/testy"
20
21 "github.com/go-kivik/kivik/v4/driver"
22 )
23
24 func TestGetSecurity(t *testing.T) {
25 type secTest struct {
26 Name string
27 DB *db
28 Error string
29 Expected interface{}
30 }
31 tests := []secTest{
32 {
33 Name: "DBNotFound",
34 Error: "database does not exist",
35 DB: func() *db {
36 c := setup(t, nil)
37 if err := c.CreateDB(context.Background(), "foo", nil); err != nil {
38 t.Fatal(err)
39 }
40 dbv, err := c.DB("foo", nil)
41 if err != nil {
42 t.Fatal(err)
43 }
44 if e := c.DestroyDB(context.Background(), "foo", nil); e != nil {
45 t.Fatal(e)
46 }
47 return dbv.(*db)
48 }(),
49 },
50 {
51 Name: "EmptySecurity",
52 DB: func() *db {
53 c := setup(t, nil)
54 if err := c.CreateDB(context.Background(), "foo", nil); err != nil {
55 t.Fatal(err)
56 }
57 dbv, err := c.DB("foo", nil)
58 if err != nil {
59 t.Fatal(err)
60 }
61 return dbv.(*db)
62 }(),
63 Expected: &driver.Security{},
64 },
65 {
66 Name: "AdminsAndMembers",
67 DB: func() *db {
68 c := setup(t, nil)
69 if err := c.CreateDB(context.Background(), "foo", nil); err != nil {
70 t.Fatal(err)
71 }
72
73 db := &db{
74 dbName: "foo",
75 client: c.(*client),
76 db: &database{
77 security: &driver.Security{
78 Admins: driver.Members{
79 Names: []string{"foo", "bar", "baz"},
80 Roles: []string{"morons"},
81 },
82 Members: driver.Members{
83 Names: []string{"bob"},
84 Roles: []string{"boring"},
85 },
86 },
87 },
88 }
89 return db
90 }(),
91 Expected: &driver.Security{
92 Admins: driver.Members{
93 Names: []string{"foo", "bar", "baz"},
94 Roles: []string{"morons"},
95 },
96 Members: driver.Members{
97 Names: []string{"bob"},
98 Roles: []string{"boring"},
99 },
100 },
101 },
102 }
103 for _, test := range tests {
104 func(test secTest) {
105 t.Run(test.Name, func(t *testing.T) {
106 t.Parallel()
107 db := test.DB
108 if db == nil {
109 db = setupDB(t)
110 }
111 sec, err := db.Security(context.Background())
112 if !testy.ErrorMatches(test.Error, err) {
113 t.Errorf("Unexpected error: %s", err)
114 }
115 if d := testy.DiffAsJSON(test.Expected, sec); d != nil {
116 t.Error(d)
117 }
118 })
119 }(test)
120 }
121 }
122
123 func TestSetSecurity(t *testing.T) {
124 type setTest struct {
125 Name string
126 Security *driver.Security
127 Error string
128 Expected *driver.Security
129 DB *db
130 }
131 tests := []setTest{
132 {
133 Name: "DBNotFound",
134 Error: "missing",
135 DB: func() *db {
136 c := setup(t, nil)
137 if err := c.CreateDB(context.Background(), "foo", nil); err != nil {
138 t.Fatal(err)
139 }
140 dbv, err := c.DB("foo", nil)
141 if err != nil {
142 t.Fatal(err)
143 }
144 if e := c.DestroyDB(context.Background(), "foo", nil); e != nil {
145 t.Fatal(e)
146 }
147 return dbv.(*db)
148 }(),
149 },
150 {
151 Name: "Valid",
152 Security: &driver.Security{
153 Admins: driver.Members{
154 Names: []string{"foo", "bar", "baz"},
155 Roles: []string{"morons"},
156 },
157 Members: driver.Members{
158 Names: []string{"bob"},
159 Roles: []string{"boring"},
160 },
161 },
162 Expected: &driver.Security{
163 Admins: driver.Members{
164 Names: []string{"foo", "bar", "baz"},
165 Roles: []string{"morons"},
166 },
167 Members: driver.Members{
168 Names: []string{"bob"},
169 Roles: []string{"boring"},
170 },
171 },
172 },
173 }
174 for _, test := range tests {
175 func(test setTest) {
176 t.Run(test.Name, func(t *testing.T) {
177 t.Parallel()
178 db := test.DB
179 if db == nil {
180 db = setupDB(t)
181 }
182 err := db.SetSecurity(context.Background(), test.Security)
183 var msg string
184 if err != nil {
185 msg = err.Error()
186 }
187 if msg != test.Error {
188 t.Errorf("Unexpected error: %s", msg)
189 }
190 if err != nil {
191 return
192 }
193 sec, err := db.Security(context.Background())
194 if err != nil {
195 t.Fatal(err)
196 }
197 if d := testy.DiffAsJSON(test.Expected, sec); d != nil {
198 t.Error(d)
199 }
200 })
201 }(test)
202 }
203 }
204
View as plain text