<commit_before>var simple = require('simple-mock')
var test = require('tape')

var signUp = require('../../lib/sign-up')

test('signUp without options', function (t) {
  t.plan(1)

  signUp({})

  .catch(t.pass.bind(t, 'rejects'))
})

test('signUp without password', function (t) {
  t.plan(1)

  signUp({}, {
    username: 'pat'
  })

  .catch(t.pass.bind(t, 'rejects'))
})

test('signUp without username', function (t) {
  t.plan(1)

  signUp({}, {
    password: '<PASSWORD>'
  })

  .catch(t.pass.bind(t, 'rejects'))
})

test('signUp with username & password', function (t) {
  t.plan(6)

  var state = {
    url: 'http://example.com',
    validate: simple.stub()
  }

  simple.mock(signUp.internals, 'request').resolveWith({
    body: 'response body'
  })
  simple.mock(signUp.internals, 'serialise').returnWith('serialise return')
  simple.mock(signUp.internals, 'deserialise').returnWith({
    id: 'deserialise id',
    username: 'deserialise username'
  })

  signUp(state, {
    username: 'pat',
    password: '<PASSWORD>'
  })

  .then(function (result) {
    t.deepEqual(state.validate.lastCall.arg, {
      username: 'pat',
      password: '<PASSWORD>'
    })
    t.deepEqual(signUp.internals.serialise.lastCall.args, [
      'account',
      {
        username: 'pat',
        password: '<PASSWORD>'
      }
    ])
    t.deepEqual(signUp.internals.request.lastCall.arg, {
      method: 'PUT',
      url: 'http://example.com/session/account',
      body: 'serialise return'
    })
    t.deepEqual(signUp.internals.deserialise.lastCall.args, [
      'response body',
      { include: 'profile' }
    ])

    t.is(result.id, 'deserialise id', 'resolves with account id')
    t.is(result.username, 'deserialise username', 'resolves with account username')

    simple.restore()
  })
  .catch(t.error)
})

test.skip('signUp with profile', function (t) {
  t.plan(1)

  var state = {
    validate: function () {}
  }

  signUp(state, {
    username: 'pat',
    password: '<PASSWORD>',
    profile: {}
  })

  .then(t.fail.bind(t, 'must throw'))

  .catch(t.pass.bind(t, 'throws error'))
})

test.skip('account.signUp with invalid options', function (t) {
  t.plan(1)

  var state = {
    validate: function (options) {
      throw new Error('Not funky enough!')
    }
  }

  signUp(state, {
    username: 'pat',
    password: '<PASSWORD>'
  })

  .then(t.fail.bind(t, 'must throw'))

  .catch(function (error) {
    t.is(error.message, 'Not funky enough!', 'throws error')
  })
})

test('signUp with request error', function (t) {
  t.plan(1)

  var state = {
    validate: function () {}
  }

  simple.mock(signUp.internals, 'request').rejectWith(new Error('Ooops'))

  signUp(state, {
    username: 'pat',
    password: '<PASSWORD>'
  })

  .then(t.fail.bind(t, 'must throw'))

  .catch(function (error) {
    t.is(typeof error, 'object', 'returns error object')
  })
})
<commit_msg>test: signUp rejects on error, does not throw<commit_after>var simple = require('simple-mock')
var test = require('tape')

var signUp = require('../../lib/sign-up')

test('signUp without options', function (t) {
  t.plan(1)

  signUp({})

  .catch(t.pass.bind(t, 'rejects'))
})

test('signUp without password', function (t) {
  t.plan(1)

  signUp({}, {
    username: 'pat'
  })

  .catch(t.pass.bind(t, 'rejects'))
})

test('signUp without username', function (t) {
  t.plan(1)

  signUp({}, {
    password: '<PASSWORD>'
  })

  .catch(t.pass.bind(t, 'rejects'))
})

test('signUp with username & password', function (t) {
  t.plan(6)

  var state = {
    url: 'http://example.com',
    validate: simple.stub()
  }

  simple.mock(signUp.internals, 'request').resolveWith({
    body: 'response body'
  })
  simple.mock(signUp.internals, 'serialise').returnWith('serialise return')
  simple.mock(signUp.internals, 'deserialise').returnWith({
    id: 'deserialise id',
    username: 'deserialise username'
  })

  signUp(state, {
    username: 'pat',
    password: '<PASSWORD>'
  })

  .then(function (result) {
    t.deepEqual(state.validate.lastCall.arg, {
      username: 'pat',
      password: '<PASSWORD>'
    })
    t.deepEqual(signUp.internals.serialise.lastCall.args, [
      'account',
      {
        username: 'pat',
        password: '<PASSWORD>'
      }
    ])
    t.deepEqual(signUp.internals.request.lastCall.arg, {
      method: 'PUT',
      url: 'http://example.com/session/account',
      body: 'serialise return'
    })
    t.deepEqual(signUp.internals.deserialise.lastCall.args, [
      'response body',
      { include: 'profile' }
    ])

    t.is(result.id, 'deserialise id', 'resolves with account id')
    t.is(result.username, 'deserialise username', 'resolves with account username')

    simple.restore()
  })
  .catch(t.error)
})

test('signUp with profile', function (t) {
  t.plan(1)

  var state = {
    validate: function () {}
  }

  signUp(state, {
    username: 'pat',
    password: '<PASSWORD>',
    profile: {}
  })

  .then(t.fail.bind(t, 'must throw'))

  .catch(t.pass.bind(t, 'throws error'))
})

test('account.signUp with invalid options', function (t) {
  t.plan(1)

  var state = {
    validate: function (options) {
      throw new Error('Not funky enough!')
    }
  }

  signUp(state, {
    username: 'pat',
    password: '<PASSWORD>'
  })

  .then(t.fail.bind(t, 'must throw'))

  .catch(function (error) {
    t.is(error.message, 'Not funky enough!', 'throws error')
  })
})

test('signUp with request error', function (t) {
  t.plan(1)

  var state = {
    validate: function () {}
  }

  simple.mock(signUp.internals, 'request').rejectWith(new Error('Ooops'))

  signUp(state, {
    username: 'pat',
    password: '<PASSWORD>'
  })

  .then(t.fail.bind(t, 'must throw'))

  .catch(function (error) {
    t.is(typeof error, 'object', 'returns error object')
  })
})

{'fraction_non_alphanumeric': 0.18263266712611992, 'fraction_numerical': 0.002412129565816678, 'mean_word_length': 3.093794076163611, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '2823470', 'n_tokens_mistral': 2102, 'n_tokens_neox': 2056, 'n_words': 409}