Skip to content

Commit 5f5cc52

Browse files
committed
add acl modules tests
1 parent 2200437 commit 5f5cc52

File tree

1 file changed

+152
-5
lines changed

1 file changed

+152
-5
lines changed

acl_commands_test.go

Lines changed: 152 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import (
1010
)
1111

1212
var TestUserName string = "goredis"
13-
var _ = Describe("ACL Users Commands", Label("NonRedisEnterprise"), func() {
13+
var _ = Describe("ACL user commands", Label("NonRedisEnterprise"), func() {
1414
var client *redis.Client
1515
var ctx context.Context
1616

1717
BeforeEach(func() {
1818
ctx = context.Background()
19-
client = redis.NewClient(redisOptions())
19+
opt := redisOptions()
20+
opt.UnstableResp3 = true
21+
client = redis.NewClient(opt)
2022
})
2123

2224
AfterEach(func() {
@@ -58,13 +60,15 @@ var _ = Describe("ACL Users Commands", Label("NonRedisEnterprise"), func() {
5860
})
5961
})
6062

61-
var _ = Describe("ACL Permissions", Label("NonRedisEnterprise"), func() {
63+
var _ = Describe("ACL permissions", Label("NonRedisEnterprise"), func() {
6264
var client *redis.Client
6365
var ctx context.Context
6466

6567
BeforeEach(func() {
6668
ctx = context.Background()
67-
client = redis.NewClient(redisOptions())
69+
opt := redisOptions()
70+
opt.UnstableResp3 = true
71+
client = redis.NewClient(opt)
6872
})
6973

7074
AfterEach(func() {
@@ -170,6 +174,147 @@ var _ = Describe("ACL Permissions", Label("NonRedisEnterprise"), func() {
170174
Expect(err).ToNot(HaveOccurred())
171175
Expect(del).To(BeEquivalentTo(1))
172176
})
177+
178+
It("set permissions for module commands", func() {
179+
SkipBeforeRedisMajor(8, "permissions for modules are supported for Redis Version >=8")
180+
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
181+
val, err := client.FTCreate(ctx, "txt", &redis.FTCreateOptions{}, &redis.FieldSchema{FieldName: "txt", FieldType: redis.SearchFieldTypeText}).Result()
182+
Expect(err).NotTo(HaveOccurred())
183+
Expect(val).To(BeEquivalentTo("OK"))
184+
WaitForIndexing(client, "txt")
185+
client.HSet(ctx, "doc1", "txt", "foo baz")
186+
client.HSet(ctx, "doc2", "txt", "foo bar")
187+
add, err := client.ACLSetUser(ctx,
188+
TestUserName,
189+
"reset",
190+
"nopass",
191+
"on",
192+
"~*",
193+
"+FT.SEARCH",
194+
"-FT.DROPINDEX",
195+
"+json.set",
196+
"+json.get",
197+
"-json.clear",
198+
"+bf.reserve",
199+
"-bf.info",
200+
"+cf.reserve",
201+
"+cms.initbydim",
202+
"+topk.reserve",
203+
"+tdigest.create",
204+
"+ts.create",
205+
"-ts.info",
206+
).Result()
207+
Expect(err).NotTo(HaveOccurred())
208+
Expect(add).To(Equal("OK"))
209+
210+
c := client.Conn()
211+
authed, err := c.AuthACL(ctx, TestUserName, "").Result()
212+
Expect(err).NotTo(HaveOccurred())
213+
Expect(authed).To(Equal("OK"))
214+
215+
// has perm for search
216+
Expect(c.FTSearch(ctx, "txt", "foo ~bar").Err()).NotTo(HaveOccurred())
217+
218+
// no perm for dropindex
219+
err = c.FTDropIndex(ctx, "txt").Err()
220+
Expect(err).ToNot(BeEmpty())
221+
Expect(err.Error()).To(ContainSubstring("NOPERM"))
222+
223+
// json set and get have perm
224+
Expect(c.JSONSet(ctx, "foo", "$", "\"bar\"").Err()).NotTo(HaveOccurred())
225+
Expect(c.JSONGet(ctx, "foo", "$").Val()).To(BeEquivalentTo("[\"bar\"]"))
226+
227+
// no perm for json clear
228+
err = c.JSONClear(ctx, "foo", "$").Err()
229+
Expect(err).ToNot(BeEmpty())
230+
Expect(err.Error()).To(ContainSubstring("NOPERM"))
231+
232+
// perm for reserve
233+
Expect(c.BFReserve(ctx, "bloom", 0.01, 100).Err()).NotTo(HaveOccurred())
234+
235+
// no perm for info
236+
err = c.BFInfo(ctx, "bloom").Err()
237+
Expect(err).ToNot(BeEmpty())
238+
Expect(err.Error()).To(ContainSubstring("NOPERM"))
239+
240+
// perm for cf.reserve
241+
Expect(c.CFReserve(ctx, "cfres", 100).Err()).NotTo(HaveOccurred())
242+
// perm for cms.initbydim
243+
Expect(c.CMSInitByDim(ctx, "cmsdim", 100, 5).Err()).NotTo(HaveOccurred())
244+
// perm for topk.reserve
245+
Expect(c.TopKReserve(ctx, "topk", 10).Err()).NotTo(HaveOccurred())
246+
// perm for tdigest.create
247+
Expect(c.TDigestCreate(ctx, "tdc").Err()).NotTo(HaveOccurred())
248+
// perm for ts.create
249+
Expect(c.TSCreate(ctx, "tsts").Err()).NotTo(HaveOccurred())
250+
// noperm for ts.info
251+
err = c.TSInfo(ctx, "tsts").Err()
252+
Expect(err).ToNot(BeEmpty())
253+
Expect(err.Error()).To(ContainSubstring("NOPERM"))
254+
255+
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
256+
})
257+
258+
It("set permissions for module categories", func() {
259+
SkipBeforeRedisMajor(8, "permissions for modules are supported for Redis Version >=8")
260+
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
261+
val, err := client.FTCreate(ctx, "txt", &redis.FTCreateOptions{}, &redis.FieldSchema{FieldName: "txt", FieldType: redis.SearchFieldTypeText}).Result()
262+
Expect(err).NotTo(HaveOccurred())
263+
Expect(val).To(BeEquivalentTo("OK"))
264+
WaitForIndexing(client, "txt")
265+
client.HSet(ctx, "doc1", "txt", "foo baz")
266+
client.HSet(ctx, "doc2", "txt", "foo bar")
267+
add, err := client.ACLSetUser(ctx,
268+
TestUserName,
269+
"reset",
270+
"nopass",
271+
"on",
272+
"~*",
273+
"+@search",
274+
"+@json",
275+
"+@bloom",
276+
"+@cuckoo",
277+
"+@topk",
278+
"+@cms",
279+
"+@timeseries",
280+
"+@tdigest",
281+
).Result()
282+
Expect(err).NotTo(HaveOccurred())
283+
Expect(add).To(Equal("OK"))
284+
285+
c := client.Conn()
286+
authed, err := c.AuthACL(ctx, TestUserName, "").Result()
287+
Expect(err).NotTo(HaveOccurred())
288+
Expect(authed).To(Equal("OK"))
289+
290+
// has perm for search
291+
Expect(c.FTSearch(ctx, "txt", "foo ~bar").Err()).NotTo(HaveOccurred())
292+
// perm for dropindex
293+
Expect(c.FTDropIndex(ctx, "txt").Err()).NotTo(HaveOccurred())
294+
// json set and get have perm
295+
Expect(c.JSONSet(ctx, "foo", "$", "\"bar\"").Err()).NotTo(HaveOccurred())
296+
Expect(c.JSONGet(ctx, "foo", "$").Val()).To(BeEquivalentTo("[\"bar\"]"))
297+
// perm for json clear
298+
Expect(c.JSONClear(ctx, "foo", "$").Err()).NotTo(HaveOccurred())
299+
// perm for reserve
300+
Expect(c.BFReserve(ctx, "bloom", 0.01, 100).Err()).NotTo(HaveOccurred())
301+
// perm for info
302+
Expect(c.BFInfo(ctx, "bloom").Err()).NotTo(HaveOccurred())
303+
// perm for cf.reserve
304+
Expect(c.CFReserve(ctx, "cfres", 100).Err()).NotTo(HaveOccurred())
305+
// perm for cms.initbydim
306+
Expect(c.CMSInitByDim(ctx, "cmsdim", 100, 5).Err()).NotTo(HaveOccurred())
307+
// perm for topk.reserve
308+
Expect(c.TopKReserve(ctx, "topk", 10).Err()).NotTo(HaveOccurred())
309+
// perm for tdigest.create
310+
Expect(c.TDigestCreate(ctx, "tdc").Err()).NotTo(HaveOccurred())
311+
// perm for ts.create
312+
Expect(c.TSCreate(ctx, "tsts").Err()).NotTo(HaveOccurred())
313+
// perm for ts.info
314+
Expect(c.TSInfo(ctx, "tsts").Err()).NotTo(HaveOccurred())
315+
316+
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
317+
})
173318
})
174319

175320
var _ = Describe("ACL Categories", func() {
@@ -178,7 +323,9 @@ var _ = Describe("ACL Categories", func() {
178323

179324
BeforeEach(func() {
180325
ctx = context.Background()
181-
client = redis.NewClient(redisOptions())
326+
opt := redisOptions()
327+
opt.UnstableResp3 = true
328+
client = redis.NewClient(opt)
182329
})
183330

184331
AfterEach(func() {

0 commit comments

Comments
 (0)