text: var Lab = require('lab');
  var Code = require('code');
  var Path = require('path');
  var Config = require('../../../config');
  var Manifest = require('../../../manifest');
  var Hapi = require('hapi');
  var HapiAuthBasic = require('hapi-auth-basic');
  var Proxyquire = require('proxyquire');
  var AuthPlugin = require('../../../server/auth');
  var StatusesPlugin = require('../../../server/api/statuses');
  var AuthenticatedUser = require('../fixtures/credentials-admin');
  
  
  var lab = exports.lab = Lab.script();
  var ModelsPlugin, request, server, stub;
  
  
  lab.before(function (done) {
  
      stub = {
          Status: {}
      };
  
      var proxy = {};
      proxy[Path.join(process.cwd(), './server/models/status')] = stub.Status;
  
      ModelsPlugin = {
          register: Proxyquire('hapi-mongo-models', proxy),
          options: Manifest.get('/plugins')['hapi-mongo-models']
      };
  
      var plugins = [HapiAuthBasic, ModelsPlugin, AuthPlugin, StatusesPlugin];
      server = new Hapi.Server();
      server.connection({ port: Config.get('/port/web') });
      server.register(plugins, function (err) {
  
          if (err) {
              return done(err);
          }
  
          done();
      });
  });
  
  
  lab.after(function (done) {
  
      server.plugins['hapi-mongo-models'].BaseModel.disconnect();
      done();
  });
  
  
  lab.experiment('Statuses Plugin Result List', function () {
  
      lab.beforeEach(function (done) {
  
          request = {
              method: 'GET',
              url: '/statuses',
              credentials: AuthenticatedUser
          };
  
          done();
      });
  
  
      lab.test('it returns an error when paged find fails', function (done) {
  
          stub.Status.pagedFind = function () {
  
              var args = Array.prototype.slice.call(arguments);
              var callback = args.pop();
  
              callback(Error('paged find failed'));
          };
  
          server.inject(request, function (response) {
  
              Code.expect(response.statusCode).to.equal(500);
              done();
          });
      });
  
  
      lab.test('it returns an array of documents successfully', function (done) {
  
          stub.Status.pagedFind = function () {
  
              var args = Array.prototype.slice.call(arguments);
              var callback = args.pop();
  
              callback(null, { data: [{}, {}, {}] });
          };
  
          server.inject(request, function (response) {
  
              Code.expect(response.statusCode).to.equal(200);
              Code.expect(response.result.data).to.be.an.array();
              Code.expect(response.result.data[0]).to.be.an.object();
  
              done();
          });
      });
  });
  
  
  lab.experiment('Statuses Plugin Read', function () {
  
      lab.beforeEach(function (done) {
  
          request = {
              method: 'GET',
              url: '/statuses/93EP150D35',
              credentials: AuthenticatedUser
          };
  
          done();
      });
  
  
      lab.test('it returns an error when find by id fails', function (done) {
  
          stub.Status.findById = function (id, callback) {
  
              callback(Error('find by id failed'));
          };
  
          server.inject(request, function (response) {
  
              Code.expect(response.statusCode).to.equal(500);
              done();
          });
      });
  
  
      lab.test('it returns a not found when find by id misses', function (done) {
  
          stub.Status.findById = function (id, callback) {
  
              callback();
          };
  
          server.inject(request, function (response) {
  
              Code.expect(response.statusCode).to.equal(404);
              Code.expect(response.result.message).to.match(/document not found/i);
  
              done();
          });
      });
  
  
      lab.test('it returns a document successfully', function (done) {
  
          stub.Status.findById = function (id, callback) {
  
              callback(null, { _id: '93EP150D35' });
          };
  
          server.inject(request, function (response) {
  
              Code.expect(response.statusCode).to.equal(200);
              Code.expect(response.result).to.be.an.object();
  
              done();
          });
      });
  });
  
  
  lab.experiment('Statuses Plugin Create', function () {
  
      lab.beforeEach(function (done) {
  
          request = {
              method: 'POST',
              url: '/statuses',
              payload: {
                  pivot: 'Account',
                  name: 'Happy'
              },
              credentials: AuthenticatedUser
          };
  
          done();
      });
  
  
      lab.test('it returns an error when create fails', function (done) {
  
          stub.Status.create = function (pivot, name, callback) {
  
              callback(Error('create failed'));
          };
  
          server.inject(request, function (response) {
  
              Code.expect(response.statusCode).to.equal(500);
              done();
          });
      });
  
  
      lab.test('it creates a document successfully', function (done) {
  
          stub.Status.create = function (pivot, name, callback) {
  
              callback(null, {});
          };
  
          server.inject(request, function (response) {
  
              Code.expect(response.statusCode).to.equal(200);
              Code.expect(response.result).to.be.an.object();
  
              done();
          });
      });
  });
  
  
  lab.experiment('Statuses Plugin Update', function () {
  
      lab.beforeEach(function (done) {
  
          request = {
              method: 'PUT',
              url: '/statuses/account-happy',
              payload: {
                  name: 'Happy'
              },
              credentials: AuthenticatedUser
          };
  
          done();
      });
  
  
      lab.test('it returns an error when update fails', function (done) {
  
          stub.Status.findByIdAndUpdate = function (id, update, callback) {
  
              callback(Error('update failed'));
          };
  
          server.inject(request, function (response) {
  
              Code.expect(response.statusCode).to.equal(500);
              done();
          });
      });
  
  
      lab.test('it returns not found when find by id misses', function (done) {
  
          stub.Status.findByIdAndUpdate = function (id, update, callback) {
  
              callback(null, undefined);
          };
  
          server.inject(request, function (response) {
  
              Code.expect(response.statusCode).to.equal(404);
              done();
          });
      });
  
  
      lab.test('it updates a document successfully', function (done) {
  
          stub.Status.findByIdAndUpdate = function (id, update, callback) {
  
              callback(null, {});
          };
  
          server.inject(request, function (response) {
  
              Code.expect(response.statusCode).to.equal(200);
              Code.expect(response.result).to.be.an.object();
  
              done();
          });
      });
  });
  
  
  lab.experiment('Statuses Plugin Delete', function () {
  
      lab.beforeEach(function (done) {
  
          request = {
              method: 'DELETE',
              url: '/statuses/93EP150D35',
              credentials: AuthenticatedUser
          };
  
          done();
      });
  
  
      lab.test('it returns an error when delete by id fails', function (done) {
  
          stub.Status.findByIdAndDelete = function (id, callback) {
  
              callback(Error('delete by id failed'));
          };
  
          server.inject(request, function (response) {
  
              Code.expect(response.statusCode).to.equal(500);
              done();
          });
      });
  
  
      lab.test('it returns a not found when delete by id misses', function (done) {
  
          stub.Status.findByIdAndDelete = function (id, callback) {
  
              callback(null, undefined);
          };
  
          server.inject(request, function (response) {
  
              Code.expect(response.statusCode).to.equal(404);
              Code.expect(response.result.message).to.match(/document not found/i);
  
              done();
          });
      });
  
  
      lab.test('it deletes a document successfully', function (done) {
  
          stub.Status.findByIdAndDelete = function (id, callback) {
  
              callback(null, 1);
          };
  
          server.inject(request, function (response) {
  
              Code.expect(response.statusCode).to.equal(200);
              Code.expect(response.result.message).to.match(/success/i);
  
              done();
          });
      });
  });
  