<commit_before>var
  should = require('should'),
  q = require('q'),
  params = require('rc')('kuzzle'),
  Kuzzle = require.main.require('lib/api/Kuzzle'),
  BadRequestError = require.main.require('lib/api/core/errors/badRequestError'),
  RequestObject = require.main.require('lib/api/core/models/requestObject'),
  ResponseObject = require.main.require('lib/api/core/models/responseObject'),
  NotFoundError = require.main.require('lib/api/core/errors/notFoundError');

describe('Test: security controller - profiles', function () {
  var
    kuzzle;

  before((done) => {
    kuzzle = new Kuzzle();
    kuzzle.start(params, {dummy: true})
      .then(() => {
        // Mock
        kuzzle.repositories.role.roles.role1 = { _id: 'role1' };
        kuzzle.repositories.profile.validateAndSaveProfile = profile => {
          if (profile._id === 'alreadyExists') {
            return q.reject();
          }

          return q({
            _index: kuzzle.config.internalIndex,
            _type: 'profiles',
            _id: profile._id,
            created: true
          });
        };
        kuzzle.repositories.profile.loadProfile = id => {
          if (id === 'badId') {
            return q(null);
          }

          return q({
            _index: kuzzle.config.internalIndex,
            _type: 'profiles',
            _id: id,
            roles: [{
              _id: 'role1',
              indexes: {}
            }]
          });
        };
        kuzzle.repositories.profile.searchProfiles = () => {
          return q([{
            _id: 'test',
            roles: [
              {
                _id: 'role1',
                indexes: {}
              }
            ]
          }]
          );
        };
        kuzzle.repositories.profile.loadMultiFromDatabase = (ids, hydrate) => {
          if (!hydrate) {
            return q(ids.map(id => {
              return {
                _id: id,
                _source: {
                  roles: ['role1']
                }
              };
            }));
          }

          return q(ids.map(id => {
            return {
              _id: id,
              roles: [{_id: 'role1'}]
            };
          }));
        };
        kuzzle.repositories.profile.deleteProfile = requestObject => {
          return q(new ResponseObject(requestObject, {_id: 'test'}));
        };

        done();
      });
  });

  describe('#createOrReplaceProfile', function () {
    it('should resolve to a responseObject on a createOrReplaceProfile call', done => {
      kuzzle.funnel.security.createOrReplaceProfile(new RequestObject({
          body: {_id: 'test', roles: ['role1']}
        }))
        .then(result => {
          should(result).be.an.instanceOf(ResponseObject);
          should(result.data.body._id).be.exactly('test');
          done();
        })
        .catch(error => {
          done(error);
        });
    });
  });

  describe('#createProfile', function () {
    it('should reject when a profile already exists with the id', () => {
      var promise = kuzzle.funnel.security.createOrReplaceProfile(new RequestObject({
          body: {_id: 'alreadyExists', roles: ['role1']}
        }));

      return should(promise).be.rejected();
    });

    it('should resolve to a responseObject on a createProfile call', () => {
      var promise = kuzzle.funnel.security.createOrReplaceProfile(new RequestObject({
        body: {_id: 'test', roles: ['role1']}
      }));

      return should(promise).be.fulfilled();
    });
  });

  describe('#getProfile', function () {
    it('should resolve to a responseObject on a getProfile call', done => {
      kuzzle.funnel.security.getProfile(new RequestObject({
          body: {_id: 'test'}
        }))
        .then(result => {<commit_msg>nitpick in unit test<commit_after>var
  should = require('should'),
  q = require('q'),
  params = require('rc')('kuzzle'),
  Kuzzle = require.main.require('lib/api/Kuzzle'),
  BadRequestError = require.main.require('lib/api/core/errors/badRequestError'),
  RequestObject = require.main.require('lib/api/core/models/requestObject'),
  ResponseObject = require.main.require('lib/api/core/models/responseObject'),
  NotFoundError = require.main.require('lib/api/core/errors/notFoundError');

describe('Test: security controller - profiles', function () {
  var
    kuzzle;

  before((done) => {
    kuzzle = new Kuzzle();
    kuzzle.start(params, {dummy: true})
      .then(() => {
        // Mock
        kuzzle.repositories.role.roles.role1 = { _id: 'role1' };
        kuzzle.repositories.profile.validateAndSaveProfile = profile => {
          if (profile._id === 'alreadyExists') {
            return q.reject();
          }

          return q({
            _index: kuzzle.config.internalIndex,
            _type: 'profiles',
            _id: profile._id,
            created: true
          });
        };
        kuzzle.repositories.profile.loadProfile = id => {
          if (id === 'badId') {
            return q(null);
          }

          return q({
            _index: kuzzle.config.internalIndex,
            _type: 'profiles',
            _id: id,
            roles: [{
              _id: 'role1',
              indexes: {}
            }]
          });
        };
        kuzzle.repositories.profile.searchProfiles = () => {
          return q([{
            _id: 'test',
            roles: [
              {
                _id: 'role1',
                indexes: {}
              }
            ]
          }]
          );
        };
        kuzzle.repositories.profile.loadMultiFromDatabase = (ids, hydrate) => {
          if (!hydrate) {
            return q(ids.map(id => {
              return {
                _id: id,
                _source: {
                  roles: ['role1']
                }
              };
            }));
          }

          return q(ids.map(id => {
            return {
              _id: id,
              roles: [{_id: 'role1'}]
            };
          }));
        };
        kuzzle.repositories.profile.deleteProfile = requestObject => {
          return q(new ResponseObject(requestObject, {_id: 'test'}));
        };

        done();
      });
  });

  describe('#createOrReplaceProfile', function () {
    it('should resolve to a responseObject on a createOrReplaceProfile call', done => {
      kuzzle.funnel.security.createOrReplaceProfile(new RequestObject({
          body: {_id: 'test', roles: ['role1']}
        }))
        .then(result => {
          should(result).be.an.instanceOf(ResponseObject);
          should(result.data.body._id).be.exactly('test');
          done();
        })
        .catch(error => {
          done(error);
        });
    });
  });

  describe('#createProfile', function () {
    it('should reject when a profile already exists with the id', () => {
      var promise = kuzzle.funnel.security.createProfile(new RequestObject({
          body: {_id: 'alreadyExists', roles: ['role1']}
        }));

      return should(promise).be.rejected();
    });

    it('should resolve to a responseObject on a createProfile call', () => {
      var promise = kuzzle.funnel.security.createProfile(new RequestObject({
        body: {_id: 'test', roles: ['role1']}
      }));

      return should(promise).be.fulfilled();
    });
  });

  describe('#getProfile', function () {
    it('should resolve to a responseObject on a getProfile call', done => {
      kuzzle.funnel.security.getProfile(new RequestObject({
          body: {_id: 'test'}
        }))
        .then(result => {
{'fraction_non_alphanumeric': 0.14772576581355257, 'fraction_numerical': 0.002386951332714494, 'mean_word_length': 1.7626373626373626, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '933529', 'n_tokens_mistral': 2205, 'n_tokens_neox': 2122, 'n_words': 395}