@tsed/platform-test-sdk 7.73.1 → 7.73.3
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/package.json +14 -14
- package/lib/cjs/components/PlatformTestSdk.js.map +0 -1
- package/lib/cjs/errors/CustomBadRequest.js.map +0 -1
- package/lib/cjs/errors/CustomInternalError.js.map +0 -1
- package/lib/cjs/index.js.map +0 -1
- package/lib/cjs/interfaces/PlatformTestingSdkOpts.js.map +0 -1
- package/lib/cjs/interfaces/index.js.map +0 -1
- package/lib/cjs/modules/feature/FeatureModule.js.map +0 -1
- package/lib/cjs/modules/feature/controllers/FeatureController.js.map +0 -1
- package/lib/cjs/tests/exports.js.map +0 -1
- package/lib/cjs/tests/testAcceptMime.js.map +0 -1
- package/lib/cjs/tests/testAuth.js.map +0 -1
- package/lib/cjs/tests/testBodyParams.js.map +0 -1
- package/lib/cjs/tests/testCache.js.map +0 -1
- package/lib/cjs/tests/testChildrenControllers.js.map +0 -1
- package/lib/cjs/tests/testCookies.js.map +0 -1
- package/lib/cjs/tests/testCustom404.js.map +0 -1
- package/lib/cjs/tests/testDeepQueryParams.js.map +0 -1
- package/lib/cjs/tests/testErrors.js.map +0 -1
- package/lib/cjs/tests/testHandlers.js.map +0 -1
- package/lib/cjs/tests/testHeaderParams.js.map +0 -1
- package/lib/cjs/tests/testHeaders.js.map +0 -1
- package/lib/cjs/tests/testInheritanceController.js.map +0 -1
- package/lib/cjs/tests/testLocals.js.map +0 -1
- package/lib/cjs/tests/testLocation.js.map +0 -1
- package/lib/cjs/tests/testMiddlewares.js.map +0 -1
- package/lib/cjs/tests/testModule.js.map +0 -1
- package/lib/cjs/tests/testMulter.js.map +0 -1
- package/lib/cjs/tests/testPathParams.js.map +0 -1
- package/lib/cjs/tests/testQueryParams.js.map +0 -1
- package/lib/cjs/tests/testRedirect.js.map +0 -1
- package/lib/cjs/tests/testResponse.js.map +0 -1
- package/lib/cjs/tests/testResponseFilter.js.map +0 -1
- package/lib/cjs/tests/testRouting.js.map +0 -1
- package/lib/cjs/tests/testScopeRequest.js.map +0 -1
- package/lib/cjs/tests/testSession.js.map +0 -1
- package/lib/cjs/tests/testStatics.js.map +0 -1
- package/lib/cjs/tests/testStream.js.map +0 -1
- package/lib/cjs/tests/testView.js.map +0 -1
- package/lib/esm/components/PlatformTestSdk.js.map +0 -1
- package/lib/esm/errors/CustomBadRequest.js.map +0 -1
- package/lib/esm/errors/CustomInternalError.js.map +0 -1
- package/lib/esm/index.js.map +0 -1
- package/lib/esm/interfaces/PlatformTestingSdkOpts.js.map +0 -1
- package/lib/esm/interfaces/index.js.map +0 -1
- package/lib/esm/modules/feature/FeatureModule.js.map +0 -1
- package/lib/esm/modules/feature/controllers/FeatureController.js.map +0 -1
- package/lib/esm/tests/exports.js.map +0 -1
- package/lib/esm/tests/testAcceptMime.js.map +0 -1
- package/lib/esm/tests/testAuth.js.map +0 -1
- package/lib/esm/tests/testBodyParams.js.map +0 -1
- package/lib/esm/tests/testCache.js.map +0 -1
- package/lib/esm/tests/testChildrenControllers.js.map +0 -1
- package/lib/esm/tests/testCookies.js.map +0 -1
- package/lib/esm/tests/testCustom404.js.map +0 -1
- package/lib/esm/tests/testDeepQueryParams.js.map +0 -1
- package/lib/esm/tests/testErrors.js.map +0 -1
- package/lib/esm/tests/testHandlers.js.map +0 -1
- package/lib/esm/tests/testHeaderParams.js.map +0 -1
- package/lib/esm/tests/testHeaders.js.map +0 -1
- package/lib/esm/tests/testInheritanceController.js.map +0 -1
- package/lib/esm/tests/testLocals.js.map +0 -1
- package/lib/esm/tests/testLocation.js.map +0 -1
- package/lib/esm/tests/testMiddlewares.js.map +0 -1
- package/lib/esm/tests/testModule.js.map +0 -1
- package/lib/esm/tests/testMulter.js.map +0 -1
- package/lib/esm/tests/testPathParams.js.map +0 -1
- package/lib/esm/tests/testQueryParams.js.map +0 -1
- package/lib/esm/tests/testRedirect.js.map +0 -1
- package/lib/esm/tests/testResponse.js.map +0 -1
- package/lib/esm/tests/testResponseFilter.js.map +0 -1
- package/lib/esm/tests/testRouting.js.map +0 -1
- package/lib/esm/tests/testScopeRequest.js.map +0 -1
- package/lib/esm/tests/testSession.js.map +0 -1
- package/lib/esm/tests/testStatics.js.map +0 -1
- package/lib/esm/tests/testStream.js.map +0 -1
- package/lib/esm/tests/testView.js.map +0 -1
- package/lib/tsconfig.esm.tsbuildinfo +0 -1
- package/lib/tsconfig.tsbuildinfo +0 -1
- package/src/components/PlatformTestSdk.ts +0 -29
- package/src/data/file.txt +0 -1
- package/src/data/response.data.json +0 -3
- package/src/data/swagger.json +0 -32
- package/src/errors/CustomBadRequest.ts +0 -16
- package/src/errors/CustomInternalError.ts +0 -16
- package/src/index.ts +0 -40
- package/src/interfaces/PlatformTestingSdkOpts.ts +0 -10
- package/src/interfaces/index.ts +0 -4
- package/src/modules/feature/FeatureModule.ts +0 -9
- package/src/modules/feature/controllers/FeatureController.ts +0 -11
- package/src/tests/exports.ts +0 -71
- package/src/tests/testAcceptMime.ts +0 -95
- package/src/tests/testAuth.ts +0 -339
- package/src/tests/testBodyParams.ts +0 -483
- package/src/tests/testCache.ts +0 -279
- package/src/tests/testChildrenControllers.ts +0 -55
- package/src/tests/testCookies.ts +0 -36
- package/src/tests/testCustom404.ts +0 -33
- package/src/tests/testDeepQueryParams.ts +0 -120
- package/src/tests/testErrors.ts +0 -226
- package/src/tests/testHandlers.ts +0 -90
- package/src/tests/testHeaderParams.ts +0 -152
- package/src/tests/testHeaders.ts +0 -91
- package/src/tests/testInheritanceController.ts +0 -200
- package/src/tests/testLocals.ts +0 -46
- package/src/tests/testLocation.ts +0 -37
- package/src/tests/testMiddlewares.ts +0 -159
- package/src/tests/testModule.ts +0 -46
- package/src/tests/testMulter.ts +0 -151
- package/src/tests/testPathParams.ts +0 -123
- package/src/tests/testQueryParams.ts +0 -233
- package/src/tests/testRedirect.ts +0 -124
- package/src/tests/testResponse.ts +0 -528
- package/src/tests/testResponseFilter.ts +0 -101
- package/src/tests/testRouting.ts +0 -65
- package/src/tests/testScopeRequest.ts +0 -111
- package/src/tests/testSession.ts +0 -103
- package/src/tests/testStatics.ts +0 -61
- package/src/tests/testStream.ts +0 -59
- package/src/tests/testView.ts +0 -68
- package/tsconfig.json +0 -63
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import {PlatformTest} from "@tsed/common";
|
|
2
|
-
import {PlatformTestingSdkOpts} from "../interfaces/index.js";
|
|
3
|
-
import {specsContainer} from "../tests/exports.js";
|
|
4
|
-
|
|
5
|
-
export class PlatformTestSdk {
|
|
6
|
-
protected constructor(private options: PlatformTestingSdkOpts) {}
|
|
7
|
-
|
|
8
|
-
static create(options: PlatformTestingSdkOpts) {
|
|
9
|
-
return new PlatformTestSdk(options);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
test(name: string, options: any = {}) {
|
|
13
|
-
specsContainer.get(name)!({
|
|
14
|
-
...this.options,
|
|
15
|
-
...options
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
bootstrap(options: any = {}) {
|
|
20
|
-
return PlatformTest.bootstrap(this.options.server, {
|
|
21
|
-
...this.options,
|
|
22
|
-
...options
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
reset() {
|
|
27
|
-
return PlatformTest.reset;
|
|
28
|
-
}
|
|
29
|
-
}
|
package/src/data/file.txt
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
test content
|
package/src/data/swagger.json
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"openapi": "3.0.1",
|
|
3
|
-
"info": {
|
|
4
|
-
"version": "1.0.0",
|
|
5
|
-
"description": "Swagger description",
|
|
6
|
-
"title": "Swagger Title",
|
|
7
|
-
"termsOfService": "http://swagger.io/terms/",
|
|
8
|
-
"contact": {
|
|
9
|
-
"email": "apiteam@swagger.io"
|
|
10
|
-
},
|
|
11
|
-
"license": {
|
|
12
|
-
"name": "Apache 2.0",
|
|
13
|
-
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
|
|
14
|
-
}
|
|
15
|
-
},
|
|
16
|
-
"components": {
|
|
17
|
-
"securitySchemes": {
|
|
18
|
-
"global_auth": {
|
|
19
|
-
"type": "oauth2",
|
|
20
|
-
"flows": {
|
|
21
|
-
"implicit": {
|
|
22
|
-
"authorizationUrl": "http://petstore.swagger.io/oauth/dialog",
|
|
23
|
-
"scopes": {
|
|
24
|
-
"admin": "Admin access"
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
|
-
"paths": {}
|
|
32
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import {ResponseErrorObject} from "@tsed/common";
|
|
2
|
-
import {BadRequest} from "@tsed/exceptions";
|
|
3
|
-
|
|
4
|
-
export class CustomBadRequest extends BadRequest implements ResponseErrorObject {
|
|
5
|
-
name = "CUSTOM_BAD_REQUEST";
|
|
6
|
-
errors: any[];
|
|
7
|
-
headers = {};
|
|
8
|
-
|
|
9
|
-
constructor(message: string) {
|
|
10
|
-
super(message);
|
|
11
|
-
this.errors = ["test"];
|
|
12
|
-
this.headers = {
|
|
13
|
-
"X-HEADER-ERROR": "deny"
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import {ResponseErrorObject} from "@tsed/common";
|
|
2
|
-
import {InternalServerError} from "@tsed/exceptions";
|
|
3
|
-
|
|
4
|
-
export class CustomInternalError extends InternalServerError implements ResponseErrorObject {
|
|
5
|
-
name = "CUSTOM_INTERNAL_SERVER_ERROR";
|
|
6
|
-
errors: any[];
|
|
7
|
-
headers = {};
|
|
8
|
-
|
|
9
|
-
constructor(message: string) {
|
|
10
|
-
super(message);
|
|
11
|
-
this.errors = ["test"];
|
|
12
|
-
this.headers = {
|
|
13
|
-
"X-HEADER-ERROR": "deny"
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file Automatically generated by @tsed/barrels.
|
|
3
|
-
*/
|
|
4
|
-
export * from "./components/PlatformTestSdk.js";
|
|
5
|
-
export * from "./errors/CustomBadRequest.js";
|
|
6
|
-
export * from "./errors/CustomInternalError.js";
|
|
7
|
-
export * from "./interfaces/index.js";
|
|
8
|
-
export * from "./interfaces/PlatformTestingSdkOpts.js";
|
|
9
|
-
export * from "./modules/feature/controllers/FeatureController.js";
|
|
10
|
-
export * from "./modules/feature/FeatureModule.js";
|
|
11
|
-
export * from "./tests/exports.js";
|
|
12
|
-
export * from "./tests/testAcceptMime.js";
|
|
13
|
-
export * from "./tests/testAuth.js";
|
|
14
|
-
export * from "./tests/testBodyParams.js";
|
|
15
|
-
export * from "./tests/testCache.js";
|
|
16
|
-
export * from "./tests/testChildrenControllers.js";
|
|
17
|
-
export * from "./tests/testCookies.js";
|
|
18
|
-
export * from "./tests/testCustom404.js";
|
|
19
|
-
export * from "./tests/testDeepQueryParams.js";
|
|
20
|
-
export * from "./tests/testErrors.js";
|
|
21
|
-
export * from "./tests/testHandlers.js";
|
|
22
|
-
export * from "./tests/testHeaderParams.js";
|
|
23
|
-
export * from "./tests/testHeaders.js";
|
|
24
|
-
export * from "./tests/testInheritanceController.js";
|
|
25
|
-
export * from "./tests/testLocals.js";
|
|
26
|
-
export * from "./tests/testLocation.js";
|
|
27
|
-
export * from "./tests/testMiddlewares.js";
|
|
28
|
-
export * from "./tests/testModule.js";
|
|
29
|
-
export * from "./tests/testMulter.js";
|
|
30
|
-
export * from "./tests/testPathParams.js";
|
|
31
|
-
export * from "./tests/testQueryParams.js";
|
|
32
|
-
export * from "./tests/testRedirect.js";
|
|
33
|
-
export * from "./tests/testResponse.js";
|
|
34
|
-
export * from "./tests/testResponseFilter.js";
|
|
35
|
-
export * from "./tests/testRouting.js";
|
|
36
|
-
export * from "./tests/testScopeRequest.js";
|
|
37
|
-
export * from "./tests/testSession.js";
|
|
38
|
-
export * from "./tests/testStatics.js";
|
|
39
|
-
export * from "./tests/testStream.js";
|
|
40
|
-
export * from "./tests/testView.js";
|
package/src/interfaces/index.ts
DELETED
package/src/tests/exports.ts
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import {PlatformTestingSdkOpts} from "../interfaces/index.js";
|
|
2
|
-
import {testAcceptMime} from "./testAcceptMime.js";
|
|
3
|
-
import {testAuth} from "./testAuth.js";
|
|
4
|
-
import {testBodyParams} from "./testBodyParams.js";
|
|
5
|
-
import {testChildrenControllers} from "./testChildrenControllers.js";
|
|
6
|
-
import {testCookies} from "./testCookies.js";
|
|
7
|
-
import {testCustom404} from "./testCustom404.js";
|
|
8
|
-
import {testDeepQueryParams} from "./testDeepQueryParams.js";
|
|
9
|
-
import {testErrors} from "./testErrors.js";
|
|
10
|
-
import {testHandlers} from "./testHandlers.js";
|
|
11
|
-
import {testHeaderParams} from "./testHeaderParams.js";
|
|
12
|
-
import {testHeaders} from "./testHeaders.js";
|
|
13
|
-
import {testInheritanceController} from "./testInheritanceController.js";
|
|
14
|
-
import {testLocals} from "./testLocals.js";
|
|
15
|
-
import {testLocation} from "./testLocation.js";
|
|
16
|
-
import {testMiddlewares} from "./testMiddlewares.js";
|
|
17
|
-
import {testModule} from "./testModule.js";
|
|
18
|
-
import {testMulter} from "./testMulter.js";
|
|
19
|
-
import {testPathParams} from "./testPathParams.js";
|
|
20
|
-
import {testQueryParams} from "./testQueryParams.js";
|
|
21
|
-
import {testRedirect} from "./testRedirect.js";
|
|
22
|
-
import {testResponse} from "./testResponse.js";
|
|
23
|
-
import {testResponseFilter} from "./testResponseFilter.js";
|
|
24
|
-
import {testRouting} from "./testRouting.js";
|
|
25
|
-
import {testScopeRequest} from "./testScopeRequest.js";
|
|
26
|
-
import {testSession} from "./testSession.js";
|
|
27
|
-
import {testStatics} from "./testStatics.js";
|
|
28
|
-
import {testStream} from "./testStream.js";
|
|
29
|
-
import {testView} from "./testView.js";
|
|
30
|
-
import {testCache} from "./testCache.js";
|
|
31
|
-
|
|
32
|
-
export const specsContainer = new Map<string, (options: PlatformTestingSdkOpts) => void>();
|
|
33
|
-
|
|
34
|
-
// HANDLERS AND MIDDLEWARES
|
|
35
|
-
specsContainer.set("handlers", testHandlers);
|
|
36
|
-
specsContainer.set("auth", testAuth);
|
|
37
|
-
specsContainer.set("middlewares", testMiddlewares);
|
|
38
|
-
|
|
39
|
-
// RESPONSES
|
|
40
|
-
specsContainer.set("headers", testHeaders);
|
|
41
|
-
specsContainer.set("acceptMime", testAcceptMime);
|
|
42
|
-
specsContainer.set("location", testLocation);
|
|
43
|
-
specsContainer.set("redirect", testRedirect);
|
|
44
|
-
specsContainer.set("view", testView);
|
|
45
|
-
specsContainer.set("errors", testErrors);
|
|
46
|
-
specsContainer.set("response", testResponse);
|
|
47
|
-
specsContainer.set("stream", testStream);
|
|
48
|
-
specsContainer.set("responseFilter", testResponseFilter);
|
|
49
|
-
specsContainer.set("custom404", testCustom404);
|
|
50
|
-
specsContainer.set("statics", testStatics);
|
|
51
|
-
specsContainer.set("cache", testCache);
|
|
52
|
-
|
|
53
|
-
// INPUTS
|
|
54
|
-
specsContainer.set("cookies", testCookies);
|
|
55
|
-
specsContainer.set("session", testSession);
|
|
56
|
-
specsContainer.set("headerParams", testHeaderParams);
|
|
57
|
-
specsContainer.set("bodyParams", testBodyParams);
|
|
58
|
-
specsContainer.set("pathParams", testPathParams);
|
|
59
|
-
specsContainer.set("queryParams", testQueryParams);
|
|
60
|
-
specsContainer.set("deepQueryParams", testDeepQueryParams);
|
|
61
|
-
specsContainer.set("locals", testLocals);
|
|
62
|
-
specsContainer.set("multer", testMulter);
|
|
63
|
-
|
|
64
|
-
// ROUTING
|
|
65
|
-
specsContainer.set("scopeRequest", testScopeRequest);
|
|
66
|
-
specsContainer.set("routing", testRouting);
|
|
67
|
-
|
|
68
|
-
// CODE ORGANIZATION
|
|
69
|
-
specsContainer.set("childrenControllers", testChildrenControllers);
|
|
70
|
-
specsContainer.set("inheritanceController", testInheritanceController);
|
|
71
|
-
specsContainer.set("module", testModule);
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import {AcceptMime, Controller, Get, HeaderParams, PlatformTest, Post} from "@tsed/common";
|
|
2
|
-
import {ContentType} from "@tsed/schema";
|
|
3
|
-
import SuperTest from "supertest";
|
|
4
|
-
import {PlatformTestingSdkOpts} from "../interfaces/index.js";
|
|
5
|
-
|
|
6
|
-
@Controller("/accept-mime")
|
|
7
|
-
class TestAcceptMimeCtrl {
|
|
8
|
-
@Post("/scenario-1")
|
|
9
|
-
@AcceptMime("application/json")
|
|
10
|
-
scenario1(@HeaderParams("Accept") accept: string) {
|
|
11
|
-
return {
|
|
12
|
-
accept
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
@Get("/scenario-2")
|
|
17
|
-
@ContentType("text")
|
|
18
|
-
@AcceptMime("text")
|
|
19
|
-
public ping() {
|
|
20
|
-
return "pong";
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function testAcceptMime(options: PlatformTestingSdkOpts) {
|
|
25
|
-
let request: SuperTest.Agent;
|
|
26
|
-
|
|
27
|
-
beforeAll(
|
|
28
|
-
PlatformTest.bootstrap(options.server, {
|
|
29
|
-
...options,
|
|
30
|
-
mount: {
|
|
31
|
-
"/rest": [TestAcceptMimeCtrl]
|
|
32
|
-
}
|
|
33
|
-
})
|
|
34
|
-
);
|
|
35
|
-
beforeAll(() => {
|
|
36
|
-
request = SuperTest(PlatformTest.callback());
|
|
37
|
-
});
|
|
38
|
-
afterAll(PlatformTest.reset);
|
|
39
|
-
describe("Scenario 1: POST /rest/accept-mime/scenario-1", () => {
|
|
40
|
-
it('should return a 200 response when Accept header match with @AcceptMime("application/json")', async () => {
|
|
41
|
-
const response = await request
|
|
42
|
-
.post("/rest/accept-mime/scenario-1")
|
|
43
|
-
.set({
|
|
44
|
-
Accept: "application/json"
|
|
45
|
-
})
|
|
46
|
-
.expect(200);
|
|
47
|
-
|
|
48
|
-
expect(response.body).toEqual({
|
|
49
|
-
accept: "application/json"
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
it('should return a 406 response when Accept header doesn\'t match with @AcceptMime("application/json")', async () => {
|
|
53
|
-
const response = await request
|
|
54
|
-
.post("/rest/accept-mime/scenario-1")
|
|
55
|
-
.set({
|
|
56
|
-
Accept: "application/xml"
|
|
57
|
-
})
|
|
58
|
-
.expect(406);
|
|
59
|
-
|
|
60
|
-
expect(response.body).toEqual({
|
|
61
|
-
name: "NOT_ACCEPTABLE",
|
|
62
|
-
message: "You must accept content-type application/json",
|
|
63
|
-
status: 406,
|
|
64
|
-
errors: []
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
describe("Scenario 2: GET /rest/accept-mime/scenario-2", () => {
|
|
69
|
-
it('should return a 200 response when Accept header match with @AcceptMime("text")', async () => {
|
|
70
|
-
const response = await request
|
|
71
|
-
.get("/rest/accept-mime/scenario-2")
|
|
72
|
-
.set({
|
|
73
|
-
Accept: "text/*, application/json"
|
|
74
|
-
})
|
|
75
|
-
.expect(200);
|
|
76
|
-
|
|
77
|
-
expect(response.text).toEqual("pong");
|
|
78
|
-
});
|
|
79
|
-
it('should return a 406 response when Accept header doesn\'t match with @AcceptMime("text")', async () => {
|
|
80
|
-
const response = await request
|
|
81
|
-
.get("/rest/accept-mime/scenario-2")
|
|
82
|
-
.set({
|
|
83
|
-
Accept: "application/xml"
|
|
84
|
-
})
|
|
85
|
-
.expect(406);
|
|
86
|
-
|
|
87
|
-
expect(response.body).toEqual({
|
|
88
|
-
name: "NOT_ACCEPTABLE",
|
|
89
|
-
message: "You must accept content-type text",
|
|
90
|
-
status: 406,
|
|
91
|
-
errors: []
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
}
|
package/src/tests/testAuth.ts
DELETED
|
@@ -1,339 +0,0 @@
|
|
|
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
|
-
}
|