Skip to content

Commit

Permalink
feature(Logout): Logout user
Browse files Browse the repository at this point in the history
    - Added column on user table

    - Added logout function in user controller

    - create router for logout

    - create tests for logout

    [Finishes #168781682]
  • Loading branch information
ngireric123 committed Nov 15, 2019
1 parent c0c8991 commit 9e7db56
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,10 @@ export default class UserController {
const status = (user[1][0].dataValues[propertyToGet]) ? 'Activated' : 'Deactivated';
return responseUtil(res, 200, `${message} Notifcation ${status}`);
}

static async logoutUser(req, res) {
const { id } = req.user.payload;
await updateUser({ logoutTime: new Date() }, id);
return responseUtil(res, 200, strings.users.success.LOGOUT);
}
}
14 changes: 14 additions & 0 deletions src/database/migrations/20191114065731-add-logoutTime-to-user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addColumn('users','logoutTime',{
type: Sequelize.DATE,
allowNull: true
},'')
},

down: (queryInterface, Sequelize) => {
return queryInterface.removeColumn('users','logoutTime');
}
};
1 change: 1 addition & 0 deletions src/database/models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = (sequelize, Datatypes) => {
currency: Datatypes.STRING,
company: Datatypes.STRING,
department: Datatypes.STRING,
logoutTime: Datatypes.DATE,
isVerified: {
type: Datatypes.BOOLEAN,
defaultValue: false,
Expand Down
11 changes: 11 additions & 0 deletions src/database/seeders/20191002153158-usersTableSeeder.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
role: 6,
emailNotif: true,
appNotif: true,
logoutTime: null,
createdAt: new Date(),
updatedAt: new Date()
},
Expand All @@ -34,6 +35,7 @@ module.exports = {
updatedAt: new Date(),
password: '$2b$10$vQp2ahUwAnRS.HHxNLK0pOQ/E41TRnxtlDJL.5vVRHsvL7DC9svNm',
isVerified: false,
logoutTime: null,
createdAt: new Date(),
updatedAt: new Date(),
},
Expand All @@ -46,6 +48,7 @@ module.exports = {
role: 6,
emailNotif: true,
appNotif: true,
logoutTime: null,
createdAt: new Date(),
updatedAt: new Date()
},
Expand All @@ -58,6 +61,7 @@ module.exports = {
role: 5,
emailNotif: true,
appNotif: true,
logoutTime: null,
createdAt: new Date(),
updatedAt: new Date()
},
Expand All @@ -71,6 +75,7 @@ module.exports = {
role: 1,
emailNotif: true,
appNotif: true,
logoutTime: null,
createdAt: new Date(),
updatedAt: new Date()
},
Expand All @@ -83,6 +88,7 @@ module.exports = {
role: 6,
emailNotif: true,
appNotif: true,
logoutTime: null,
createdAt: new Date(),
updatedAt: new Date()
},
Expand All @@ -95,6 +101,7 @@ module.exports = {
role: 5,
emailNotif: true,
appNotif: true,
logoutTime: null,
createdAt: new Date(),
updatedAt: new Date()
},
Expand All @@ -107,6 +114,7 @@ module.exports = {
role: 4,
emailNotif: true,
appNotif: true,
logoutTime: null,
createdAt: new Date(),
updatedAt: new Date()
},
Expand All @@ -119,6 +127,7 @@ module.exports = {
role: 4,
emailNotif: true,
appNotif: true,
logoutTime: null,
createdAt: new Date(),
updatedAt: new Date()
},
Expand All @@ -131,6 +140,7 @@ module.exports = {
appNotif: true,
lineManager: 8,
role: 2,
logoutTime: null,
createdAt: new Date(),
updatedAt: new Date()
},
Expand All @@ -143,6 +153,7 @@ module.exports = {
appNotif: true,
lineManager: 8,
role: 5,
logoutTime: null,
createdAt: new Date(),
updatedAt: new Date()
},
Expand Down
7 changes: 5 additions & 2 deletions src/routes/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import confirmPassword from '../../middlewares/confirmPassword';
import user from '../../middlewares/users';
import wrongSwitch from '../../middlewares/wrongSwitch';

const { signup, signIn, switchNotif } = UserController;
const {
signup, signIn, switchNotif, logoutUser
} = UserController;
const { updateProfile, getProfile } = profile;
const {
validateProfile, validateLogin, validateSignup, validateEmail, validateResetpassword
Expand Down Expand Up @@ -272,7 +274,7 @@ const router = express.Router();
* '401':
* description: Please Login
*/

router.patch('/logout', validateToken, logoutUser);
router.post('/register', validateSignup, verifyExist, confirmPassword, signup);
router.get('/verify/:token', UserController.userVerify);
router.post('/forgotpassword', validateEmail, UserController.Providelink);
Expand All @@ -282,4 +284,5 @@ router.post('/login', validateLogin, signIn);
router.get('/profile/:email', validateToken, user.compareData, getProfile);
router.patch('/:switchParam', validateToken, wrongSwitch, switchNotif);


export default router;
1 change: 0 additions & 1 deletion src/tests/RequestStats.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ describe('stats Requests Tests', () => {
.get('/api/v1/requests/stats?startDate=2019-12-07&endDate=2020-11-03')
.set('Authorization', `Bearer ${userToken}`)
.end((err, res) => {

res.should.have.property('status').eql(400);
res.body.should.have.property('message').eql('Either startDate or endDate must not be greater than today\'s date');
done();
Expand Down
2 changes: 2 additions & 0 deletions src/tests/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import accommodationSearchTests from './accommodationSearchTests.spec'
import bookmarkTests from './bookmarkTests.spec';
import unitTests from './unitTests.spec';
import likeTests from './likeTests.spec';
import logoutTests from './logoutTests.spec';

describe('Default Tests', defaultTests);
describe('Edit Request Tests', editRequest);
Expand Down Expand Up @@ -49,3 +50,4 @@ describe('Accommodation Search Tests', accommodationSearchTests);
describe('Bookmarks Tests', bookmarkTests);
describe('Unit Tests', unitTests);
describe('Likes Tests', likeTests);
describe('Logout Tests', logoutTests);
23 changes: 23 additions & 0 deletions src/tests/logoutTests.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import chai from 'chai';
import chaiHttp from 'chai-http';
import { describe, it } from 'mocha';
import app from '../index';
import mockData from './mockData/mockData';
import generateToken from '../utils/generateToken';

const token = generateToken(mockData.verifiedUser1);

chai.should();
chai.use(chaiHttp);

describe('User Logout Test', () => {
it('check for successfull logout', (done) => {
chai.request(app)
.patch('/api/v1/users/logout')
.set('Authorization', `Bearer ${token}`)
.end((err, res) => {
res.should.have.status(200);
done();
});
});
});
3 changes: 2 additions & 1 deletion src/utils/stringsUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const strings = {
SUCCESS_UPDATE: 'User Updated',
SUCCESSFUL_LOGIN: 'User logged in successfully!',
SUCCESSFUL_ASSIGN: 'you have assigned the role to this user',
ROLE_ADDED: 'role added successfully'
ROLE_ADDED: 'role added successfully',
LOGOUT: 'You have been logged out successfully!'
},
error: {
BAD_SIGNUP_REQUEST: 'Input Error please check error!',
Expand Down

0 comments on commit 9e7db56

Please sign in to comment.