<commit_before>var init_user = require('../../lib/models/User')
  , config = require('../../lib/config.json')
  , hash = require('password-hash')
  , backend = require('../backend')
  , crypto = require('crypto')
  , test = require('tape')

test('can init User', function(t) {
  config.backend = backend()

  var User = init_user(config)

  t.ok(User, 'returns an object')
  t.equal(typeof User.find, 'function', 'User has a find method')
  t.equal(typeof User.create, 'function', 'User has a create method')
  t.equal(typeof User.update, 'function', 'User has an update method')
  t.equal(typeof User.auth, 'function', 'User has an auth method')
  t.end()
})

test('create user', function(t) {
  config.backend = backend()

  var User = init_user(config)
    , user_data = {}

  user_data.password_sha = '<PASSWORD>'
  user_data.date = '2014-01-01'

  t.plan(4)

  User.create('ZeroCool', user_data, function(err) {
    t.ok(!err, 'no error')
  })

  User.create(null, user_data, function(err) {
    t.ok(err, 'requires username')
  })

  User.create('ZeroCool', {date: 'foo'}, function(err) {
    t.ok(err, 'requires password')
  })

  User.create('ZeroCool', {password_sha: 'foo'}, function(err) {
    t.ok(err, 'requires date')
  })
})

test('find user', function(t) {
  var User = init_user(config)

  var data = {
      name: 'ZeroCool'
    , email: '<EMAIL>'
    , salt: 'saltine'
    , date: '2014-01-01'
    , password_sha: sha('<PASSWORD>')
  }

  t.plan(9)

  User.create(data.name, data, function(err) {
    t.ok(!err, 'create user to find')

    User.find(data.name, function(err, user) {
      t.ok(!err, 'no error')
      t.equal(user.name, data.name, 'gets name')
      t.equal(user.email, data.email, 'gets email')
      t.equal(user.date, data.date, 'gets date')
      t.ok('_rev', 'gets _rev')
      t.equal(Object.keys(user).length, 4, 'nothing else')
    })

    User.find('other guy', function(err, user) {
      t.ok(err, 'error when not found')
      t.ok(!user, 'no user for other guy')
    })
  })
})

test('update user', function(t) {
  var User = init_user(config)

  var data = {
      name: 'ZeroCool'
    , email: '<EMAIL>'
    , salt: 'saltine'
    , date: '2014-01-01'
    , password_sha: sha('<PASSWORD>')
  }

  t.plan(12)

  User.create(data.name, data, created)

  function created(err) {
    t.ok(!err, 'create user to find')

    data.email = '<EMAIL>'
    data.salt = '<PASSWORD>'
    data.date = '2015-01-01'
    data.password_sha = sha('<PASSWORD>')

    User.find(data.name, function(err, user) {
      t.ok(!err, 'no err')
      User.update(user, data, updated)
    })
  }

  function updated(err, user) {
    t.ok(!err, 'no error')
    t.equal(user.email, data.email, 'gets email')
    t.equal(user.date, data.date, 'gets date')
    t.ok('_rev', 'gets _rev')
    t.equal(Object.keys(user).length, 4, 'nothing else')

    User.find(data.name, found)
  }

  function found(err, user) {
    t.ok(!err, 'no error')
    t.equal(user.email, data.email, 'gets email')
    t.equal(user.date, data.date, 'gets date')
    t.ok('_rev', 'gets _rev')
    t.equal(Object.keys(user).length, 4, 'nothing else')
  }
})

function sha(s) {
  return crypto.createHash('sha1').update(s).digest('hex')
}<commit_msg>added tests for user auth<commit_after>var init_user = require('../../lib/models/User')
  , config = require('../../lib/config.json')
  , backend = require('../backend')
  , hash = require('password-hash')
  , crypto = require('crypto')
  , test = require('tape')

test('can init User', function(t) {
  config.backend = backend()

  var User = init_user(config)

  t.ok(User, 'returns an object')
  t.equal(typeof User.find, 'function', 'User has a find method')
  t.equal(typeof User.create, 'function', 'User has a create method')
  t.equal(typeof User.update, 'function', 'User has an update method')
  t.equal(typeof User.auth, 'function', 'User has an auth method')
  t.end()
})

test('create user', function(t) {
  config.backend = backend()

  var User = init_user(config)
    , user_data = {}

  user_data.password_sha = '<PASSWORD>'
  user_data.date = '2014-01-01'

  t.plan(4)

  User.create('ZeroCool', user_data, function(err) {
    t.ok(!err, 'no error')
  })

  User.create(null, user_data, function(err) {
    t.ok(err, 'requires username')
  })

  User.create('ZeroCool', {date: 'foo'}, function(err) {
    t.ok(err, 'requires password')
  })

  User.create('ZeroCool', {password_sha: 'foo'}, function(err) {
    t.ok(err, 'requires date')
  })
})

test('find user', function(t) {
  config.backend = backend()

  var User = init_user(config)

  var data = {
      name: 'ZeroCool'
    , email: '<EMAIL>'
    , salt: 'saltine'
    , date: '2014-01-01'
    , password_sha: sha('<PASSWORD>')
  }

  t.plan(9)

  User.create(data.name, data, function(err) {
    t.ok(!err, 'create user to find')

    User.find(data.name, function(err, user) {
      t.ok(!err, 'no error')
      t.equal(user.name, data.name, 'gets name')
      t.equal(user.email, data.email, 'gets email')
      t.equal(user.date, data.date, 'gets date')
      t.ok('_rev', 'gets _rev')
      t.equal(Object.keys(user).length, 4, 'nothing else')
    })

    User.find('other guy', function(err, user) {
      t.ok(err, 'error when not found')
      t.ok(!user, 'no user for other guy')
    })
  })
})

test('update user', function(t) {
  config.backend = backend()

  var User = init_user(config)

  var data = {
      name: 'ZeroCool'
    , email: '<EMAIL>'
    , salt: 'saltine'
    , date: '2014-01-01'
    , password_sha: sha('<PASSWORD>')
  }

  t.plan(12)

  User.create(data.name, data, created)

  function created(err) {
    t.ok(!err, 'create user to find')

    data.email = '<EMAIL>'
    data.salt = '<PASSWORD>'
    data.date = '2015-01-01'
    data.password_sha = sha('<PASSWORD>')

    User.find(data.name, function(err, user) {
      t.ok(!err, 'no err')
      User.update(user, data, updated)
    })
  }

  function updated(err, user) {
    t.ok(!err, 'no error')
    t.equal(user.email, data.email, 'gets email')
    t.equal(user.date, data.date, 'gets date')
    t.ok('_rev', 'gets _rev')
    t.equal(Object.keys(user).length, 4, 'nothing else')

    User.find(data.name, found)
  }

  function found(err, user) {
    t.ok(!err, 'no error')
    t.equal(user.email, data.email, 'gets email')
    t.equal(user.date, data.date, 'gets date')
    t.ok('_rev', 'gets _rev')
    t.equal(Object.keys(user).length, 4, 'nothing else')
  }
})

test('auth user', function(t) {
  config.backend = backend()

  var User = init_user(config)

  var data = {
      email: '<EMAIL>'
    , salt: '<PASSWORD>'
    , date: '2014-01-01'
  }

  data.password_sha = sha('<PASSWORD>')

  t.plan(12)

  User.create('ZeroCool', data, created)

  function created(err, user) {
    t.ok(!err, 'no err')
    User.auth('ZeroCool', 'hunter2', authed)
  }

  function authed(err, user) {
    t.ok(!err, 'no error')
    t.ok(user, 'returns user')
    t.equal(user.name, 'ZeroCool', 'gets name')
    t.equal(user.email, data.email, 'gets email')
    t.equal(user.date, data.date, 'gets date')
    t.ok('_rev', 'gets _rev')
    t.equal(Object.keys(user).length, 4, 'nothing else')

    User.auth('ZeroCool', 'hunter3', not_found)
    User.auth('ZeroCoo1', 'hunter2', not_found)
  }

  function not_found(err, user) {
    t.ok(!user, 'no user')
    t.ok(err, 'returns error')
  }
})

function sha(s) {
  return crypto.createHash('sha1').update(s).digest('hex')
}
{'fraction_non_alphanumeric': 0.18922540873760385, 'fraction_numerical': 0.012731171267756634, 'mean_word_length': 3.1716042481833426, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, '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': '6241127', 'n_tokens_mistral': 2873, 'n_tokens_neox': 2724, 'n_words': 602}