Skip to content

Commit

Permalink
Merge pull request #10 from thalysonalexr/add-backend
Browse files Browse the repository at this point in the history
Add backend
  • Loading branch information
thalysonalexr authored Apr 28, 2020
2 parents 8a9b05c + 9587ecf commit ba25870
Show file tree
Hide file tree
Showing 45 changed files with 1,218 additions and 243 deletions.
1 change: 1 addition & 0 deletions backend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
17 changes: 17 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM node:lts-alpine

RUN mkdir -p /home/node/api/node_modules && chown -R node:node /home/node/api

WORKDIR /home/node/api

COPY package.json yarn.* ./

USER node

RUN yarn

COPY --chown=node:node . .

CMD ["yarn", "start"]

EXPOSE 3000
11 changes: 6 additions & 5 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"main": "server.ts",
"scripts": {
"dev": "cross-env NODE_ENV=development ts-node-dev --inspect -r tsconfig-paths/register --respawn --transpileOnly src/server.ts",
"test": "cross-env NODE_ENV=test jest"
"test": "cross-env NODE_ENV=test UPLOAD_PATH=tests UPLOAD_TYPE=local jest --runInBand"
},
"engines": {
"node": ">=12.0.0"
"node": ">=12.16.1"
},
"keywords": [
"Coffee",
Expand All @@ -19,9 +19,9 @@
"devDependencies": {
"@shelf/jest-mongodb": "^1.1.5",
"@types/bcryptjs": "^2.4.2",
"@types/compression": "^1.7.0",
"@types/cors": "^2.8.6",
"@types/express": "^4.17.4",
"@types/express-validator": "^3.0.0",
"@types/factory-girl": "^5.0.2",
"@types/faker": "^4.1.11",
"@types/jest": "^25.2.1",
Expand All @@ -45,15 +45,16 @@
},
"dependencies": {
"bcryptjs": "^2.4.3",
"compression": "^1.7.4",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-validator": "^6.4.0",
"jimp": "^0.10.3",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.9.7",
"mongoose-paginate": "^5.0.3",
"morgan": "^1.10.0",
"multer": "^1.4.2",
"promisify": "^0.0.3"
"multer": "^1.4.2"
}
}
4 changes: 3 additions & 1 deletion backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import express from 'express'
import morgan from 'morgan'
import cors from 'cors'
import path from 'path'
import compression from 'compression'
import routes from './routes'

const app = express()
Expand All @@ -12,7 +13,8 @@ app.use(cors())
app.use(express.urlencoded({ extended: true }))
app.use(express.json())
app.use(morgan('dev'))
app.use('/files', express.static(path.resolve(__dirname, '..', 'tmp', process.env.UPLOAD_PATH)))
app.use(compression())
app.use('/v1/files', express.static(path.resolve(__dirname, '..', 'tmp', process.env.UPLOAD_PATH)))
app.use('/v1', routes)

export default app
5 changes: 5 additions & 0 deletions backend/src/app/core/schemas/IUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ export interface IUser {
email: string
password: string
role: string
avatar: {
name: string
key: string
size: number
},
createdAt: Date
updatedAt: Date
}
4 changes: 2 additions & 2 deletions backend/src/app/domain/controllers/AdminController.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ describe('Admin actions', () => {
})

const response = await request(app)
.delete(`/v1/coffee/${id}/destroy`)
.delete(`/v1/coffees/${id}/destroy`)
.set('Authorization', `Bearer ${token}`)

expect(response.status).toBe(204)
Expand All @@ -167,7 +167,7 @@ describe('Admin actions', () => {
await Coffee.deleteMany({})

const response = await request(app)
.delete(`/v1/coffee/${id}/destroy`)
.delete(`/v1/coffees/${id}/destroy`)
.set('Authorization', `Bearer ${token}`)

expect(response.status).toBe(404)
Expand Down
86 changes: 66 additions & 20 deletions backend/src/app/domain/controllers/CoffeeController.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('Coffee actions', () => {
}

const response = await request(app)
.post('/v1/coffee')
.post('/v1/coffees')
.set('Authorization', `Bearer ${token}`)
.send(coffee)

Expand Down Expand Up @@ -82,7 +82,7 @@ describe('Coffee actions', () => {
})

const response = await request(app)
.get(`/v1/coffee/${id}`)
.get(`/v1/coffees/${id}`)

expect(response.status).toBe(200)
expect(response.body).toStrictEqual(
Expand Down Expand Up @@ -113,7 +113,7 @@ describe('Coffee actions', () => {
await Coffee.deleteMany({})

const response = await request(app)
.get(`/v1/coffee/${id}`)
.get(`/v1/coffees/${id}`)

expect(response.status).toBe(404)
})
Expand All @@ -128,7 +128,7 @@ describe('Coffee actions', () => {
await Coffee.deleteMany({})

const response = await request(app)
.get(`/v1/coffee/${id}`)
.get(`/v1/coffees/${id}`)

expect(response.status).toBe(404)
})
Expand All @@ -142,13 +142,13 @@ describe('Coffee actions', () => {
const token = generateTokenJwt(process.env.SECRET, { id: user.id })

const response = await request(app)
.get(`/v1/coffee/me`)
.get(`/v1/coffees/me`)
.set('Authorization', `Bearer ${token}`)

expect(response.status).toBe(200)
expect(response.body).toStrictEqual(
expect.objectContaining({
cafes: expect.arrayContaining([
coffees: expect.arrayContaining([
expect.any(Object),
expect.any(Object),
]),
Expand All @@ -170,12 +170,39 @@ describe('Coffee actions', () => {
})

const response = await request(app)
.get(`/v1/coffee?type=${filter}`)
.get(`/v1/coffees?type=${filter}`)

expect(response.status).toBe(200)
expect(response.body).toStrictEqual(
expect.objectContaining({
cafes: expect.arrayContaining([
coffees: expect.arrayContaining([
expect.any(Object),
expect.any(Object),
]),
pages: expect.any(Number),
total: expect.any(Number),
})
)
})

it('should be able get all coffee filter preparation', async () => {
const user = await factory.create<UserModel>('User')

const filter = 'Expresso'

await factory.create<CoffeeModel>('Coffee', { author: user.id })
await factory.create<CoffeeModel>('Coffee', {
preparation: filter,
author: user.id
})

const response = await request(app)
.get(`/v1/coffees?preparation=${filter}`)

expect(response.status).toBe(200)
expect(response.body).toStrictEqual(
expect.objectContaining({
coffees: expect.arrayContaining([
expect.any(Object),
expect.any(Object),
]),
Expand All @@ -192,12 +219,12 @@ describe('Coffee actions', () => {
await factory.create<CoffeeModel>('Coffee', { author: user.id })

const response = await request(app)
.get('/v1/coffee')
.get('/v1/coffees')

expect(response.status).toBe(200)
expect(response.body).toStrictEqual(
expect.objectContaining({
cafes: expect.arrayContaining([
coffees: expect.arrayContaining([
expect.any(Object),
expect.any(Object),
]),
Expand All @@ -223,7 +250,7 @@ describe('Coffee actions', () => {
}

const response = await request(app)
.put(`/v1/coffee/${id}`)
.put(`/v1/coffees/${id}`)
.set('Authorization', `Bearer ${token}`)
.send(coffee)

Expand Down Expand Up @@ -255,7 +282,7 @@ describe('Coffee actions', () => {
await Coffee.deleteMany({})

const response = await request(app)
.put(`/v1/coffee/${id}`)
.put(`/v1/coffees/${id}`)
.set('Authorization', `Bearer ${token}`)
.send({
type: faker.name.title(),
Expand All @@ -276,7 +303,7 @@ describe('Coffee actions', () => {
const token = generateTokenJwt(process.env.SECRET, { id: user.id })

const response = await request(app)
.delete(`/v1/coffee/${id}`)
.delete(`/v1/coffees/${id}`)
.set('Authorization', `Bearer ${token}`)

expect(response.status).toBe(204)
Expand All @@ -291,7 +318,7 @@ describe('Coffee actions', () => {
await Coffee.deleteMany({})

const response = await request(app)
.delete(`/v1/coffee/${id}`)
.delete(`/v1/coffees/${id}`)
.set('Authorization', `Bearer ${token}`)

expect(response.status).toBe(404)
Expand All @@ -304,7 +331,7 @@ describe('Coffee actions', () => {
const token = generateTokenJwt(process.env.SECRET, { id: user.id })

const response = await request(app)
.put(`/v1/coffee/${id}/image`)
.put(`/v1/coffees/${id}/image`)
.attach('image', path.resolve(dirExamples, 'example.pdf'))
.set('Authorization', `Bearer ${token}`)

Expand All @@ -318,7 +345,7 @@ describe('Coffee actions', () => {
const token = generateTokenJwt(process.env.SECRET, { id: user.id })

const response = await request(app)
.put(`/v1/coffee/${id}/image`)
.put(`/v1/coffees/${id}/image`)
.attach('image', path.resolve(dirExamples, 'full.jpg'))
.set('Authorization', `Bearer ${token}`)

Expand All @@ -334,7 +361,7 @@ describe('Coffee actions', () => {
await Coffee.deleteMany({})

const response = await request(app)
.put(`/v1/coffee/${id}/image`)
.put(`/v1/coffees/${id}/image`)
.attach('image', path.resolve(dirExamples, 'example.jpg'))
.set('Authorization', `Bearer ${token}`)

Expand All @@ -348,7 +375,26 @@ describe('Coffee actions', () => {
const token = generateTokenJwt(process.env.SECRET, { id: user.id })

const response = await request(app)
.put(`/v1/coffee/${id}/image`)
.put(`/v1/coffees/${id}/image`)
.attach('image', path.resolve(dirExamples, 'example.jpg'))
.set('Authorization', `Bearer ${token}`)

expect(response.status).toBe(200)
})

it('should be able replace image of coffee', async () => {
const user = await factory.create<UserModel>('User')

const { id } = await factory.create<CoffeeModel>('Coffee', { author: user.id })
const token = generateTokenJwt(process.env.SECRET, { id: user.id })

await request(app)
.put(`/v1/coffees/${id}/image`)
.attach('image', path.resolve(dirExamples, 'example.jpg'))
.set('Authorization', `Bearer ${token}`)

const response = await request(app)
.put(`/v1/coffees/${id}/image`)
.attach('image', path.resolve(dirExamples, 'example.jpg'))
.set('Authorization', `Bearer ${token}`)

Expand All @@ -362,12 +408,12 @@ describe('Coffee actions', () => {
const token = generateTokenJwt(process.env.SECRET, { id: user.id })

await request(app)
.put(`/v1/coffee/${id}/image`)
.put(`/v1/coffees/${id}/image`)
.attach('image', path.resolve(dirExamples, 'example.jpg'))
.set('Authorization', `Bearer ${token}`)

const response = await request(app)
.delete(`/v1/coffee/${id}`)
.delete(`/v1/coffees/${id}`)
.set('Authorization', `Bearer ${token}`)

expect(response.status).toBe(204)
Expand Down
Loading

0 comments on commit ba25870

Please sign in to comment.