From cdf0a4da0f89f4558ea322b8248220ec5cd04478 Mon Sep 17 00:00:00 2001 From: Chris Brame Date: Fri, 7 Sep 2018 02:02:19 -0400 Subject: [PATCH] fix(test): updated test for priority changes --- src/models/ticket.js | 4 ++- src/settings/defaults.js | 5 ++-- test/0_database.js | 13 ++++++--- test/models/ticket.js | 55 +++++++++++++++++++++++---------------- test/models/tickettype.js | 2 +- 5 files changed, 49 insertions(+), 30 deletions(-) diff --git a/src/models/ticket.js b/src/models/ticket.js index 69d9a2594..2085d84fa 100644 --- a/src/models/ticket.js +++ b/src/models/ticket.js @@ -311,7 +311,9 @@ ticketSchema.methods.setTicketPriority = function(ownerId, priority, callback) { }; self.history.push(historyItem); - callback(null, self); + self.populate('priority').execPopulate() + .then(function(updatedSelf){ return callback(null, updatedSelf); }) + .catch(function(err) { return callback(err, null); }); }; /** diff --git a/src/settings/defaults.js b/src/settings/defaults.js index afe1d879b..9a7bc713f 100644 --- a/src/settings/defaults.js +++ b/src/settings/defaults.js @@ -142,13 +142,12 @@ function ticketPriorityDefaults(callback) { priorities.push(normal); priorities.push(urgent); priorities.push(critical); - async.each(priorities, function(item, next) { prioritySchema.findOne({migrationNum: item.migrationNum}, function(err, priority) { - if (!err && _.isUndefined(priority)) { + if (!err && (_.isUndefined(priority) || _.isNull(priority))) { return item.save(next); } else { - return next(); + return next(err); } }); }, callback); diff --git a/test/0_database.js b/test/0_database.js index 31de69a78..eaf88cd1a 100644 --- a/test/0_database.js +++ b/test/0_database.js @@ -8,7 +8,7 @@ var path = require('path'); winston.setLevels(winston.config.cli.levels); winston.remove(winston.transports.Console); var database, db; -var CONNECTION_URI = 'mongodb://localhost/polonel_trudesk31908899'; +var CONNECTION_URI = 'mongodb://localhost:27017/polonel_trudesk31908899'; //Global Setup for tests before(function(done) { @@ -31,7 +31,7 @@ before(function(done) { mongoose.connection.db.dropDatabase(function(err) { expect(err).to.not.exist; cb(); - }); + }) }, function(cb) { var counter = require('../src/models/counters'); @@ -44,6 +44,13 @@ before(function(done) { cb(); }); }, + function(cb) { + var typeSchema = require('../src/models/tickettype'); + typeSchema.insertMany([{name: 'Task'}, {name: 'Issue'}], cb); + }, + function(cb) { + require('../src/settings/defaults').init(cb); + }, function(cb) { var userSchema = require('../src/models/user'); userSchema.create({ @@ -123,7 +130,7 @@ before(function(done) { //Global Teardown for tests after(function(done) { this.timeout(5000); - mongoose.connection.db.dropDatabase(function() { + mongoose.connection.dropDatabase(function() { mongoose.connection.close(function() { server.close(); diff --git a/test/models/ticket.js b/test/models/ticket.js index e257607f5..4caf26dc4 100644 --- a/test/models/ticket.js +++ b/test/models/ticket.js @@ -4,6 +4,7 @@ var should = require('chai').should(); var m = require('mongoose'); var ticketSchema = require('../../src/models/ticket'); var groupSchema = require('../../src/models/group'); +var prioritySchema = require('../../src/models/ticketpriority'); describe('ticket.js', function() { //it('should clear collections.', function(done) { @@ -17,28 +18,33 @@ describe('ticket.js', function() { //}); it('should create ticket', function(done) { - ticketSchema.create({ - owner: m.Types.ObjectId(), - group: m.Types.ObjectId(), - status: 0, - tags: [], - date: new Date(), - subject: 'Dummy Test Subject', - issue: 'Dummy Test Issue', - priority: 0, - type: m.Types.ObjectId(), - history: [] - - }, function(err, t) { + prioritySchema.findOne({default: true}).exec(function(err, p) { expect(err).to.not.exist; - expect(t).to.be.a('object'); - expect(t._doc).to.include.keys( - '_id', 'uid', 'owner','group', 'status', 'tags', 'date', 'subject', 'issue', 'priority', 'type', 'history', 'attachments', 'comments', 'deleted' - ); + expect(p).to.be.a('object'); - expect(t.uid).to.equal(1000); + ticketSchema.create({ + owner: m.Types.ObjectId(), + group: m.Types.ObjectId(), + status: 0, + tags: [], + date: new Date(), + subject: 'Dummy Test Subject', + issue: 'Dummy Test Issue', + priority: p._id, + type: m.Types.ObjectId(), + history: [] - done(); + }, function(err, t) { + expect(err).to.not.exist; + expect(t).to.be.a('object'); + expect(t._doc).to.include.keys( + '_id', 'uid', 'owner','group', 'status', 'tags', 'date', 'subject', 'issue', 'priority', 'type', 'history', 'attachments', 'comments', 'deleted' + ); + + expect(t.uid).to.equal(1000); + + done(); + }); }); }); @@ -118,11 +124,16 @@ describe('ticket.js', function() { it('should set ticket priority', function(done) { ticketSchema.getTicketByUid(1000, function(err, ticket) { var ownerId = m.Types.ObjectId(); - ticket.setTicketPriority(ownerId, 3, function(err, ticket) { + prioritySchema.getByMigrationNum(3, function(err, priority) { expect(err).to.not.exist; - expect(ticket.priority).to.equal(3); + expect(priority).to.be.a('object'); - done(); + ticket.setTicketPriority(ownerId, priority, function(err, ticket) { + expect(err).to.not.exist; + expect(ticket.priority.name).to.equal('Critical'); + + done(); + }); }); }); }); diff --git a/test/models/tickettype.js b/test/models/tickettype.js index 41611bfc9..7b35dbb1e 100644 --- a/test/models/tickettype.js +++ b/test/models/tickettype.js @@ -22,7 +22,7 @@ describe('ticketType.js', function() { ticketTypeSchema.getTypes(function(err, types) { expect(err).to.not.exist; expect(types).to.be.a('array'); - expect(types).to.have.length(1); + expect(types).to.have.length(3); //Has default ticket types already done(); });