@tsed/platform-test-sdk 7.12.0
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 +31 -0
- package/lib/cjs/components/PlatformTestSdk.js.map +1 -0
- package/lib/cjs/data/swagger.json +32 -0
- package/lib/cjs/errors/CustomBadRequest.js +18 -0
- package/lib/cjs/errors/CustomBadRequest.js.map +1 -0
- package/lib/cjs/errors/CustomInternalError.js +18 -0
- package/lib/cjs/errors/CustomInternalError.js.map +1 -0
- package/lib/cjs/index.js +42 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/cjs/interfaces/PlatformTestingSdkOpts.js +3 -0
- package/lib/cjs/interfaces/PlatformTestingSdkOpts.js.map +1 -0
- package/lib/cjs/interfaces/index.js +8 -0
- package/lib/cjs/interfaces/index.js.map +1 -0
- package/lib/cjs/modules/feature/FeatureModule.js +17 -0
- package/lib/cjs/modules/feature/FeatureModule.js.map +1 -0
- package/lib/cjs/modules/feature/controllers/FeatureController.js +23 -0
- package/lib/cjs/modules/feature/controllers/FeatureController.js.map +1 -0
- package/lib/cjs/package.json +3 -0
- package/lib/cjs/tests/exports.js +66 -0
- package/lib/cjs/tests/exports.js.map +1 -0
- package/lib/cjs/tests/testAcceptMime.js +103 -0
- package/lib/cjs/tests/testAcceptMime.js.map +1 -0
- package/lib/cjs/tests/testAuth.js +343 -0
- package/lib/cjs/tests/testAuth.js.map +1 -0
- package/lib/cjs/tests/testBodyParams.js +526 -0
- package/lib/cjs/tests/testBodyParams.js.map +1 -0
- package/lib/cjs/tests/testCache.js +292 -0
- package/lib/cjs/tests/testCache.js.map +1 -0
- package/lib/cjs/tests/testChildrenControllers.js +64 -0
- package/lib/cjs/tests/testChildrenControllers.js.map +1 -0
- package/lib/cjs/tests/testCookies.js +45 -0
- package/lib/cjs/tests/testCookies.js.map +1 -0
- package/lib/cjs/tests/testCtrlInheritance.js +161 -0
- package/lib/cjs/tests/testCtrlInheritance.js.map +1 -0
- package/lib/cjs/tests/testCustom404.js +32 -0
- package/lib/cjs/tests/testCustom404.js.map +1 -0
- package/lib/cjs/tests/testErrors.js +252 -0
- package/lib/cjs/tests/testErrors.js.map +1 -0
- package/lib/cjs/tests/testHandlers.js +110 -0
- package/lib/cjs/tests/testHandlers.js.map +1 -0
- package/lib/cjs/tests/testHeaderParams.js +166 -0
- package/lib/cjs/tests/testHeaderParams.js.map +1 -0
- package/lib/cjs/tests/testHeaders.js +101 -0
- package/lib/cjs/tests/testHeaders.js.map +1 -0
- package/lib/cjs/tests/testInheritanceController.js +173 -0
- package/lib/cjs/tests/testInheritanceController.js.map +1 -0
- package/lib/cjs/tests/testLocals.js +63 -0
- package/lib/cjs/tests/testLocals.js.map +1 -0
- package/lib/cjs/tests/testLocation.js +42 -0
- package/lib/cjs/tests/testLocation.js.map +1 -0
- package/lib/cjs/tests/testMiddlewares.js +210 -0
- package/lib/cjs/tests/testMiddlewares.js.map +1 -0
- package/lib/cjs/tests/testModule.js +49 -0
- package/lib/cjs/tests/testModule.js.map +1 -0
- package/lib/cjs/tests/testMulter.js +162 -0
- package/lib/cjs/tests/testMulter.js.map +1 -0
- package/lib/cjs/tests/testPathParams.js +142 -0
- package/lib/cjs/tests/testPathParams.js.map +1 -0
- package/lib/cjs/tests/testQueryParams.js +354 -0
- package/lib/cjs/tests/testQueryParams.js.map +1 -0
- package/lib/cjs/tests/testRedirect.js +143 -0
- package/lib/cjs/tests/testRedirect.js.map +1 -0
- package/lib/cjs/tests/testResponse.js +622 -0
- package/lib/cjs/tests/testResponse.js.map +1 -0
- package/lib/cjs/tests/testResponseFilter.js +107 -0
- package/lib/cjs/tests/testResponseFilter.js.map +1 -0
- package/lib/cjs/tests/testRouting.js +75 -0
- package/lib/cjs/tests/testRouting.js.map +1 -0
- package/lib/cjs/tests/testScopeRequest.js +116 -0
- package/lib/cjs/tests/testScopeRequest.js.map +1 -0
- package/lib/cjs/tests/testSession.js +128 -0
- package/lib/cjs/tests/testSession.js.map +1 -0
- package/lib/cjs/tests/testStatics.js +58 -0
- package/lib/cjs/tests/testStatics.js.map +1 -0
- package/lib/cjs/tests/testView.js +83 -0
- package/lib/cjs/tests/testView.js.map +1 -0
- package/lib/esm/components/PlatformTestSdk.js +27 -0
- package/lib/esm/components/PlatformTestSdk.js.map +1 -0
- package/lib/esm/data/swagger.json +32 -0
- package/lib/esm/errors/CustomBadRequest.js +14 -0
- package/lib/esm/errors/CustomBadRequest.js.map +1 -0
- package/lib/esm/errors/CustomInternalError.js +14 -0
- package/lib/esm/errors/CustomInternalError.js.map +1 -0
- package/lib/esm/index.js +39 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/interfaces/PlatformTestingSdkOpts.js +2 -0
- package/lib/esm/interfaces/PlatformTestingSdkOpts.js.map +1 -0
- package/lib/esm/interfaces/index.js +5 -0
- package/lib/esm/interfaces/index.js.map +1 -0
- package/lib/esm/modules/feature/FeatureModule.js +14 -0
- package/lib/esm/modules/feature/FeatureModule.js.map +1 -0
- package/lib/esm/modules/feature/controllers/FeatureController.js +20 -0
- package/lib/esm/modules/feature/controllers/FeatureController.js.map +1 -0
- package/lib/esm/package.json +3 -0
- package/lib/esm/tests/exports.js +63 -0
- package/lib/esm/tests/exports.js.map +1 -0
- package/lib/esm/tests/testAcceptMime.js +99 -0
- package/lib/esm/tests/testAcceptMime.js.map +1 -0
- package/lib/esm/tests/testAuth.js +338 -0
- package/lib/esm/tests/testAuth.js.map +1 -0
- package/lib/esm/tests/testBodyParams.js +522 -0
- package/lib/esm/tests/testBodyParams.js.map +1 -0
- package/lib/esm/tests/testCache.js +288 -0
- package/lib/esm/tests/testCache.js.map +1 -0
- package/lib/esm/tests/testChildrenControllers.js +60 -0
- package/lib/esm/tests/testChildrenControllers.js.map +1 -0
- package/lib/esm/tests/testCookies.js +41 -0
- package/lib/esm/tests/testCookies.js.map +1 -0
- package/lib/esm/tests/testCtrlInheritance.js +156 -0
- package/lib/esm/tests/testCtrlInheritance.js.map +1 -0
- package/lib/esm/tests/testCustom404.js +27 -0
- package/lib/esm/tests/testCustom404.js.map +1 -0
- package/lib/esm/tests/testErrors.js +248 -0
- package/lib/esm/tests/testErrors.js.map +1 -0
- package/lib/esm/tests/testHandlers.js +105 -0
- package/lib/esm/tests/testHandlers.js.map +1 -0
- package/lib/esm/tests/testHeaderParams.js +162 -0
- package/lib/esm/tests/testHeaderParams.js.map +1 -0
- package/lib/esm/tests/testHeaders.js +97 -0
- package/lib/esm/tests/testHeaders.js.map +1 -0
- package/lib/esm/tests/testInheritanceController.js +167 -0
- package/lib/esm/tests/testInheritanceController.js.map +1 -0
- package/lib/esm/tests/testLocals.js +59 -0
- package/lib/esm/tests/testLocals.js.map +1 -0
- package/lib/esm/tests/testLocation.js +38 -0
- package/lib/esm/tests/testLocation.js.map +1 -0
- package/lib/esm/tests/testMiddlewares.js +206 -0
- package/lib/esm/tests/testMiddlewares.js.map +1 -0
- package/lib/esm/tests/testModule.js +45 -0
- package/lib/esm/tests/testModule.js.map +1 -0
- package/lib/esm/tests/testMulter.js +156 -0
- package/lib/esm/tests/testMulter.js.map +1 -0
- package/lib/esm/tests/testPathParams.js +138 -0
- package/lib/esm/tests/testPathParams.js.map +1 -0
- package/lib/esm/tests/testQueryParams.js +349 -0
- package/lib/esm/tests/testQueryParams.js.map +1 -0
- package/lib/esm/tests/testRedirect.js +139 -0
- package/lib/esm/tests/testRedirect.js.map +1 -0
- package/lib/esm/tests/testResponse.js +615 -0
- package/lib/esm/tests/testResponse.js.map +1 -0
- package/lib/esm/tests/testResponseFilter.js +103 -0
- package/lib/esm/tests/testResponseFilter.js.map +1 -0
- package/lib/esm/tests/testRouting.js +71 -0
- package/lib/esm/tests/testRouting.js.map +1 -0
- package/lib/esm/tests/testScopeRequest.js +112 -0
- package/lib/esm/tests/testScopeRequest.js.map +1 -0
- package/lib/esm/tests/testSession.js +122 -0
- package/lib/esm/tests/testSession.js.map +1 -0
- package/lib/esm/tests/testStatics.js +54 -0
- package/lib/esm/tests/testStatics.js.map +1 -0
- package/lib/esm/tests/testView.js +79 -0
- package/lib/esm/tests/testView.js.map +1 -0
- package/lib/types/components/PlatformTestSdk.d.ts +9 -0
- package/lib/types/errors/CustomBadRequest.d.ts +8 -0
- package/lib/types/errors/CustomInternalError.d.ts +8 -0
- package/lib/types/index.d.ts +38 -0
- package/lib/types/interfaces/PlatformTestingSdkOpts.d.ts +8 -0
- package/lib/types/interfaces/index.d.ts +4 -0
- package/lib/types/modules/feature/FeatureModule.d.ts +2 -0
- package/lib/types/modules/feature/controllers/FeatureController.d.ts +3 -0
- package/lib/types/tests/exports.d.ts +2 -0
- package/lib/types/tests/testAcceptMime.d.ts +2 -0
- package/lib/types/tests/testAuth.d.ts +8 -0
- package/lib/types/tests/testBodyParams.d.ts +3 -0
- package/lib/types/tests/testCache.d.ts +2 -0
- package/lib/types/tests/testChildrenControllers.d.ts +2 -0
- package/lib/types/tests/testCookies.d.ts +11 -0
- package/lib/types/tests/testCtrlInheritance.d.ts +18 -0
- package/lib/types/tests/testCustom404.d.ts +2 -0
- package/lib/types/tests/testErrors.d.ts +19 -0
- package/lib/types/tests/testHandlers.d.ts +12 -0
- package/lib/types/tests/testHeaderParams.d.ts +16 -0
- package/lib/types/tests/testHeaders.d.ts +8 -0
- package/lib/types/tests/testInheritanceController.d.ts +28 -0
- package/lib/types/tests/testLocals.d.ts +2 -0
- package/lib/types/tests/testLocation.d.ts +2 -0
- package/lib/types/tests/testMiddlewares.d.ts +8 -0
- package/lib/types/tests/testModule.d.ts +2 -0
- package/lib/types/tests/testMulter.d.ts +21 -0
- package/lib/types/tests/testPathParams.d.ts +2 -0
- package/lib/types/tests/testQueryParams.d.ts +6 -0
- package/lib/types/tests/testRedirect.d.ts +2 -0
- package/lib/types/tests/testResponse.d.ts +16 -0
- package/lib/types/tests/testResponseFilter.d.ts +2 -0
- package/lib/types/tests/testRouting.d.ts +7 -0
- package/lib/types/tests/testScopeRequest.d.ts +12 -0
- package/lib/types/tests/testSession.d.ts +16 -0
- package/lib/types/tests/testStatics.d.ts +2 -0
- package/lib/types/tests/testView.d.ts +2 -0
- package/package.json +44 -0
- 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 +39 -0
- package/src/interfaces/PlatformTestingSdkOpts.ts +10 -0
- package/src/interfaces/index.ts +5 -0
- package/src/modules/feature/FeatureModule.ts +9 -0
- package/src/modules/feature/controllers/FeatureController.ts +11 -0
- package/src/tests/exports.ts +69 -0
- package/src/tests/testAcceptMime.ts +95 -0
- package/src/tests/testAuth.ts +339 -0
- package/src/tests/testBodyParams.ts +455 -0
- package/src/tests/testCache.ts +270 -0
- package/src/tests/testChildrenControllers.ts +55 -0
- package/src/tests/testCookies.ts +36 -0
- package/src/tests/testCtrlInheritance.ts +140 -0
- package/src/tests/testCustom404.ts +33 -0
- package/src/tests/testErrors.ts +223 -0
- package/src/tests/testHandlers.ts +92 -0
- package/src/tests/testHeaderParams.ts +149 -0
- package/src/tests/testHeaders.ts +91 -0
- package/src/tests/testInheritanceController.ts +156 -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 +140 -0
- package/src/tests/testPathParams.ts +122 -0
- package/src/tests/testQueryParams.ts +323 -0
- package/src/tests/testRedirect.ts +124 -0
- package/src/tests/testResponse.ts +558 -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 +104 -0
- package/src/tests/testStatics.ts +61 -0
- package/src/tests/testView.ts +68 -0
- package/tsconfig.compile.esm.json +12 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Automatically generated by barrelsby.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export * from "./components/PlatformTestSdk";
|
|
6
|
+
export * from "./errors/CustomBadRequest";
|
|
7
|
+
export * from "./errors/CustomInternalError";
|
|
8
|
+
export * from "./interfaces/PlatformTestingSdkOpts";
|
|
9
|
+
export * from "./modules/feature/FeatureModule";
|
|
10
|
+
export * from "./modules/feature/controllers/FeatureController";
|
|
11
|
+
export * from "./tests/exports";
|
|
12
|
+
export * from "./tests/testAcceptMime";
|
|
13
|
+
export * from "./tests/testAuth";
|
|
14
|
+
export * from "./tests/testBodyParams";
|
|
15
|
+
export * from "./tests/testCache";
|
|
16
|
+
export * from "./tests/testChildrenControllers";
|
|
17
|
+
export * from "./tests/testCookies";
|
|
18
|
+
export * from "./tests/testCtrlInheritance";
|
|
19
|
+
export * from "./tests/testCustom404";
|
|
20
|
+
export * from "./tests/testErrors";
|
|
21
|
+
export * from "./tests/testHandlers";
|
|
22
|
+
export * from "./tests/testHeaderParams";
|
|
23
|
+
export * from "./tests/testHeaders";
|
|
24
|
+
export * from "./tests/testInheritanceController";
|
|
25
|
+
export * from "./tests/testLocals";
|
|
26
|
+
export * from "./tests/testLocation";
|
|
27
|
+
export * from "./tests/testMiddlewares";
|
|
28
|
+
export * from "./tests/testModule";
|
|
29
|
+
export * from "./tests/testMulter";
|
|
30
|
+
export * from "./tests/testPathParams";
|
|
31
|
+
export * from "./tests/testQueryParams";
|
|
32
|
+
export * from "./tests/testRedirect";
|
|
33
|
+
export * from "./tests/testResponse";
|
|
34
|
+
export * from "./tests/testResponseFilter";
|
|
35
|
+
export * from "./tests/testRouting";
|
|
36
|
+
export * from "./tests/testScopeRequest";
|
|
37
|
+
export * from "./tests/testSession";
|
|
38
|
+
export * from "./tests/testStatics";
|
|
39
|
+
export * from "./tests/testView";
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import {PlatformTestingSdkOpts} from "../interfaces";
|
|
2
|
+
import {testAcceptMime} from "./testAcceptMime";
|
|
3
|
+
import {testAuth} from "./testAuth";
|
|
4
|
+
import {testBodyParams} from "./testBodyParams";
|
|
5
|
+
import {testChildrenControllers} from "./testChildrenControllers";
|
|
6
|
+
import {testCookies} from "./testCookies";
|
|
7
|
+
import {testCtrlInheritance} from "./testCtrlInheritance";
|
|
8
|
+
import {testCustom404} from "./testCustom404";
|
|
9
|
+
import {testErrors} from "./testErrors";
|
|
10
|
+
import {testHandlers} from "./testHandlers";
|
|
11
|
+
import {testHeaderParams} from "./testHeaderParams";
|
|
12
|
+
import {testHeaders} from "./testHeaders";
|
|
13
|
+
import {testInheritanceController} from "./testInheritanceController";
|
|
14
|
+
import {testLocals} from "./testLocals";
|
|
15
|
+
import {testLocation} from "./testLocation";
|
|
16
|
+
import {testMiddlewares} from "./testMiddlewares";
|
|
17
|
+
import {testModule} from "./testModule";
|
|
18
|
+
import {testMulter} from "./testMulter";
|
|
19
|
+
import {testPathParams} from "./testPathParams";
|
|
20
|
+
import {testQueryParams} from "./testQueryParams";
|
|
21
|
+
import {testRedirect} from "./testRedirect";
|
|
22
|
+
import {testResponse} from "./testResponse";
|
|
23
|
+
import {testResponseFilter} from "./testResponseFilter";
|
|
24
|
+
import {testRouting} from "./testRouting";
|
|
25
|
+
import {testScopeRequest} from "./testScopeRequest";
|
|
26
|
+
import {testSession} from "./testSession";
|
|
27
|
+
import {testStatics} from "./testStatics";
|
|
28
|
+
import {testView} from "./testView";
|
|
29
|
+
import {testCache} from "./testCache";
|
|
30
|
+
|
|
31
|
+
export const specsContainer = new Map<string, (options: PlatformTestingSdkOpts) => void>();
|
|
32
|
+
|
|
33
|
+
// HANDLERS AND MIDDLEWARES
|
|
34
|
+
specsContainer.set("handlers", testHandlers);
|
|
35
|
+
specsContainer.set("auth", testAuth);
|
|
36
|
+
specsContainer.set("middlewares", testMiddlewares);
|
|
37
|
+
|
|
38
|
+
// RESPONSES
|
|
39
|
+
specsContainer.set("headers", testHeaders);
|
|
40
|
+
specsContainer.set("acceptMime", testAcceptMime);
|
|
41
|
+
specsContainer.set("location", testLocation);
|
|
42
|
+
specsContainer.set("redirect", testRedirect);
|
|
43
|
+
specsContainer.set("view", testView);
|
|
44
|
+
specsContainer.set("errors", testErrors);
|
|
45
|
+
specsContainer.set("response", testResponse);
|
|
46
|
+
specsContainer.set("responseFilter", testResponseFilter);
|
|
47
|
+
specsContainer.set("custom404", testCustom404);
|
|
48
|
+
specsContainer.set("statics", testStatics);
|
|
49
|
+
specsContainer.set("cache", testCache);
|
|
50
|
+
|
|
51
|
+
// INPUTS
|
|
52
|
+
specsContainer.set("cookies", testCookies);
|
|
53
|
+
specsContainer.set("session", testSession);
|
|
54
|
+
specsContainer.set("headerParams", testHeaderParams);
|
|
55
|
+
specsContainer.set("bodyParams", testBodyParams);
|
|
56
|
+
specsContainer.set("pathParams", testPathParams);
|
|
57
|
+
specsContainer.set("queryParams", testQueryParams);
|
|
58
|
+
specsContainer.set("locals", testLocals);
|
|
59
|
+
specsContainer.set("multer", testMulter);
|
|
60
|
+
|
|
61
|
+
// ROUTING
|
|
62
|
+
specsContainer.set("scopeRequest", testScopeRequest);
|
|
63
|
+
specsContainer.set("routing", testRouting);
|
|
64
|
+
specsContainer.set("inheritance", testCtrlInheritance);
|
|
65
|
+
|
|
66
|
+
// CODE ORGANIZATION
|
|
67
|
+
specsContainer.set("childrenControllers", testChildrenControllers);
|
|
68
|
+
specsContainer.set("inheritanceController", testInheritanceController);
|
|
69
|
+
specsContainer.set("module", testModule);
|
|
@@ -0,0 +1,95 @@
|
|
|
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";
|
|
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.SuperTest<SuperTest.Test>;
|
|
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
|
+
}
|
|
@@ -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";
|
|
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.SuperTest<SuperTest.Test>;
|
|
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
|
+
}
|