-
Notifications
You must be signed in to change notification settings - Fork 1
/
callback_test.go
129 lines (123 loc) · 2.65 KB
/
callback_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
package immune
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestInjectCallbackID(t *testing.T) {
type args struct {
field string
v interface{}
r M
}
tests := []struct {
name string
args args
hasSeparator bool
wantCallbackID string
wantErrMsg string
wantErr bool
}{
{
name: "should_inject_one_level_callback_id",
args: args{
field: "data",
v: "123-564-2242",
r: M{
"data": map[string]interface{}{},
},
},
wantCallbackID: "123-564-2242",
hasSeparator: false,
wantErr: false,
},
{
name: "should_inject_two_level_callback_id",
args: args{
field: "data.ref",
v: "123-564-2242-2394",
r: M{
"data": map[string]interface{}{
"ref": map[string]interface{}{},
},
},
},
wantCallbackID: "123-564-2242-2394",
hasSeparator: true,
wantErr: false,
},
{
name: "should_inject_three_level_callback_id",
args: args{
field: "data.ref.caller",
v: "123-564-2242-32823",
r: M{
"data": map[string]interface{}{
"ref": map[string]interface{}{
"caller": map[string]interface{}{},
},
},
},
},
wantCallbackID: "123-564-2242-32823",
hasSeparator: true,
wantErr: false,
},
{
name: "should_error_for_incorrect_field_type",
args: args{
field: "data",
v: "123-564-2242-32823",
r: M{
"data": 123,
},
},
wantErr: true,
wantErrMsg: "the field data, is not an object in the request body",
},
{
name: "should_error_for_incorrect_field_type",
args: args{
field: "data",
v: "123-564-2242-32823",
r: M{
"ref": 123,
},
},
wantErr: true,
wantErrMsg: "the field data, does not exist",
},
{
name: "should_error_for_field_not_found",
args: args{
field: "data.ref.marvel",
v: "123-564-2242-32823",
r: M{
"data": map[string]interface{}{},
},
},
wantErr: true,
wantErrMsg: "field data.ref: not found",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := InjectCallbackID(tt.args.field, tt.args.v, tt.args.r)
if tt.wantErr {
require.Error(t, err)
require.Equal(t, tt.wantErrMsg, err.Error())
return
}
require.NoError(t, err)
if !tt.hasSeparator {
m := tt.args.r[tt.args.field].(map[string]interface{})
require.Equal(t, tt.wantCallbackID, m[CallbackIDFieldName])
return
}
parts := strings.Split(tt.args.field, ".")
m, err := getM(tt.args.r, parts)
require.NoError(t, err)
require.Equal(t, tt.wantCallbackID, m[CallbackIDFieldName])
})
}
}