-
Notifications
You must be signed in to change notification settings - Fork 4
/
approver_test.go
193 lines (173 loc) · 4.09 KB
/
approver_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
// SPDX-FileCopyrightText: 2024 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package bascule
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/suite"
)
type ApproversTestSuite struct {
TestSuite
}
func (suite *ApproversTestSuite) TestAuthorize() {
const placeholderResource = "placeholder resource"
approveErr := errors.New("expected Authorize error")
testCases := []struct {
name string
results []error
expectedErr error
}{
{
name: "EmptyApprovers",
results: nil,
},
{
name: "OneSuccess",
results: []error{nil},
},
{
name: "OneFailure",
results: []error{approveErr},
expectedErr: approveErr,
},
{
name: "FirstFailure",
results: []error{approveErr, errors.New("should not be called")},
expectedErr: approveErr,
},
{
name: "MiddleFailure",
results: []error{nil, approveErr, errors.New("should not be called")},
expectedErr: approveErr,
},
{
name: "AllSuccess",
results: []error{nil, nil, nil},
},
}
suite.Run("Append", func() {
for _, testCase := range testCases {
suite.Run(testCase.name, func() {
var (
testCtx = suite.testContext()
testToken = suite.testToken()
as Approvers[string]
)
for _, err := range testCase.results {
err := err
as = as.Append(
ApproverFunc[string](func(ctx context.Context, resource string, token Token) error {
suite.Same(testCtx, ctx)
suite.Equal(testToken, token)
suite.Equal(placeholderResource, resource)
return err
}),
)
}
suite.Equal(
testCase.expectedErr,
as.Approve(testCtx, placeholderResource, testToken),
)
})
}
})
suite.Run("AppendFunc", func() {
for _, testCase := range testCases {
suite.Run(testCase.name, func() {
var (
testCtx = suite.testContext()
testToken = suite.testToken()
as Approvers[string]
)
for _, err := range testCase.results {
err := err
as = as.AppendFunc(
func(ctx context.Context, resource string, token Token) error {
suite.Same(testCtx, ctx)
suite.Equal(testToken, token)
suite.Equal(placeholderResource, resource)
return err
},
)
}
suite.Equal(
testCase.expectedErr,
as.Approve(testCtx, placeholderResource, testToken),
)
})
}
})
}
func (suite *ApproversTestSuite) TestAny() {
const placeholderResource = "placeholder resource"
approveErr := errors.New("expected Authorize error")
testCases := []struct {
name string
results []error
expectedErr error
}{
{
name: "EmptyApprovers",
results: nil,
},
{
name: "OneSuccess",
results: []error{nil, errors.New("should not be called")},
},
{
name: "OnlyFailure",
results: []error{approveErr},
expectedErr: approveErr,
},
{
name: "FirstFailure",
results: []error{approveErr, nil},
},
{
name: "LastSuccess",
results: []error{approveErr, approveErr, nil},
},
}
for _, testCase := range testCases {
suite.Run(testCase.name, func() {
var (
testCtx = suite.testContext()
testToken = suite.testToken()
as Approvers[string]
)
for _, err := range testCase.results {
err := err
as = as.Append(
ApproverFunc[string](func(ctx context.Context, resource string, token Token) error {
suite.Same(testCtx, ctx)
suite.Equal(testToken, token)
suite.Equal(placeholderResource, resource)
return err
}),
)
}
anyAs := as.Any()
suite.Equal(
testCase.expectedErr,
anyAs.Approve(testCtx, placeholderResource, testToken),
)
if len(as) > 0 {
// the any instance should be distinct
as[0] = ApproverFunc[string](
func(context.Context, string, Token) error {
suite.Fail("should not have been called")
return nil
},
)
suite.Equal(
testCase.expectedErr,
anyAs.Approve(testCtx, placeholderResource, testToken),
)
}
})
}
}
func TestApprovers(t *testing.T) {
suite.Run(t, new(ApproversTestSuite))
}