Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ndyakov committed Feb 4, 2025
1 parent e94b310 commit c609df4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
11 changes: 3 additions & 8 deletions acl_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,13 @@ func (c cmdable) ACLLogReset(ctx context.Context) *StatusCmd {
}

func (c cmdable) ACLDelUser(ctx context.Context, username string) *IntCmd {
args := make([]interface{}, 3, 3)
args[0] = "acl"
args[1] = "deluser"
args[2] = username
cmd := NewIntCmd(ctx, args...)
cmd := NewIntCmd(ctx, "acl", "deluser", username)
_ = c(ctx, cmd)
return cmd
}

func (c cmdable) ACLSetUser(ctx context.Context, username string, rules ...string) *StatusCmd {
args := make([]interface{}, 3+len(rules), 3+len(rules))
args := make([]interface{}, 3+len(rules))
args[0] = "acl"
args[1] = "setuser"
args[2] = username
Expand All @@ -84,8 +80,7 @@ func (c cmdable) ACLCat(ctx context.Context) *StringSliceCmd {
func (c cmdable) ACLCatArgs(ctx context.Context, options *ACLCatArgs) *StringSliceCmd {
// if there is a category passed, build new cmd, if there isn't - use the ACLCat method
if options != nil && options.Category != "" {
args := []interface{}{"acl", "cat", options.Category}
cmd := NewStringSliceCmd(ctx, args...)
cmd := NewStringSliceCmd(ctx, "acl", "cat", options.Category)
_ = c(ctx, cmd)
return cmd
}
Expand Down
63 changes: 62 additions & 1 deletion acl_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,55 @@ import (
)

var TestUserName string = "goredis"
var _ = Describe("ACL Users Commands", Label("NonRedisEnterprise"), func() {
var client *redis.Client
var ctx context.Context

BeforeEach(func() {
ctx = context.Background()
client = redis.NewClient(redisOptions())
})

AfterEach(func() {
_, err := client.ACLDelUser(context.Background(), TestUserName).Result()
Expect(client.Close()).NotTo(HaveOccurred())
Expect(err).NotTo(HaveOccurred())
})

It("list only default user", func() {
res, err := client.ACLList(ctx).Result()
Expect(err).NotTo(HaveOccurred())
Expect(res).To(HaveLen(1))
Expect(res[0]).To(ContainSubstring("default"))
})

It("setuser and deluser", func() {
res, err := client.ACLList(ctx).Result()
Expect(err).NotTo(HaveOccurred())
Expect(res).To(HaveLen(1))
Expect(res[0]).To(ContainSubstring("default"))

add, err := client.ACLSetUser(ctx, TestUserName, "nopass", "on", "allkeys", "+set", "+get").Result()
Expect(err).NotTo(HaveOccurred())
Expect(add).To(Equal("OK"))

var _ = Describe("ACL Commands", func() {
resAfter, err := client.ACLList(ctx).Result()
Expect(err).NotTo(HaveOccurred())
Expect(resAfter).To(HaveLen(2))
Expect(resAfter[1]).To(ContainSubstring(TestUserName))

deletedN, err := client.ACLDelUser(ctx, TestUserName).Result()
Expect(err).NotTo(HaveOccurred())
Expect(deletedN).To(BeNumerically("==", 1))

resAfterDeletion, err := client.ACLList(ctx).Result()
Expect(err).NotTo(HaveOccurred())
Expect(resAfterDeletion).To(HaveLen(1))
Expect(resAfterDeletion[0]).To(BeEquivalentTo(res[0]))
})
})

var _ = Describe("ACL Permissions", Label("NonRedisEnterprise"), func() {
var client *redis.Client
var ctx context.Context

Expand Down Expand Up @@ -57,6 +104,20 @@ var _ = Describe("ACL Commands", func() {
Expect(resAfterDeletion).To(HaveLen(1))
Expect(resAfterDeletion[0]).To(BeEquivalentTo(res[0]))
})
})

var _ = Describe("ACL Categories", func() {
var client *redis.Client
var ctx context.Context

BeforeEach(func() {
ctx = context.Background()
client = redis.NewClient(redisOptions())
})

AfterEach(func() {
Expect(client.Close()).NotTo(HaveOccurred())
})

It("lists acl categories and subcategories", func() {
res, err := client.ACLCat(ctx).Result()
Expand Down
4 changes: 4 additions & 0 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2313,6 +2313,10 @@ var _ = Describe("Commands", func() {
limitedLogEntries, err := client.ACLLog(ctx, 2).Result()
Expect(err).NotTo(HaveOccurred())
Expect(len(limitedLogEntries)).To(Equal(2))

// cleanup after creating the user
err = client.Do(ctx, "acl", "deluser", "test").Err()
Expect(err).NotTo(HaveOccurred())
})

It("should ACL LOG RESET", Label("NonRedisEnterprise"), func() {
Expand Down

0 comments on commit c609df4

Please sign in to comment.