Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
82 changes: 82 additions & 0 deletions tests/AlbumBusiness.test.ts
Original file line number Diff line number Diff line change
@@ -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.")
// }
// })
})
4 changes: 2 additions & 2 deletions src/tests/UserBusiness.test.ts → tests/UserBusiness.test.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down