forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_success_test.go
194 lines (178 loc) · 4.58 KB
/
handler_success_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
package gateway
import (
"context"
"net/http"
"testing"
"github.com/TykTechnologies/graphql-go-tools/pkg/graphql"
"github.com/TykTechnologies/tyk-pump/analytics"
"github.com/TykTechnologies/tyk/test"
"github.com/stretchr/testify/assert"
"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/config"
ctxpkg "github.com/TykTechnologies/tyk/ctx"
"github.com/TykTechnologies/tyk/user"
)
type bindContextFunc = func(context.Context) context.Context
type bindAPIDefFunc = func(*APISpec)
func testRequestWithContext(binding bindContextFunc) *http.Request {
req, _ := http.NewRequest("GET", "/", nil)
ctx := req.Context()
if binding != nil {
ctx = binding(ctx)
}
return req.WithContext(ctx)
}
func testAPISpec(binding bindAPIDefFunc) *APISpec {
spec := &APISpec{
APIDefinition: &apidef.APIDefinition{},
GlobalConfig: config.Config{},
}
if binding != nil {
binding(spec)
}
return spec
}
func TestRecordDetail(t *testing.T) {
testcases := []struct {
title string
spec *APISpec
binding bindContextFunc
expect bool
}{
{
title: "empty session",
spec: testAPISpec(nil),
expect: false,
},
{
title: "empty session, enabled analytics",
spec: testAPISpec(func(spec *APISpec) {
spec.EnableDetailedRecording = true
}),
expect: true,
},
{
title: "empty session, enabled config",
spec: testAPISpec(func(spec *APISpec) {
spec.GlobalConfig.EnforceOrgDataDetailLogging = false
spec.GlobalConfig.AnalyticsConfig.EnableDetailedRecording = true
}),
expect: true,
},
{
title: "normal session",
spec: testAPISpec(nil),
// attach user session
binding: func(ctx context.Context) context.Context {
session := &user.SessionState{
EnableDetailedRecording: true,
}
return context.WithValue(ctx, ctxpkg.SessionData, session)
},
expect: true,
},
{
title: "org empty session",
spec: testAPISpec(func(spec *APISpec) {
spec.GlobalConfig.EnforceOrgDataDetailLogging = true
}),
expect: false,
},
{
title: "org session",
spec: testAPISpec(func(spec *APISpec) {
spec.GlobalConfig.EnforceOrgDataDetailLogging = true
}),
// attach user session
binding: func(ctx context.Context) context.Context {
session := &user.SessionState{
EnableDetailedRecording: true,
}
return context.WithValue(ctx, ctxpkg.OrgSessionContext, session)
},
expect: true,
},
}
for _, tc := range testcases {
t.Run(tc.title, func(t *testing.T) {
req := testRequestWithContext(tc.binding)
got := recordDetail(req, tc.spec)
assert.Equal(t, tc.expect, got)
})
}
}
func TestAnalyticsIgnoreSubgraph(t *testing.T) {
ts := StartTest(nil)
defer ts.Close()
accountsSubgraph := BuildAPI(func(spec *APISpec) {
spec.Name = "subgraph-accounts"
spec.APIID = "subgraph-accounts"
spec.Proxy.TargetURL = testSubgraphAccounts
spec.Proxy.ListenPath = "/subgraph-accounts"
spec.GraphQL = apidef.GraphQLConfig{
Enabled: true,
ExecutionMode: apidef.GraphQLExecutionModeSubgraph,
Version: apidef.GraphQLConfigVersion2,
Schema: gqlSubgraphSchemaAccounts,
Subgraph: apidef.GraphQLSubgraphConfig{
SDL: gqlSubgraphSDLAccounts,
},
}
})[0]
superGraph := BuildAPI(func(spec *APISpec) {
spec.Name = "supergraph"
spec.APIID = "supergraph"
spec.Proxy.ListenPath = "/supergraph"
spec.GraphQL = apidef.GraphQLConfig{
Enabled: true,
ExecutionMode: apidef.GraphQLExecutionModeSupergraph,
Version: apidef.GraphQLConfigVersion2,
Supergraph: apidef.GraphQLSupergraphConfig{
Subgraphs: []apidef.GraphQLSubgraphEntity{
{
Name: "subgraph-accounts",
APIID: "subgraph-accounts",
SDL: gqlSubgraphSDLAccounts,
URL: "tyk://subgraph-accounts",
},
},
MergedSDL: gqlMergedSupergraphSDL,
},
Schema: gqlMergedSupergraphSDL,
}
})[0]
ts.Gw.LoadAPI(accountsSubgraph, superGraph)
ts.Gw.Analytics.mockEnabled = true
ts.Gw.Analytics.mockRecordHit = func(record *analytics.AnalyticsRecord) {
if record.APIID != "subgraph-accounts" {
return
}
found := false
for _, val := range record.Tags {
if val == "tyk-graph-analytics" {
found = true
break
}
}
if record.ApiSchema != "" && found {
t.Error("subgraph request should not tagged or have schema")
}
}
_, err := ts.Run(t,
test.TestCase{
Path: "/supergraph",
Data: graphql.Request{
Query: `query Query { me { id username} }`,
},
Code: 200,
},
test.TestCase{
Path: "/supergraph",
Data: graphql.Request{
Query: `query Query { mem { id username} }`,
},
Code: 400,
},
)
assert.NoError(t, err)
}