text: 'use strict';
  
  const AdminGroupsPlugin = require('../../../server/api/admin-groups');
  const AuthPlugin = require('../../../server/auth');
  const AuthenticatedUser = require('../fixtures/credentials-admin');
  const Code = require('code');
  const Config = require('../../../config/config');
  const Hapi = require('hapi');
  const HapiAuth = require('hapi-auth-cookie');
  const Lab = require('lab');
  const MakeMockModel = require('../fixtures/make-mock-model');
  const Manifest = require('../../../config/manifest');
  const Path = require('path');
  const Proxyquire = require('proxyquire');
  
  
  const lab = exports.lab = Lab.script();
  let request;
  let server;
  let stub;
  
  
  lab.before((done) => {
  
      stub = {
          AdminGroup: MakeMockModel()
      };
  
      const proxy = {};
      proxy[Path.join(process.cwd(), './server/models/admin-group')] = stub.AdminGroup;
  
      const ModelsPlugin = {
          register: Proxyquire('hapi-mongo-models', proxy),
          options: Manifest.get('/registrations').filter((reg) => {
  
              if (reg.plugin &&
                  reg.plugin.register &&
                  reg.plugin.register === 'hapi-mongo-models') {
  
                  return true;
              }
  
              return false;
          })[0].plugin.options
      };
  
      const plugins = [HapiAuth, ModelsPlugin, AuthPlugin, AdminGroupsPlugin];
      server = new Hapi.Server();
      server.connection({ port: Config.get('/port/web') });
      server.register(plugins, (err) => {
  
          if (err) {
              return done(err);
          }
  
          server.initialize(done);
      });
  });
  
  
  lab.after((done) => {
  
      server.plugins['hapi-mongo-models'].MongoModels.disconnect();
      done();
  });
  
  
  lab.experiment('Admin Groups Plugin Result List', () => {
  
      lab.beforeEach((done) => {
  
          request = {
              method: 'GET',
              url: '/admin-groups',
              credentials: AuthenticatedUser
          };
  
          done();
      });
  
  
      lab.test('it returns an error when paged find fails', (done) => {
  
          stub.AdminGroup.pagedFind = function () {
  
              const args = Array.prototype.slice.call(arguments);
              const callback = args.pop();
  
              callback(Error('paged find failed'));
          };
  
          server.inject(request, (response) => {
  
              Code.expect(response.statusCode).to.equal(500);
              done();
          });
      });
  
  
      lab.test('it returns an array of documents successfully', (done) => {
  
          stub.AdminGroup.pagedFind = function () {
  
              const args = Array.prototype.slice.call(arguments);
              const callback = args.pop();
  
              callback(null, { data: [{}, {}, {}] });
          };
  
          server.inject(request, (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.test('it returns an array of documents successfully (using filters)', (done) => {
  
          stub.AdminGroup.pagedFind = function () {
  
              const args = Array.prototype.slice.call(arguments);
              const callback = args.pop();
  
              callback(null, { data: [{}, {}, {}] });
          };
  
          request.url += '?name=ren';
  
          server.inject(request, (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('Admin Groups Plugin Read', () => {
  
      lab.beforeEach((done) => {
  
          request = {
              method: 'GET',
              url: '/admin-groups/93EP150D35',
              credentials: AuthenticatedUser
          };
  
          done();
      });
  
  
      lab.test('it returns an error when find by id fails', (done) => {
  
          stub.AdminGroup.findById = function (id, callback) {
  
              callback(Error('find by id failed'));
          };
  
          server.inject(request, (response) => {
  
              Code.expect(response.statusCode).to.equal(500);
              done();
          });
      });
  
  
      lab.test('it returns a not found when find by id misses', (done) => {
  
          stub.AdminGroup.findById = function (id, callback) {
  
              callback();
          };
  
          server.inject(request, (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', (done) => {
  
          stub.AdminGroup.findById = function (id, callback) {
  
              callback(null, { _id: '93EP150D35' });
          };
  
          server.inject(request, (response) => {
  
              Code.expect(response.statusCode).to.equal(200);
              Code.expect(response.result).to.be.an.object();
  
              done();
          });
      });
  });
  
  
  lab.experiment('Admin Groups Plugin Create', () => {
  
      lab.beforeEach((done) => {
  
          request = {
              method: 'POST',
              url: '/admin-groups',
              payload: {
                  name: 'Sales'
              },
              credentials: AuthenticatedUser
          };
  
          done();
      });
  
  
      lab.test('it returns an error when create fails', (done) => {
  
          stub.AdminGroup.create = function (name, callback) {
  
              callback(Error('create failed'));
          };
  
          server.inject(request, (response) => {
  
              Code.expect(response.statusCode).to.equal(500);
              done();
          });
      });
  
  
      lab.test('it creates a document successfully', (done) => {
  
          stub.AdminGroup.create = function (name, callback) {
  
              callback(null, {});
          };
  
          server.inject(request, (response) => {
  
              Code.expect(response.statusCode).to.equal(200);
              Code.expect(response.result).to.be.an.object();
  
              done();
          });
      });
  });
  
  
  lab.experiment('Admin Groups Plugin Update', () => {
  
      lab.beforeEach((done) => {
  
          request = {
              method: 'PUT',
              url: '/admin-groups/sales',
              payload: {
                  name: 'Salez'
              },
              credentials: AuthenticatedUser
          };
  
          done();
      });
  
  
      lab.test('it returns an error when update fails', (done) => {
  
          stub.AdminGroup.findByIdAndUpdate = function (id, update, callback) {
  
              callback(Error('update failed'));
          };
  
          server.inject(request, (response) => {
  
              Code.expect(response.statusCode).to.equal(500);
              done();
          });
      });
  
  
      lab.test('it returns not found when find by id misses', (done) => {
  
          stub.AdminGroup.findByIdAndUpdate = function (id, update, callback) {
  
              callback(null, undefined);
          };
  
          server.inject(request, (response) => {
  
              Code.expect(response.statusCode).to.equal(404);
              done();
          });
      });
  
  
      lab.test('it updates a document successfully', (done) => {
  
          stub.AdminGroup.findByIdAndUpdate = function (id, update, callback) {
  
              callback(null, {});
          };
  
          server.inject(request, (response) => {
  
              Code.expect(response.statusCode).to.equal(200);
              Code.expect(response.result).to.be.an.object();
  
              done();
          });
      });
  });
  
  
  lab.experiment('Admin Groups Plugin Update Permissions', () => {
  
      lab.beforeEach((done) => {
  
          request = {
              method: 'PUT',
              url: '/admin-groups/sales/permissions',
              payload: {
                  permissions: { SPACE_RACE: true }
              },
              credentials: AuthenticatedUser
          };
  
          done();
      });
  
  
      lab.test('it returns an error when update fails', (done) => {
  
          stub.AdminGroup.findByIdAndUpdate = function (id, update, callback) {
  
              callback(Error('update failed'));
          };
  
          server.inject(request, (response) => {
  
              Code.expect(response.statusCode).to.equal(500);
              done();
          });
      });
  
  
      lab.test('it updates a document successfully', (done) => {
  
          stub.AdminGroup.findByIdAndUpdate = function (id, update, callback) {
  
              callback(null, {});
          };
  
          server.inject(request, (response) => {
  
              Code.expect(response.statusCode).to.equal(200);
              Code.expect(response.result).to.be.an.object();
  
              done();
          });
      });
  });
  
  
  lab.experiment('Admin Groups Plugin Delete', () => {
  
      lab.beforeEach((done) => {
  
          request = {
              method: 'DELETE',
              url: '/admin-groups/93EP150D35',
              credentials: AuthenticatedUser
          };
  
          done();
      });
  
  
      lab.test('it returns an error when delete by id fails', (done) => {
  
          stub.AdminGroup.findByIdAndDelete = function (id, callback) {
  
              callback(Error('delete by id failed'));
          };
  
          server.inject(request, (response) => {
  
              Code.expect(response.statusCode).to.equal(500);
              done();
          });
      });
  
  
      lab.test('it returns a not found when delete by id misses', (done) => {
  
          stub.AdminGroup.findByIdAndDelete = function (id, callback) {
  
              callback(null, undefined);
          };
  
          server.inject(request, (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', (done) => {
  
          stub.AdminGroup.findByIdAndDelete = function (id, callback) {
  
              callback(null, 1);
          };
  
          server.inject(request, (response) => {
  
              Code.expect(response.statusCode).to.equal(200);
              Code.expect(response.result.success).to.be.true();
  
              done();
          });
      });
  });
  