<commit_before>var rfr = require('rfr');
var Lab = require('lab');
var lab = exports.lab = Lab.script();
var Code = require('code');
var expect = Code.expect;

var Utility = rfr('app/util/Utility');
var logger = Utility.createLogger(__filename);

var Storage = rfr('app/models/Storage.js');
var TestUtils = rfr('test/TestUtils');

lab.experiment('User Model Tests', function () {

  var user = {
    username: 'Jane Tan',
    alias: 'Jane the kid',
    email: '<EMAIL>',
    password: '<PASSWORD>',
    accessToken: 'atoken',
    platformType: 'facebook',
    platformId: 'asdfadf-asdfasdf-asdfasdfaf-dfddf',
    description: 'a long long long description about jane'
  };

  lab.beforeEach({timeout: 10000}, function(done) {
    // Delete database, run before every single test
    TestUtils.resetDatabase(done);
  });

  lab.test('Create User', function (done) {
    Storage.createUser(user).then(function(user) {
      expect(user.username).to.equal('Jane Tan');
      done();
    });
  });

  lab.test('Create User with missing username parameters', function (done) {
    var baduser = {
      email: '<EMAIL>',
      password: '<PASSWORD>'
    };

    Storage.createUser(baduser).then(function(res) {
      expect(res).to.be.false();
      done();
    });
  });

  lab.test('Create User with duplicate emails', function (done) {
    Storage.createUser(user).then(function() {
      Storage.createUser(user).then(function(res) {
        expect(res).to.be.false();
        done();
      });
    });
  });

  lab.test('Create Admin account', function (done) {
    var admin = {
      username: 'admin1',
      email: '<EMAIL>',
      password: '<PASSWORD>',
      permissions: 'admin'
    };

    Storage.createUser(admin).then(function(res) {
      expect(res.username).to.equal('admin1');
      expect(res.permissions).to.equal('admin');
      done();
    });
  });

  lab.test('Get User by Id', function (done) {
    Storage.createUser(user).then(function(user) {
      Storage.getUserById(user.userId).then(function(data) {
        expect(data.username).to.equal('Jane Tan');
        done();
      });
    });
  });

  lab.test('Get User by Email', function (done) {
    Storage.createUser(user).then(function(user) {
      Storage.getUserByEmail('<EMAIL>').then(function(data) {
        expect(data.username).to.equal('Jane Tan');
        done();
      });
    });
  });

  lab.test('Get User by platformId', function (done) {
    Storage.createUser(user).then(function(user) {
      Storage.getUserByPlatformId('facebook',
          'asdfadf-asdfasdf-asdfasdfaf-dfddf').then(function(data) {
            expect(data.username).to.equal('Jane Tan');
            done();
          });
    });
  });

  lab.test('Get User by invalid platform parameters', function (done) {
    Storage.createUser(user).then(function(user) {
      Storage.getUserByPlatformId('twitter',
          'asdfadf-asdfasdf-asdfasdfaf-dfddf').then(function(data) {
            expect(data).to.be.false();
            done();
          });
    });
  });

  lab.test('Get User by username and password', function (done) {
    Storage.createUser(user).then(function(user) {
      Storage.getUserByUsernamePassword('Jane Tan', 'secretpass')
        .then(function(data) {
          expect(data.username).to.equal('Jane Tan');
          done();
        });
    });
  });

  lab.test('Get User by invalid username and password', function (done) {
    Storage.createUser(user).then(function(user) {
      Storage.getUserByUsernamePassword('wrong username', 'secretpass')
        .then(function(data) {
          expect(data).to.be.false();
          done();
        });
    });
  });

  lab.test('Get User by with same username but different password',
      function (done) {
        Storage.createUser(user).then(function(user) {
          Storage.getUserByUsernamePassword('<NAME>', '<PASSWORD>')
            .then(function(data) {
              expect(data).to.be.false();
              done();
            });
        });
      });

  lab.test('Get Non-Existing User', function (done) {
    Storage.getUserById('19f9bd98-ffff-aaaa-bbbb-3109f617667d')
      .then(function(res) {
        expect(res).to.be.false();
        done();
      });
  });

  lab.test('Delete Non-Existing User', function (done) {
    Storage.deleteUserById('19f9bd98-ffff-aaaa-bbbb-3109f617667d')
      .then(function(res) {
        expect(res).to.be.false();
        done();
      });
  });

  lab.test('Delete User', function (done) {
    Storage.createUser(user).then(function(user) {
      Storage.deleteUserById(user.userId).then(function(res) {
        expect(res).to.be.true();
        done();
      });
    });
  });

  lab.test('Update particulars', function (done) {
    var newParticulars = {
      password: '<PASSWORD>',
      accessToken: 'averynewtoken',
      alias: 'anewalias',
      description: 'a new description, so new and new',
      email: '<EMAIL>'
    };

    Storage.createUser(user).then(function(user) {
      Storage.updateParticulars(user.userId, newParticulars)
        .then(function(updatedUser) {
          expect(updatedUser.alias).to.equal('anewalias');
          expect(updatedUser.password).to.equal('<PASSWORD>');
          expect(updatedUser.accessToken).to.equal('averynewtoken');
          expect(updatedUser.description).
            to.equal('a new description, so new and new');
          expect(updatedUser.email).to.equal('<EMAIL>');
          done();
        });
    });
  });

  lab.test('Update particulars with one invalid parameter', function (done) {
    var newParticulars = {
      newColAlias: 23
    };

    Storage.createUser(user).then(function(user) {
      Storage.updateParticulars(user.userId, newParticulars)
        .catch(function(err) {
          expect(err).to.be.instanceof(Error);
          done();
        });
    });
  });

  lab.test('Update particulars with one valid/invalid param', function (done) {
    var newParticulars = {
      newColAlias: 23,
      description: 'simple'
    };

    Storage.createUser(user).then(function(user) {
      Storage.updateParticulars(user.userId, newParticulars)
        .catch(function(err, data) {
          expect(err).to.be.instanceof(Error);
          done();
        });
    });
  });

  lab.test('Update particulars with invalid email', function (done) {
    var newParticulars = {
      newEmail: 'newemail',
      description: 'simple'
    };

    Storage.createUser(user).then(function(user) {
      Storage.updateParticulars(user.userId, newParticulars)
        .catch(function(err, data) {
          expect(err).to.be.instanceof(Error);
          done();
        });
    });
  });

  lab.test('Update particulars with invalid userId', function (done) {
    var newParticulars = {
      newEmail: 'newemail',
      description: 'simple'
    };

    Storage.createUser(user).then(function(user) {
      Storage.updateParticulars('abcd-abcd', newParticulars)
        .catch(function(err, data) {
          expect(err).to.be.instanceof(Error);
          done();
        });
    });
  });

  lab.test('Get a list of users', function (done) {
    Storage.models.User.bulkCreate([
      {username: 'Jane', password: '<PASSWORD>', email: '<EMAIL>'},
      {username: 'Alan', password: '<PASSWORD>', email: '<EMAIL>'},
      {username: 'John', password: '<PASSWORD>', email: '<EMAIL>'}
    ])
    .then(function() {
      Storage.getListOfUsers()
      .then(function(users) {
        expect(users[0].username).to.equal('Alan');
        expect(users[1].username).to.equal('Jane');
        expect(users[2].username).to.equal('John');
        done();
      });
    });
  });

});
<commit_msg>Add tests for get user by username<commit_after>var rfr = require('rfr');
var Lab = require('lab');
var lab = exports.lab = Lab.script();
var Code = require('code');
var expect = Code.expect;

var Utility = rfr('app/util/Utility');
var logger = Utility.createLogger(__filename);

var Storage = rfr('app/models/Storage.js');
var TestUtils = rfr('test/TestUtils');

lab.experiment('User Model Tests', function () {

  var user = {
    username: 'Jane Tan',
    alias: 'Jane the kid',
    email: '<EMAIL>',
    password: '<PASSWORD>',
    accessToken: 'atoken',
    platformType: 'facebook',
    platformId: 'asdfadf-asdfasdf-asdfasdfaf-dfddf',
    description: 'a long long long description about jane'
  };

  lab.beforeEach({timeout: 10000}, function(done) {
    // Delete database, run before every single test
    TestUtils.resetDatabase(done);
  });

  lab.test('Create User', function (done) {
    Storage.createUser(user).then(function(user) {
      expect(user.username).to.equal('<NAME>');
      done();
    });
  });

  lab.test('Create User with missing username parameters', function (done) {
    var baduser = {
      email: '<EMAIL>',
      password: '<PASSWORD>'
    };

    Storage.createUser(baduser).then(function(res) {
      expect(res).to.be.false();
      done();
    });
  });

  lab.test('Create User with duplicate emails', function (done) {
    Storage.createUser(user).then(function() {
      Storage.createUser(user).then(function(res) {
        expect(res).to.be.false();
        done();
      });
    });
  });

  lab.test('Create Admin account', function (done) {
    var admin = {
      username: 'admin1',
      email: '<EMAIL>',
      password: '<PASSWORD>',
      permissions: 'admin'
    };

    Storage.createUser(admin).then(function(res) {
      expect(res.username).to.equal('admin1');
      expect(res.permissions).to.equal('admin');
      done();
    });
  });

  lab.test('Get User by Id', function (done) {
    Storage.createUser(user).then(function(user) {
      Storage.getUserById(user.userId).then(function(data) {
        expect(data.username).to.equal('Jane Tan');
        done();
      });
    });
  });

  lab.test('Get User by Email', function (done) {
    Storage.createUser(user).then(function(user) {
      Storage.getUserByEmail('<EMAIL>').then(function(data) {
        expect(data.username).to.equal('Jane Tan');
        done();
      });
    });
  });

  lab.test('Get User by platformId', function (done) {
    Storage.createUser(user).then(function(user) {
      Storage.getUserByPlatformId('facebook',
          'asdfadf-asdfasdf-asdfasdfaf-dfddf').then(function(data) {
            expect(data.username).to.equal('<NAME>');
            done();
          });
    });
  });

  lab.test('Get User by invalid platform parameters', function (done) {
    Storage.createUser(user).then(function(user) {
      Storage.getUserByPlatformId('twitter',
          'asdfadf-asdfasdf-asdfasdfaf-dfddf').then(function(data) {
            expect(data).to.be.false();
            done();
          });
    });
  });

  lab.test('Get User by username', function (done) {
    Storage.createUser(user).then(function(user) {
      Storage.getUserByUsername('Jane Tan')
        .then(function(data) {
          expect(data.username).to.equal('Jane Tan');
          done();
        });
    });
  });

  lab.test('Get User by invalid username', function (done) {
    Storage.createUser(user).then(function(user) {
      Storage.getUserByUsername('wrong username')
        .then(function(data) {
          expect(data).to.be.false();
          done();
        });
    });
  });

  lab.test('Get User by username and password', function (done) {
    Storage.createUser(user).then(function(user) {
      Storage.getUserByUsernamePassword('<NAME>', '<PASSWORD>')
        .then(function(data) {
          expect(data.username).to.equal('Jane Tan');
          done();
        });
    });
  });

  lab.test('Get User by invalid username and password', function (done) {
    Storage.createUser(user).then(function(user) {
      Storage.getUserByUsernamePassword('wrong username', '<PASSWORD>')
        .then(function(data) {
          expect(data).to.be.false();
          done();
        });
    });
  });

  lab.test('Get User by with same username but different password',
      function (done) {
        Storage.createUser(user).then(function(user) {
          Storage.getUserByUsernamePassword('<NAME>', '<PASSWORD>')
            .then(function(data) {
              expect(data).to.be.false();
              done();
            });
        });
      });

  lab.test('Get Non-Existing User', function (done) {
    Storage.getUserById('19f9bd98-ffff-aaaa-bbbb-3109f617667d')
      .then(function(res) {
        expect(res).to.be.false();
        done();
      });
  });

  lab.test('Delete Non-Existing User', function (done) {
    Storage.deleteUserById('19f9bd98-ffff-aaaa-bbbb-3109f617667d')
      .then(function(res) {
        expect(res).to.be.false();
        done();
      });
  });

  lab.test('Delete User', function (done) {
    Storage.createUser(user).then(function(user) {
      Storage.deleteUserById(user.userId).then(function(res) {
        expect(res).to.be.true();
        done();
      });
    });
  });

  lab.test('Update particulars', function (done) {
    var newParticulars = {
      password: '<PASSWORD>',
      accessToken: '<PASSWORD>',
      alias: 'anewalias',
      description: 'a new description, so new and new',
      email: '<EMAIL>'
    };

    Storage.createUser(user).then(function(user) {
      Storage.updateParticulars(user.userId, newParticulars)
        .then(function(updatedUser) {
          expect(updatedUser.alias).to.equal('anewalias');
          expect(updatedUser.password).to.equal('<PASSWORD>');
          expect(updatedUser.accessToken).to.equal('averynewtoken');
          expect(updatedUser.description).
            to.equal('a new description, so new and new');
          expect(updatedUser.email).to.equal('<EMAIL>');
          done();
        });
    });
  });

  lab.test('Update particulars with one invalid parameter', function (done) {
    var newParticulars = {
      newColAlias: 23
    };

    Storage.createUser(user).then(function(user) {
      Storage.updateParticulars(user.userId, newParticulars)
        .catch(function(err) {
          expect(err).to.be.instanceof(Error);
          done();
        });
    });
  });

  lab.test('Update particulars with one valid/invalid param', function (done) {
    var newParticulars = {
      newColAlias: 23,
      description: 'simple'
    };

    Storage.createUser(user).then(function(user) {
      Storage.updateParticulars(user.userId, newParticulars)
        .catch(function(err, data) {
          expect(err).to.be.instanceof(Error);
          done();
        });
    });
  });

  lab.test('Update particulars with invalid email', function (done) {
    var newParticulars = {
      newEmail: 'newemail',
      description: 'simple'
    };

    Storage.createUser(user).then(function(user) {
      Storage.updateParticulars(user.userId, newParticulars)
        .catch(function(err, data) {
          expect(err).to.be.instanceof(Error);
          done();
        });
    });
  });

  lab.test('Update particulars with invalid userId', function (done) {
    var newParticulars = {
      newEmail: 'newemail',
      description: 'simple'
    };

    Storage.createUser(user).then(function(user) {
      Storage.updateParticulars('abcd-abcd', newParticulars)
        .catch(function(err, data) {
          expect(err).to.be.instanceof(Error);
          done();
        });
    });
  });

  lab.test('Get a list of users', function (done) {
    Storage.models.User.bulkCreate([
      {username: 'Jane', password: '<PASSWORD>', email: '<EMAIL>'},
      {username: 'Alan', password: '<PASSWORD>', email: '<EMAIL>'},
      {username: 'John', password: '<PASSWORD>', email: '<EMAIL>'}
    ])
    .then(function() {
      Storage.getListOfUsers()
      .then(function(users) {
        expect(users[0].username).to.equal('Alan');
        expect(users[1].username).to.equal('Jane');
        expect(users[2].username).to.equal('John');
        done();
      });
    });
  });

});

{'fraction_non_alphanumeric': 0.16843033509700175, 'fraction_numerical': 0.005542957923910304, 'mean_word_length': 2.714787084698175, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 47, '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': '7563837', 'n_tokens_mistral': 4970, 'n_tokens_neox': 4870, 'n_words': 825}