-
Notifications
You must be signed in to change notification settings - Fork 30
/
action_test.go
233 lines (215 loc) · 6.1 KB
/
action_test.go
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package v1
import (
"context"
"errors"
"sort"
"strings"
"testing"
"time"
"github.com/odpf/shield/model"
shieldv1 "github.com/odpf/shield/proto/v1"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
)
var testActionMap = map[string]model.Action{
"read": {
Id: "read",
Name: "Read",
Namespace: model.Namespace{
Id: "resource-1",
Name: "Resource 1",
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
},
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
},
"write": {
Id: "write",
Name: "Write",
Namespace: model.Namespace{
Id: "resource-1",
Name: "Resource 1",
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
},
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
},
"manage": {
Id: "manage",
Name: "Manage",
Namespace: model.Namespace{
Id: "resource-1",
Name: "Resource 1",
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
},
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
},
}
func TestListActions(t *testing.T) {
t.Parallel()
table := []struct {
title string
mockActionSrc mockActionSrv
req *shieldv1.ListActionsRequest
want *shieldv1.ListActionsResponse
err error
}{
{
title: "error in Action Service",
mockActionSrc: mockActionSrv{ListActionsFunc: func(ctx context.Context) (actions []model.Action, err error) {
return []model.Action{}, errors.New("some error")
}},
want: nil,
err: status.Errorf(codes.Internal, internalServerError.Error()),
},
{
title: "success",
mockActionSrc: mockActionSrv{ListActionsFunc: func(ctx context.Context) (actions []model.Action, err error) {
var testActionList []model.Action
for _, act := range testActionMap {
testActionList = append(testActionList, act)
}
sort.Slice(testActionList[:], func(i, j int) bool {
return strings.Compare(testActionList[i].Id, testActionList[j].Id) < 1
})
return testActionList, nil
}},
want: &shieldv1.ListActionsResponse{Actions: []*shieldv1.Action{
{
Id: "manage",
Name: "Manage",
Namespace: &shieldv1.Namespace{
Id: "resource-1",
Name: "Resource 1",
CreatedAt: timestamppb.New(time.Time{}),
UpdatedAt: timestamppb.New(time.Time{}),
},
CreatedAt: timestamppb.New(time.Time{}),
UpdatedAt: timestamppb.New(time.Time{}),
},
{
Id: "read",
Name: "Read",
Namespace: &shieldv1.Namespace{
Id: "resource-1",
Name: "Resource 1",
CreatedAt: timestamppb.New(time.Time{}),
UpdatedAt: timestamppb.New(time.Time{}),
},
CreatedAt: timestamppb.New(time.Time{}),
UpdatedAt: timestamppb.New(time.Time{}),
},
{
Id: "write",
Name: "Write",
Namespace: &shieldv1.Namespace{
Id: "resource-1",
Name: "Resource 1",
CreatedAt: timestamppb.New(time.Time{}),
UpdatedAt: timestamppb.New(time.Time{}),
},
CreatedAt: timestamppb.New(time.Time{}),
UpdatedAt: timestamppb.New(time.Time{}),
},
}},
err: nil,
},
}
for _, tt := range table {
t.Run(tt.title, func(t *testing.T) {
t.Parallel()
mockDep := Dep{ActionService: tt.mockActionSrc}
resp, err := mockDep.ListActions(context.Background(), tt.req)
assert.EqualValues(t, tt.want, resp)
assert.EqualValues(t, tt.err, err)
})
}
}
func TestCreateAction(t *testing.T) {
t.Parallel()
table := []struct {
title string
mockActionSrv mockActionSrv
req *shieldv1.CreateActionRequest
want *shieldv1.CreateActionResponse
err error
}{
{
title: "error in creating action",
mockActionSrv: mockActionSrv{CreateActionFunc: func(ctx context.Context, act model.Action) (model.Action, error) {
return model.Action{}, errors.New("some error")
}},
req: &shieldv1.CreateActionRequest{Body: &shieldv1.ActionRequestBody{
Id: "read",
Name: "Read",
NamespaceId: "team",
}},
want: nil,
err: grpcInternalServerError,
},
{
title: "success",
mockActionSrv: mockActionSrv{CreateActionFunc: func(ctx context.Context, act model.Action) (model.Action, error) {
return model.Action{
Id: "read",
Name: "Read",
Namespace: model.Namespace{
Id: "team",
Name: "Team",
},
}, nil
}},
req: &shieldv1.CreateActionRequest{Body: &shieldv1.ActionRequestBody{
Id: "read",
Name: "Read",
NamespaceId: "team",
}},
want: &shieldv1.CreateActionResponse{Action: &shieldv1.Action{
Id: "read",
Name: "Read",
Namespace: &shieldv1.Namespace{
Id: "team",
Name: "Team",
CreatedAt: timestamppb.New(time.Time{}),
UpdatedAt: timestamppb.New(time.Time{}),
},
CreatedAt: timestamppb.New(time.Time{}),
UpdatedAt: timestamppb.New(time.Time{}),
}},
err: nil,
},
}
for _, tt := range table {
t.Run(tt.title, func(t *testing.T) {
t.Parallel()
mockDep := Dep{ActionService: tt.mockActionSrv}
resp, err := mockDep.CreateAction(context.Background(), tt.req)
assert.EqualValues(t, tt.want, resp)
assert.EqualValues(t, tt.err, err)
})
}
}
type mockActionSrv struct {
GetActionFunc func(ctx context.Context, id string) (model.Action, error)
CreateActionFunc func(ctx context.Context, act model.Action) (model.Action, error)
ListActionsFunc func(ctx context.Context) ([]model.Action, error)
UpdateActionFunc func(ctx context.Context, id string, act model.Action) (model.Action, error)
}
func (m mockActionSrv) GetAction(ctx context.Context, id string) (model.Action, error) {
return m.GetActionFunc(ctx, id)
}
func (m mockActionSrv) ListActions(ctx context.Context) ([]model.Action, error) {
return m.ListActionsFunc(ctx)
}
func (m mockActionSrv) CreateAction(ctx context.Context, act model.Action) (model.Action, error) {
return m.CreateActionFunc(ctx, act)
}
func (m mockActionSrv) UpdateAction(ctx context.Context, id string, act model.Action) (model.Action, error) {
return m.UpdateActionFunc(ctx, id, act)
}