<commit_before>request = require 'request'
async = require 'async'
should = require 'should'
_ = require 'underscore'
uuid = require 'uuid'

startApp = require './../start_app'
base = require './../base'
options = require './../options'
UserTestHelper = require './user_test_helper'

makeUser = (testUserEmail, status, cb)->
  ops = options()
  ops.body =
    firstName: 'Test'
    lastName: 'User'
    email: testUserEmail
    password: '<PASSWORD>'
  request.post (base '/users'), ops, (e,r,b)->
    r.statusCode.should.be.equal status
    UserTestHelper.validate b if r.statusCode is 200
    cb(b)

describe 'Users', ->
  before (done) ->
    startApp -> done()

  after (done) ->
    request (base '/users'), options(), (e,r,b)->
      testUsers = _.filter b, (u)->
        return u.firstName is 'Test' and u.lastName is 'User'
      async.each testUsers, (u, cb)->
        request.del (base "/users/#{u.id}"), options(), (e,r,b)->
          r.statusCode.should.be.equal 200
          cb()
      , done

  it 'Get all Users', (done)->
    request (base '/users'), options(), (e,r,b)->
      r.statusCode.should.be.equal 200
      should.equal UserTestHelper.users.length <= b.length, yes
      _.each b, UserTestHelper.validate
      done()

  it 'Creates a new user', (done) ->
    t = "<EMAIL>#{uuid.v1()}<EMAIL>"
    makeUser t, 200, (b)->
      UserTestHelper.validate b
      b.should.have.property 'credentials'
      b.credentials.should.have.property 'secret'
      done()

  it 'can get each user individually', (done)->
    request (base '/users'), options(), (e,r,b)->
      iterator = (id, cb)->
        request (base "/users/#{id}"), options(), (e,r,b)->
          r.statusCode.should.be.equal 200
          UserTestHelper.validate b
          cb()

      async.eachLimit _.map(b, (u)-> u.id), 100, iterator, done

  it 'cant have two users with the same email', (done)->
    t = "<EMAIL>"
    makeUser t, 200, -> makeUser t, 400, (error)->
      error.should.have.property 'error', 'user already exists'
      done()

  it 'can delete a user', (done)->
    t = "<EMAIL>"
    makeUser t, 200, (user)->
      request.del (base "/users/#{user.id}"), options(), (e,r,b)->
        r.statusCode.should.be.equal 200
        request (base "/users/#{user.id}"), options(), (e,r,b)->
          r.statusCode.should.be.equal 404
          done()

  it 'can handle a non existent user', (done)->
    request (base "/users/not-an-id"), options(), (e,r,b)->
      r.statusCode.should.be.equal 404
      done()

<commit_msg>Add user objs to the user routes<commit_after>request = require 'request'
async = require 'async'
should = require 'should'
_ = require 'underscore'
uuid = require 'uuid'

startApp = require './../start_app'
base = require './../base'
options = require './../options'
UserTestHelper = require './user_test_helper'

makeUser = (testUserEmail, status, cb)->
  ops = options()
  ops.body =
    firstName: 'Test'
    lastName: 'User'
    email: testUserEmail
    password: '<PASSWORD>'
  request.post (base '/users'), ops, (e,r,b)->
    r.statusCode.should.be.equal status
    UserTestHelper.validate b if r.statusCode is 200
    cb(b)

describe 'Users', ->
  before (done) ->
    startApp -> done()

  after (done) ->
    request (base '/users'), options(), (e,r,b)->
      testUsers = _.filter b, (u)->
        return u.firstName is 'Test' and u.lastName is 'User'
      async.each testUsers, (u, cb)->
        request.del (base "/users/#{u.id}"), options(), (e,r,b)->
          r.statusCode.should.be.equal 200
          cb()
      , done

  it.skip 'Get all Users', (done)->
    #Will require an Admin account
    request (base '/users'), options(), (e,r,b)->
      r.statusCode.should.be.equal 200
      should.equal UserTestHelper.users.length <= b.length, yes
      _.each b, UserTestHelper.validate
      done()

  it 'Creates a new user', (done) ->
    t = "<EMAIL>"
    makeUser t, 200, (b)->
      UserTestHelper.validate b
      b.should.have.property 'credentials'
      b.credentials.should.have.property 'secret'
      done()

  it 'can get each user individually', (done)->
    request (base '/users'), options(), (e,r,b)->
      iterator = (u, cb)->
        request (base "/users/#{u.id}"), options(u), (e,r,b)->
          r.statusCode.should.be.equal 200
          UserTestHelper.validate b
          cb()

      async.eachLimit b, 100, iterator, done

  it 'cant have two users with the same email', (done)->
    t = "<EMAIL>"
    makeUser t, 200, -> makeUser t, 400, (error)->
      error.should.have.property 'error', 'user already exists'
      done()

  it 'can delete a user', (done)->
    t = "<EMAIL>"
    makeUser t, 200, (user)->
      request.del (base "/users/#{user.id}"), options(user), (e,r,b)->
        r.statusCode.should.be.equal 200
        request (base "/users/#{user.id}"), options(user), (e,r,b)->
          r.statusCode.should.be.equal 404
          done()

  it 'can handle a non existent user', (done)->
    request (base "/users/not-an-id"), options(), (e,r,b)->
      r.statusCode.should.be.equal 404
      done()


{'fraction_non_alphanumeric': 0.17430651190241983, 'fraction_numerical': 0.014361597481802085, 'mean_word_length': 2.5702247191011236, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 67, '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': '2194211', 'n_tokens_mistral': 1748, 'n_tokens_neox': 1655, 'n_words': 473}