UNPKG

@tsed/platform-test-sdk

Version:
236 lines (235 loc) 7.93 kB
import { __decorate, __metadata, __param } from "tslib"; import { Controller } from "@tsed/di"; import { NotFound } from "@tsed/exceptions"; import { PlatformTest } from "@tsed/platform-http/testing"; import { Middleware, UseAuth } from "@tsed/platform-middlewares"; import { BodyParams, PathParams, QueryParams } from "@tsed/platform-params"; import { Description, Get, MaxLength, MinLength, Post, Property, Returns, Summary } from "@tsed/schema"; import SuperTest from "supertest"; import { v4 } from "uuid"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; export class Resource { } __decorate([ Property(), __metadata("design:type", String) ], Resource.prototype, "id", void 0); __decorate([ Property(), MinLength(3), MaxLength(100), __metadata("design:type", String) ], Resource.prototype, "name", void 0); export class BaseController { get(id) { const resource = this.resources.find((resource) => resource.id === id); if (!resource) { return Promise.reject(new NotFound("Not found")); } return Promise.resolve(resource); } list() { return Promise.resolve(this.resources); } } __decorate([ Get("/:id"), Summary("Return an element by his resource"), __param(0, Description("Id of the resource")), __param(0, PathParams("id")), __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", Promise) ], BaseController.prototype, "get", null); __decorate([ Get("/"), Summary("Return all elements from a service"), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], BaseController.prototype, "list", null); let ResourcesCtrl = class ResourcesCtrl extends BaseController { constructor() { super(...arguments); this.resources = [{ id: "1", name: "John" }]; } async get(id) { const resource = await super.get(id); resource.name = resource.name + " hello You!"; return resource; } post(resource) { resource.id = v4(); this.resources.push(resource); return resource; } }; __decorate([ Get("/:id"), Summary("Return an element by his resource"), __param(0, Description("Id of the resource")), __param(0, PathParams("id")), __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", Promise) ], ResourcesCtrl.prototype, "get", null); __decorate([ Post("/"), (Returns(201).Type(Resource)), __param(0, BodyParams()), __metadata("design:type", Function), __metadata("design:paramtypes", [Resource]), __metadata("design:returntype", void 0) ], ResourcesCtrl.prototype, "post", null); ResourcesCtrl = __decorate([ Controller("/resources") ], ResourcesCtrl); export { ResourcesCtrl }; let AuthMiddleware = class AuthMiddleware { use() { return true; } }; AuthMiddleware = __decorate([ Middleware() ], AuthMiddleware); class AttachmentController { getAll(parentID) { return `All attachments of ${parentID}`; } } __decorate([ Get("/:parentID/attachments"), __param(0, PathParams("parentID")), __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", void 0) ], AttachmentController.prototype, "getAll", null); let FindingsController = class FindingsController extends AttachmentController { get() { return "hello Finding"; } }; __decorate([ Get("/"), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], FindingsController.prototype, "get", null); FindingsController = __decorate([ Controller("/findings"), UseAuth(AuthMiddleware) ], FindingsController); export { FindingsController }; export class TestBaseController { scenario3(search) { return { search }; } scenario5(q) { return { data: q }; } } __decorate([ Get("/"), __param(0, QueryParams("search")), __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", void 0) ], TestBaseController.prototype, "scenario3", null); __decorate([ Get("/override"), __param(0, QueryParams("q")), __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", void 0) ], TestBaseController.prototype, "scenario5", null); let TestChildController = class TestChildController extends TestBaseController { scenario4(id) { return { id }; } scenario5(s) { return { data: s }; } }; __decorate([ Get("/:id"), __param(0, PathParams("id")), __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", Object) ], TestChildController.prototype, "scenario4", null); __decorate([ Get("/override"), __param(0, QueryParams("s")), __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", void 0) ], TestChildController.prototype, "scenario5", null); TestChildController = __decorate([ Controller("/test") ], TestChildController); export { TestChildController }; export function testInheritanceController(options) { let request; beforeAll(PlatformTest.bootstrap(options.server, { ...options, mount: { "/rest": [ResourcesCtrl, FindingsController, TestChildController] } })); beforeAll(() => { request = SuperTest(PlatformTest.callback()); }); afterAll(PlatformTest.reset); describe("Scenario 1:", () => { it("should return list", async () => { const { body } = await request.get("/rest/resources").expect(200); expect(body).toEqual([{ id: "1", name: "John" }]); }); it("should return a resource", async () => { const { body } = await request.get("/rest/resources/1").expect(200); expect(body).toEqual({ id: "1", name: "John hello You!" }); }); it("should add a resource", async () => { const { body } = await request .post("/rest/resources") .send({ name: "july" }) .expect(201); expect(body.name).toEqual("july"); expect(typeof body.id).toBe("string"); const { body: resource } = await request.get(`/rest/resources/${body.id}`).expect(200); expect(resource.id).toEqual(body.id); }); }); describe("scenario2: FindingsController", () => { it("should call /rest/findings/:parentID/attachments", async () => { const { text } = await request.get("/rest/findings/1/attachments").expect(200); expect(text).toEqual("All attachments of 1"); }); it("should call /rest/findings", async () => { const { text } = await request.get("/rest/findings").expect(200); expect(text).toEqual("hello Finding"); }); }); it("Scenario3: should call inherited method", async () => { const { body } = await request.get("/rest/test?search=test").expect(200); expect(body).toEqual({ search: "test" }); }); it("Scenario4: should the Child method", async () => { const { body } = await request.get("/rest/test/1").expect(200); expect(body).toEqual({ id: "1" }); }); it("Scenario5: should call the Child method and not the base method", async () => { const { body } = await request.get("/rest/test/1").expect(200); expect(body).toEqual({ id: "1" }); }); }