Skip to content

Commit

Permalink
1803-user-delete
Browse files Browse the repository at this point in the history
  • Loading branch information
basarbk committed Aug 13, 2020
1 parent e90030b commit cf1985b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
44 changes: 44 additions & 0 deletions __tests__/UserDelete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@ const app = require('../src/app');
const User = require('../src/user/User');
const Token = require('../src/auth/Token');
const Hoax = require('../src/hoax/Hoax');
const FileAttachment = require('../src/file/FileAttachment');
const bcrypt = require('bcrypt');
const en = require('../locales/en/translation.json');
const tr = require('../locales/tr/translation.json');
const fs = require('fs');
const path = require('path');
const config = require('config');

const { uploadDir, profileDir, attachmentDir } = config;
const profileFolder = path.join('.', uploadDir, profileDir);
const attachmentFolder = path.join('.', uploadDir, attachmentDir);

beforeEach(async () => {
await User.destroy({ truncate: { cascade: true } });
Expand Down Expand Up @@ -117,4 +125,40 @@ describe('User Delete', () => {
const hoaxes = await Hoax.findAll();
expect(hoaxes.length).toBe(0);
});
it('removes profile image when user is deleted', async () => {
const user = await addUser();
const token = await auth({ auth: credentials });
const storedFileName = 'profile-image-for-user1';
const testFilePath = path.join('.', '__tests__', 'resources', 'test-png.png');
const targetPath = path.join(profileFolder, storedFileName);
fs.copyFileSync(testFilePath, targetPath);
user.image = storedFileName;
await user.save();
await deleteUser(user.id, { token });
expect(fs.existsSync(targetPath)).toBe(false);
});
it('deletes hoax attachment from storage and database when delete user request sent from authorized user', async () => {
const savedUser = await addUser();
const token = await auth({ auth: credentials });

const storedFileName = 'hoax-attachment-for-user1';
const testFilePath = path.join('.', '__tests__', 'resources', 'test-png.png');
const targetPath = path.join(attachmentFolder, storedFileName);
fs.copyFileSync(testFilePath, targetPath);

const storedAttachment = await FileAttachment.create({
filename: storedFileName,
});

await request(app)
.post('/api/1.0/hoaxes')
.set('Authorization', `Bearer ${token}`)
.send({ content: 'Hoax content', fileAttachment: storedAttachment.id });

await deleteUser(savedUser.id, { token: token });

const storedAttachmentAfterDelete = await FileAttachment.findOne({ where: { id: storedAttachment.id } });
expect(storedAttachmentAfterDelete).toBeNull();
expect(fs.existsSync(targetPath)).toBe(false);
});
});
23 changes: 23 additions & 0 deletions src/file/FileService.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { randomString } = require('../shared/generator');
const FileType = require('file-type');
const FileAttachment = require('./FileAttachment');
const Sequelize = require('sequelize');
const Hoax = require('../hoax/Hoax');

const { uploadDir, profileDir, attachmentDir } = config;
const profileFolder = path.join('.', uploadDir, profileDir);
Expand Down Expand Up @@ -105,6 +106,27 @@ const deleteAttachment = async (filename) => {
} catch (err) {}
};

const deleteUserFiles = async (user) => {
if (user.image) {
await deleteProfileImage(user.image);
}
const attachments = await FileAttachment.findAll({
attributes: ['filename'],
include: {
model: Hoax,
where: {
userId: user.id,
},
},
});
if (attachments.length === 0) {
return;
}
for (let attachment of attachments) {
await deleteAttachment(attachment.getDataValue('filename'));
}
};

module.exports = {
createFolders,
saveProfileImage,
Expand All @@ -115,4 +137,5 @@ module.exports = {
associateFileToHoax,
removeUnusedAttachments,
deleteAttachment,
deleteUserFiles,
};
4 changes: 3 additions & 1 deletion src/user/UserService.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ const updateUser = async (id, updatedBody) => {
};

const deleteUser = async (id) => {
await User.destroy({ where: { id: id } });
const user = await User.findOne({ where: { id: id } });
await FileService.deleteUserFiles(user);
await user.destroy();
};

const passwordResetRequest = async (email) => {
Expand Down

0 comments on commit cf1985b

Please sign in to comment.