Skip to content

Commit

Permalink
feat(users): add user delete option ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil committed Nov 14, 2019
1 parent 03ddcec commit a0b75df
Show file tree
Hide file tree
Showing 6 changed files with 525 additions and 10 deletions.
4 changes: 2 additions & 2 deletions modules/tasks/tests/tasks.crud.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ describe('Tasks CRUD Tests :', () => {
}
});

test('should be able to edit a task if', async () => {
test('should be able to update a task if', async () => {
// edit task
try {
const result = await agent.put(`/api/tasks/${task2.id}`)
Expand All @@ -183,7 +183,7 @@ describe('Tasks CRUD Tests :', () => {
}
});

test('should not be able to edit a task with a bad id', async () => {
test('should not be able to update a task with a bad id', async () => {
// edit task
try {
const result = await agent.put('/api/tasks/test')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ exports.reset = async (req, res) => {
/**
* Change Password
*/
exports.changePassword = async (req, res) => {
exports.updatePassword = async (req, res) => {
let user;
let password;

Expand Down
18 changes: 17 additions & 1 deletion modules/users/controllers/users/users.profile.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,28 @@ exports.update = async (req, res) => {
}
};

/**
* @desc Endpoint to ask the service to delete the user connected
* @param {Object} req - Express request object
* @param {Object} res - Express response object
*/
exports.delete = async (req, res) => {
try {
const result = await UserService.delete(req.user);
result.id = req.user.id;
responses.success(res, 'user deleted')(result);
} catch (err) {
responses.error(res, 422, 'Unprocessable Entity', errors.getMessage(err))(err);
}
};


/**
* @desc Endpoint to ask the service to update a user profile picture
* @param {Object} req - Express request object
* @param {Object} res - Express response object
*/
exports.changeProfilePicture = async (req, res) => {
exports.updateProfilePicture = async (req, res) => {
try {
await UserService.uploadImage(req, res, config.uploads.profile.avatar);
if (req.user.profileImageURL) await UserService.deleteImage(req.user.profileImageURL);
Expand Down
17 changes: 12 additions & 5 deletions modules/users/routes/users.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ module.exports = (app) => {

// Setting up the users profile api
app.route('/api/users/me').get(passport.authenticate('jwt'), users.me);
app.route('/api/users').put(passport.authenticate('jwt'), model.isValid(usersSchema.User), users.update);
app.route('/api/users/accounts').delete(users.removeOAuthProvider);
app.route('/api/users/accounts').post(model.isValid(usersSchema.User), users.addOAuthProviderUserProfile);
app.route('/api/users/password').post(passport.authenticate('jwt'), users.changePassword);
app.route('/api/users/picture').post(passport.authenticate('jwt'), users.changeProfilePicture);

app.route('/api/users')
.put(passport.authenticate('jwt'), model.isValid(usersSchema.User), users.update)
.delete(passport.authenticate('jwt'), users.delete);

app.route('/api/users/accounts')
.delete(users.removeOAuthProvider)
.post(model.isValid(usersSchema.User), users.addOAuthProviderUserProfile);

app.route('/api/users/password').post(passport.authenticate('jwt'), users.updatePassword);

app.route('/api/users/picture').post(passport.authenticate('jwt'), users.updateProfilePicture);
};
37 changes: 37 additions & 0 deletions modules/users/tests/user.crud.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,43 @@ describe('User CRUD Tests :', () => {
}
});

test('should be able to delete current user', async () => {
// Init user edited
_userEdited.email = 'register_new_user_@test.com';

try {
const result = await agent.post('/api/auth/signup')
.send(_userEdited)
.expect(200);
userEdited = result.body.user;

expect(result.body.user._id).toBe(result.body.user.id);
expect(result.body.user.email).toBe(_userEdited.email);
expect(result.body.user.provider).toBe('local');
expect(result.body.user.roles).toBeInstanceOf(Array);
expect(result.body.user.roles).toHaveLength(1);
expect(result.body.user.roles).toEqual(
expect.arrayContaining(['user']),
);
} catch (err) {
console.log(err);
expect(err).toBeFalsy();
}

// delete task
try {
const result = await agent.delete('/api/users')
.expect(200);
expect(result.body.type).toBe('success');
expect(result.body.message).toBe('user deleted');
expect(result.body.data.id).toBe(userEdited.id);
expect(result.body.data.deletedCount).toBe(1);
} catch (err) {
console.log(err);
expect(err).toBeFalsy();
}
});

test('should be able to change profile picture if signed in', async () => {
try {
const result = await agent.post('/api/users/picture')
Expand Down
Loading

0 comments on commit a0b75df

Please sign in to comment.