-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_test.go
234 lines (200 loc) · 7.48 KB
/
handler_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
234
package authorizer_test
import (
"errors"
"fmt"
"nuledger/authorizer"
"nuledger/authorizer/rule"
"nuledger/authorizer/rules"
"nuledger/iop"
mock_authorizer "nuledger/mocks/authorizer"
"nuledger/model"
"nuledger/model/violation"
"nuledger/util"
"testing"
"time"
"github.com/golang/mock/gomock"
. "github.com/smartystreets/goconvey/convey"
)
var startTime = time.Date(2021, time.March, 31, 21, 11, 43, 0, time.Local)
func TestHandler(t *testing.T) {
Convey("Given an authorizer Handler", t, func() {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
Convey("When it is sent an acceptable input", func() {
Convey("And ledger returns a successful response", func() {
returnedAccount := &model.Account{ActiveCard: true, AvailableLimit: 20170415}
expected := iop.StateOutput{Account: returnedAccount, Violations: []violation.Code{}}
Convey("It should forward exact response in output", func() {
validate := func(output iop.StateOutput, err error) {
So(err, ShouldBeNil)
So(output, ShouldResemble, expected)
}
testHandlerOperations(ctrl, validate, returnedAccount, nil)
})
})
Convey("And ledger returns an error", func() {
Convey("It should return any violation.Error as a violation", func() {
violErr := violation.NewError("custom-validation-code", "Hello violations")
expected := iop.StateOutput{Account: nil, Violations: []violation.Code{violErr.Code}}
validate := func(output iop.StateOutput, err error) {
So(err, ShouldBeNil)
So(output, ShouldResemble, expected)
}
testHandlerOperations(ctrl, validate, nil, violErr)
})
Convey("It should handle aggregated violation errors", func() {
returnedError := util.AggregateError{[]error{
violation.NewError("custom-validation-code", "Hello violations"),
violation.NewError("yet-another-validation-code", "Old friend"),
}}
expected := iop.StateOutput{Account: nil, Violations: []violation.Code{"custom-validation-code", "yet-another-validation-code"}}
validate := func(output iop.StateOutput, err error) {
So(err, ShouldBeNil)
So(output, ShouldResemble, expected)
}
testHandlerOperations(ctrl, validate, nil, returnedError)
})
Convey("Any other error should be propagated", func() {
regularErr := errors.New("This is just a regular error")
validate := func(output iop.StateOutput, err error) {
So(err, ShouldNotBeNil)
So(output, ShouldBeZeroValue)
So(err, ShouldEqual, regularErr)
}
testHandlerOperations(ctrl, validate, nil, regularErr)
})
Convey("Even if aggregated with other violation errors", func() {
regularErr := errors.New("This is just a regular error")
returnedError := util.AggregateError{[]error{
violation.NewError("custom-validation-code", "Hello violations"),
regularErr,
}}
validate := func(output iop.StateOutput, err error) {
So(err, ShouldNotBeNil)
So(output, ShouldBeZeroValue)
So(err, ShouldEqual, regularErr)
}
testHandlerOperations(ctrl, validate, nil, returnedError)
})
})
})
})
}
func TestHandlerBadInput(t *testing.T) {
Convey("Given the authorizer Handler gets some bad input", t, func() {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
ledger := mock_authorizer.NewMockLedger(ctrl)
var handler iop.DataHandler = &authorizer.Handler{ledger}
Convey("It should return a fatal error", func() {
test := func(input iop.OperationInput) {
output, err := handler.Handle(input)
So(err, ShouldNotBeNil)
So(output, ShouldBeZeroValue)
}
Convey("For an empty input", func() {
test(iop.OperationInput{})
})
Convey("For ambiguous operations", func() {
test(iop.OperationInput{&model.Account{}, &model.Transaction{}})
})
})
Convey("It should still forward the request", func() {
uniqueAccount := &model.Account{ActiveCard: true, AvailableLimit: 812674182736172}
expected := iop.StateOutput{Account: uniqueAccount, Violations: []violation.Code{}}
test := func(input iop.OperationInput) {
output, err := handler.Handle(input)
So(err, ShouldBeNil)
So(output, ShouldResemble, expected)
}
testBothOperations := func(account *model.Account, transaction *model.Transaction) {
Convey("For CreateAccount operation", func() {
ledger.EXPECT().
CreateAccount(gomock.Eq(*account)).
Return(uniqueAccount, nil)
test(iop.OperationInput{Account: account})
})
Convey("For PerformTransaction operation", func() {
ledger.EXPECT().
PerformTransaction(gomock.Eq(*transaction)).
Return(uniqueAccount, nil)
test(iop.OperationInput{Transaction: transaction})
})
}
Convey("For empty operation objects", func() {
testBothOperations(&model.Account{}, &model.Transaction{})
})
Convey("For semantically bad operation objects", func() {
badAccount := &model.Account{AvailableLimit: -23}
badTransaction := &model.Transaction{Amount: -42}
testBothOperations(badAccount, badTransaction)
})
})
})
}
func TestDefaultAuthorizers(t *testing.T) {
Convey("Given the default authorizers", t, func() {
authzer := authorizer.DefaultAuthorizer()
Convey("They should be a rule list", func() {
So(authzer, ShouldHaveSameTypeAs, rule.List{})
list := authzer.(rule.List)
Convey("With all required authorization rules", func() {
So(list, ShouldHaveLength, 5)
So(list, ShouldContain, &rules.ChronologicalOrder{})
So(freqAnalyzerCount(list), ShouldEqual, 2)
So(containsAuthFunc(list, rules.AccountCardActive), ShouldBeTrue)
So(containsAuthFunc(list, rules.SufficientLimit), ShouldBeTrue)
})
})
})
Convey("Given a default handler", t, func() {
handler := authorizer.NewHandler()
_, err := handler.Handle(iop.OperationInput{Account: &model.Account{}})
So(err, ShouldBeNil)
Convey("It should use the default authorizers", func() {
expected := iop.StateOutput{Account: &model.Account{}, Violations: []violation.Code{violation.CardNotActive}}
output, err := handler.Handle(iop.OperationInput{Transaction: &model.Transaction{}})
So(err, ShouldBeNil)
So(output, ShouldResemble, expected)
})
})
}
func freqAnalyzerCount(list rule.List) int {
count := 0
for _, auth := range list {
if _, isFreq := auth.(*rules.FrequencyAnalyzer); isFreq {
count++
}
}
return count
}
func containsAuthFunc(list rule.List, authFunc rule.AuthorizerFunc) bool {
for _, auth := range list {
if elmFunc, isFunc := auth.(rule.AuthorizerFunc); isFunc {
if fmt.Sprint(authFunc) == fmt.Sprint(elmFunc) {
return true
}
}
}
return false
}
func testHandlerOperations(ctrl *gomock.Controller, validate func(iop.StateOutput, error), returnAccount *model.Account, returnErr error) {
ledger := mock_authorizer.NewMockLedger(ctrl)
var handler iop.DataHandler = &authorizer.Handler{ledger}
Convey("For CreateAccount (Account) operation", func() {
account := &model.Account{ActiveCard: true, AvailableLimit: 20210902}
createAccountOp := iop.OperationInput{Account: account}
ledger.EXPECT().
CreateAccount(gomock.Eq(*account)).
Return(returnAccount, returnErr)
validate(handler.Handle(createAccountOp))
})
Convey("For PerformTransaction (Transaction) operation", func() {
transaction := &model.Transaction{Merchant: "Amazon Web Services", Amount: 142, Time: startTime}
performTxOp := iop.OperationInput{Transaction: transaction}
ledger.EXPECT().
PerformTransaction(gomock.Eq(*transaction)).
Return(returnAccount, returnErr)
validate(handler.Handle(performTxOp))
})
}