From 700fe38f037101d05f6482acb5d4005ad6ecfb55 Mon Sep 17 00:00:00 2001 From: Diego Miyabara Date: Wed, 7 Oct 2020 18:07:51 -0300 Subject: [PATCH] =?UTF-8?q?Implementa=C3=A7=C3=A3o=20de=20testes=20de=20er?= =?UTF-8?q?ro=20ao=20criar=20albuns=20nome=20e=20descricao=20vazios?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 17 +++++ package.json | 4 +- src/index.ts | 4 +- tests/AlbumBusiness.test.ts | 82 +++++++++++++++++++++++ {src/tests => tests}/UserBusiness.test.ts | 4 +- tsconfig.json | 2 +- 6 files changed, 108 insertions(+), 5 deletions(-) create mode 100644 tests/AlbumBusiness.test.ts rename {src/tests => tests}/UserBusiness.test.ts (98%) diff --git a/package-lock.json b/package-lock.json index 4f19f63..a6563d4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -961,6 +961,14 @@ "@types/node": "*" } }, + "@types/cors": { + "version": "2.8.7", + "resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.7.tgz", + "integrity": "sha512-sOdDRU3oRS7LBNTIqwDkPJyq0lpHYcbMTt0TrjzsXbk/e37hcLTH6eZX7CdbDeN0yJJvzw9hFBZkbtCSbk/jAQ==", + "requires": { + "@types/express": "*" + } + }, "@types/express": { "version": "4.17.8", "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.8.tgz", @@ -1799,6 +1807,15 @@ "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" }, + "cors": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", + "requires": { + "object-assign": "^4", + "vary": "^1" + } + }, "cross-spawn": { "version": "6.0.5", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-6.0.5.tgz", diff --git a/package.json b/package.json index ab27ff3..8e6ec86 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Back-end do projeto Full-Stack módulo 6", "main": "index.js", "scripts": { - "test": "clear && echo \\\"Running tests...\\\" && jest", + "test": "clear && echo \"Running tests...\" && jest", "start:dev": "ts-node-dev ./src/index.ts", "start": "tsc && node ./build/index.js" }, @@ -25,12 +25,14 @@ "homepage": "https://github.com/diegomiyabara/Projeto-Fullstack-Backend#readme", "dependencies": { "@types/bcryptjs": "^2.4.2", + "@types/cors": "^2.8.7", "@types/express": "^4.17.7", "@types/jest": "^25.2.3", "@types/jsonwebtoken": "^8.5.0", "@types/knex": "^0.16.1", "@types/uuid": "^8.0.0", "bcryptjs": "^2.4.3", + "cors": "^2.8.5", "dotenv": "^8.2.0", "express": "^4.17.1", "jest": "^26.0.1", diff --git a/src/index.ts b/src/index.ts index fda18ad..dbdf356 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,17 +4,19 @@ import express from "express"; import { userRouter } from "./routes/userRouter"; import { albumRouter } from "./routes/albumRouter"; import { imageRouter } from "./routes/imageRouter"; +import cors from 'cors' dotenv.config(); const app = express(); app.use(express.json()); +app.use(cors({ origin: true })); app.use("/user", userRouter); app.use("/album", albumRouter); app.use("/image", imageRouter); -const server = app.listen(3000, () => { +const server = app.listen(process.env.PORT || 3003, () => { if (server) { const address = server.address() as AddressInfo; console.log(`Server running on http://localhost:${address.port}`); diff --git a/tests/AlbumBusiness.test.ts b/tests/AlbumBusiness.test.ts new file mode 100644 index 0000000..1f67123 --- /dev/null +++ b/tests/AlbumBusiness.test.ts @@ -0,0 +1,82 @@ +import { AlbumBusiness } from '../src/business/AlbumBusiness' +import { Album, AlbumInputDTO } from '../src/model/Album' +import { UserRole } from '../src/model/User' + +let albumDatabase = { + createAlbum: jest.fn(async(album: Album, token: string) => {}), + getAllAlbuns: jest.fn(async(token: string) => undefined), + getAlbunsByUserId: jest.fn(async(token: string) => undefined), + getAlbumById: jest.fn(async(albumId: string, token:string) => undefined) +} as any + +const idGenerator = { + generate: jest.fn(() => "id mock") +} as any + +const authenticator = { + generateToken: jest.fn((payload: {id: string, role: UserRole}) => "fake token"), + getData: jest.fn((token: string) => {return {id: "id", name: "Admin",role: "ADMIN"}}) +} as any + +const albumBusiness: AlbumBusiness = new AlbumBusiness(albumDatabase,idGenerator,authenticator) + +describe("Create album endpoint", () => { + test("Return error when album name is not informed", async() => { + expect.assertions(2) + try { + const token = authenticator.generate + + const album:AlbumInputDTO = { + name: "", + description: "Férias em meio a pandemia", + albumImageUrl: "https://picsum.photos/seed/picsum/200/300" + } + + await albumBusiness.createAlbum(album, token) + + } catch (error) { + expect(error.code).toBe(422) + expect(error.message).toBe("All inputs must be filled!") + } + }) + + test("Return error when album description is not informed", async() => { + expect.assertions(2) + try { + const token = authenticator.generate + + const album:AlbumInputDTO = { + name: "Férias 2020", + description: "", + albumImageUrl: "https://picsum.photos/seed/picsum/200/300" + } + + await albumBusiness.createAlbum(album, token) + + } catch (error) { + expect(error.code).toBe(422) + expect(error.message).toBe("All inputs must be filled!") + } + }) + + // test("Return error when album name already exists", async() => { + // expect.assertions(2) + // try { + // const token = authenticator.generateToken + + // albumDatabase = {getAlbunsByUserId: jest.fn(async(token: string) => [new Album("id mock", "Férias 2020", "Ferias inesqueciveis", "id")])} + + // const album:AlbumInputDTO = { + // name: "Férias 2020", + // description: "Férias top demais", + // albumImageUrl: "https://picsum.photos/seed/picsum/200/300" + // } + + // await albumBusiness.createAlbum(album, token) + + // } catch (error) { + // expect(error.code).toBe(422) + // expect(error.message).toBe("You already have an album with the name Férias 2020.") + // } + // }) +}) \ No newline at end of file diff --git a/src/tests/UserBusiness.test.ts b/tests/UserBusiness.test.ts similarity index 98% rename from src/tests/UserBusiness.test.ts rename to tests/UserBusiness.test.ts index 7fc8e73..62d1aaf 100644 --- a/src/tests/UserBusiness.test.ts +++ b/tests/UserBusiness.test.ts @@ -1,5 +1,5 @@ -import { UserBusiness } from "../business/UserBusiness" -import { LoginInputDTO, User, UserRole } from "../model/User" +import { UserBusiness } from "../src/business/UserBusiness" +import { LoginInputDTO, User, UserRole } from "../src/model/User" let userDatabase = { diff --git a/tsconfig.json b/tsconfig.json index 01f58d9..78565d8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,7 +15,7 @@ "sourceMap": true, /* Generates corresponding '.map' file. */ // "outFile": "./", /* Concatenate and emit output to single file. */ "outDir": "./build", /* Redirect output structure to the directory. */ - "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ + "rootDirs": ["./src", "./tests"], /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ // "composite": true, /* Enable project compilation */ // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ "removeComments": true, /* Do not emit comments to output. */