diff --git a/.gitignore b/.gitignore index 6fe20f0..5693cd7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ node_modules .env -package-lock.json \ No newline at end of file +package-lock.json diff --git a/.vscode/settings.json b/.vscode/settings.json index c3a7563..6681eb0 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,4 +4,4 @@ "editor.codeActionsOnSave": { "source.fixAll.eslint": true } - } \ No newline at end of file +} \ No newline at end of file diff --git a/README.md b/README.md index b0579fa..4a8a767 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Real Estate is an App that facilitates to access and rent or buy available homes ## Team Members :busts_in_silhouette::- 1. Mohammed haroon 2. MOhammed salah -3. ameera +3. Ameera Abed Alnaser Khalil 4. Elham Fadel diff --git a/package.json b/package.json index 26c7405..84ccd3e 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "start": "cross-env NODE_ENV=production node server", "dev": "cross-env NODE_ENV=development nodemon server", "test": "cross-env NODE_ENV=test jest", - "build:db": "NODE_ENV=test node server/database/data/build" + "build:db": "cross-env NODE_ENV=development node server/database/config" }, "repository": { "type": "git", diff --git a/server/app.js b/server/app.js index 7bc115b..7e55586 100644 --- a/server/app.js +++ b/server/app.js @@ -24,7 +24,7 @@ if (NODE_ENV === 'production') { res.sendFile(join(__dirname, '..', 'client', 'build', 'index.html')); }); } -router.use(notFoundError); -router.use(serverError); +app.use(notFoundError); +app.use(serverError); module.exports = app; diff --git a/server/controllers/users/index.js b/server/controllers/users/index.js index 0f91d85..adee2b1 100644 --- a/server/controllers/users/index.js +++ b/server/controllers/users/index.js @@ -1,5 +1,11 @@ +const signup = require('./signup'); const userEstateshandler = require('./userEstates'); const getAllUsers = require('./getAllUsers'); const login = require('./login'); -module.exports = { getAllUsers, userEstateshandler, login }; +module.exports = { + signup, + getAllUsers, + userEstateshandler, + login, +}; diff --git a/server/controllers/users/login.js b/server/controllers/users/login.js index 499f678..87b99eb 100644 --- a/server/controllers/users/login.js +++ b/server/controllers/users/login.js @@ -1,3 +1,4 @@ +/* eslint-disable no-console */ /* eslint-disable consistent-return */ const bcrypt = require('bcrypt'); const { loginSchema } = require('../../utils/validation/loginSchema'); diff --git a/server/controllers/users/signup.js b/server/controllers/users/signup.js new file mode 100644 index 0000000..ab56711 --- /dev/null +++ b/server/controllers/users/signup.js @@ -0,0 +1,24 @@ +const { hash } = require('bcrypt'); +const agentSchema = require('../../utils/validation/agentSchema'); +const signUpQuery = require('../../database/quieres/account/signUp'); +const { signToken } = require('../../utils'); + +module.exports = async (req, res, next) => { + try { + const { + error, value: { + password, email, username, phone, + }, + } = agentSchema.validate(req.body); + if (error) return res.status(400).json({ message: error.details[0].message }); + const hasedPasword = await hash(password, 10); + await signUpQuery(username, email, phone, hasedPasword); + const token = await signToken(email, username, phone); + return res.status(201).cookie('token', token).json({ message: 'user created' }); + } catch (err) { + if (err.code === '23505') { + return res.status(400).json({ message: 'The user is already exists' }); + } + return next(err); + } +}; diff --git a/server/controllers/users/userEstates.js b/server/controllers/users/userEstates.js index 63622a3..5f7a1ae 100644 --- a/server/controllers/users/userEstates.js +++ b/server/controllers/users/userEstates.js @@ -10,7 +10,7 @@ const userEstateshandler = async (req, res) => { data: rows, }); } - res.status(404).json({ + return res.status(404).json({ message: 'enter valid user id', }); } catch (err) { diff --git a/server/database/config/build.js b/server/database/config/build.js index 93646ef..a35d117 100644 --- a/server/database/config/build.js +++ b/server/database/config/build.js @@ -1,11 +1,12 @@ const { readFileSync } = require('fs'); const { join } = require('path'); -const connection = require('../connection'); +const connection = require('./connection'); const dbBuild = () => { let sql = readFileSync(join(__dirname, 'build.sql')).toString(); sql += readFileSync(join(__dirname, 'fakeData.sql')).toString(); return connection.query(sql); }; -module.exports = { dbBuild }; + +module.exports = dbBuild; diff --git a/server/database/connection.js b/server/database/config/connection.js similarity index 99% rename from server/database/connection.js rename to server/database/config/connection.js index 7ef7a43..2e4a695 100644 --- a/server/database/connection.js +++ b/server/database/config/connection.js @@ -18,6 +18,7 @@ switch (NODE_ENV) { default: throw new Error('There\'s no environment'); } + const options = { connectionString: dbUrl, ssl: { rejectUnauthorized: false }, diff --git a/server/database/config/databaseConfig.sql b/server/database/config/databaseConfig.sql new file mode 100644 index 0000000..7e74e32 --- /dev/null +++ b/server/database/config/databaseConfig.sql @@ -0,0 +1,7 @@ +CREATE DATABASE housydev; +CREATE USER housyuser WITH superuser password '123456'; +ALTER DATABASE housydev OWNER TO housyuser; +-- test db +CREATE DATABASE housytest; +CREATE USER housyuser2 WITH superuser password '123456'; +ALTER DATABASE housytest OWNER TO housyuser2; \ No newline at end of file diff --git a/server/database/config/fakeData.sql b/server/database/config/fakeData.sql index 5f62648..55c810a 100644 --- a/server/database/config/fakeData.sql +++ b/server/database/config/fakeData.sql @@ -1,43 +1,25 @@ -insert into admins (username, password , email) values ('ameera', 'F1A1CC71ED10594F97B1B6CF94A00727', 'ameera2021abed@gmail.com'); -insert into admins (username, password , email) values ('haroon', 'F1A1CC71ED10594F97B1B6CF94A00727', 'hro19502001@gmail.com'); -insert into admins (username, password , email) values ('elham', 'F1A1CC71ED10594F97B1B6CF94A00727', 'elham2000fadel@gmail.com'); -insert into admins (username, password , email) values ('sallah', 'F1A1CC71ED10594F97B1B6CF94A00727', 'mohmsal96@gmail.com'); -insert into agents (name, email, password , phone, avater) values ('Kai', 'kallport0@patch.com', '6D29E04CD937DC37CB72E42736CA238C', '481-649-8020', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSrVnja3DFheGQjch5AL1n0Rk8nOFHm6Ny60w&usqp=CAU'); -insert into agents (name, email, password , phone, avater) values ('Trixie', 'tbeadon1@plala.or.jp', '6D29E04CD937DC37CB72E42736CA238C', '663-871-7450', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSrVnja3DFheGQjch5AL1n0Rk8nOFHm6Ny60w&usqp=CAU'); -insert into agents (name, email, password , phone, avater) values ('Allina', 'aburford2@tumblr.com', '6D29E04CD937DC37CB72E42736CA238C', '630-385-8312', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSrVnja3DFheGQjch5AL1n0Rk8nOFHm6Ny60w&usqp=CAU'); -insert into agents (name, email, password , phone, avater) values ('Maye', 'mstrass3@yellowpages.com', '6D29E04CD937DC37CB72E42736CA238C', '210-886-2847', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSrVnja3DFheGQjch5AL1n0Rk8nOFHm6Ny60w&usqp=CAU'); -insert into agents (name, email, password , phone, avater) values ('Lillian', 'abutrford2@tumblr.com', '6D29E04CD937DC37CB72E42736CA238C', '270-209-1221', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSrVnja3DFheGQjch5AL1n0Rk8nOFHm6Ny60w&usqp=CAU'); -insert into agents (name, email, password , phone, avater) values ('Emlyn', 'edrissell5@free.fr', '6D29E04CD937DC37CB72E42736CA238C', '426-753-9984', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSrVnja3DFheGQjch5AL1n0Rk8nOFHm6Ny60w&usqp=CAU'); -insert into estates ( agent_id, title, price, description, type, category, street, city, region, bathrooms, bedrooms, rooms, space, approved, rate, available) values (1, 'suscipit ligula in', '190483.22', 'tristique', 'Buy', 'House', '3152 Morningstar Park', 'Edinburgh of the Seven Seas', 'Saint Helena', 2, 1, 1, 194, false, 2, false); -insert into estates ( agent_id, title, price, description, type, category, street, city, region, bathrooms, bedrooms, rooms, space, approved, rate, available) values (2, 'ipsum primis in', '242471.89', 'est quam pharetra magna ac consequat metus sapien ut nunc vestibulum ante', 'Buy', 'House', '100 Butternut Hill', 'Bern', 'Switzerland', 1, 2, 2, 226, true, 5, true); -insert into estates ( agent_id, title, price, description, type, category, street, city, region, bathrooms, bedrooms, rooms, space, approved, rate, available) values (3, 'rhoncus sed vestibulum', '84726.00', 'massa volutpat convallis morbi odio odio elementum eu interdum eu tincidunt in', 'Buy', 'House', '0824 Mcguire Way', 'Kungshamn', 'Sweden', 1, 3, 3, 235, true, 5, false); -insert into estates ( agent_id, title, price, description, type, category, street, city, region, bathrooms, bedrooms, rooms, space, approved, rate, available) values (4, 'sociis natoque penatibus et', '106226.71', 'quis odio consequat', 'Buy', 'House', '15918 Mcguire Point', 'Ranong', 'Thailand', 2, 4, 4, 244, true, 1, false); -insert into estates ( agent_id, title, price, description, type, category, street, city, region, bathrooms, bedrooms, rooms, space, approved, rate, available) values (5, 'in faucibus orci luctus', '116162.27', 'parturient montes nascetur ridiculus mus vivamus vestibulum sagittis sapien cum', 'Buy', 'House', '898 Dixon Crossing', 'Gelap', 'Indonesia', 3, 2, 4, 150, false, 3, false); -insert into estates ( agent_id, title, price, description, type, category, street, city, region, bathrooms, bedrooms, rooms, space, approved, rate, available) values (6, 'vestibulum ante ipsum primis', '194193.55', 'leo odio porttitor id consequat in consequat ut nulla sed', 'Buy', 'House', '0891 7th Park', 'Álimos', 'Greece', 1, 3, 2, 174, false, 1, false); -insert into images ( estate_id, image) values (1,'https://images.unsplash.com/photo-1580587771525-78b9dba3b914?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8bHV4dXJ5JTIwaG91c2V8ZW58MHx8MHx8&ixlib=rb-1.2.1&w=1000&q=80'); -insert into images ( estate_id, image) values (1,'https://archello.s3.eu-central-1.amazonaws.com/images/2018/05/17/1.tobiarchitects.1526566679.5654.jpg'); -insert into images ( estate_id, image) values (1,'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRHpF_yr6jMER79FRSejAszXCsyd__87nEF6g&usqp=CAU'); -insert into images ( estate_id, image) values (1,'https://images.unsplash.com/photo-1580587771525-78b9dba3b914?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8bHV4dXJ5JTIwaG91c2V8ZW58MHx8MHx8&ixlib=rb-1.2.1&w=1000&q=80'); +-- +INSERT INTO agents (name, email, password , phone) + VALUES ('Kai', 'kallport0@patch.com', '$2b$10$oNaAu46EHAyOCiufPgchaOQDq5opRxSFHB20m.e3wzDBlM5Yzztf2', '677-871-7450'), + ('Trixie', 'tbeadon1@plala.or.jp', '$2b$10$oNaAu46EHAyOCiufPgchaOQDq5opRxSFHB20m.e3wzDBlM5Yzztf2', '0599832685'), +('Allina', 'aburford2@tumblr.com', '$2b$10$oNaAu46EHAyOCiufPgchaOQDq5opRxSFHB20m.e3wzDBlM5Yzztf2', '630-385-8312'); -insert into images ( estate_id, image) values (2,'https://cdn.vox-cdn.com/thumbor/6itLJS9BZ-B5gXPjM1AB_z-ZKVI=/0x0:3000x2000/1200x800/filters:focal(1260x760:1740x1240)/cdn.vox-cdn.com/uploads/chorus_image/image/65890203/iStock_1067331614.7.jpg'); -insert into images ( estate_id, image) values (2,'https://archello.s3.eu-central-1.amazonaws.com/images/2018/05/17/1.tobiarchitects.1526566679.5654.jpg'); -insert into images ( estate_id, image) values (2,'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRHpF_yr6jMER79FRSejAszXCsyd__87nEF6g&usqp=CAU'); -insert into images ( estate_id, image) values (2,'https://images.unsplash.com/photo-1580587771525-78b9dba3b914?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8bHV4dXJ5JTIwaG91c2V8ZW58MHx8MHx8&ixlib=rb-1.2.1&w=1000&q=80'); +INSERT INTO admins (username, password , email) VALUES ('ameera', '$2b$10$oNaAu46EHAyOCiufPgchaOQDq5opRxSFHB20m.e3wzDBlM5Yzztf2', 'ameera2021abed@gmail.com'), + ('haroon', '$2b$10$oNaAu46EHAyOCiufPgchaOQDq5opRxSFHB20m.e3wzDBlM5Yzztf2', 'hro19502001@gmail.com'), + ('elham', '$2b$10$oNaAu46EHAyOCiufPgchaOQDq5opRxSFHB20m.e3wzDBlM5Yzztf2', 'elham2000fadel@gmail.com'), +('sallah', '$2b$10$oNaAu46EHAyOCiufPgchaOQDq5opRxSFHB20m.e3wzDBlM5Yzztf2', 'mohmsal96@gmail.com'); -insert into images ( estate_id, image) values (3,'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSh4O9GCySQw_9C24XfInhq-lYgfnHlRSMB5g&usqp=CAU'); -insert into images ( estate_id, image) values (3,'https://archello.s3.eu-central-1.amazonaws.com/images/2018/05/17/1.tobiarchitects.1526566679.5654.jpg'); -insert into images ( estate_id, image) values (3,'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRHpF_yr6jMER79FRSejAszXCsyd__87nEF6g&usqp=CAU'); -insert into images ( estate_id, image) values (3,'https://images.unsplash.com/photo-1580587771525-78b9dba3b914?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8bHV4dXJ5JTIwaG91c2V8ZW58MHx8MHx8&ixlib=rb-1.2.1&w=1000&q=80'); -insert into images ( estate_id, image) values (4,'https://images.unsplash.com/photo-1580587771525-78b9dba3b914?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8bHV4dXJ5JTIwaG91c2V8ZW58MHx8MHx8&ixlib=rb-1.2.1&w=1000&q=80'); -insert into images ( estate_id, image) values (4,'https://archello.s3.eu-central-1.amazonaws.com/images/2018/05/17/1.tobiarchitects.1526566679.5654.jpg'); -insert into images ( estate_id, image) values (4,'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRHpF_yr6jMER79FRSejAszXCsyd__87nEF6g&usqp=CAU'); -insert into images ( estate_id, image) values (4,'https://images.unsplash.com/photo-1580587771525-78b9dba3b914?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8bHV4dXJ5JTIwaG91c2V8ZW58MHx8MHx8&ixlib=rb-1.2.1&w=1000&q=80'); +INSERT INTO estates ( agent_id, title, price, description, type, category, street, city, region, bathrooms, bedrooms, rooms, space, approved, rate, available) VALUES (1, 'suscipit ligula in', 190483.22, 'tristique', 'Buy', 'House', '3152 Morningstar Park', 'Edinburgh of the Seven Seas', 'Saint Helena', 2, 1, 1, 194, false, 2, false), + (2, 'ipsum primis in', 242471.89, 'est quam pharetra magna ac consequat metus sapien ut nunc vestibulum ante', 'Buy', 'House', '100 Butternut Hill', 'Bern', 'Switzerland', 1, 2, 2, 226, true, 5, true), + (3, 'rhoncus sed vestibulum', 84726.00, 'massa volutpat convallis morbi odio odio elementum eu interdum eu tincidunt in', 'Buy', 'House', '0824 Mcguire Way', 'Kungshamn', 'Sweden', 1, 3, 3, 235, true, 5, false), +(1, 'sociis natoque penatibus et', 106226.71, 'quis odio consequat', 'Buy', 'House', '15918 Mcguire Point', 'Ranong', 'Thailand', 2, 4, 4, 244, true, 1, false), + (2, 'in faucibus orci luctus', 116162.27, 'parturient montes nascetur ridiculus mus vivamus vestibulum sagittis sapien cum', 'Buy', 'House', '898 Dixon Crossing', 'Gelap', 'Indonesia', 3, 2, 4, 150, false, 3, false), + (3, 'vestibulum ante ipsum primis', 194193.55, 'leo odio porttitor id consequat in consequat ut nulla sed', 'Buy', 'House', '0891 7th Park', 'Álimos', 'Greece', 1, 3, 2, 174, false, 1, false); -insert into images ( estate_id, image) values (5,'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTsj7rODPg-8QIdi6VONhE84ZFng3D5WpNfbA&usqp=CAU'); -insert into images ( estate_id, image) values (5,'https://archello.s3.eu-central-1.amazonaws.com/images/2018/05/17/1.tobiarchitects.1526566679.5654.jpg'); -insert into images ( estate_id, image) values (5,'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRHpF_yr6jMER79FRSejAszXCsyd__87nEF6g&usqp=CAU'); -insert into images ( estate_id, image) values (5,'https://images.unsplash.com/photo-1580587771525-78b9dba3b914?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8bHV4dXJ5JTIwaG91c2V8ZW58MHx8MHx8&ixlib=rb-1.2.1&w=1000&q=80'); -insert into images ( estate_id, image) values (6,'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSBOKk95d1JPgPWHYPk_Z2NXd_ntTr-N0E0yQ&usqp=CAU'); -insert into images ( estate_id, image) values (6,'https://archello.s3.eu-central-1.amazonaws.com/images/2018/05/17/1.tobiarchitects.1526566679.5654.jpg'); -insert into images ( estate_id, image) values (6,'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRHpF_yr6jMER79FRSejAszXCsyd__87nEF6g&usqp=CAU'); -insert into images ( estate_id, image) values (6,'https://images.unsplash.com/photo-1580587771525-78b9dba3b914?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8bHV4dXJ5JTIwaG91c2V8ZW58MHx8MHx8&ixlib=rb-1.2.1&w=1000&q=80'); +INSERT INTO images ( estate_id, image) VALUES (1,'https://archello.s3.eu-central-1.amazonaws.com/images/2018/05/17/1.tobiarchitects.1526566679.5654.jpg'), + (2,'https://archello.s3.eu-central-1.amazonaws.com/images/2018/05/17/2.tobiarchitects.1526566679.5654.jpg'), + (3,'https://archello.s3.eu-central-1.amazonaws.com/images/2018/05/17/3.tobiarchitects.1526566679.5654.jpg'), + (1,'https://archello.s3.eu-central-1.amazonaws.com/images/2018/05/17/4.tobiarchitects.1526566679.5654.jpg'), + (2,'https://archello.s3.eu-central-1.amazonaws.com/images/2018/05/17/5.tobiarchitects.1526566679.5654.jpg'), + (3,'https://archello.s3.eu-central-1.amazonaws.com/images/2018/05/17/6.tobiarchitects.1526566679.5654.jpg'); + diff --git a/server/database/config/index.js b/server/database/config/index.js new file mode 100644 index 0000000..b975dc6 --- /dev/null +++ b/server/database/config/index.js @@ -0,0 +1,13 @@ +/* eslint-disable no-console */ +const connection = require('./connection'); +const dbBuild = require('./build'); + +(async () => { + try { + await dbBuild(); + console.log('Build Successfully'); + connection.end(); + } catch (e) { + console.log('Build Failed', e); + } +})(); diff --git a/server/database/quieres/account/signUp.js b/server/database/quieres/account/signUp.js new file mode 100644 index 0000000..80d429c --- /dev/null +++ b/server/database/quieres/account/signUp.js @@ -0,0 +1,3 @@ +const connection = require('../../config/connection'); + +module.exports = (userName, email, phone, password) => connection.query('INSERT INTO agents (name,email,phone,password) VALUES ($1,$2,$3,$4)', [userName, email, phone, password]); diff --git a/server/database/quieres/checkEmailQuery.js b/server/database/quieres/checkEmailQuery.js index 9423b86..984afed 100644 --- a/server/database/quieres/checkEmailQuery.js +++ b/server/database/quieres/checkEmailQuery.js @@ -1,4 +1,4 @@ -const connection = require('../connection'); +const connection = require('../config/connection'); const checkEmailQuery = (email) => connection.query('SELECT * FROM agents WHERE email= ($1)', [email]); diff --git a/server/database/quieres/getAllUsersQuery.js b/server/database/quieres/getAllUsersQuery.js index 24231b0..680e88c 100644 --- a/server/database/quieres/getAllUsersQuery.js +++ b/server/database/quieres/getAllUsersQuery.js @@ -1,4 +1,4 @@ -const connection = require('../connection'); +const connection = require('../config/connection'); const getAllUsersQuery = () => connection.query('SELECT * FROM agents'); diff --git a/server/database/quieres/index.js b/server/database/quieres/index.js index 1302794..f3c27ed 100644 --- a/server/database/quieres/index.js +++ b/server/database/quieres/index.js @@ -1,4 +1,4 @@ -const userEstatesQuery = require('./UserEstatesQuiery'); +const userEstatesQuery = require('./userEstatesQuiery'); const getAllUsersQuery = require('./getAllUsersQuery'); const checkEmailQuery = require('./checkEmailQuery'); diff --git a/server/database/quieres/UserEstatesQuiery.js b/server/database/quieres/userEstatesQuiery.js similarity index 66% rename from server/database/quieres/UserEstatesQuiery.js rename to server/database/quieres/userEstatesQuiery.js index 811caa3..f1b86ed 100644 --- a/server/database/quieres/UserEstatesQuiery.js +++ b/server/database/quieres/userEstatesQuiery.js @@ -1,3 +1,3 @@ -const connection = require('../connection'); +const connection = require('../config/connection'); module.exports = (userId) => connection.query('SELECT * FROM estates WHERE agent_id = $1', [userId]); diff --git a/server/routes/auth.js b/server/routes/auth.js new file mode 100644 index 0000000..ad9201b --- /dev/null +++ b/server/routes/auth.js @@ -0,0 +1,7 @@ +const router = require('express').Router(); +const { logout, login } = require('../controllers'); + +router.get('/logout', logout); +router.post('/login', login); + +module.exports = router; diff --git a/server/routes/estate.js b/server/routes/estate.js new file mode 100644 index 0000000..e69de29 diff --git a/server/routes/index.js b/server/routes/index.js index 7c90092..e55de02 100644 --- a/server/routes/index.js +++ b/server/routes/index.js @@ -1,10 +1,9 @@ const router = require('express').Router(); -const { userEstateshandler } = require('../controllers'); -const { getAllUsers, logout, login } = require('../controllers'); -router.get('/users/:userId/estates', userEstateshandler); -router.post('/login', login); -router.get('/users', getAllUsers); -router.get('/logout', logout); +const auth = require('./auth'); +const users = require('./users'); + +router.use('/', auth); +router.use('/users', users); module.exports = router; diff --git a/server/routes/users.js b/server/routes/users.js new file mode 100644 index 0000000..14f0c13 --- /dev/null +++ b/server/routes/users.js @@ -0,0 +1,9 @@ +const router = require('express').Router(); +const { userEstateshandler, getAllUsers } = require('../controllers'); +const signup = require('../controllers/users/signup'); + +router.post('/signup', signup); +router.get('/:userId/estates', userEstateshandler); +router.get('/', getAllUsers); + +module.exports = router; diff --git a/server/test/index.test.js b/server/test/index.test.js index 28cb9b4..f36ddff 100644 --- a/server/test/index.test.js +++ b/server/test/index.test.js @@ -1,8 +1,8 @@ /* eslint-disable no-undef */ const supertest = require('supertest'); const app = require('../app'); -const { dbBuild } = require('../database/config/build'); -const connection = require('../database/connection'); +const dbBuild = require('../database/config/build'); +const connection = require('../database/config/connection'); beforeEach(() => dbBuild()); afterAll(() => connection.end()); @@ -13,7 +13,7 @@ describe('Tests Server', () => { .get('/api/v1/users') .expect(200) .expect('Content-Type', /json/); - return expect(6).toEqual(res.body.data.length); + return expect(3).toEqual(res.body.data.length); }); test(' login route /login ', async () => { @@ -21,7 +21,7 @@ describe('Tests Server', () => { .post('/api/v1/login') .send({ email: 'kallport0@patch.com', - password: '6D29E04CD937DC37CB72E42736CA238C', + password: '12345', }) .expect(200); expect(res.body).toEqual({ message: 'You are Logged Successfully' }); @@ -53,7 +53,7 @@ describe('user estates', () => { agent_id: 3, title: 'rhoncus sed vestibulum', price: '84726.00', - description: 'massa volutpat convallis morbi odio odio elementum eu interdum eu tincidunt in', + description: 'massa volutpat convallismorbi odio odio elementum eu interdum eu tincidunt in', type: 'Buy', category: 'House', street: '0824 Mcguire Way', @@ -83,3 +83,72 @@ describe('user estates', () => { }); }); }); + +describe('test signup endpoint with all cases ', () => { + test('test sign up endpoint when success', async () => { + const res = await supertest(app) + .post('/api/v1/users/signup') + .send({ + username: 'test', + password: 'test123456', + email: 'test@gmail.com', + phone: '0597853626', + confirmedPassword: 'test123456', + }) + .expect(201) + .expect((response) => expect(response.header['set-cookie'][0].split('=')[0]).toBe('token')) + .expect('Content-Type', /json/); + return expect(res.body).toEqual({ + message: 'user created', + }); + }); + + test('test signup error validation phone" length must be 10 characters long ', async () => { + const res = await supertest(app) + .post('/api/v1/users/signup') + .send({ + username: 'Kai', + password: '1234567894455', + email: 'kallport0@patch.com', + phone: '677', + confirmedPassword: '1234567894455', + }) + .expect(400) + .expect('Content-Type', /json/); + return expect(res.body).toEqual({ + message: '"phone" length must be at least 9 characters long', + }); + }); + test('test signup username or phone already exists ', async () => { + const res = await supertest(app) + .post('/api/v1/users/signup') + .send({ + username: 'Kai', + password: '1234567894455', + email: 'kallport0@patch.com', + phone: '0599832683', + confirmedPassword: '1234567894455', + }) + .expect(400) + .expect('Content-Type', /json/); + return expect(res.body).toEqual({ + message: 'The user is already exists', + }); + }); + test('test signup confirmpassword ', async () => { + const res = await supertest(app) + .post('/api/v1/users/signup') + .send({ + username: 'test', + password: 'test123456', + email: 'test@gmail.com', + phone: '0597853626', + confirmedPassword: 'test12345', + }) + .expect(400) + .expect('Content-Type', /json/); + return expect(res.body).toEqual({ + message: '"confirmedPassword" must be [ref:password]', + }); + }); +}); diff --git a/server/utils/validation/agentSchema.js b/server/utils/validation/agentSchema.js new file mode 100644 index 0000000..6857fe8 --- /dev/null +++ b/server/utils/validation/agentSchema.js @@ -0,0 +1,9 @@ +const joi = require('joi'); + +module.exports = joi.object({ + username: joi.string().required(), + email: joi.string().email().required(), + password: joi.string().min(5).required(), + phone: joi.string().min(9).required(), + confirmedPassword: joi.ref('password'), +});