Skip to content

Commit

Permalink
1605-hoax-user-relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
basarbk committed Aug 7, 2020
1 parent f495314 commit 3f2d301
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 3 deletions.
9 changes: 8 additions & 1 deletion __tests__/HoaxSubmit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ beforeAll(async () => {
});

beforeEach(async () => {
await Hoax.destroy({ truncate: true });
await User.destroy({ truncate: { cascade: true } });
});

Expand Down Expand Up @@ -137,4 +136,12 @@ describe('Post Hoax', () => {
expect(response.body.validationErrors.content).toBe(message);
}
);

it('stores hoax owner id in database', async () => {
const user = await addUser();
await postHoax({ content: 'Hoax content' }, { auth: credentials });
const hoaxes = await Hoax.findAll();
const hoax = hoaxes[0];
expect(hoax.userId).toBe(user.id);
});
});
15 changes: 15 additions & 0 deletions __tests__/UserDelete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const request = require('supertest');
const app = require('../src/app');
const User = require('../src/user/User');
const Token = require('../src/auth/Token');
const Hoax = require('../src/hoax/Hoax');
const sequelize = require('../src/config/database');
const bcrypt = require('bcrypt');
const en = require('../locales/en/translation.json');
Expand Down Expand Up @@ -109,4 +110,18 @@ describe('User Delete', () => {
const tokenInDB = await Token.findOne({ where: { token: token2 } });
expect(tokenInDB).toBeNull();
});
it('deletes hoax from database when delete user request sent from authorized user', async () => {
const savedUser = await addUser();
const token = await auth({ auth: credentials });

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

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

const hoaxes = await Hoax.findAll();
expect(hoaxes.length).toBe(0);
});
});
19 changes: 19 additions & 0 deletions database/migrations/20200807213327-hoax-user-relationship.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.addColumn('hoaxes', 'userId', {
type: Sequelize.INTEGER,
references: {
model: 'users',
key: 'id',
},
onDelete: 'cascade',
});
},

// eslint-disable-next-line no-unused-vars
down: async (queryInterface, Sequelize) => {
await queryInterface.deleteColumn('hoaxes', 'userId');
},
};
2 changes: 1 addition & 1 deletion src/hoax/HoaxRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ router.post(
if (!errors.isEmpty()) {
return next(new ValidationException(errors.array()));
}
await HoaxService.save(req.body);
await HoaxService.save(req.body, req.authenticatedUser);
return res.send({ message: req.t('hoax_submit_success') });
}
);
Expand Down
3 changes: 2 additions & 1 deletion src/hoax/HoaxService.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const Hoax = require('./Hoax');

const save = async (body) => {
const save = async (body, user) => {
const hoax = {
content: body.content,
timestamp: Date.now(),
userId: user.id,
};
await Hoax.create(hoax);
};
Expand Down
2 changes: 2 additions & 0 deletions src/user/User.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Sequelize = require('sequelize');
const sequelize = require('../config/database');
const Token = require('../auth/Token');
const Hoax = require('../hoax/Hoax');

const Model = Sequelize.Model;

Expand Down Expand Up @@ -38,5 +39,6 @@ User.init(
);

User.hasMany(Token, { onDelete: 'cascade', foreignKey: 'userId' });
User.hasMany(Hoax, { onDelete: 'cascade', foreignKey: 'userId' });

module.exports = User;

0 comments on commit 3f2d301

Please sign in to comment.