From c203bf16eb91a6f3f3201c4e0f0f4f16fff97918 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Wed, 9 Nov 2016 16:26:23 -0700 Subject: [PATCH] Improved tests and fixed them for new users model --- app.js | 7 +- models/user.js | 47 ++++++- package.json | 3 +- tests/models/user.js | 45 +++++-- tests/routes/api/comments/index.js | 196 ++++++++++++++++------------- tests/routes/api/stream/index.js | 20 +-- 6 files changed, 206 insertions(+), 112 deletions(-) diff --git a/app.js b/app.js index 29817c970..04e4222cd 100644 --- a/app.js +++ b/app.js @@ -6,7 +6,12 @@ const path = require('path'); const app = express(); // Middleware declarations. -app.use(morgan('dev')); + +// Add the logging middleware only if we aren't testing. +if (app.get('env') !== 'test') { + app.use(morgan('dev')); +} + app.use(bodyParser.json()); app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); diff --git a/models/user.js b/models/user.js index fe06b9ab2..6800b59ef 100644 --- a/models/user.js +++ b/models/user.js @@ -8,14 +8,21 @@ const UserSchema = new mongoose.Schema({ id: { type: String, default: uuid.v4, - unique: true + unique: true, + required: true }, displayName: String, disabled: Boolean, password: String, profiles: [{ - id: String, - provider: String + id: { + type: String, + required: true + }, + provider: { + type: String, + required: true + } }], roles: [String] }); @@ -163,6 +170,18 @@ UserSchema.statics.changePassword = function(id, password) { }); }; +/** + * Creates local users. + * @param {Array} users Users to create + * @return {Promise} Resolves with the users that were created + */ +UserSchema.statics.createLocalUsers = function(users) { + return Promise.all(users.map((user) => { + return User + .createLocalUser(user.email, user.password, user.displayName); + })); +}; + /** * Creates the local user with a given email, password, and name. * @param {String} email email of the new user @@ -171,6 +190,18 @@ UserSchema.statics.changePassword = function(id, password) { * @param {Function} done callback */ UserSchema.statics.createLocalUser = function(email, password, displayName) { + if (!email) { + return Promise.reject('email is required'); + } + + if (!password) { + return Promise.reject('password is required'); + } + + if (!displayName) { + return Promise.reject('displayName is required'); + } + return new Promise((resolve, reject) => { bcrypt.hash(password, SALT_ROUNDS, (err, hashedPassword) => { if (err) { @@ -262,9 +293,17 @@ UserSchema.statics.removeRoleFromUser = function(id, role) { }); }; +/** + * Finds a user with the id. + * @param {String} id user id (uuid) +*/ +UserSchema.statics.findById = function(id) { + return User.findOne({id}); +}; + /** * Finds users in an array of idd. - * @param {String} idd array of user identifiers (uuid) + * @param {Array} ids array of user identifiers (uuid) */ UserSchema.statics.findByIdArray = function(ids) { return User.find({ diff --git a/package.json b/package.json index d24ac8a0a..754742516 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,8 @@ "pre-git": { "commit-msg": [], "pre-commit": [ - "npm run lint" + "npm run lint", + "npm test" ], "pre-push": [ "npm test" diff --git a/tests/models/user.js b/tests/models/user.js index 46fc7703c..056aafeed 100644 --- a/tests/models/user.js +++ b/tests/models/user.js @@ -6,12 +6,18 @@ const expect = require('chai').expect; describe('User: models', () => { let mockUsers; beforeEach(() => { - return User.create([{ - display_name: 'Stampi', + return User.createLocalUsers([{ + email: 'stampi@gmail.com', + displayName: 'Stampi', + password: '1Coral!' }, { - display_name: 'Sockmonster', + email: 'sockmonster@gmail.com', + displayName: 'Sockmonster', + password: '2Coral!' }, { - display_name: 'Marvel', + email: 'marvel@gmail.com', + displayName: 'Marvel', + password: '3Coral!' }]).then((users) => { mockUsers = users; }); @@ -19,10 +25,12 @@ describe('User: models', () => { describe('#findById()', () => { it('should find a user by id', () => { - return User.findById(mockUsers[0].id).then((result) => { - expect(result).to.have.property('display_name') - .and.to.equal('Stampi'); - }); + return User + .findById(mockUsers[0].id) + .then((user) => { + expect(user).to.have.property('displayName') + .and.to.equal('Stampi'); + }); }); }); @@ -34,4 +42,25 @@ describe('User: models', () => { }); }); }); + + describe('#findLocalUser', () => { + + it('should find a user when we give the right credentials', () => { + return User + .findLocalUser(mockUsers[0].profiles[0].id, '1Coral!') + .then((user) => { + expect(user).to.have.property('displayName') + .and.to.equal(mockUsers[0].displayName); + }); + }); + + it('should not find the user when we give the wrong credentials', () => { + return User + .findLocalUser(mockUsers[0].profiles[0].id, '1Coral!') + .then((user) => { + expect(user).to.equal(false); + }); + }); + + }); }); diff --git a/tests/routes/api/comments/index.js b/tests/routes/api/comments/index.js index 378860bcb..492f7c238 100644 --- a/tests/routes/api/comments/index.js +++ b/tests/routes/api/comments/index.js @@ -32,11 +32,13 @@ describe('Get /comments', () => { }]; const users = [{ - id: '123', - display_name: 'Ana', + displayName: 'Ana', + email: 'ana@gmail.com', + password: '123' }, { - id: '456', - display_name: 'Maria', + displayName: 'Maria', + email: 'maria@gmail.com', + password: '123' }]; const actions = [{ @@ -48,11 +50,11 @@ describe('Get /comments', () => { }]; beforeEach(() => { - return Comment.create(comments).then(() => { - return User.create(users); - }).then(() => { - return Action.create(actions); - }); + return Promise.all([ + Comment.create(comments), + User.createLocalUsers(users), + Action.create(actions) + ]); }); it('should return all the comments', function(done){ @@ -86,11 +88,13 @@ describe('Get moderation queues rejected, pending, flags', () => { }]; const users = [{ - id: '123', - display_name: 'Ana', + displayName: 'Ana', + email: 'ana@gmail.com', + password: '123' }, { - id: '456', - display_name: 'Maria', + displayName: 'Maria', + email: 'maria@gmail.com', + password: '123' }]; const actions = [{ @@ -104,11 +108,11 @@ describe('Get moderation queues rejected, pending, flags', () => { }]; beforeEach(() => { - return Comment.create(comments).then(() => { - return User.create(users); - }).then(() => { - return Action.create(actions); - }); + return Promise.all([ + Comment.create(comments), + User.createLocalUsers(users), + Action.create(actions) + ]); }); it('should return all the rejected comments', function(done){ @@ -148,11 +152,13 @@ describe('Get moderation queues rejected, pending, flags', () => { describe('Post /comments', () => { const users = [{ - id: '123', - display_name: 'Ana', + displayName: 'Ana', + email: 'ana@gmail.com', + password: '123' }, { - id: '456', - display_name: 'Maria', + displayName: 'Maria', + email: 'maria@gmail.com', + password: '123' }]; const actions = [{ @@ -164,9 +170,10 @@ describe('Post /comments', () => { }]; beforeEach(() => { - return User.create(users).then(() => { - return Action.create(actions); - }); + return Promise.all([ + User.createLocalUsers(users), + Action.create(actions) + ]); }); it('it should create a comment', function(done) { @@ -199,11 +206,13 @@ describe('Get /:comment_id', () => { }]; const users = [{ - id: '123', - display_name: 'Ana', + displayName: 'Ana', + email: 'ana@gmail.com', + password: '123' }, { - id: '456', - display_name: 'Maria', + displayName: 'Maria', + email: 'maria@gmail.com', + password: '123' }]; const actions = [{ @@ -217,11 +226,11 @@ describe('Get /:comment_id', () => { }]; beforeEach(() => { - return Comment.create(comments).then(() => { - return User.create(users); - }).then(() => { - return Action.create(actions); - }); + return Promise.all([ + Comment.create(comments), + User.createLocalUsers(users), + Action.create(actions) + ]); }); it('should return the right comment for the comment_id', function(done){ @@ -256,11 +265,13 @@ describe('Put /:comment_id', () => { }]; const users = [{ - id: '123', - display_name: 'Ana', + displayName: 'Ana', + email: 'ana@gmail.com', + password: '123' }, { - id: '456', - display_name: 'Maria', + displayName: 'Maria', + email: 'maria@gmail.com', + password: '123' }]; const actions = [{ @@ -272,11 +283,11 @@ describe('Put /:comment_id', () => { }]; beforeEach(() => { - return Comment.create(comments).then(() => { - return User.create(users); - }).then(() => { - return Action.create(actions); - }); + return Promise.all([ + Comment.create(comments), + User.createLocalUsers(users), + Action.create(actions) + ]); }); it('it should update comment', function(done) { @@ -311,11 +322,13 @@ describe('Remove /:comment_id', () => { }]; const users = [{ - id: '123', - display_name: 'Ana', + displayName: 'Ana', + email: 'ana@gmail.com', + password: '123' }, { - id: '456', - display_name: 'Maria', + displayName: 'Maria', + email: 'maria@gmail.com', + password: '123' }]; const actions = [{ @@ -327,27 +340,32 @@ describe('Remove /:comment_id', () => { }]; beforeEach(() => { - return Comment.create(comments).then(() => { - return User.create(users); - }).then(() => { - return Action.create(actions); - }); + return Promise.all([ + Comment.create(comments), + User.createLocalUsers(users), + Action.create(actions) + ]); }); - it('it should remove comment', function(done) { - chai.request(app) + it('it should remove comment', () => { + return chai.request(app) .delete('/api/v1/comments/abc') - .end(function(err, res){ - expect(err).to.be.null; + .then((res) => { expect(res).to.have.status(201); - Comment.findById('abc').then((comment) => { - expect(comment).to.be.empty; - }); - done(); + + return Comment.findById('abc'); + }) + .then((comment) => { + expect(comment).to.be.null; }); }); }); +process.on('unhandledRejection', (reason) => { + console.error('Reason: '); + console.error(reason); +}); + describe('Post /:comment_id/status', () => { const comments = [{ @@ -370,11 +388,13 @@ describe('Post /:comment_id/status', () => { }]; const users = [{ - id: '123', - display_name: 'Ana', + displayName: 'Ana', + email: 'ana@gmail.com', + password: '123' }, { - id: '456', - display_name: 'Maria', + displayName: 'Maria', + email: 'maria@gmail.com', + password: '123' }]; const actions = [{ @@ -386,23 +406,21 @@ describe('Post /:comment_id/status', () => { }]; beforeEach(() => { - return Comment.create(comments).then(() => { - return User.create(users); - }).then(() => { - return Action.create(actions); - }); + return Promise.all([ + Comment.create(comments), + User.createLocalUsers(users), + Action.create(actions) + ]); }); - it('it should update status', function(done) { - chai.request(app) + it('it should update status', function() { + return chai.request(app) .post('/api/v1/comments/abc/status') - .send({'status': 'accepted'}) - .end(function(err, res){ - expect(err).to.be.null; + .send({status: 'accepted'}) + .then((res) => { expect(res).to.have.status(200); expect(res).to.have.body; expect(res.body).to.have.property('status', 'accepted'); - done(); }); }); }); @@ -429,11 +447,13 @@ describe('Post /:comment_id/actions', () => { }]; const users = [{ - id: '123', - display_name: 'Ana', + displayName: 'Ana', + email: 'ana@gmail.com', + password: '123' }, { - id: '456', - display_name: 'Maria', + displayName: 'Maria', + email: 'maria@gmail.com', + password: '123' }]; const actions = [{ @@ -445,26 +465,24 @@ describe('Post /:comment_id/actions', () => { }]; beforeEach(() => { - return Comment.create(comments).then(() => { - return User.create(users); - }).then(() => { - return Action.create(actions); - }); + return Promise.all([ + Comment.create(comments), + User.createLocalUsers(users), + Action.create(actions) + ]); }); - it('it should update actions', function(done) { - chai.request(app) + it('it should update actions', () => { + return chai.request(app) .post('/api/v1/comments/abc/actions') .send({'user_id': '456', 'action_type': 'flag'}) - .end(function(err, res){ - expect(err).to.be.null; + .then((res) => { expect(res).to.have.status(200); expect(res).to.have.body; expect(res.body).to.have.property('item_type', 'comment'); expect(res.body).to.have.property('action_type', 'flag'); expect(res.body).to.have.property('item_id', 'abc'); expect(res.body).to.have.property('user_id', '456'); - done(); }); }); }); diff --git a/tests/routes/api/stream/index.js b/tests/routes/api/stream/index.js index b1348e88a..6859d1dc0 100644 --- a/tests/routes/api/stream/index.js +++ b/tests/routes/api/stream/index.js @@ -30,11 +30,13 @@ describe('api/stream: routes', () => { }]; const users = [{ - id: '123', - display_name: 'John', + displayName: 'Ana', + email: 'ana@gmail.com', + password: '123' }, { - id: '456', - display_name: 'Paul', + displayName: 'Maria', + email: 'maria@gmail.com', + password: '123' }]; const actions = [{ @@ -46,11 +48,11 @@ describe('api/stream: routes', () => { }]; beforeEach(() => { - return Comment.create(comments).then(() => { - return User.create(users); - }).then(() => { - return Action.create(actions); - }); + return Promise.all([ + Comment.create(comments), + User.createLocalUsers(users), + Action.create(actions) + ]); }); it('should return a stream with comments, users and actions', function(done){