-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_test.go
More file actions
97 lines (87 loc) · 2.09 KB
/
Copy pathcustom_test.go
File metadata and controls
97 lines (87 loc) · 2.09 KB
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
package hyperacc
import (
"errors"
"testing"
"github.com/hyperledger/fabric-contract-api-go/v2/contractapi"
"github.com/stretchr/testify/assert"
)
func TestCustomRule_Check(t *testing.T) {
tests := []struct {
name string
ruleName string
checkFunc func(contractapi.TransactionContextInterface) error
expectError bool
}{
{
name: "custom check passes",
ruleName: "my-custom-rule",
checkFunc: func(ctx contractapi.TransactionContextInterface) error {
return nil
},
expectError: false,
},
{
name: "custom check fails with access error",
ruleName: "failing-rule",
checkFunc: func(ctx contractapi.TransactionContextInterface) error {
return NewAccessError("custom failure")
},
expectError: true,
},
{
name: "custom check fails with generic error",
ruleName: "generic-error-rule",
checkFunc: func(ctx contractapi.TransactionContextInterface) error {
return errors.New("something went wrong")
},
expectError: true,
},
{
name: "empty rule name",
ruleName: "",
checkFunc: func(ctx contractapi.TransactionContextInterface) error {
return nil
},
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctxMock, _ := setupMocks()
rule := Custom(tt.ruleName, tt.checkFunc)
assert.Equal(t, tt.ruleName, rule.name)
err := rule.Check(ctxMock)
if tt.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}
func TestAlwaysDenyRule_Check(t *testing.T) {
tests := []struct {
name string
message string
expectedMsg string
}{
{
name: "deny with custom message",
message: "custom deny message",
expectedMsg: "access error: custom deny message",
},
{
name: "deny with empty message uses default",
message: "",
expectedMsg: "access error: access denied",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rule := AlwaysDeny(tt.message)
err := rule.Check(nil)
assert.Error(t, err)
assert.Equal(t, tt.expectedMsg, err.Error())
})
}
}