router.put('/:id', (req, res) => { let course = Courses.find(c => c.id === parseInt(req.params.id)) if (!course) { return res.status(404).send(`The Course with this id ${req.params.id} is not available`) } const result = courseValidation(req.body) if (result.error) { return res.status(400).send(result.error.details[0].message) } course.name = req.body.name res.send(JSON.stringify(course)) })
router.post('/', async (req, res) => { const result = validate(req.body) if (result.error) { return res.status(400).send(result.error.details[0].message) } let user = await Users.findOne({ email: req.body.email }).lean(true) if (user) return res.status(400).send('user already registered!') user = new Users(_.pick(req.body, ['name', 'email', 'password'])) const salt = await bcrypt.genSalt(10) user.password = await bcrypt.hash(user.password, salt) await user.save() const token = user.generateAuthToken() res.header('x-auth-auth', token).send(JSON.stringify(_.pick(user, ['_id', 'name', 'email']))) })
// not start with -, + or _ it(`should forbid a name starting with '-'`, () => { const validation = indexName.validate('-abcde') expect(validation.error).not.toBeNull() })
// max 255 binary 'char' it(`should allow a 255 characters ascii name`, () => { const validation = indexName.validate(repeat('a', 255)) expect(validation.error).toBeNull() })
// not contain #, \, /, *, ?, ", <, >, or | it(`should forbid a name containing '#'`, () => { const validation = indexName.validate('ab#cde') expect(validation.error).not.toBeNull() })
// not empty it(`should forbid an empty name`, () => { const validation = indexNameWithoutSuffix.validate('') expect(validation.error).not.toBeNull() })
// not . or .. it(`should forbid the name '.'`, () => { const validation = indexName.validate('.') expect(validation.error).not.toBeNull() })
// not contain #, \, /, *, ?, ", <, >, or | it(`should forbid a name containing '#'`, () => { const validation = indexNameWithoutSuffix.validate('ab#cde') expect(validation.error).not.toBeNull() })
// not empty it(`should forbid an empty name`, () => { const validation = indexName.validate('') expect(validation.error).not.toBeNull() })