1 package rulesengine
2
3 import (
4 "context"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8 )
9
10 type mockDS struct {
11 DeleteCommandFunc func(ctx context.Context, name string) (DeleteResult, error)
12 AddCommandsFunc func(ctx context.Context, names []string) (AddNameResult, error)
13 DeletePrivilegeFunc func(ctx context.Context, name string) (DeleteResult, error)
14 AddPrivilegesFunc func(ctx context.Context, names []string) (AddNameResult, error)
15 Dataset
16 }
17
18 func (m mockDS) DeleteCommand(ctx context.Context, name string) (DeleteResult, error) {
19 return m.DeleteCommandFunc(ctx, name)
20 }
21
22 func (m mockDS) AddCommands(ctx context.Context, names []string) (AddNameResult, error) {
23 return m.AddCommandsFunc(ctx, names)
24 }
25
26 func (m mockDS) DeletePrivilege(ctx context.Context, name string) (DeleteResult, error) {
27 return m.DeletePrivilegeFunc(ctx, name)
28 }
29
30 func (m mockDS) AddPrivileges(ctx context.Context, names []string) (AddNameResult, error) {
31 return m.AddPrivilegesFunc(ctx, names)
32 }
33
34 func TestDeleteCommand(t *testing.T) {
35 t.Parallel()
36
37 tests := map[string]struct {
38 name string
39 assertErr assert.ErrorAssertionFunc
40 }{
41 "Empty name": {
42 name: "",
43 assertErr: ErrorEqualMsg("empty command name"),
44 },
45 "Valid name": {
46 name: "test",
47 assertErr: assert.NoError,
48 },
49 }
50
51 for name, tc := range tests {
52 tc := tc
53 t.Run(name, func(t *testing.T) {
54 t.Parallel()
55
56 ds := mockDS{
57 DeleteCommandFunc: func(_ context.Context, _ string) (DeleteResult, error) {
58 return DeleteResult{}, nil
59 },
60 }
61 reng := New(ds)
62
63 _, err := reng.DeleteCommand(context.Background(), tc.name)
64 tc.assertErr(t, err)
65 })
66 }
67 }
68
69 func TestAddCommands(t *testing.T) {
70 t.Parallel()
71
72 tests := map[string]struct {
73 commands []PostCommandPayload
74 assertErr assert.ErrorAssertionFunc
75 }{
76 "Empty command list": {
77 commands: []PostCommandPayload{},
78 assertErr: ErrorEqualMsg("empty command list"),
79 },
80 "Valid command list": {
81 commands: []PostCommandPayload{{Name: "test"}},
82 assertErr: assert.NoError,
83 },
84 "Invalid command": {
85 commands: []PostCommandPayload{{Name: ""}},
86 assertErr: ErrorEqualMsg("invalid command at 0: empty command name"),
87 },
88 "Multiple invalid commands": {
89 commands: []PostCommandPayload{{Name: ""}, {Name: ""}},
90 assertErr: ErrorEqualMsg("invalid command at 0: empty command name\ninvalid command at 1: empty command name"),
91 },
92 "Too many commands": {
93 commands: func() []PostCommandPayload {
94 commands := make([]PostCommandPayload, 501)
95 for i := 0; i < maxCommands+1; i++ {
96 commands[i] = PostCommandPayload{Name: "test"}
97 }
98 return commands
99 }(),
100 assertErr: ErrorEqualMsg("total number of commands 501 exceeds max 500"),
101 },
102 }
103
104 for name, tc := range tests {
105 tc := tc
106 t.Run(name, func(t *testing.T) {
107 t.Parallel()
108 ds := mockDS{
109 AddCommandsFunc: func(_ context.Context, _ []string) (AddNameResult, error) {
110 return AddNameResult{}, nil
111 },
112 }
113 reng := New(ds)
114
115 _, err := reng.AddCommands(context.Background(), tc.commands)
116 tc.assertErr(t, err)
117 })
118 }
119 }
120
View as plain text