Skip to content

Commit

Permalink
1602-saving-hoax-to-database
Browse files Browse the repository at this point in the history
  • Loading branch information
basarbk committed Aug 7, 2020
1 parent 873f85b commit 9eb0104
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 5 deletions.
27 changes: 27 additions & 0 deletions __tests__/HoaxSubmit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const app = require('../src/app');
const en = require('../locales/en/translation.json');
const tr = require('../locales/tr/translation.json');
const User = require('../src/user/User');
const Hoax = require('../src/hoax/Hoax');
const sequelize = require('../src/config/database');
const bcrypt = require('bcrypt');

Expand All @@ -13,6 +14,7 @@ beforeAll(async () => {
});

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

Expand Down Expand Up @@ -71,4 +73,29 @@ describe('Post Hoax', () => {
const response = await postHoax({ content: 'Hoax content' }, { auth: credentials });
expect(response.status).toBe(200);
});
it('saves the hoax to database when authorized user sends valid request', async () => {
await addUser();
await postHoax({ content: 'Hoax content' }, { auth: credentials });
const hoaxes = await Hoax.findAll();
expect(hoaxes.length).toBe(1);
});
it('saves the hoax content and timestamp to database', async () => {
await addUser();
const beforeSubmit = Date.now();
await postHoax({ content: 'Hoax content' }, { auth: credentials });
const hoaxes = await Hoax.findAll();
const savedHoax = hoaxes[0];
expect(savedHoax.content).toBe('Hoax content');
expect(savedHoax.timestamp).toBeGreaterThan(beforeSubmit);
expect(savedHoax.timestamp).toBeLessThan(Date.now());
});
it.each`
language | message
${'tr'} | ${tr.hoax_submit_success}
${'en'} | ${en.hoax_submit_success}
`('returns $message to success submit when language is $language', async ({ language, message }) => {
await addUser();
const response = await postHoax({ content: 'Hoax content' }, { auth: credentials, language });
expect(response.body.message).toBe(message);
});
});
3 changes: 2 additions & 1 deletion locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
"unauthroized_password_reset": "You are not authorized to update your password. Please follow the password reset steps again.",
"profile_image_size": "Your profile image cannot be bigger than 2MB",
"unsupported_image_file": "Only JPEG or PNG files are allowed",
"unauthorized_hoax_submit": "You are not authorized to post hoax"
"unauthorized_hoax_submit": "You are not authorized to post hoax",
"hoax_submit_success": "Hoax is saved"
}
3 changes: 2 additions & 1 deletion locales/tr/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
"unauthroized_password_reset": "Şifrenizi yenileme yetkiniz bulunmuyor. Lütfen şifre yenileme adımlarını tekrarlayınız.",
"profile_image_size": "Kullandığınız resim 2MB'dan büyük olamaz",
"unsupported_image_file": "Sadece JPEG ya da PNG dosyalarını kullanabilirsiniz",
"unauthorized_hoax_submit": "Efsane göndermeye yetkiniz bulunmamaktadır"
"unauthorized_hoax_submit": "Efsane göndermeye yetkiniz bulunmamaktadır",
"hoax_submit_success": "Efsaneniz kaydedildi"
}
24 changes: 24 additions & 0 deletions src/hoax/Hoax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const Sequelize = require('sequelize');
const sequelize = require('../config/database');

const Model = Sequelize.Model;

class Hoax extends Model {}

Hoax.init(
{
content: {
type: Sequelize.STRING,
},
timestamp: {
type: Sequelize.BIGINT,
},
},
{
sequelize,
modelName: 'hoax',
timestamps: false,
}
);

module.exports = Hoax;
8 changes: 5 additions & 3 deletions src/hoax/HoaxRouter.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const express = require('express');
const router = express.Router();
const AuthenticationException = require('../auth/AuthenticationException');
const HoaxService = require('./HoaxService');

router.post('/api/1.0/hoaxes', (req, res) => {
router.post('/api/1.0/hoaxes', async (req, res, next) => {
if (req.authenticatedUser) {
return res.send();
await HoaxService.save(req.body);
return res.send({ message: req.t('hoax_submit_success') });
}
throw new AuthenticationException('unauthorized_hoax_submit');
next(new AuthenticationException('unauthorized_hoax_submit'));
});

module.exports = router;
11 changes: 11 additions & 0 deletions src/hoax/HoaxService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const Hoax = require('./Hoax');

const save = async (body) => {
const hoax = {
content: body.content,
timestamp: Date.now(),
};
await Hoax.create(hoax);
};

module.exports = { save };

0 comments on commit 9eb0104

Please sign in to comment.