-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathutils_test.go
150 lines (145 loc) · 3.71 KB
/
utils_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
package controller
import (
"fmt"
"net/http"
"testing"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/model"
"github.com/thomaspoignant/go-feature-flag/ffcontext"
)
func Test_assertRequest(t *testing.T) {
tests := []struct {
name string
req *model.AllFlagRequest
wantErr *echo.HTTPError
want ffcontext.Context
}{
{
name: "no request",
req: nil,
wantErr: echo.NewHTTPError(
http.StatusBadRequest,
"assertRequest: impossible to find user in request"),
},
{
name: "request without evaluation context and user",
req: &model.AllFlagRequest{User: nil, EvaluationContext: nil},
wantErr: echo.NewHTTPError(
http.StatusBadRequest,
"assertRequest: impossible to find user in request"),
},
{
name: "user without key",
req: &model.AllFlagRequest{User: nil},
wantErr: echo.NewHTTPError(
http.StatusBadRequest,
"assertRequest: impossible to find user in request"),
},
{
name: "user with User and EvaluationContext, empty key for evaluation context",
req: &model.AllFlagRequest{
User: &model.UserRequest{Key: "my-key"},
EvaluationContext: &model.EvaluationContextRequest{Key: ""},
},
wantErr: echo.NewHTTPError(
http.StatusBadRequest,
"empty key for evaluation context, impossible to retrieve flags"),
},
{
name: "invalid user but valid evaluation context should pass",
req: &model.AllFlagRequest{
User: &model.UserRequest{Key: ""},
EvaluationContext: &model.EvaluationContextRequest{Key: "my-key"},
},
wantErr: nil,
},
{
name: "valid evaluation context and no user",
req: &model.AllFlagRequest{
EvaluationContext: &model.EvaluationContextRequest{Key: "my-key"},
},
wantErr: nil,
},
{
name: "valid user and no evluation context",
req: &model.AllFlagRequest{
User: &model.UserRequest{Key: "my-key"},
},
wantErr: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := assertRequest(tt.req)
assert.Equal(t, tt.wantErr, err)
})
}
}
func Test_evaluationContextFromRequest(t *testing.T) {
tests := []struct {
name string
req *model.AllFlagRequest
wantErr error
want ffcontext.Context
}{
{
name: "no request",
req: nil,
wantErr: echo.NewHTTPError(
http.StatusBadRequest,
"evaluationContextFromRequest: impossible to convert the request, req nil"),
},
{
name: "user without key",
req: &model.AllFlagRequest{User: nil},
wantErr: fmt.Errorf("userRequestToUser: impossible to convert user, userRequest nil"),
},
{
name: "valid use-case with EvaluationContext",
req: &model.AllFlagRequest{
User: nil,
EvaluationContext: &model.EvaluationContextRequest{
Key: "key-1",
Custom: map[string]interface{}{
"anonymous": false,
"custom-field": true,
},
},
},
want: ffcontext.
NewEvaluationContextBuilder("key-1").
AddCustom("anonymous", false).
AddCustom("custom-field", true).
Build(),
},
{
name: "valid use-case with User",
req: &model.AllFlagRequest{
User: &model.UserRequest{
Key: "key-1",
Anonymous: false,
Custom: map[string]interface{}{
"custom-field": true,
},
},
},
want: ffcontext.NewEvaluationContextBuilder("key-1").
AddCustom("anonymous", false).
AddCustom("custom-field", true).
Build(),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := evaluationContextFromRequest(tt.req)
if err != nil {
assert.Error(t, err)
assert.Equal(t, tt.wantErr, err)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}