<commit_before>const should = require('should');
const uuid = require('uuid');
const redisConfig = require('../../lib/config').systemConfig.db.redis;
const services = require('../../lib/services');
const userService = services.user;
const db = require('../../lib/db');

describe('User service tests', function () {
  describe('Insert tests', function () {
    before(function () {
      return db.flushdb();
    });

    it('should insert a user', function () {
      const user = {
        username: 'irfanbaqui',
        firstname: 'irfan',
        lastname: 'baqui',
        email: '<EMAIL>'
      };

      return userService
        .insert(user)
        .then(function (newUser) {
          const expectedUserProps = ['firstname', 'lastname', 'email', 'isActive', 'username', 'id', 'createdAt', 'updatedAt'];
          Object.keys(newUser).should.eql(expectedUserProps);
          newUser.should.have.properties(user);
          should.ok(newUser.isActive);
          return db.hgetall(redisConfig.namespace.concat('-', 'user').concat(':', newUser.id))
            .then(function (userObj) {
              userObj.isActive = userObj.isActive === 'true';
              should.deepEqual(userObj, newUser);
            });
        });
    });

    it('should throw an error when inserting a user with missing properties', function (done) {
      const user = {
        username: 'irfanbaqui-1',
        lastname: 'baqui',
        email: '<EMAIL>'
      };

      userService
        .insert(user)
        .then(function (newUser) {
          should.not.exist(newUser);
        })
        .catch(function (err) {
          should.exist(err);
          err.message.should.eql('firstname is required');
          done();
        });
    });

    it('should throw an error when inserting a user with existing username', function (done) {
      const user = {
        username: 'irfanbaqui',
        firstname: 'irfan',
        lastname: 'baqui',
        email: '<EMAIL>'
      };

      userService
        .insert(user)
        .then(function (newUser) {
          should.not.exist(newUser);
        })
        .catch(function (err) {
          should.exist(err);
          err.message.should.eql('username already exists');
          done();
        });
    });
  });<commit_msg>Do not rely on elements order<commit_after>const should = require('should');
const uuid = require('uuid');
const redisConfig = require('../../lib/config').systemConfig.db.redis;
const services = require('../../lib/services');
const userService = services.user;
const db = require('../../lib/db');

describe('User service tests', function () {
  describe('Insert tests', function () {
    before(function () {
      return db.flushdb();
    });

    it('should insert a user', function () {
      const user = {
        username: 'irfanbaqui',
        firstname: 'irfan',
        lastname: 'baqui',
        email: '<EMAIL>'
      };

      return userService
        .insert(user)
        .then(function (newUser) {
          const expectedUserProps = ['firstname', 'lastname', 'email', 'isActive', 'username', 'id', 'createdAt', 'updatedAt'];
          should(Object.keys(newUser)).containDeep(expectedUserProps);
          newUser.should.have.properties(user);
          should.ok(newUser.isActive);
          return db.hgetall(redisConfig.namespace.concat('-', 'user').concat(':', newUser.id))
            .then(function (userObj) {
              userObj.isActive = userObj.isActive === 'true';
              should.deepEqual(userObj, newUser);
            });
        });
    });

    it('should throw an error when inserting a user with missing properties', function (done) {
      const user = {
        username: 'irfanbaqui-1',
        lastname: 'baqui',
        email: '<EMAIL>'
      };

      userService
        .insert(user)
        .then(function (newUser) {
          should.not.exist(newUser);
        })
        .catch(function (err) {
          should.exist(err);
          err.message.should.eql('data should have required property \'firstname\'');
          done();
        });
    });

    it('should throw an error when inserting a user with existing username', function (done) {
      const user = {
        username: 'irfanbaqui',
        firstname: 'irfan',
        lastname: 'baqui',
        email: '<EMAIL>'
      };

      userService
        .insert(user)
        .then(function (newUser) {
          should.not.exist(newUser);
        })
        .catch(function (err) {
          should.exist(err);
          err.message.should.eql('username already exists');
          done();
        });
    });
  });
{'fraction_non_alphanumeric': 0.15459670424978317, 'fraction_numerical': 0.0004336513443191674, 'mean_word_length': 2.1552667578659372, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '3683867', 'n_tokens_mistral': 1325, 'n_tokens_neox': 1295, 'n_words': 288}