-
Notifications
You must be signed in to change notification settings - Fork 2
/
controller_test.go
433 lines (342 loc) · 10.7 KB
/
controller_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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package raiden_test
import (
"encoding/json"
"errors"
"fmt"
"testing"
"github.com/sev-2/raiden"
"github.com/sev-2/raiden/pkg/mock"
"github.com/stretchr/testify/assert"
"github.com/valyala/fasthttp"
)
type Payload struct {
Type string `query:"type"`
Name string `json:"name" validate:"required"`
}
type Result struct {
Message string `json:"message"`
}
type Controller struct {
raiden.ControllerBase
// Reserved Field
Payload *Payload
Result Result
}
func (h *Controller) Get(ctx raiden.Context) error {
h.Result.Message = fmt.Sprintf("%s - hello %s", h.Payload.Type, h.Payload.Name)
return ctx.SendJson(h.Result)
}
func (h *Controller) Post(ctx raiden.Context) error {
h.Result.Message = fmt.Sprintf("Data: %v", ctx.RequestContext().Request.String())
return ctx.SendJson(h.Result)
}
type DataController struct {
raiden.ControllerBase
// Reserved Field
Payload map[string]any
Result map[string]any
}
func (h *DataController) BeforeGet(ctx raiden.Context) error {
ctx.Set("message", "from before get middleware")
return nil
}
func (h *DataController) Get(ctx raiden.Context) error {
msg := ctx.Get("message")
msg, isString := msg.(string)
if !isString {
return ctx.SendErrorWithCode(500, errors.New("get - invalid message type"))
}
if msg != "from before get middleware" {
return ctx.SendErrorWithCode(500, errors.New("get - invalid message value"))
}
h.Result = map[string]any{
"message": msg,
}
ctx.Set("message", "from get handler")
return ctx.SendJson(h.Result)
}
func (h *DataController) AfterGet(ctx raiden.Context) error {
msg := ctx.Get("message")
msg, isString := msg.(string)
if !isString {
return ctx.SendErrorWithCode(500, errors.New("after get - invalid message type"))
}
if msg != "from get handler" {
return ctx.SendErrorWithCode(500, errors.New("after get - invalid message value"))
}
return nil
}
func (h *DataController) BeforePost(ctx raiden.Context) error {
ctx.Set("message", "from before post middleware")
return nil
}
func (h *DataController) Post(ctx raiden.Context) error {
msg := ctx.Get("message")
msg, isString := msg.(string)
if !isString {
return ctx.SendErrorWithCode(500, errors.New("post - invalid message type"))
}
if msg != "from before post middleware" {
return ctx.SendErrorWithCode(500, errors.New("post - invalid message value"))
}
h.Result = map[string]any{
"message": msg,
}
ctx.Set("message", "from post handler")
return ctx.SendJson(h.Result)
}
func (h *DataController) AfterPost(ctx raiden.Context) error {
msg := ctx.Get("message")
msg, isString := msg.(string)
if !isString {
return ctx.SendErrorWithCode(500, errors.New("after post - invalid message type"))
}
if msg != "from post handler" {
return ctx.SendErrorWithCode(500, errors.New("after post - invalid message value"))
}
return nil
}
func TestController_Handler(t *testing.T) {
var testResult any
mockContext := mock.MockContext{
SendJsonFn: func(data any) error {
testResult = data
return nil
},
SendErrorWithCodeFn: func(statusCode int, err error) error {
return err
},
}
// setup required data
controller := &Controller{}
// setup request
jsonBodyString := "{\"name\":\"raiden\"}"
requestCtx := &fasthttp.RequestCtx{
Request: fasthttp.Request{},
}
requestCtx.Request.SetBodyRaw([]byte(jsonBodyString))
requestCtx.Request.URI().QueryArgs().Set("type", "greeting")
// inject request
err := raiden.MarshallAndValidate(requestCtx, controller)
assert.NoError(t, err)
err = controller.Get(&mockContext)
assert.NoError(t, err)
result, isResult := testResult.(Result)
assert.Equal(t, true, isResult)
assert.Equal(t, fmt.Sprintf("%s - hello %s", "greeting", "raiden"), result.Message)
}
func TestController_PassData(t *testing.T) {
var reqCtx fasthttp.RequestCtx
context := raiden.Ctx{
RequestCtx: &reqCtx,
}
// setup required data
controller := &DataController{}
err := controller.BeforeGet(&context)
assert.NoError(t, err)
err = controller.Get(&context)
assert.NoError(t, err)
err = controller.AfterGet(&context)
assert.NoError(t, err)
response := reqCtx.Response.Body()
assert.NotNil(t, response)
mapData := make(map[string]any)
err = json.Unmarshal(response, &mapData)
assert.NoError(t, err)
message, isMessageExist := mapData["message"]
assert.True(t, isMessageExist)
assert.Equal(t, "from before get middleware", message)
}
func newMockCtx() *raiden.Ctx {
ctx := &raiden.Ctx{
Context: mockCtx,
RequestCtx: &fasthttp.RequestCtx{},
}
ctx.SetSpan(mockSpan)
return ctx
}
// Test ControllerBase methods
func TestControllerBase(t *testing.T) {
ctx := newMockCtx()
base := &raiden.ControllerBase{}
// Test all ControllerBase methods
assert.NoError(t, base.BeforeAll(ctx))
assert.NoError(t, base.AfterAll(ctx))
assert.NoError(t, base.BeforeGet(ctx))
assert.Error(t, base.Get(ctx))
assert.NoError(t, base.AfterGet(ctx))
assert.NoError(t, base.BeforePost(ctx))
assert.Error(t, base.Post(ctx))
assert.NoError(t, base.AfterPost(ctx))
assert.NoError(t, base.BeforePut(ctx))
assert.Error(t, base.Put(ctx))
assert.NoError(t, base.AfterPut(ctx))
assert.NoError(t, base.BeforePatch(ctx))
assert.Error(t, base.Patch(ctx))
assert.NoError(t, base.AfterPatch(ctx))
assert.NoError(t, base.BeforeDelete(ctx))
assert.Error(t, base.Delete(ctx))
assert.NoError(t, base.AfterDelete(ctx))
assert.NoError(t, base.BeforeOptions(ctx))
assert.Error(t, base.Options(ctx))
assert.NoError(t, base.AfterOptions(ctx))
assert.NoError(t, base.BeforeHead(ctx))
assert.Error(t, base.Head(ctx))
assert.NoError(t, base.AfterHead(ctx))
}
// Test RestController methods
func TestRestController(t *testing.T) {
ctx := newMockCtx()
rest := raiden.RestController{Controller: &raiden.ControllerBase{}, TableName: "test_table"}
// Test RestController methods
assert.NoError(t, rest.BeforeAll(ctx))
assert.NoError(t, rest.AfterAll(ctx))
assert.NoError(t, rest.BeforeGet(ctx))
assert.NoError(t, rest.AfterGet(ctx))
assert.NoError(t, rest.BeforePost(ctx))
assert.NoError(t, rest.AfterPost(ctx))
assert.NoError(t, rest.BeforePut(ctx))
assert.NoError(t, rest.AfterPut(ctx))
assert.NoError(t, rest.BeforePatch(ctx))
assert.NoError(t, rest.AfterPatch(ctx))
assert.NoError(t, rest.BeforeDelete(ctx))
assert.NoError(t, rest.AfterDelete(ctx))
assert.NoError(t, rest.BeforeOptions(ctx))
assert.NoError(t, rest.AfterOptions(ctx))
assert.NoError(t, rest.BeforeHead(ctx))
assert.NoError(t, rest.AfterHead(ctx))
}
// Test StorageController methods
func TestStorageController(t *testing.T) {
ctx := newMockCtx()
storage := raiden.StorageController{Controller: &raiden.ControllerBase{}, BucketName: "test_bucket"}
// Test StorageController methods
assert.NoError(t, storage.BeforeAll(ctx))
assert.NoError(t, storage.AfterAll(ctx))
assert.NoError(t, storage.BeforeGet(ctx))
assert.Error(t, storage.Get(ctx))
assert.NoError(t, storage.AfterGet(ctx))
assert.NoError(t, storage.BeforePost(ctx))
assert.Error(t, storage.Post(ctx))
assert.NoError(t, storage.AfterPost(ctx))
assert.NoError(t, storage.BeforePut(ctx))
assert.Error(t, storage.Put(ctx))
assert.NoError(t, storage.AfterPut(ctx))
assert.NoError(t, storage.BeforePatch(ctx))
assert.Error(t, storage.Patch(ctx))
assert.NoError(t, storage.AfterPatch(ctx))
assert.NoError(t, storage.BeforeDelete(ctx))
assert.Error(t, storage.Delete(ctx))
assert.NoError(t, storage.AfterDelete(ctx))
assert.NoError(t, storage.BeforeOptions(ctx))
assert.NoError(t, storage.AfterOptions(ctx))
assert.NoError(t, storage.BeforeHead(ctx))
assert.NoError(t, storage.AfterHead(ctx))
}
// Test AuthProxy
func TestAuthProxy(t *testing.T) {
ctx := &mock.MockContext{
RequestCtx: &fasthttp.RequestCtx{},
ConfigFn: func() *raiden.Config {
return &raiden.Config{SupabasePublicUrl: "/"}
},
}
handler := raiden.AuthProxy(
ctx.Config(),
func(req *fasthttp.Request) {},
func(resp *fasthttp.Response) error { return nil },
)
ctx.Request.SetRequestURI("/")
handler(ctx.RequestCtx)
assert.Equal(t, fasthttp.StatusOK, ctx.Response.StatusCode())
}
// Test AuthProxy
func TestAuthProxy_ActualCallWithoutMock(t *testing.T) {
ctx := &mock.MockContext{
RequestCtx: &fasthttp.RequestCtx{},
ConfigFn: func() *raiden.Config {
return &raiden.Config{SupabasePublicUrl: "/"}
},
}
handler := raiden.AuthProxy(
ctx.Config(),
func(req *fasthttp.Request) {},
func(resp *fasthttp.Response) error { return nil },
)
ctx.Request.SetRequestURI("/auth/v1/signup")
handler(ctx.RequestCtx)
assert.Equal(t, fasthttp.StatusInternalServerError, ctx.Response.StatusCode())
}
func TestAuthProxy_NotAllowedPath(t *testing.T) {
ctx := &mock.MockContext{
RequestCtx: &fasthttp.RequestCtx{},
ConfigFn: func() *raiden.Config {
return &raiden.Config{SupabasePublicUrl: "/"}
},
}
handler := raiden.AuthProxy(
ctx.Config(),
func(req *fasthttp.Request) {},
func(resp *fasthttp.Response) error { return nil },
)
ctx.Request.SetRequestURI("/auth/v1/anymore")
handler(ctx.RequestCtx)
assert.Equal(t, fasthttp.StatusNotFound, ctx.Response.StatusCode())
}
func TestAuthProxy_AllowedWithSpecificPath(t *testing.T) {
ctx := &mock.MockContext{
RequestCtx: &fasthttp.RequestCtx{},
ConfigFn: func() *raiden.Config {
return &raiden.Config{SupabasePublicUrl: "/"}
},
}
handler := raiden.AuthProxy(
ctx.Config(),
func(req *fasthttp.Request) {},
func(resp *fasthttp.Response) error { return nil },
)
ctx.Request.SetRequestURI("/auth/v1/saml/metadata")
handler(ctx.RequestCtx)
assert.Equal(t, fasthttp.StatusInternalServerError, ctx.Response.StatusCode())
}
// Test MarshallAndValidate
func TestMarshallAndValidate(t *testing.T) {
ctx := newMockCtx()
type Request struct {
Search string `query:"q"`
Resource string `path:"resource" validate:"required"`
}
type Controller struct {
raiden.ControllerBase
Payload *Request
}
controller := &Controller{}
ctx.QueryArgs().Set("q", "search_value")
ctx.SetUserValue("resource", "resource_value")
err := raiden.MarshallAndValidate(ctx.RequestContext(), controller)
assert.NoError(t, err)
assert.Equal(t, "search_value", controller.Payload.Search)
assert.Equal(t, "resource_value", controller.Payload.Resource)
}
func TestController_PassDataRestPost(t *testing.T) {
var reqCtx fasthttp.RequestCtx
context := raiden.Ctx{
RequestCtx: &reqCtx,
}
// setup required data
controller := &DataController{}
err := controller.BeforePost(&context)
assert.NoError(t, err)
err = controller.Post(&context)
assert.NoError(t, err)
err = controller.AfterPost(&context)
assert.NoError(t, err)
response := reqCtx.Response.Body()
assert.NotNil(t, response)
mapData := make(map[string]any)
err = json.Unmarshal(response, &mapData)
assert.NoError(t, err)
message, isMessageExist := mapData["message"]
assert.True(t, isMessageExist)
assert.Equal(t, "from before post middleware", message)
}