it('should be able to update an study ', function(done) { db.Study .findOne({ where: { id: 1 }, include: [ db.User ] }) .then(function(study) { study.updateAttributes({ title: 'Study Title Updated', content: 'Study Content Updated' }) .then(function() { done(); }) .catch(function(err) { should.not.exist(err); }); }) .catch(function(err) { done(); }); });
exports.delete = function(req, res) { // console.log('* studies.server.controller - delete *'); var id = req.params.studyId; db.Study .findOne({ where: { id: id }, include: [ db.User ] }) .then(function(study) { study.destroy() .then(function() { return res.json(study); }) .catch(function(err) { return res.status(400).send({ message: errorHandler.getErrorMessage(err) }); }); return null; }) .catch(function(err) { return res.status(400).send({ message: errorHandler.getErrorMessage(err) }); }); };
it('should be able to show the list of studys', function(done) { var limit = 10; var offset = 0; db.Study .findAll({ where: { id: 1 }, include: [ db.User ], 'limit': limit, 'offset': offset, 'order': [ ['createdAt', 'DESC'] ] }) .then(function(studys) { done(); }) .catch(function(err) { should.not.exist(err); }); });
it('should not be able to delete an study if not signed in', function(done) { study = { title: 'Study Title', content: 'Study Content', userId: user.id }; // Create new study model instance var studyObj = db.Study.build(study); // Save the study studyObj.save() .then(function() { // Try deleting study request(app).delete('/api/studies/' + studyObj.id) .expect(403) .end(function(studyDeleteErr, studyDeleteRes) { // Set message assertion (studyDeleteRes.body.message).should.match('User is not authorized'); // Handle study error error done(studyDeleteErr); }); }) .catch(function(err) { should.not.exist(err); }); });
.then(function(roles) { //console.log(user.dataValues.id); study = db.Study .build({ title: 'Study Title', content: 'Study Content', userId: user.dataValues.id }); done(); })
after(function(done){ db.Study.destroy({ where: { title: 'Study Title' } }) .then(function(){ done(); }) .catch(function(err) { should.not.exist(err); }); });
exports.list = function(req, res) { // console.log('* studies.server.controller - list *'); db.Study.findAll({ include: [ db.User ] }) .then(function(studies) { return res.json(studies); }) .catch(function(err) { return res.status(400).send({ message: errorHandler.getErrorMessage(err) }); }); };
exports.create = function(req, res) { // console.log('* studies.server.controller - create *'); // save and return and instance of study on the res object. db.Study.create({ title: req.body.title, content: req.body.content, userId: req.user.id }) .then(function(newStudy) { return res.json(newStudy); }) .catch(function(err) { return res.status(400).send({ message: errorHandler.getErrorMessage(err) }); }); };
it('should be able to delete an study ', function(done) { db.Study .findOne({ where: { id: 1 }, include: [ db.User ] }) .then(function(study) { study.destroy() .then(function() { done(); }) .catch(function(err) { should.not.exist(err); }); }) .catch(function(err) { done(); }); });
exports.update = function(req, res) { // console.log('* studies.server.controller - update *'); var id = req.params.studyId; db.Study .findOne({ where: { id: id }, include: [ db.User ] }) .then(function(study) { study.updateAttributes({ title: req.body.title, content: req.body.content }) .then(function() { return res.json(study); }) .catch(function(err) { return res.status(400).send({ message: errorHandler.getErrorMessage(err) }); }); return null; }) .catch(function(err) { return res.status(400).send({ message: errorHandler.getErrorMessage(err) }); }); };
it('should be able to get a single study if not signed in', function(done) { study = { title: 'Study Title', content: 'Study Content', userId: user.id }; // Create new study model instance var studyObj = db.Study.build(study); // Save the study studyObj.save() .then(function() { // Request studies request(app).get('/api/studies/' + studyObj.id) .end(function(req, res) { // Set assertion res.body.should.be.instanceof(Object).and.have.property('title', study.title); done(); }); }) .catch(function(err) { should.not.exist(err); }); });
it('should be able to get a list of studies if not signed in', function(done) { // Create new study model instance study = { title: 'Study Title', content: 'Study Content', userId: user.id }; var studyObj = db.Study.build(study); // Save the study studyObj.save() .then(function() { // Request studies request(app).get('/api/studies') .end(function(req, res) { // Set assertion res.body.should.be.instanceof(Array).and.not.have.lengthOf(0); done(); }); }) .catch(function(err) { should.not.exist(err); }); });