-
Notifications
You must be signed in to change notification settings - Fork 11
/
rejecter_test.go
79 lines (71 loc) · 1.57 KB
/
rejecter_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
package cel
import (
"bytes"
"fmt"
"testing"
"time"
"github.com/krakendio/krakend-cel/v2/internal"
"github.com/luraproject/lura/v2/config"
"github.com/luraproject/lura/v2/logging"
)
func TestRejecter_Reject(t *testing.T) {
timeNow = func() time.Time {
loc, _ := time.LoadLocation("UTC")
return time.Date(2018, 12, 10, 0, 0, 0, 0, loc)
}
defer func() { timeNow = time.Now }()
buff := bytes.NewBuffer(make([]byte, 1024))
logger, err := logging.NewLogger("DEBUG", buff, "pref")
if err != nil {
t.Error("building the logger:", err.Error())
return
}
rejecter := NewRejecter(logger, &config.EndpointConfig{
Endpoint: "/",
ExtraConfig: config.ExtraConfig{
internal.Namespace: []internal.InterpretableDefinition{
{CheckExpression: "has(JWT.user_id) && has(JWT.enabled_days) && (timestamp(now).getDayOfWeek() in JWT.enabled_days)"},
},
},
})
defer func() {
fmt.Println(buff.String())
}()
if rejecter == nil {
t.Error("nil rejecter")
return
}
for _, tc := range []struct {
data map[string]interface{}
expected bool
}{
{
data: map[string]interface{}{},
expected: true,
},
{
data: map[string]interface{}{
"user_id": 1,
},
expected: true,
},
{
data: map[string]interface{}{
"user_id": 1,
"enabled_days": []int{},
},
expected: true,
},
{
data: map[string]interface{}{
"user_id": 1,
"enabled_days": []int{1, 2, 3, 4, 5},
},
expected: false,
},
} {
if res := rejecter.Reject(tc.data); res != tc.expected {
t.Errorf("%+v => unexpected response %v", tc.data, res)
}
}
}