From 78bcae5a0ea8be2b93b59600dce21d2a5756c6fc Mon Sep 17 00:00:00 2001 From: Damyan Dimitrov Date: Tue, 10 Jan 2023 22:51:56 +0200 Subject: [PATCH] fix create home without owner id --- routes/home.js | 239 +++++++++++++++++++------------------ test/home/homePost.test.js | 33 +++++ 2 files changed, 154 insertions(+), 118 deletions(-) diff --git a/routes/home.js b/routes/home.js index cd4729b..f37c410 100644 --- a/routes/home.js +++ b/routes/home.js @@ -1,5 +1,5 @@ if (process.env.NODE_ENV !== 'production') { - require('dotenv').config(); + require('dotenv').config(); } const router = require('express').Router(); @@ -8,146 +8,149 @@ const { createHome, getHomeById, deleteHome, updateHome } = require('../services const { body, validationResult } = require('express-validator'); const reqBodyToObject = (req) => ({ - title: req.body.title, - city: req.body.city, - neighborhood: req.body.neighborhood, - address: req.body.address, - price: req.body.price, - size: req.body.size, - year: req.body.year, - description: req.body.description, - longitude: req.body.longitude, - latitude: req.body.latitude, - owner_id: req.body.owner_id + title: req.body.title, + city: req.body.city, + neighborhood: req.body.neighborhood, + address: req.body.address, + price: req.body.price, + size: req.body.size, + year: req.body.year, + description: req.body.description, + longitude: req.body.longitude, + latitude: req.body.latitude, + owner_id: req.body.owner_id, }); //Create home router.post( - '/', - body('title') - .isLength({ min: 3 }) - .withMessage('Property title must be at least 3 charaters long') - .isLength({ max: 100 }) - .withMessage('Property title must be less 100 charaters long'), - body('city') - .isLength({ min: 3 }) - .withMessage('City must be at least 3 charaters long') - .isLength({ max: 100 }) - .withMessage('City must be less 100 charaters long'), - body('neighborhood') - .isLength({ min: 3 }) - .withMessage('Neighborhood must be at least 3 charaters long') - .isLength({ max: 100 }) - .withMessage('Neighborhood must be less 100 charaters long'), - body('address') - .isLength({ min: 3 }) - .withMessage('Address must be at least 3 charaters long') - .isLength({ max: 255 }) - .withMessage('Address must be less 255 charaters long'), - body('description') - .isLength({ min: 5 }) - .withMessage(`Description must be at least 5 character long`) - .isLength({ max: 255 }) - .withMessage(`Description must less 255 character long`), - async (req, res) => { - const home = reqBodyToObject(req) - - if (!home.owner_id) return res.status(400).json("No owner id") - - const { errors } = validationResult(req); - if (errors.length > 0) { - res.status(400).json(errors); - return; - } + '/', + body('title') + .isLength({ min: 3 }) + .withMessage('Property title must be at least 3 charaters long') + .isLength({ max: 100 }) + .withMessage('Property title must be less 100 charaters long'), + body('city') + .isLength({ min: 3 }) + .withMessage('City must be at least 3 charaters long') + .isLength({ max: 100 }) + .withMessage('City must be less 100 charaters long'), + body('neighborhood') + .isLength({ min: 3 }) + .withMessage('Neighborhood must be at least 3 charaters long') + .isLength({ max: 100 }) + .withMessage('Neighborhood must be less 100 charaters long'), + body('address') + .isLength({ min: 3 }) + .withMessage('Address must be at least 3 charaters long') + .isLength({ max: 255 }) + .withMessage('Address must be less 255 charaters long'), + body('description') + .isLength({ min: 5 }) + .withMessage(`Description must be at least 5 character long`) + .isLength({ max: 255 }) + .withMessage(`Description must less 255 character long`), + async (req, res) => { + const home = reqBodyToObject(req); + + if (!isValidObjectId(home.owner_id)) { + res.status(400).json('Invalid owner!'); + return; + } + + const { errors } = validationResult(req); + if (errors.length > 0) { + res.status(400).json(errors); + return; + } - const newHome = await createHome(home); - return res.status(201).json(newHome); - } + const newHome = await createHome(home); + return res.status(201).json(newHome); + } ); //Get home router.get('/:home_id', async (req, res) => { - const homeId = req.params.home_id; - - if (!isValidObjectId(homeId)) { - res.status(400).json('Invalid home id!'); - return; - } + const homeId = req.params.home_id; - const homeInfo = await getHomeById(homeId); + if (!isValidObjectId(homeId)) { + res.status(400).json('Invalid home id!'); + return; + } - if (!homeInfo) { - res.status(404).send('Home with this id do not exists'); - return; - } + const homeInfo = await getHomeById(homeId); + + if (!homeInfo) { + res.status(404).send('Home with this id do not exists'); + return; + } - const blobStorageUrl = process.env.AZURE_FILES_STORAGE_ACCOUNT_URL - const CONTAINER_NAME = process.env.CONTAINER_NAME; + const blobStorageUrl = process.env.AZURE_FILES_STORAGE_ACCOUNT_URL; + const CONTAINER_NAME = process.env.CONTAINER_NAME; - const homeResponse = { - photo_url: `${blobStorageUrl}/${CONTAINER_NAME}/${homeInfo._id}/${homeInfo.photo_name}`, - ...homeInfo - } + const homeResponse = { + photo_url: `${blobStorageUrl}/${CONTAINER_NAME}/${homeInfo._id}/${homeInfo.photo_name}`, + ...homeInfo, + }; - res.status(200).json(homeResponse); - return; + res.status(200).json(homeResponse); + return; }); //Delete home router.delete('/', async (req, res) => { - const homeId = req.body.home_id; - if (!isValidObjectId(homeId)) { - res.status(400).json('Invalid home id!'); - return; - } - - await deleteHome(homeId); - res.status(200).json('Home has been deleted'); + const homeId = req.body.home_id; + if (!isValidObjectId(homeId)) { + res.status(400).json('Invalid home id!'); + return; + } + + await deleteHome(homeId); + res.status(200).json('Home has been deleted'); }); //Update home router.put( - '/', - body('title') - .isLength({ min: 3 }) - .withMessage('Property title must be at least 3 charaters long') - .isLength({ max: 100 }) - .withMessage('Property title must be less 100 charaters long'), - body('city') - .isLength({ min: 3 }) - .withMessage('City must be at least 3 charaters long') - .isLength({ max: 100 }) - .withMessage('City must be less 100 charaters long'), - body('neighborhood') - .isLength({ min: 3 }) - .withMessage('Neighborhood must be at least 3 charaters long') - .isLength({ max: 100 }) - .withMessage('Neighborhood must be less 100 charaters long'), - body('address') - .isLength({ min: 3 }) - .withMessage('Address must be at least 3 charaters long') - .isLength({ max: 255 }) - .withMessage('Address must be less 255 charaters long'), - body('description') - .isLength({ min: 5 }) - .withMessage(`Description must be at least 5 character long`) - .isLength({ max: 255 }) - .withMessage(`Description must less 255 character long`), - async (req, res) => { - const homeId = req.body.home_id; - if (!isValidObjectId(homeId)) { - res.status(400).json('Invalid home id!'); - return; - } - const home = reqBodyToObject(req) - const { errors } = validationResult(req); - if (errors.length > 0) { - res.status(400).json(errors); - return; + '/', + body('title') + .isLength({ min: 3 }) + .withMessage('Property title must be at least 3 charaters long') + .isLength({ max: 100 }) + .withMessage('Property title must be less 100 charaters long'), + body('city') + .isLength({ min: 3 }) + .withMessage('City must be at least 3 charaters long') + .isLength({ max: 100 }) + .withMessage('City must be less 100 charaters long'), + body('neighborhood') + .isLength({ min: 3 }) + .withMessage('Neighborhood must be at least 3 charaters long') + .isLength({ max: 100 }) + .withMessage('Neighborhood must be less 100 charaters long'), + body('address') + .isLength({ min: 3 }) + .withMessage('Address must be at least 3 charaters long') + .isLength({ max: 255 }) + .withMessage('Address must be less 255 charaters long'), + body('description') + .isLength({ min: 5 }) + .withMessage(`Description must be at least 5 character long`) + .isLength({ max: 255 }) + .withMessage(`Description must less 255 character long`), + async (req, res) => { + const homeId = req.body.home_id; + if (!isValidObjectId(homeId)) { + res.status(400).json('Invalid home id!'); + return; + } + const home = reqBodyToObject(req); + const { errors } = validationResult(req); + if (errors.length > 0) { + res.status(400).json(errors); + return; + } + const currentHome = await updateHome(homeId, home); + return res.status(200).json(currentHome); } - const currentHome = await updateHome(homeId, home); - return res.status(200).json(currentHome); - } ); module.exports = router; diff --git a/test/home/homePost.test.js b/test/home/homePost.test.js index e55d5fe..36a7b9f 100644 --- a/test/home/homePost.test.js +++ b/test/home/homePost.test.js @@ -1,11 +1,24 @@ const mockingoose = require('mockingoose'); const Homes = require('../../models/Homes'); +const User = require('../../models/User'); const request = require('supertest'); const app = require('../../app'); describe('Positive POST /home', () => { beforeEach(() => { mockingoose.resetAll(); + mockingoose(User).toReturn( + { + _id: '632beab54298559b57ff172f', + first_name: 'John', + last_name: 'Doe', + email: 'mail@abv.bg', + phone_number: '34343434', + role: 'admin', + password: '123adminsffe', + }, + 'findOne' + ); mockingoose(Homes).toReturn( { _id: '652beab54298559b57ff172f', @@ -40,6 +53,7 @@ describe('Positive POST /home', () => { latitude: '50', owner_id: '632beab54298559b57ff172f', }); + console.log(res.body) expect(res.status).toEqual(201); expect(res.body.title).toEqual('Nice house'); expect(res.body.city).toEqual('Dobrich'); @@ -53,3 +67,22 @@ describe('Positive POST /home', () => { expect(res.body.latitude).toEqual('50'); }); }); + +describe("Negative POST /home", () => { + it('should not create new home without owner_id', async () => { + const res = await request(app).post('/home').send({ + title: 'Nice house', + city: 'Dobrich', + neighborhood: 'Drujba', + address: 'Blok 42 A 2 8', + price: '10000', + size: '150', + year: '1960', + description: 'Really nice house, have 2 rooms', + longitude: '30', + latitude: '50', + }) + expect(res.status).toEqual(400); + expect(res.body).toEqual('Invalid owner!'); + }) +}) \ No newline at end of file