@tsed/platform-test-sdk 7.73.3 → 7.73.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/components/PlatformTestSdk.js.map +1 -0
- package/lib/cjs/errors/CustomBadRequest.js.map +1 -0
- package/lib/cjs/errors/CustomInternalError.js.map +1 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/cjs/interfaces/PlatformTestingSdkOpts.js.map +1 -0
- package/lib/cjs/interfaces/index.js.map +1 -0
- package/lib/cjs/modules/feature/FeatureModule.js.map +1 -0
- package/lib/cjs/modules/feature/controllers/FeatureController.js.map +1 -0
- package/lib/cjs/tests/exports.js.map +1 -0
- package/lib/cjs/tests/testAcceptMime.js.map +1 -0
- package/lib/cjs/tests/testAuth.js.map +1 -0
- package/lib/cjs/tests/testBodyParams.js.map +1 -0
- package/lib/cjs/tests/testCache.js.map +1 -0
- package/lib/cjs/tests/testChildrenControllers.js.map +1 -0
- package/lib/cjs/tests/testCookies.js.map +1 -0
- package/lib/cjs/tests/testCustom404.js.map +1 -0
- package/lib/cjs/tests/testDeepQueryParams.js.map +1 -0
- package/lib/cjs/tests/testErrors.js.map +1 -0
- package/lib/cjs/tests/testHandlers.js.map +1 -0
- package/lib/cjs/tests/testHeaderParams.js.map +1 -0
- package/lib/cjs/tests/testHeaders.js.map +1 -0
- package/lib/cjs/tests/testInheritanceController.js.map +1 -0
- package/lib/cjs/tests/testLocals.js.map +1 -0
- package/lib/cjs/tests/testLocation.js.map +1 -0
- package/lib/cjs/tests/testMiddlewares.js.map +1 -0
- package/lib/cjs/tests/testModule.js.map +1 -0
- package/lib/cjs/tests/testMulter.js.map +1 -0
- package/lib/cjs/tests/testPathParams.js.map +1 -0
- package/lib/cjs/tests/testQueryParams.js.map +1 -0
- package/lib/cjs/tests/testRedirect.js.map +1 -0
- package/lib/cjs/tests/testResponse.js.map +1 -0
- package/lib/cjs/tests/testResponseFilter.js.map +1 -0
- package/lib/cjs/tests/testRouting.js.map +1 -0
- package/lib/cjs/tests/testScopeRequest.js.map +1 -0
- package/lib/cjs/tests/testSession.js.map +1 -0
- package/lib/cjs/tests/testStatics.js.map +1 -0
- package/lib/cjs/tests/testStream.js.map +1 -0
- package/lib/cjs/tests/testView.js.map +1 -0
- package/lib/esm/components/PlatformTestSdk.js.map +1 -0
- package/lib/esm/errors/CustomBadRequest.js.map +1 -0
- package/lib/esm/errors/CustomInternalError.js.map +1 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/interfaces/PlatformTestingSdkOpts.js.map +1 -0
- package/lib/esm/interfaces/index.js.map +1 -0
- package/lib/esm/modules/feature/FeatureModule.js.map +1 -0
- package/lib/esm/modules/feature/controllers/FeatureController.js.map +1 -0
- package/lib/esm/tests/exports.js.map +1 -0
- package/lib/esm/tests/testAcceptMime.js.map +1 -0
- package/lib/esm/tests/testAuth.js.map +1 -0
- package/lib/esm/tests/testBodyParams.js.map +1 -0
- package/lib/esm/tests/testCache.js.map +1 -0
- package/lib/esm/tests/testChildrenControllers.js.map +1 -0
- package/lib/esm/tests/testCookies.js.map +1 -0
- package/lib/esm/tests/testCustom404.js.map +1 -0
- package/lib/esm/tests/testDeepQueryParams.js.map +1 -0
- package/lib/esm/tests/testErrors.js.map +1 -0
- package/lib/esm/tests/testHandlers.js.map +1 -0
- package/lib/esm/tests/testHeaderParams.js.map +1 -0
- package/lib/esm/tests/testHeaders.js.map +1 -0
- package/lib/esm/tests/testInheritanceController.js.map +1 -0
- package/lib/esm/tests/testLocals.js.map +1 -0
- package/lib/esm/tests/testLocation.js.map +1 -0
- package/lib/esm/tests/testMiddlewares.js.map +1 -0
- package/lib/esm/tests/testModule.js.map +1 -0
- package/lib/esm/tests/testMulter.js.map +1 -0
- package/lib/esm/tests/testPathParams.js.map +1 -0
- package/lib/esm/tests/testQueryParams.js.map +1 -0
- package/lib/esm/tests/testRedirect.js.map +1 -0
- package/lib/esm/tests/testResponse.js.map +1 -0
- package/lib/esm/tests/testResponseFilter.js.map +1 -0
- package/lib/esm/tests/testRouting.js.map +1 -0
- package/lib/esm/tests/testScopeRequest.js.map +1 -0
- package/lib/esm/tests/testSession.js.map +1 -0
- package/lib/esm/tests/testStatics.js.map +1 -0
- package/lib/esm/tests/testStream.js.map +1 -0
- package/lib/esm/tests/testView.js.map +1 -0
- package/lib/tsconfig.esm.tsbuildinfo +1 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/package.json +14 -14
- package/src/components/PlatformTestSdk.ts +29 -0
- package/src/data/file.txt +1 -0
- package/src/data/response.data.json +3 -0
- package/src/data/swagger.json +32 -0
- package/src/errors/CustomBadRequest.ts +16 -0
- package/src/errors/CustomInternalError.ts +16 -0
- package/src/index.ts +40 -0
- package/src/interfaces/PlatformTestingSdkOpts.ts +10 -0
- package/src/interfaces/index.ts +4 -0
- package/src/modules/feature/FeatureModule.ts +9 -0
- package/src/modules/feature/controllers/FeatureController.ts +11 -0
- package/src/tests/exports.ts +71 -0
- package/src/tests/testAcceptMime.ts +95 -0
- package/src/tests/testAuth.ts +339 -0
- package/src/tests/testBodyParams.ts +483 -0
- package/src/tests/testCache.ts +279 -0
- package/src/tests/testChildrenControllers.ts +55 -0
- package/src/tests/testCookies.ts +36 -0
- package/src/tests/testCustom404.ts +33 -0
- package/src/tests/testDeepQueryParams.ts +120 -0
- package/src/tests/testErrors.ts +226 -0
- package/src/tests/testHandlers.ts +90 -0
- package/src/tests/testHeaderParams.ts +152 -0
- package/src/tests/testHeaders.ts +91 -0
- package/src/tests/testInheritanceController.ts +200 -0
- package/src/tests/testLocals.ts +46 -0
- package/src/tests/testLocation.ts +37 -0
- package/src/tests/testMiddlewares.ts +159 -0
- package/src/tests/testModule.ts +46 -0
- package/src/tests/testMulter.ts +151 -0
- package/src/tests/testPathParams.ts +123 -0
- package/src/tests/testQueryParams.ts +233 -0
- package/src/tests/testRedirect.ts +124 -0
- package/src/tests/testResponse.ts +528 -0
- package/src/tests/testResponseFilter.ts +101 -0
- package/src/tests/testRouting.ts +65 -0
- package/src/tests/testScopeRequest.ts +111 -0
- package/src/tests/testSession.ts +103 -0
- package/src/tests/testStatics.ts +61 -0
- package/src/tests/testStream.ts +59 -0
- package/src/tests/testView.ts +68 -0
- package/tsconfig.json +63 -0
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
import {Context, Controller, Get, Inject, Injectable, Middleware, PlatformTest, Post, Req, UseAuth} from "@tsed/common";
|
|
2
|
+
import {useDecorators} from "@tsed/core";
|
|
3
|
+
import {BadRequest, Forbidden, Unauthorized} from "@tsed/exceptions";
|
|
4
|
+
import {In, Returns, Security} from "@tsed/schema";
|
|
5
|
+
import SuperTest from "supertest";
|
|
6
|
+
import baseSpec from "../data/swagger.json";
|
|
7
|
+
import {PlatformTestingSdkOpts} from "../interfaces/index.js";
|
|
8
|
+
|
|
9
|
+
@Injectable()
|
|
10
|
+
export class TokenService {
|
|
11
|
+
private _token: string = "EMPTY";
|
|
12
|
+
|
|
13
|
+
token(token?: string) {
|
|
14
|
+
if (token) {
|
|
15
|
+
this._token = token;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return this._token;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
isValid(token: string | undefined) {
|
|
22
|
+
return String(token).match(/token/);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
@Middleware()
|
|
27
|
+
class OAuthMiddleware {
|
|
28
|
+
@Inject()
|
|
29
|
+
tokenService: TokenService;
|
|
30
|
+
|
|
31
|
+
public use(@Context() ctx: Context) {
|
|
32
|
+
const {endpoint, request} = ctx;
|
|
33
|
+
|
|
34
|
+
const options = endpoint.get(OAuthMiddleware) || {};
|
|
35
|
+
|
|
36
|
+
if (!request.get("authorization")) {
|
|
37
|
+
throw new Unauthorized("Unauthorized");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (!this.tokenService.isValid(request.get("authorization"))) {
|
|
41
|
+
throw new BadRequest("Bad token format");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (options && options.role === "admin" && request.get("authorization") !== "admin_token") {
|
|
45
|
+
throw new Forbidden("Forbidden");
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
ctx.getRequest().user = {
|
|
49
|
+
id: "id",
|
|
50
|
+
name: "name"
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function OAuth(options: any = {}): Function {
|
|
56
|
+
return useDecorators(
|
|
57
|
+
UseAuth(OAuthMiddleware, options),
|
|
58
|
+
Security("global_auth", ...(options.scopes || [])),
|
|
59
|
+
In("header").Type(String).Name("Authorization").Required(),
|
|
60
|
+
Returns(401).Description("Unauthorized"),
|
|
61
|
+
Returns(403).Description("Forbidden")
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
@Controller("/auth")
|
|
66
|
+
class TestAuthCtrl {
|
|
67
|
+
@Inject()
|
|
68
|
+
tokenService: TokenService;
|
|
69
|
+
|
|
70
|
+
@Post("/authorize")
|
|
71
|
+
authorize() {
|
|
72
|
+
this.tokenService.token("access_token");
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
access_token: this.tokenService.token()
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
@Get("/userinfo")
|
|
80
|
+
@OAuth()
|
|
81
|
+
token(@Req("user") user: any) {
|
|
82
|
+
return user;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
@Get("/admin")
|
|
86
|
+
@OAuth({role: "admin", scopes: ["admin"]})
|
|
87
|
+
admin() {
|
|
88
|
+
return {
|
|
89
|
+
granted: true
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
@Post("/stepUp")
|
|
94
|
+
stepUp() {
|
|
95
|
+
this.tokenService.token("admin_token");
|
|
96
|
+
|
|
97
|
+
return {
|
|
98
|
+
access_token: this.tokenService.token()
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function testAuth(options: PlatformTestingSdkOpts) {
|
|
104
|
+
let request: SuperTest.Agent;
|
|
105
|
+
|
|
106
|
+
beforeAll(
|
|
107
|
+
PlatformTest.bootstrap(options.server, {
|
|
108
|
+
...options,
|
|
109
|
+
mount: {
|
|
110
|
+
"/rest": [TestAuthCtrl]
|
|
111
|
+
},
|
|
112
|
+
swagger: [
|
|
113
|
+
{
|
|
114
|
+
path: "/doc",
|
|
115
|
+
spec: baseSpec as any
|
|
116
|
+
}
|
|
117
|
+
]
|
|
118
|
+
})
|
|
119
|
+
);
|
|
120
|
+
beforeAll(() => {
|
|
121
|
+
request = SuperTest.agent(PlatformTest.callback());
|
|
122
|
+
});
|
|
123
|
+
afterAll(PlatformTest.reset);
|
|
124
|
+
|
|
125
|
+
describe("Scenario 1: Create token, test token and stepup token", () => {
|
|
126
|
+
it("should create a token, call /userinfo to get userinfo and try admin route", async () => {
|
|
127
|
+
await request.get("/rest/auth/userinfo").expect(401);
|
|
128
|
+
await request
|
|
129
|
+
.get("/rest/auth/userinfo")
|
|
130
|
+
.set({
|
|
131
|
+
Authorization: "wrong"
|
|
132
|
+
})
|
|
133
|
+
.expect(400);
|
|
134
|
+
|
|
135
|
+
const {body} = await request.post("/rest/auth/authorize").expect(200);
|
|
136
|
+
|
|
137
|
+
expect(body.access_token).toEqual("access_token");
|
|
138
|
+
|
|
139
|
+
const {body: userInfo} = await request
|
|
140
|
+
.get("/rest/auth/userinfo")
|
|
141
|
+
.set({
|
|
142
|
+
Authorization: body.access_token
|
|
143
|
+
})
|
|
144
|
+
.expect(200);
|
|
145
|
+
|
|
146
|
+
expect(userInfo).toEqual({
|
|
147
|
+
id: "id",
|
|
148
|
+
name: "name"
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
await request
|
|
152
|
+
.get("/rest/auth/admin")
|
|
153
|
+
.set({
|
|
154
|
+
Authorization: body.access_token
|
|
155
|
+
})
|
|
156
|
+
.expect(403);
|
|
157
|
+
|
|
158
|
+
const {
|
|
159
|
+
body: {access_token}
|
|
160
|
+
} = await request.post("/rest/auth/stepUp").expect(200);
|
|
161
|
+
|
|
162
|
+
expect(access_token).toEqual("admin_token");
|
|
163
|
+
|
|
164
|
+
const {body: result} = await request
|
|
165
|
+
.get("/rest/auth/admin")
|
|
166
|
+
.set({
|
|
167
|
+
Authorization: access_token
|
|
168
|
+
})
|
|
169
|
+
.expect(200);
|
|
170
|
+
|
|
171
|
+
expect(result).toEqual({
|
|
172
|
+
granted: true
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
describe("Scenario 2: GET /swagger.json", () => {
|
|
178
|
+
it("should generate the swagger.spec", async () => {
|
|
179
|
+
const {body: spec} = await request.get("/doc/swagger.json").expect(200);
|
|
180
|
+
|
|
181
|
+
expect(spec).toEqual({
|
|
182
|
+
openapi: "3.0.1",
|
|
183
|
+
info: {
|
|
184
|
+
version: "1.0.0",
|
|
185
|
+
title: "Swagger Title",
|
|
186
|
+
description: "Swagger description",
|
|
187
|
+
termsOfService: "http://swagger.io/terms/",
|
|
188
|
+
contact: {email: "apiteam@swagger.io"},
|
|
189
|
+
license: {name: "Apache 2.0", url: "http://www.apache.org/licenses/LICENSE-2.0.html"}
|
|
190
|
+
},
|
|
191
|
+
paths: {
|
|
192
|
+
"/rest/auth/authorize": {
|
|
193
|
+
post: {
|
|
194
|
+
operationId: "testAuthCtrlAuthorize",
|
|
195
|
+
responses: {"200": {description: "Success"}},
|
|
196
|
+
parameters: [],
|
|
197
|
+
tags: ["TestAuthCtrl"]
|
|
198
|
+
}
|
|
199
|
+
},
|
|
200
|
+
"/rest/auth/userinfo": {
|
|
201
|
+
get: {
|
|
202
|
+
operationId: "testAuthCtrlToken",
|
|
203
|
+
responses: {
|
|
204
|
+
"401": {
|
|
205
|
+
content: {"application/json": {schema: {$ref: "#/components/schemas/Unauthorized"}}},
|
|
206
|
+
description: "Unauthorized"
|
|
207
|
+
},
|
|
208
|
+
"403": {
|
|
209
|
+
content: {"application/json": {schema: {$ref: "#/components/schemas/Forbidden"}}},
|
|
210
|
+
description: "Forbidden"
|
|
211
|
+
}
|
|
212
|
+
},
|
|
213
|
+
security: [{global_auth: []}],
|
|
214
|
+
parameters: [
|
|
215
|
+
{
|
|
216
|
+
name: "Authorization",
|
|
217
|
+
required: true,
|
|
218
|
+
in: "header",
|
|
219
|
+
schema: {type: "string"}
|
|
220
|
+
}
|
|
221
|
+
],
|
|
222
|
+
tags: ["TestAuthCtrl"]
|
|
223
|
+
}
|
|
224
|
+
},
|
|
225
|
+
"/rest/auth/admin": {
|
|
226
|
+
get: {
|
|
227
|
+
operationId: "testAuthCtrlAdmin",
|
|
228
|
+
responses: {
|
|
229
|
+
"401": {
|
|
230
|
+
content: {"application/json": {schema: {$ref: "#/components/schemas/Unauthorized"}}},
|
|
231
|
+
description: "Unauthorized"
|
|
232
|
+
},
|
|
233
|
+
"403": {
|
|
234
|
+
content: {"application/json": {schema: {$ref: "#/components/schemas/Forbidden"}}},
|
|
235
|
+
description: "Forbidden"
|
|
236
|
+
}
|
|
237
|
+
},
|
|
238
|
+
security: [{global_auth: ["admin"]}],
|
|
239
|
+
parameters: [
|
|
240
|
+
{
|
|
241
|
+
name: "Authorization",
|
|
242
|
+
required: true,
|
|
243
|
+
in: "header",
|
|
244
|
+
schema: {type: "string"}
|
|
245
|
+
}
|
|
246
|
+
],
|
|
247
|
+
tags: ["TestAuthCtrl"]
|
|
248
|
+
}
|
|
249
|
+
},
|
|
250
|
+
"/rest/auth/stepUp": {
|
|
251
|
+
post: {
|
|
252
|
+
operationId: "testAuthCtrlStepUp",
|
|
253
|
+
responses: {"200": {description: "Success"}},
|
|
254
|
+
parameters: [],
|
|
255
|
+
tags: ["TestAuthCtrl"]
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
},
|
|
259
|
+
components: {
|
|
260
|
+
securitySchemes: {
|
|
261
|
+
global_auth: {
|
|
262
|
+
type: "oauth2",
|
|
263
|
+
flows: {
|
|
264
|
+
implicit: {
|
|
265
|
+
authorizationUrl: "http://petstore.swagger.io/oauth/dialog",
|
|
266
|
+
scopes: {admin: "Admin access"}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
},
|
|
271
|
+
schemas: {
|
|
272
|
+
Unauthorized: {
|
|
273
|
+
type: "object",
|
|
274
|
+
properties: {
|
|
275
|
+
name: {
|
|
276
|
+
type: "string",
|
|
277
|
+
description: "The error name",
|
|
278
|
+
minLength: 1,
|
|
279
|
+
example: "UNAUTHORIZED",
|
|
280
|
+
default: "UNAUTHORIZED"
|
|
281
|
+
},
|
|
282
|
+
message: {type: "string", description: "An error message", minLength: 1},
|
|
283
|
+
status: {
|
|
284
|
+
type: "number",
|
|
285
|
+
description: "The status code of the exception",
|
|
286
|
+
example: 401,
|
|
287
|
+
default: 401
|
|
288
|
+
},
|
|
289
|
+
errors: {
|
|
290
|
+
type: "array",
|
|
291
|
+
items: {$ref: "#/components/schemas/GenericError"},
|
|
292
|
+
description: "A list of related errors"
|
|
293
|
+
},
|
|
294
|
+
stack: {type: "string", description: "The stack trace (only in development mode)"}
|
|
295
|
+
},
|
|
296
|
+
required: ["name", "message", "status"]
|
|
297
|
+
},
|
|
298
|
+
GenericError: {
|
|
299
|
+
type: "object",
|
|
300
|
+
properties: {
|
|
301
|
+
name: {type: "string", description: "The error name", minLength: 1},
|
|
302
|
+
message: {type: "string", description: "An error message", minLength: 1}
|
|
303
|
+
},
|
|
304
|
+
additionalProperties: true,
|
|
305
|
+
required: ["name", "message"]
|
|
306
|
+
},
|
|
307
|
+
Forbidden: {
|
|
308
|
+
type: "object",
|
|
309
|
+
properties: {
|
|
310
|
+
name: {
|
|
311
|
+
type: "string",
|
|
312
|
+
description: "The error name",
|
|
313
|
+
minLength: 1,
|
|
314
|
+
example: "FORBIDDEN",
|
|
315
|
+
default: "FORBIDDEN"
|
|
316
|
+
},
|
|
317
|
+
message: {type: "string", description: "An error message", minLength: 1},
|
|
318
|
+
status: {
|
|
319
|
+
type: "number",
|
|
320
|
+
description: "The status code of the exception",
|
|
321
|
+
example: 403,
|
|
322
|
+
default: 403
|
|
323
|
+
},
|
|
324
|
+
errors: {
|
|
325
|
+
type: "array",
|
|
326
|
+
items: {$ref: "#/components/schemas/GenericError"},
|
|
327
|
+
description: "A list of related errors"
|
|
328
|
+
},
|
|
329
|
+
stack: {type: "string", description: "The stack trace (only in development mode)"}
|
|
330
|
+
},
|
|
331
|
+
required: ["name", "message", "status"]
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
},
|
|
335
|
+
tags: [{name: "TestAuthCtrl"}]
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
}
|