Skip to content

Commit

Permalink
1603-validation
Browse files Browse the repository at this point in the history
  • Loading branch information
basarbk committed Aug 7, 2020
1 parent 9eb0104 commit fe23b22
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
39 changes: 39 additions & 0 deletions __tests__/HoaxSubmit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,43 @@ describe('Post Hoax', () => {
const response = await postHoax({ content: 'Hoax content' }, { auth: credentials, language });
expect(response.body.message).toBe(message);
});
it.each`
language | message
${'tr'} | ${tr.validation_failure}
${'en'} | ${en.validation_failure}
`(
'returns 400 and $message when hoax content is less than 10 characters is $language',
async ({ language, message }) => {
await addUser();
const response = await postHoax({ content: '123456789' }, { auth: credentials, language });
expect(response.status).toBe(400);
expect(response.body.message).toBe(message);
}
);
it('returns validation error body when an invalid hoax post by authorized user', async () => {
await addUser();
const nowInMillis = Date.now();
const response = await postHoax({ content: '123456789' }, { auth: credentials });
const error = response.body;
expect(error.timestamp).toBeGreaterThan(nowInMillis);
expect(error.path).toBe('/api/1.0/hoaxes');
expect(Object.keys(error)).toEqual(['path', 'timestamp', 'message', 'validationErrors']);
});

it.each`
language | content | contentForDescription | message
${'tr'} | ${null} | ${'null'} | ${tr.hoax_content_size}
${'tr'} | ${'a'.repeat(9)} | ${'short'} | ${tr.hoax_content_size}
${'tr'} | ${'a'.repeat(5001)} | ${'very long'} | ${tr.hoax_content_size}
${'en'} | ${null} | ${'null'} | ${en.hoax_content_size}
${'en'} | ${'a'.repeat(9)} | ${'short'} | ${en.hoax_content_size}
${'en'} | ${'a'.repeat(5001)} | ${'very long '} | ${en.hoax_content_size}
`(
'returns $message when the content is $contentForDescription and the language is $langauge',
async ({ language, content, message }) => {
await addUser();
const response = await postHoax({ content: content }, { auth: credentials, language });
expect(response.body.validationErrors.content).toBe(message);
}
);
});
3 changes: 2 additions & 1 deletion locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
"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",
"hoax_submit_success": "Hoax is saved"
"hoax_submit_success": "Hoax is saved",
"hoax_content_size": "Hoax must be min 10 and max 5000 characters"
}
3 changes: 2 additions & 1 deletion locales/tr/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
"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",
"hoax_submit_success": "Efsaneniz kaydedildi"
"hoax_submit_success": "Efsaneniz kaydedildi",
"hoax_content_size": "Gönderiniz en az 10 en fazla 5000 karakter olmalı"
}
19 changes: 15 additions & 4 deletions src/hoax/HoaxRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@ const express = require('express');
const router = express.Router();
const AuthenticationException = require('../auth/AuthenticationException');
const HoaxService = require('./HoaxService');
const { check, validationResult } = require('express-validator');
const ValidationException = require('../error/ValidationException');

router.post('/api/1.0/hoaxes', async (req, res, next) => {
if (req.authenticatedUser) {
router.post(
'/api/1.0/hoaxes',
check('content').isLength({ min: 10, max: 5000 }).withMessage('hoax_content_size'),
async (req, res, next) => {
if (!req.authenticatedUser) {
return next(new AuthenticationException('unauthorized_hoax_submit'));
}

const errors = validationResult(req);
if (!errors.isEmpty()) {
return next(new ValidationException(errors.array()));
}
await HoaxService.save(req.body);
return res.send({ message: req.t('hoax_submit_success') });
}
next(new AuthenticationException('unauthorized_hoax_submit'));
});
);

module.exports = router;

0 comments on commit fe23b22

Please sign in to comment.