<commit_before>var nock = require('nock')
var simple = require('simple-mock')
var store = require('humble-localstorage')
var test = require('tape')

var Account = require('../../index')

var baseURL = 'http://localhost:3000'
var signUpResponse = require('../fixtures/signup.json')
var signInResponse = require('../fixtures/signin.json')
var options = {
  username: signUpResponse.data.attributes.username,
  password: '<PASSWORD>'
}

test('events', function (t) {
  t.plan(9)

  var account = new Account({
    url: baseURL
  })

  nock(baseURL)
    .put('/session/account')
    .reply(201, signUpResponse)
    .put('/session')
    .reply(201, signInResponse)
    .delete('/session')
    .reply(204)

  var signUpHandler = simple.stub()
  var signInHandler = simple.stub()
  var signOutHandler = simple.stub()
  account.on('signup', signUpHandler)
  account.on('signin', signInHandler)
  account.on('signout', signOutHandler)

  account.signUp(options)

  .then(function () {
    t.deepEqual(signUpHandler.lastCall.arg, {
      id: 'abc4567',
      username: '<EMAIL>',
      profile: {
        email: '<EMAIL>',
        fullName: '<NAME>'
      }
    }, '"signup" event emitted with account object')

    return account.signIn(options)
  })

  .then(function (accountProperties) {
    var storeAccountData = store.getObject('account')
    t.is(accountProperties.username, options.username, 'returns correct username')
    t.is(storeAccountData.username, accountProperties.username, 'stored correct username in session')
    t.is(storeAccountData.session.id, signInResponse.data.id, 'stored correct session id')

    t.deepEqual(signInHandler.lastCall.arg, {
      id: 'abc4567',
      username: '<EMAIL>',
      session: { id: 'sessionid123' }
    }, '"signin" event emitted with account object')

    return account.signOut()
  })

  .then(function () {
    t.deepEqual(signOutHandler.lastCall.arg, {
      id: 'abc4567',
      username: '<EMAIL>',
      session: { id: 'sessionid123' }
    }, '"signout" event emitted with account object')

    t.is(signUpHandler.callCount, 1, '"signup" event emitted once')
    t.is(signInHandler.callCount, 1, '"signin" event emitted once')
    t.is(signOutHandler.callCount, 1, '"signout" event emitted once')
  })
<commit_msg>test: add integration test for reauthenticate event<commit_after>var nock = require('nock')
var simple = require('simple-mock')
var store = require('humble-localstorage')
var test = require('tape')

var Account = require('../../index')

var baseURL = 'http://localhost:3000'
var signUpResponse = require('../fixtures/signup.json')
var signInResponse = require('../fixtures/signin.json')
var options = {
  username: signUpResponse.data.attributes.username,
  password: '<PASSWORD>'
}

test('events', function (t) {
  t.plan(10)

  var account = new Account({
    url: baseURL
  })

  nock(baseURL)
    .put('/session/account')
    .reply(201, signUpResponse)
    .put('/session').twice()
    .reply(201, signInResponse)
    .delete('/session')
    .reply(204)

  var signUpHandler = simple.stub()
  var signInHandler = simple.stub()
  var signOutHandler = simple.stub()
  var reauthenticateHandler = simple.stub()
  account.on('signup', signUpHandler)
  account.on('signin', signInHandler)
  account.on('signout', signOutHandler)
  account.on('reauthenticate', reauthenticateHandler)

  account.signUp(options)

  .then(function () {
    t.deepEqual(signUpHandler.lastCall.arg, {
      id: 'abc4567',
      username: 'ch<EMAIL>',
      profile: {
        email: '<EMAIL>',
        fullName: '<NAME>'
      }
    }, '"signup" event emitted with account object')

    return account.signIn(options)
  })

  // Check whether signing in again emits 'reauthenticate' instead of 'signin'
  .then(function (accountProperties) {
    return account.signIn(options)
  })

  .then(function (accountProperties) {
    var storeAccountData = store.getObject('account')
    t.is(accountProperties.username, options.username, 'returns correct username')
    t.is(storeAccountData.username, accountProperties.username, 'stored correct username in session')
    t.is(storeAccountData.session.id, signInResponse.data.id, 'stored correct session id')

    t.deepEqual(signInHandler.lastCall.arg, {
      id: 'abc4567',
      username: '<EMAIL>',
      session: { id: 'sessionid123' }
    }, '"signin" event emitted with account object')

    return account.signOut()
  })

  .then(function () {
    t.deepEqual(signOutHandler.lastCall.arg, {
      id: 'abc4567',
      username: '<EMAIL>',
      session: { id: 'sessionid123' }
    }, '"signout" event emitted with account object')

    t.is(signUpHandler.callCount, 1, '"signup" event emitted once')
    t.is(signInHandler.callCount, 1, '"signin" event emitted once')
    t.is(signOutHandler.callCount, 1, '"signout" event emitted once')
    t.is(reauthenticateHandler.callCount, 1, '"reauthenticate" event emitted once')
  })

{'fraction_non_alphanumeric': 0.1478472786352559, 'fraction_numerical': 0.01462225832656377, 'mean_word_length': 3.7815533980582523, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '5028241', 'n_tokens_mistral': 1629, 'n_tokens_neox': 1554, 'n_words': 343}