Skip to content

Commit

Permalink
edit endpoint #10
Browse files Browse the repository at this point in the history
  • Loading branch information
ElhamFadel committed Nov 3, 2021
2 parents 7733346 + d868c90 commit 7efc436
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 2 deletions.
7 changes: 7 additions & 0 deletions server/controllers/admins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const adminLogin = require('./login');
const signupAdmin = require('./signupAdmin');

module.exports = {
adminLogin,
signupAdmin,
};
35 changes: 35 additions & 0 deletions server/controllers/admins/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* eslint-disable consistent-return */
const bcrypt = require('bcrypt');
const { loginSchema } = require('../../utils/validation/loginSchema');
const { checkAdminQuery } = require('../../database/quieres');
const { signToken } = require('../../utils');

const adminLogin = async (req, res, next) => {
try {
const { email, password } = req.body;
await loginSchema.validateAsync(req.body);

const { rows } = await checkAdminQuery(email);

if (!rows.length) {
return res.status(400).json({ message: 'Invalid email or password' });
}

const compared = await bcrypt.compare(password, rows[0].password);
if (!compared) {
return res.status(400).json({ message: 'Invalid email or password' });
}
const token = await signToken(email, rows[0].id);
return res.cookie('token', token).json({ message: 'You are Logged Successfully' });
} catch (err) {
if (err.details) {
res.status(400).json({
message: err.details[0].message,
});
} else {
return next(err);
}
}
};

module.exports = adminLogin;
24 changes: 24 additions & 0 deletions server/controllers/admins/signupAdmin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { hash } = require('bcrypt');
const adminSchema = require('../../utils/validation/adminSchema');
const { signUpAdminQuery } = require('../../database/quieres');
const { signToken } = require('../../utils');

module.exports = async (req, res, next) => {
try {
const {
error, value: {
password, email, username,
},
} = adminSchema.validate(req.body);
if (error) return res.status(400).json({ message: error.details[0].message });
const hasedPasword = await hash(password, 10);
await signUpAdminQuery(username, hasedPasword, email);
const token = await signToken(email, username);
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);
}
};
4 changes: 3 additions & 1 deletion server/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ const {
} = require('./users');
const { deleteEstate, editEstate } = require('./estates');
const logout = require('./logout');
const { adminLogin } = require('./admins');

module.exports = {
getAllUsers, userEstateshandler, logout, login, deleteEstate, editEstate, putAgent,
getAllUsers, userEstateshandler, logout, login, deleteEstate, editEstate, putAgent,adminLogin

};
2 changes: 1 addition & 1 deletion server/controllers/logout.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const logout = (req, res) => {
res.clearCookie('token');
res.clearCookie('token').json({ message: 'Logout Successfully' });
};
module.exports = logout;
2 changes: 2 additions & 0 deletions server/database/quieres/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const checkAdminQuery = require('./checkAdmin');
const checkEmailQuery = require('./checkEmailQuery');
const editEstateQuery = require('./editEstatesQuery');
const deleteEstateQuery = require('./deleteEstateQuery');
const signUpAdminQuery = require('./signUpAdminQuery');

module.exports = {
getAllUsersQuery,
Expand All @@ -16,4 +17,5 @@ module.exports = {
checkEmailQuery,
editEstateQuery,
deleteEstateQuery,
signUpAdminQuery,
};
3 changes: 3 additions & 0 deletions server/database/quieres/signUpAdminQuery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const connection = require('../config/connection');

module.exports = (userName, password, email) => connection.query('INSERT INTO admins (username,password, email) VALUES ($1,$2,$3)', [userName, password, email]);
7 changes: 7 additions & 0 deletions server/routes/admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const router = require('express').Router();
const { signupAdmin, adminLogin } = require('../controllers/admins');

router.post('/signup', signupAdmin);
router.post('/login', adminLogin);

module.exports = router;
2 changes: 2 additions & 0 deletions server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ const router = require('express').Router();
const estate = require('./estate');
const users = require('./users');
const { logout } = require('../controllers');
const admins = require('./admin');

router.get('/logout', logout);
router.use('/admin', admins);
router.use('/estate', estate);
router.use('/user', users);

Expand Down
1 change: 1 addition & 0 deletions server/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { isAuth } = require('../middleware');

router.use('/estate', estate);
router.post('/signup', signup);
router.post('/login', login);
router.get('/:userId/estates', userEstateshandler);
router.put('/', isAuth, putAgent);
router.get('/', getAllUsers);
Expand Down
38 changes: 38 additions & 0 deletions server/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ describe('Tests login route', () => {
});
});

describe('Tests login route to admin', () => {
test(' login route /login ', async () => {
const res = await supertest(app)
.post('/api/v1/admin/login')
.send({
email: '[email protected]',
password: '1234567894455',
})
.expect(200);
return expect(res.body).toEqual({ message: 'You are Logged Successfully' });
});

test(' login route /login with error in email or password ', async () => {
const res = await supertest(app)
.post('/api/v1/admin/login')
.send({
email: '[email protected]',
password: '1234566',
})
.expect(400);
return expect(res.body).toEqual({ message: 'Invalid email or password' });
});
});

describe('user estates', () => {
test('get users estates', async () => {
const res = await supertest(app)
Expand Down Expand Up @@ -294,6 +318,20 @@ describe('test Edit Agent data /user/:iduser ', () => {
.expect('Content-Type', /json/);
return expect(res.body).toEqual({
message: 'There\'s no Agent, put correct id',
describe('test signup as admin ', () => {
test('test sign up endpoint when success', async () => {
const res = await supertest(app)
.post('/api/v1/admin/signup')
.send({
username: 'test',
password: 'test123456',
email: '[email protected]',
})
.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',
});
});
});
7 changes: 7 additions & 0 deletions server/utils/validation/adminSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const joi = require('joi');

module.exports = joi.object({
username: joi.string().required(),
email: joi.string().email().required(),
password: joi.string().min(5).required(),
});

0 comments on commit 7efc436

Please sign in to comment.