-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmethod.handler_test.go
227 lines (185 loc) · 5.91 KB
/
method.handler_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
package jonson
import (
"context"
"testing"
"time"
)
type TestSystem struct {
}
func NewTestSystem() *TestSystem {
return &TestSystem{}
}
type CurrentTimeV1Result struct {
Ts int64 `json:"ts"`
}
func (t *TestSystem) CurrentTimeV1(ctx *Context, public *TestPublic, _ HttpGet) (*CurrentTimeV1Result, error) {
nw := t.getCurrentTime(ctx)
RequireLogger(ctx).Info("current time", "currentTime", nw)
return &CurrentTimeV1Result{
Ts: nw,
}, nil
}
func (t *TestSystem) getCurrentTime(ctx *Context) int64 {
nw := RequireTime(ctx).Now().Unix()
RequireLogger(ctx).Info("getCurrentTime()", "currentTime", nw)
return nw
}
type MeV1Result struct {
Uuid string
Name string
HttpMethod RpcHttpMethod
}
func (t *TestSystem) MeV1(ctx *Context, private *TestPrivate, _ HttpGet) (*MeV1Result, error) {
return &MeV1Result{
Uuid: private.AccountUuid(),
Name: "Silvio",
HttpMethod: RequireRpcMeta(ctx).HttpMethod,
}, nil
}
func (t *TestSystem) MeErrorV1(ctx *Context, private *TestPrivate, _ HttpGet) (*MeV1Result, error) {
return nil, ErrInternal.CloneWithData(&ErrorData{
Details: []*Error{
{
Code: 10000,
Message: "failed to retrieve profiles",
},
},
})
}
// CheckRequirables makes sure all default requireables are available
// which are available for all handlers (http, rpc over http, ws)
func (t *TestSystem) CheckRequirablesV1(ctx *Context, public *TestPublic, _ HttpGet) (err error) {
defer func() {
if r := recover(); r != nil {
err = getRecoverError(r)
}
}()
RequireHttpRequest(ctx)
RequireHttpResponseWriter(ctx)
RequireSecret(ctx)
RequireRpcMeta(ctx)
return nil
}
type GetProfileV1Params struct {
Params
Uuid string `json:"uuid"`
}
func (g *GetProfileV1Params) JonsonValidate(v *Validator) {
if len(g.Uuid) > 36 {
v.Path("uuid").Message("uuid invalid")
}
}
type GetProfileV1Result struct {
Name string `json:"name"`
HttpMethod RpcHttpMethod
}
func (t *TestSystem) GetProfileV1(ctx *Context, private *TestPrivate, _ HttpPost, params *GetProfileV1Params) (*GetProfileV1Result, error) {
if params.Uuid != testAccountUuid {
return nil, ErrInvalidParams.CloneWithData(&ErrorData{
Details: []*Error{
{
Code: 10001,
Message: "profile not found",
},
},
})
}
return &GetProfileV1Result{
Name: "Silvio",
HttpMethod: RequireRpcMeta(ctx).HttpMethod,
}, nil
}
func TestMethodHandler(t *testing.T) {
tm := time.Now()
testProvider := NewTestProvider()
factory := NewFactory()
factory.RegisterProvider(testProvider)
factory.RegisterProvider(NewTimeProvider(func() Time {
return newMockTime(tm)
}))
testSystem := NewTestSystem()
secret := NewDebugSecret()
methodHandler := NewMethodHandler(factory, secret, &MethodHandlerOptions{
MissingValidationLevel: MissingValidationLevelFatal,
})
methodHandler.RegisterSystem(testSystem)
t.Run("can get current time", func(t *testing.T) {
ctx := NewContext(context.Background(), factory, methodHandler)
_res, err := methodHandler.CallMethod(ctx, "test-system/current-time.v1", RpcHttpMethodGet, nil, nil)
if err != nil {
t.Fatal(err)
}
res := _res.(*CurrentTimeV1Result)
if res.Ts != tm.Unix() {
t.Fatalf("expected time to match, got: %d %d", res.Ts, tm.Unix())
}
})
t.Run("returns error on errorness endpoint", func(t *testing.T) {
// toggle flag to grant access to private
testProvider.setLoggedIn(true)
ctx := NewContext(context.Background(), factory, methodHandler)
_, err := methodHandler.CallMethod(ctx, "test-system/me-error.v1", RpcHttpMethodGet, nil, nil)
if err == nil {
t.Fatal("expected call to fail")
}
errRes := err.(*Error)
if errRes.Code != ErrInternal.Code {
t.Fatalf("expected err internal, got: %v", errRes.Code)
}
})
t.Run("fails accessing 'me' due to missing authorization", func(t *testing.T) {
// toggle flag to grant access to private
testProvider.setLoggedIn(false)
ctx := NewContext(context.Background(), factory, methodHandler)
_, err := methodHandler.CallMethod(ctx, "test-system/me-error.v1", RpcHttpMethodGet, nil, nil)
if err == nil {
t.Fatal("expected call to fail")
}
errRes := err.(*Error)
if errRes.Code != ErrUnauthorized.Code {
t.Fatalf("expected err unauthorized, got: %v", errRes.Code)
}
})
t.Run("first can access and then won't be able to access due to missing permissions using the same initial context", func(t *testing.T) {
// toggle flag to grant access to private
testProvider.setLoggedIn(true)
ctx := NewContext(context.Background(), factory, methodHandler)
_, err := methodHandler.CallMethod(ctx, "test-system/me.v1", RpcHttpMethodGet, nil, nil)
if err != nil {
t.Fatalf("expected call succeed: %s", err)
}
testProvider.setLoggedIn(false)
_, err = methodHandler.CallMethod(ctx, "test-system/me.v1", RpcHttpMethodGet, nil, nil)
if err == nil {
t.Fatalf("expected call fail: user got logged out")
}
})
t.Run("http method is GET", func(t *testing.T) {
// toggle flag to grant access to private
testProvider.setLoggedIn(true)
ctx := NewContext(context.Background(), factory, methodHandler)
_res, err := methodHandler.CallMethod(ctx, "test-system/me.v1", RpcHttpMethodGet, nil, nil)
if err != nil {
t.Fatalf("expected call succeed: %s", err)
}
res := _res.(*MeV1Result)
if res.HttpMethod != RpcHttpMethodGet {
t.Fatalf("expected http method to equal GET, got: %s", res.HttpMethod)
}
})
t.Run("http method is POST", func(t *testing.T) {
// toggle flag to grant access to private
testProvider.setLoggedIn(true)
ctx := NewContext(context.Background(), factory, methodHandler)
_res, err := methodHandler.CallMethod(ctx, "test-system/get-profile.v1", RpcHttpMethodPost, &GetProfileV1Params{
Uuid: testAccountUuid,
}, nil)
if err != nil {
t.Fatalf("expected call succeed: %s", err)
}
res := _res.(*GetProfileV1Result)
if res.HttpMethod != RpcHttpMethodPost {
t.Fatalf("expected http method to equal POST, got: %s", res.HttpMethod)
}
})
}