@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.
Files changed (231) hide show
  1. package/lib/cjs/components/PlatformTestSdk.js +31 -0
  2. package/lib/cjs/components/PlatformTestSdk.js.map +1 -0
  3. package/lib/cjs/data/swagger.json +32 -0
  4. package/lib/cjs/errors/CustomBadRequest.js +18 -0
  5. package/lib/cjs/errors/CustomBadRequest.js.map +1 -0
  6. package/lib/cjs/errors/CustomInternalError.js +18 -0
  7. package/lib/cjs/errors/CustomInternalError.js.map +1 -0
  8. package/lib/cjs/index.js +42 -0
  9. package/lib/cjs/index.js.map +1 -0
  10. package/lib/cjs/interfaces/PlatformTestingSdkOpts.js +3 -0
  11. package/lib/cjs/interfaces/PlatformTestingSdkOpts.js.map +1 -0
  12. package/lib/cjs/interfaces/index.js +8 -0
  13. package/lib/cjs/interfaces/index.js.map +1 -0
  14. package/lib/cjs/modules/feature/FeatureModule.js +17 -0
  15. package/lib/cjs/modules/feature/FeatureModule.js.map +1 -0
  16. package/lib/cjs/modules/feature/controllers/FeatureController.js +23 -0
  17. package/lib/cjs/modules/feature/controllers/FeatureController.js.map +1 -0
  18. package/lib/cjs/package.json +3 -0
  19. package/lib/cjs/tests/exports.js +66 -0
  20. package/lib/cjs/tests/exports.js.map +1 -0
  21. package/lib/cjs/tests/testAcceptMime.js +103 -0
  22. package/lib/cjs/tests/testAcceptMime.js.map +1 -0
  23. package/lib/cjs/tests/testAuth.js +343 -0
  24. package/lib/cjs/tests/testAuth.js.map +1 -0
  25. package/lib/cjs/tests/testBodyParams.js +526 -0
  26. package/lib/cjs/tests/testBodyParams.js.map +1 -0
  27. package/lib/cjs/tests/testCache.js +292 -0
  28. package/lib/cjs/tests/testCache.js.map +1 -0
  29. package/lib/cjs/tests/testChildrenControllers.js +64 -0
  30. package/lib/cjs/tests/testChildrenControllers.js.map +1 -0
  31. package/lib/cjs/tests/testCookies.js +45 -0
  32. package/lib/cjs/tests/testCookies.js.map +1 -0
  33. package/lib/cjs/tests/testCtrlInheritance.js +161 -0
  34. package/lib/cjs/tests/testCtrlInheritance.js.map +1 -0
  35. package/lib/cjs/tests/testCustom404.js +32 -0
  36. package/lib/cjs/tests/testCustom404.js.map +1 -0
  37. package/lib/cjs/tests/testErrors.js +252 -0
  38. package/lib/cjs/tests/testErrors.js.map +1 -0
  39. package/lib/cjs/tests/testHandlers.js +110 -0
  40. package/lib/cjs/tests/testHandlers.js.map +1 -0
  41. package/lib/cjs/tests/testHeaderParams.js +166 -0
  42. package/lib/cjs/tests/testHeaderParams.js.map +1 -0
  43. package/lib/cjs/tests/testHeaders.js +101 -0
  44. package/lib/cjs/tests/testHeaders.js.map +1 -0
  45. package/lib/cjs/tests/testInheritanceController.js +173 -0
  46. package/lib/cjs/tests/testInheritanceController.js.map +1 -0
  47. package/lib/cjs/tests/testLocals.js +63 -0
  48. package/lib/cjs/tests/testLocals.js.map +1 -0
  49. package/lib/cjs/tests/testLocation.js +42 -0
  50. package/lib/cjs/tests/testLocation.js.map +1 -0
  51. package/lib/cjs/tests/testMiddlewares.js +210 -0
  52. package/lib/cjs/tests/testMiddlewares.js.map +1 -0
  53. package/lib/cjs/tests/testModule.js +49 -0
  54. package/lib/cjs/tests/testModule.js.map +1 -0
  55. package/lib/cjs/tests/testMulter.js +162 -0
  56. package/lib/cjs/tests/testMulter.js.map +1 -0
  57. package/lib/cjs/tests/testPathParams.js +142 -0
  58. package/lib/cjs/tests/testPathParams.js.map +1 -0
  59. package/lib/cjs/tests/testQueryParams.js +354 -0
  60. package/lib/cjs/tests/testQueryParams.js.map +1 -0
  61. package/lib/cjs/tests/testRedirect.js +143 -0
  62. package/lib/cjs/tests/testRedirect.js.map +1 -0
  63. package/lib/cjs/tests/testResponse.js +622 -0
  64. package/lib/cjs/tests/testResponse.js.map +1 -0
  65. package/lib/cjs/tests/testResponseFilter.js +107 -0
  66. package/lib/cjs/tests/testResponseFilter.js.map +1 -0
  67. package/lib/cjs/tests/testRouting.js +75 -0
  68. package/lib/cjs/tests/testRouting.js.map +1 -0
  69. package/lib/cjs/tests/testScopeRequest.js +116 -0
  70. package/lib/cjs/tests/testScopeRequest.js.map +1 -0
  71. package/lib/cjs/tests/testSession.js +128 -0
  72. package/lib/cjs/tests/testSession.js.map +1 -0
  73. package/lib/cjs/tests/testStatics.js +58 -0
  74. package/lib/cjs/tests/testStatics.js.map +1 -0
  75. package/lib/cjs/tests/testView.js +83 -0
  76. package/lib/cjs/tests/testView.js.map +1 -0
  77. package/lib/esm/components/PlatformTestSdk.js +27 -0
  78. package/lib/esm/components/PlatformTestSdk.js.map +1 -0
  79. package/lib/esm/data/swagger.json +32 -0
  80. package/lib/esm/errors/CustomBadRequest.js +14 -0
  81. package/lib/esm/errors/CustomBadRequest.js.map +1 -0
  82. package/lib/esm/errors/CustomInternalError.js +14 -0
  83. package/lib/esm/errors/CustomInternalError.js.map +1 -0
  84. package/lib/esm/index.js +39 -0
  85. package/lib/esm/index.js.map +1 -0
  86. package/lib/esm/interfaces/PlatformTestingSdkOpts.js +2 -0
  87. package/lib/esm/interfaces/PlatformTestingSdkOpts.js.map +1 -0
  88. package/lib/esm/interfaces/index.js +5 -0
  89. package/lib/esm/interfaces/index.js.map +1 -0
  90. package/lib/esm/modules/feature/FeatureModule.js +14 -0
  91. package/lib/esm/modules/feature/FeatureModule.js.map +1 -0
  92. package/lib/esm/modules/feature/controllers/FeatureController.js +20 -0
  93. package/lib/esm/modules/feature/controllers/FeatureController.js.map +1 -0
  94. package/lib/esm/package.json +3 -0
  95. package/lib/esm/tests/exports.js +63 -0
  96. package/lib/esm/tests/exports.js.map +1 -0
  97. package/lib/esm/tests/testAcceptMime.js +99 -0
  98. package/lib/esm/tests/testAcceptMime.js.map +1 -0
  99. package/lib/esm/tests/testAuth.js +338 -0
  100. package/lib/esm/tests/testAuth.js.map +1 -0
  101. package/lib/esm/tests/testBodyParams.js +522 -0
  102. package/lib/esm/tests/testBodyParams.js.map +1 -0
  103. package/lib/esm/tests/testCache.js +288 -0
  104. package/lib/esm/tests/testCache.js.map +1 -0
  105. package/lib/esm/tests/testChildrenControllers.js +60 -0
  106. package/lib/esm/tests/testChildrenControllers.js.map +1 -0
  107. package/lib/esm/tests/testCookies.js +41 -0
  108. package/lib/esm/tests/testCookies.js.map +1 -0
  109. package/lib/esm/tests/testCtrlInheritance.js +156 -0
  110. package/lib/esm/tests/testCtrlInheritance.js.map +1 -0
  111. package/lib/esm/tests/testCustom404.js +27 -0
  112. package/lib/esm/tests/testCustom404.js.map +1 -0
  113. package/lib/esm/tests/testErrors.js +248 -0
  114. package/lib/esm/tests/testErrors.js.map +1 -0
  115. package/lib/esm/tests/testHandlers.js +105 -0
  116. package/lib/esm/tests/testHandlers.js.map +1 -0
  117. package/lib/esm/tests/testHeaderParams.js +162 -0
  118. package/lib/esm/tests/testHeaderParams.js.map +1 -0
  119. package/lib/esm/tests/testHeaders.js +97 -0
  120. package/lib/esm/tests/testHeaders.js.map +1 -0
  121. package/lib/esm/tests/testInheritanceController.js +167 -0
  122. package/lib/esm/tests/testInheritanceController.js.map +1 -0
  123. package/lib/esm/tests/testLocals.js +59 -0
  124. package/lib/esm/tests/testLocals.js.map +1 -0
  125. package/lib/esm/tests/testLocation.js +38 -0
  126. package/lib/esm/tests/testLocation.js.map +1 -0
  127. package/lib/esm/tests/testMiddlewares.js +206 -0
  128. package/lib/esm/tests/testMiddlewares.js.map +1 -0
  129. package/lib/esm/tests/testModule.js +45 -0
  130. package/lib/esm/tests/testModule.js.map +1 -0
  131. package/lib/esm/tests/testMulter.js +156 -0
  132. package/lib/esm/tests/testMulter.js.map +1 -0
  133. package/lib/esm/tests/testPathParams.js +138 -0
  134. package/lib/esm/tests/testPathParams.js.map +1 -0
  135. package/lib/esm/tests/testQueryParams.js +349 -0
  136. package/lib/esm/tests/testQueryParams.js.map +1 -0
  137. package/lib/esm/tests/testRedirect.js +139 -0
  138. package/lib/esm/tests/testRedirect.js.map +1 -0
  139. package/lib/esm/tests/testResponse.js +615 -0
  140. package/lib/esm/tests/testResponse.js.map +1 -0
  141. package/lib/esm/tests/testResponseFilter.js +103 -0
  142. package/lib/esm/tests/testResponseFilter.js.map +1 -0
  143. package/lib/esm/tests/testRouting.js +71 -0
  144. package/lib/esm/tests/testRouting.js.map +1 -0
  145. package/lib/esm/tests/testScopeRequest.js +112 -0
  146. package/lib/esm/tests/testScopeRequest.js.map +1 -0
  147. package/lib/esm/tests/testSession.js +122 -0
  148. package/lib/esm/tests/testSession.js.map +1 -0
  149. package/lib/esm/tests/testStatics.js +54 -0
  150. package/lib/esm/tests/testStatics.js.map +1 -0
  151. package/lib/esm/tests/testView.js +79 -0
  152. package/lib/esm/tests/testView.js.map +1 -0
  153. package/lib/types/components/PlatformTestSdk.d.ts +9 -0
  154. package/lib/types/errors/CustomBadRequest.d.ts +8 -0
  155. package/lib/types/errors/CustomInternalError.d.ts +8 -0
  156. package/lib/types/index.d.ts +38 -0
  157. package/lib/types/interfaces/PlatformTestingSdkOpts.d.ts +8 -0
  158. package/lib/types/interfaces/index.d.ts +4 -0
  159. package/lib/types/modules/feature/FeatureModule.d.ts +2 -0
  160. package/lib/types/modules/feature/controllers/FeatureController.d.ts +3 -0
  161. package/lib/types/tests/exports.d.ts +2 -0
  162. package/lib/types/tests/testAcceptMime.d.ts +2 -0
  163. package/lib/types/tests/testAuth.d.ts +8 -0
  164. package/lib/types/tests/testBodyParams.d.ts +3 -0
  165. package/lib/types/tests/testCache.d.ts +2 -0
  166. package/lib/types/tests/testChildrenControllers.d.ts +2 -0
  167. package/lib/types/tests/testCookies.d.ts +11 -0
  168. package/lib/types/tests/testCtrlInheritance.d.ts +18 -0
  169. package/lib/types/tests/testCustom404.d.ts +2 -0
  170. package/lib/types/tests/testErrors.d.ts +19 -0
  171. package/lib/types/tests/testHandlers.d.ts +12 -0
  172. package/lib/types/tests/testHeaderParams.d.ts +16 -0
  173. package/lib/types/tests/testHeaders.d.ts +8 -0
  174. package/lib/types/tests/testInheritanceController.d.ts +28 -0
  175. package/lib/types/tests/testLocals.d.ts +2 -0
  176. package/lib/types/tests/testLocation.d.ts +2 -0
  177. package/lib/types/tests/testMiddlewares.d.ts +8 -0
  178. package/lib/types/tests/testModule.d.ts +2 -0
  179. package/lib/types/tests/testMulter.d.ts +21 -0
  180. package/lib/types/tests/testPathParams.d.ts +2 -0
  181. package/lib/types/tests/testQueryParams.d.ts +6 -0
  182. package/lib/types/tests/testRedirect.d.ts +2 -0
  183. package/lib/types/tests/testResponse.d.ts +16 -0
  184. package/lib/types/tests/testResponseFilter.d.ts +2 -0
  185. package/lib/types/tests/testRouting.d.ts +7 -0
  186. package/lib/types/tests/testScopeRequest.d.ts +12 -0
  187. package/lib/types/tests/testSession.d.ts +16 -0
  188. package/lib/types/tests/testStatics.d.ts +2 -0
  189. package/lib/types/tests/testView.d.ts +2 -0
  190. package/package.json +44 -0
  191. package/src/components/PlatformTestSdk.ts +29 -0
  192. package/src/data/file.txt +1 -0
  193. package/src/data/response.data.json +3 -0
  194. package/src/data/swagger.json +32 -0
  195. package/src/errors/CustomBadRequest.ts +16 -0
  196. package/src/errors/CustomInternalError.ts +16 -0
  197. package/src/index.ts +39 -0
  198. package/src/interfaces/PlatformTestingSdkOpts.ts +10 -0
  199. package/src/interfaces/index.ts +5 -0
  200. package/src/modules/feature/FeatureModule.ts +9 -0
  201. package/src/modules/feature/controllers/FeatureController.ts +11 -0
  202. package/src/tests/exports.ts +69 -0
  203. package/src/tests/testAcceptMime.ts +95 -0
  204. package/src/tests/testAuth.ts +339 -0
  205. package/src/tests/testBodyParams.ts +455 -0
  206. package/src/tests/testCache.ts +270 -0
  207. package/src/tests/testChildrenControllers.ts +55 -0
  208. package/src/tests/testCookies.ts +36 -0
  209. package/src/tests/testCtrlInheritance.ts +140 -0
  210. package/src/tests/testCustom404.ts +33 -0
  211. package/src/tests/testErrors.ts +223 -0
  212. package/src/tests/testHandlers.ts +92 -0
  213. package/src/tests/testHeaderParams.ts +149 -0
  214. package/src/tests/testHeaders.ts +91 -0
  215. package/src/tests/testInheritanceController.ts +156 -0
  216. package/src/tests/testLocals.ts +46 -0
  217. package/src/tests/testLocation.ts +37 -0
  218. package/src/tests/testMiddlewares.ts +159 -0
  219. package/src/tests/testModule.ts +46 -0
  220. package/src/tests/testMulter.ts +140 -0
  221. package/src/tests/testPathParams.ts +122 -0
  222. package/src/tests/testQueryParams.ts +323 -0
  223. package/src/tests/testRedirect.ts +124 -0
  224. package/src/tests/testResponse.ts +558 -0
  225. package/src/tests/testResponseFilter.ts +101 -0
  226. package/src/tests/testRouting.ts +65 -0
  227. package/src/tests/testScopeRequest.ts +111 -0
  228. package/src/tests/testSession.ts +104 -0
  229. package/src/tests/testStatics.ts +61 -0
  230. package/src/tests/testView.ts +68 -0
  231. 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,10 @@
1
+ import {PlatformAdapter} from "@tsed/common";
2
+ import {Type} from "@tsed/core";
3
+
4
+ export interface PlatformTestingSdkOpts {
5
+ rootDir: string;
6
+ platform: Type<PlatformAdapter<any>>;
7
+ server: Type<any>;
8
+
9
+ [key: string]: any;
10
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * @file Automatically generated by barrelsby.
3
+ */
4
+
5
+ export * from "./PlatformTestingSdkOpts";
@@ -0,0 +1,9 @@
1
+ import {Module} from "@tsed/di";
2
+ import {FeatureController} from "./controllers/FeatureController";
3
+
4
+ @Module({
5
+ mount: {
6
+ "/rest": [FeatureController]
7
+ }
8
+ })
9
+ export class FeatureModule {}
@@ -0,0 +1,11 @@
1
+ import {Controller, Get} from "@tsed/common";
2
+ import {Hidden} from "@tsed/swagger";
3
+
4
+ @Hidden()
5
+ @Controller("/features")
6
+ export class FeatureController {
7
+ @Get("/")
8
+ get() {
9
+ return "From feature";
10
+ }
11
+ }
@@ -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
+ }