Created
          March 4, 2014 10:13 
        
      - 
      
- 
        Save holyketzer/9343724 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | /** | |
| * Module dependencies. | |
| */ | |
| var config = require('../app/config/config') | |
| , url = 'http://localhost:' + config.get('port') | |
| , dbUri = config.get('mongoose:uri') | |
| , request = require('supertest') | |
| , agent = request(url); | |
| var mongoose = require('mongoose') | |
| , async = require('async') | |
| , should = require('should') | |
| , util = require('util'); | |
| /** | |
| * Clear database | |
| * | |
| * @param {Function} done | |
| * @api public | |
| */ | |
| exports.clear_db = function (done) { | |
| function clearDB() { | |
| mongoose.connection.db.collections( | |
| function (err, collections) { | |
| if (!err) { | |
| collections.forEach(function (collection) { | |
| if (collection.collectionName !== 'system.indexes') { | |
| /*console.log(collection.collectionName);*/ | |
| collection.remove(function () {}); | |
| } | |
| }) | |
| } else { | |
| throw err; | |
| } | |
| }); | |
| done(); | |
| } | |
| if (mongoose.connection.readyState === 0) { | |
| console.log('Trying to connect to mongoDB'); | |
| mongoose.connect(dbUri, function (err) { | |
| if (err) { | |
| throw err; | |
| } | |
| clearDB(); | |
| }); | |
| } else { | |
| clearDB(); | |
| } | |
| }; | |
| // Create default test user | |
| exports.create_default_user = function (done) { | |
| var default_user_json = { | |
| firstname: 'John', | |
| lastname: 'Smith', | |
| phone: '1234567890', | |
| email: '[email protected]', | |
| password: '*****' | |
| } | |
| this.sendPostRequest('/api/account/register', default_user_json).end(function (err, res) { | |
| if (err) { | |
| done(err); | |
| return; | |
| } | |
| exports.defaultUser = res.body; | |
| done(); | |
| }) | |
| }; | |
| /** | |
| * Utility functions for REST requests | |
| */ | |
| exports.sendGetRequest = function (uri, user) { | |
| var request = agent.get(uri); | |
| addAuthToRequest(request, user); | |
| return request; | |
| }; | |
| exports.sendPostRequest = function (uri, data, user) { | |
| var request = agent.post(uri); | |
| addAuthToRequest(request, user); | |
| addDataToRequest(request, data); | |
| return request; | |
| }; | |
| exports.sendPostRequestWithFile = function (uri, fieldId, filePath, user) { | |
| var request = agent.post(uri); | |
| addAuthToRequest(request, user); | |
| addFileToRequest(request, fieldId, filePath); | |
| return request; | |
| }; | |
| exports.sendPutRequest = function (uri, data, user) { | |
| var request = agent.put(uri); | |
| addAuthToRequest(request, user); | |
| addDataToRequest(request, data); | |
| return request; | |
| }; | |
| exports.sendDeleteRequest = function (uri, user) { | |
| var request = agent.del(uri); | |
| addAuthToRequest(request, user); | |
| return request; | |
| }; | |
| var addAuthToRequest = function (request, user) { | |
| if (user) { | |
| request.auth(user.email, user.password); | |
| } | |
| }; | |
| var addDataToRequest = function (request, data) { | |
| if (data) { | |
| request.set('Content-Type', 'application/json'); | |
| request.send(data); | |
| } | |
| }; | |
| var addFileToRequest = function (request, fieldId, filePath) { | |
| if (fieldId && filePath) { | |
| request.attach(fieldId, filePath, filePath); | |
| } | |
| }; | |
| /** | |
| * Utility functions for assert models | |
| */ | |
| exports.assertIsEqualProducts = function (actual, expected) { | |
| actual.productID.should.equal(expected.productID); | |
| }; | |
| exports.assertIsEqualAmc = function (actual, expected) { | |
| actual.amcID.should.equal(expected.amcID); | |
| }; | |
| /** | |
| * Utility functions for assert rest error | |
| */ | |
| exports.assertIsEmptyResponseError = function (err) { | |
| if (err) { | |
| should.fail('During request sending an error is occurred: ' + err); | |
| } | |
| }; | |
| exports.assertDBError = function (err) { | |
| err.name.should.equal('DBError'); | |
| err.status.should.equal(500); | |
| err.message.should.equal('Unable to complete DB call.'); | |
| }; | |
| exports.assertNotFoundError = function (err, type) { | |
| err.name.should.equal('NotFoundError'); | |
| err.status.should.equal(404); | |
| err.message.should.equal(util.format('%s is not found.', type)); | |
| }; | |
| exports.assertDuplicatedValueError = function (err) { | |
| err.name.should.equal('DuplicatedValueError'); | |
| err.status.should.equal(400); | |
| err.message.should.equal('Device is duplicated.'); | |
| }; | |
| exports.assertAuthError = function (err) { | |
| err.name.should.equal('AuthError'); | |
| err.status.should.equal(401); | |
| err.message.should.equal('Authentication failure.'); | |
| }; | |
| exports.assertEmailConfirmationError = function (err) { | |
| err.name.should.equal('EmailConfirmationError'); | |
| err.status.should.equal(400); | |
| err.message.should.equal('Email confirmation token is not correct.'); | |
| }; | |
| exports.assertValidationError = function (err) { | |
| err.name.should.equal('DBError'); | |
| err.status.should.equal(500); | |
| err.message.should.equal('Unable to complete DB call.'); | |
| err.inner.name.should.equal('ValidationError'); | |
| err.inner.message.should.equal('Validation failed'); | |
| }; | |
| /** | |
| * Utility functions for JSON comparison | |
| */ | |
| jsonEquals = function (x, y) { | |
| // we do this because two objects may have the same data fields and data but different prototypes | |
| x1 = JSON.parse(JSON.stringify(x)); | |
| x2 = JSON.parse(JSON.stringify(y)); | |
| p = null; | |
| for (p in x1) { | |
| if (typeof (x2[p]) == 'undefined') { | |
| return false; | |
| } | |
| } | |
| for (p in x1) { | |
| if (x1[p]) { | |
| switch (typeof(x1[p])) { | |
| case 'object': | |
| if (!jsonEquals(x1[p], x2[p])) { | |
| return false; | |
| } | |
| break; | |
| case 'function': | |
| if (typeof(x2[p]) == 'undefined' || (p != 'equals' && x1[p].toString() != x2[p].toString())) { | |
| return false; | |
| } | |
| break; | |
| default: | |
| if (x1[p] != x2[p]) { | |
| return false; | |
| } | |
| } | |
| } else { | |
| if (x2[p]) { | |
| return false; | |
| } | |
| } | |
| } | |
| for (p in x2) { | |
| if (typeof(x1[p]) == 'undefined') { | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| exports.shouldBeEqual = function (o1, o2) { | |
| jsonEquals(o1, o2).should.equal(true, "JSON " + JSON.stringify(o1) + " doesn't equal with " + JSON.stringify(o2)) | |
| }; | |
| exports.checkOrdersList = function (orders, expectedUnassignedIDs, expectedInProgressIDs, expectedSubmittedIDs, expectedAcceptedIDs) { | |
| var whereStatus = function(orderList, status) { | |
| return orderList.filter(function(order) { return order.status === status }) | |
| } | |
| checkOrdersArray(whereStatus(orders, undefined), expectedUnassignedIDs); | |
| checkOrdersArray(whereStatus(orders, "in_progress"), expectedInProgressIDs); | |
| checkOrdersArray(whereStatus(orders, "submitted"), expectedSubmittedIDs); | |
| checkOrdersArray(whereStatus(orders, "accepted"), expectedAcceptedIDs); | |
| }; | |
| checkOrdersArray = function (array, expectedIDs) { | |
| if (!expectedIDs) { | |
| array.length.should.equal(0); | |
| } else { | |
| array.length.should.equal(expectedIDs.length); | |
| expectedIDs.forEach(function (id) { | |
| var contains = false; | |
| array.forEach(function (order) { | |
| if (order.orderID === id) { | |
| contains = true; | |
| } | |
| }) | |
| should(contains).ok; | |
| }) | |
| } | |
| }; | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment