<commit_before>'use strict';

require('../../../server/run');

var _               = require('lodash');
var mongoose        = require('mongoose');
var request         = require('supertest');
var expect          = require('chai').expect;
var App             = require('../../../server/app');
var User            = require('../../../server/model/user');
var UserSeed        = require('../../../seeds/user');
var UserSettings    = require('../../../server/settings').user;
var TestUtils       = require('../../../tests/utils');
var SuperAgentUtils = require('../superagent_utils');

var server = request(App.server.https);
var agent  = request.agent(App.server.https);

var agentUtils = new SuperAgentUtils(agent);

var performLogin = function(user, password){
  return server
  .post('/api/auth/login')
  .send({
    email    : user.email,
    password : password
  })
  .endAsync()
  .then(agentUtils.saveJWT.bind(agentUtils));
};

describe('/api/users', function(){

  describe('GET', function(){

    // Increase default timeout to let user seed do its work.
    var usersToSeed = 10;
    TestUtils.seedingTimeout(this, usersToSeed);

    var seededUsers;

    before(function(done){
      App.setup()
      .thenReturn(User.removeAsync())
      .thenReturn(UserSeed.seed(usersToSeed))
      .then(_.partialRight(TestUtils.prepareSeededObjects, UserSettings.paths, function(item){ return item.email; }))
      .then(function(seeded){
        seededUsers = seeded;
        return performLogin(_.first(seeded), 'test');
      })
      .then(function(res){
        done();
      })
      .catch(done);
    });

    it('/ should return user list', function(done){
      agentUtils.withJWT(server.get('/api/users'))
      .expect(200)
      .expect('Content-Type', /json/)
      .endAsync()
      .then(function(res){
        expect(res.body).to.be.eql(seededUsers);
        done();
      })
      .catch(done);
    });

    it('?skip=4 should return user list skipping the first four users', function(done){
      agentUtils.withJWT(server.get('/api/users?skip=4'))
      .expect(200)
      .expect('Content-Type', /json/)
      .endAsync()
      .then(function(res){
        expect(res.body).to.be.eql(_.rest(seededUsers, 4));
        done();
      })
      .catch(done);
    });

    it('?skip=invalid_skip_param should return user list without skipping anything', function(done){
      agentUtils.withJWT(server.get('/api/users?skip=invalid_skip_param'))
      .expect(200)
      .expect('Content-Type', /json/)
      .endAsync()
      .then(function(res){
        expect(res.body).to.be.eql(seededUsers);
        done();
      })
      .catch(done);
    });

    it('?limit=4 should return the first four users', function(done){
      agentUtils.withJWT(server.get('/api/users?limit=4'))
      .expect(200)
      .expect('Content-Type', /json/)
      .endAsync()
      .then(function(res){
        expect(res.body).to.be.eql(_.first(seededUsers, 4));
        done();
      })
      .catch(done);
    });

    it('?limit=invalid_limit_param should return all users', function(done){
      agentUtils.withJWT(server.get('/api/users?limit=invalid_limit_param'))
      .expect(200)
      .expect('Content-Type', /json/)
      .endAsync()
      .then(function(res){
        expect(res.body).to.be.eql(seededUsers);
        done();
      })
      .catch(done);
    });

    it('/:id should return user with _id = :id', function(done){
      var user = _.first(seededUsers);
      agentUtils.withJWT(server.get('/api/users/' + user._id))
      .expect(200)
      .expect('Content-Type', /json/)
      .endAsync()
      .then(function(res){
        TestUtils.assertObjectIds(user._id, res.body._id);
        TestUtils.assertUnorderedArrays(_.keys(res.body), UserSettings.paths);
        done();
      })
      .catch(done);
    });

    it('/total should return total user list size', function(done){
      agentUtils.withJWT(server.get('/api/users/total'))
      .expect(200)
      .expect('Content-Type', /json/)
      .endAsync()
      .then(function(res){
        expect(res.body).to.eql({ total: seededUsers.length });
        done();
      })
      .catch(done);
    });

    it('/:not_found_id should respond with 404', function(done){
      agentUtils.withJWT(server.get('/api/users/' + mongoose.Types.ObjectId()))
      .expect(404)
      .expect('Content-Type', /json/)
      .endAsync()
      .then(function(res){
        expect(res.body).not.to.be.empty;
        expect(res.body.message).to.eql(UserSettings.errors.notFound);
        done();
      })
      .catch(done);
    });

    it('/:invalid_id should respond with 400', function(done){
      agentUtils.withJWT(server.get('/api/users/powerfromhell'))
      .expect(400)
      .expect('Content-Type', /json/)
      .endAsync()
      .then(function(res){
        expect(res.body).not.to.be.empty;
        expect(res.body.message).to.eql(UserSettings.errors.invalidId);
        done();
      })
      .catch(done);
    });

  });

  describe('POST', function(){

    before(function(done){
      App.setup()
      .thenReturn(User.removeAsync())
      .thenReturn(UserSeed.seed(1))
      .then(_.partialRight(TestUtils.prepareSeededObjects, UserSettings.paths, function(item){ return item.email; }))
      .then(function(seeded){
        return performLogin(_.first(seeded), 'test');
      })
      .then(function(res){
        done();
      })
      .catch(done);
    });

    it('/ should create a new user', function(done){
      agentUtils.withJWT(server.post('/api/users'))
      .send({
        email: '<EMAIL>',
        password: '<PASSWORD>'
      })
      .expect(200)
      .expect('Content-Type', /json/)
      .endAsync()
      .then(function(res){
        User.findByUsernameAsync('<EMAIL>')
        .then(function(user){
          expect(user).to.be.instanceof(Object);
          expect(user.email).to.eql('<EMAIL>');
          done();
        });
      })
      .catch(done);
    });

    it('/ should fail creation of a user with an already-existing email', function(done){
      agentUtils.withJWT(server.post('/api/users'))
      .send({
        email: '<EMAIL>',
        password: '<PASSWORD>'
      })
      .expect(500)<commit_msg>Fixed User specs<commit_after>'use strict';

require('../../../server/run');

var _               = require('lodash');
var mongoose        = require('mongoose');
var request         = require('supertest');
var expect          = require('chai').expect;
var App             = require('../../../server/app');
var User            = require('../../../server/model/user');
var UserSeed        = require('../../../seeds/user');
var UserSettings    = require('../../../server/settings').user;
var TestUtils       = require('../../../tests/utils');
var SuperAgentUtils = require('../superagent_utils');

var server = request(App.server.https);
var agent  = request.agent(App.server.https);

var agentUtils = new SuperAgentUtils(agent);

var performLogin = function(user, password){
  return server
  .post('/api/auth/login')
  .send({
    email    : user.email,
    password : password
  })
  .endAsync()
  .then(agentUtils.saveJWT.bind(agentUtils));
};

describe('/api/users', function(){

  describe('GET', function(){

    var usersToSeed = 10;
    TestUtils.seedingTimeout(this, usersToSeed);

    var seededUsers;

    before(function(done){
      App.setup()
      .thenReturn(User.removeAsync())
      .thenReturn(UserSeed.seed(usersToSeed))
      .then(_.partialRight(TestUtils.prepareSeededObjects, UserSettings.paths, function(item){ return item.email; }))
      .then(function(seeded){
        seededUsers = seeded;
        return performLogin(_.first(seeded), 'test');
      })
      .then(function(res){
        done();
      })
      .catch(done);
    });

    after(function(done){
      User.removeAsync()
      .then(function(){
        done();
      })
      .catch(done);
    });

    it('/ should return user list', function(done){
      agentUtils.withJWT(server.get('/api/users'))
      .expect(200)
      .expect('Content-Type', /json/)
      .endAsync()
      .then(function(res){
        expect(res.body).to.be.eql(seededUsers);
        done();
      })
      .catch(done);
    });

    it('?skip=4 should return user list skipping the first four users', function(done){
      agentUtils.withJWT(server.get('/api/users?skip=4'))
      .expect(200)
      .expect('Content-Type', /json/)
      .endAsync()
      .then(function(res){
        expect(res.body).to.be.eql(_.slice(seededUsers, 4));
        done();
      })
      .catch(done);
    });

    it('?skip=invalid_skip_param should return user list without skipping anything', function(done){
      agentUtils.withJWT(server.get('/api/users?skip=invalid_skip_param'))
      .expect(200)
      .expect('Content-Type', /json/)
      .endAsync()
      .then(function(res){
        expect(res.body).to.be.eql(seededUsers);
        done();
      })
      .catch(done);
    });

    it('?limit=4 should return the first four users', function(done){
      agentUtils.withJWT(server.get('/api/users?limit=4'))
      .expect(200)
      .expect('Content-Type', /json/)
      .endAsync()
      .then(function(res){
        expect(res.body).to.be.eql(_.slice(seededUsers, 0, 4));
        done();
      })
      .catch(done);
    });

    it('?limit=invalid_limit_param should return all users', function(done){
      agentUtils.withJWT(server.get('/api/users?limit=invalid_limit_param'))
      .expect(200)
      .expect('Content-Type', /json/)
      .endAsync()
      .then(function(res){
        expect(res.body).to.be.eql(seededUsers);
        done();
      })
      .catch(done);
    });

    it('/:id should return user with _id = :id', function(done){
      var user = _.first(seededUsers);
      agentUtils.withJWT(server.get('/api/users/' + user._id))
      .expect(200)
      .expect('Content-Type', /json/)
      .endAsync()
      .then(function(res){
        TestUtils.assertObjectIds(user._id, res.body._id);
        TestUtils.assertUnorderedArrays(_.keys(res.body), UserSettings.paths);
        done();
      })
      .catch(done);
    });

    it('/total should return total user list size', function(done){
      agentUtils.withJWT(server.get('/api/users/total'))
      .expect(200)
      .expect('Content-Type', /json/)
      .endAsync()
      .then(function(res){
        expect(res.body).to.eql({ total: seededUsers.length });
        done();
      })
      .catch(done);
    });

    it('/:not_found_id should respond with 404', function(done){
      agentUtils.withJWT(server.get('/api/users/' + mongoose.Types.ObjectId()))
      .expect(404)
      .expect('Content-Type', /json/)
      .endAsync()
      .then(function(res){
        expect(res.body).not.to.be.empty;
        expect(res.body.message).to.eql(UserSettings.errors.notFound);
        done();
      })
      .catch(done);
    });

    it('/:invalid_id should respond with 400', function(done){
      agentUtils.withJWT(server.get('/api/users/powerfromhell'))
      .expect(400)
      .expect('Content-Type', /json/)
      .endAsync()
      .then(function(res){
        expect(res.body).not.to.be.empty;
        expect(res.body.message).to.eql(UserSettings.errors.invalidId);
        done();
      })
      .catch(done);
    });

  });

  describe('POST', function(){

    var usersToSeed = 1;
    TestUtils.seedingTimeout(this, usersToSeed, 3000);

    before(function(done){
      App.setup()
      .thenReturn(User.removeAsync())
      .thenReturn(UserSeed.seed(usersToSeed))
      .then(_.partialRight(TestUtils.prepareSeededObjects, UserSettings.paths, function(item){ return item.email; }))
      .then(function(seeded){
        return performLogin(_.first(seeded), 'test');
      })
      .then(function(res){
        done();
      })
      .catch(done);
    });

    after(function(done){
      User.removeAsync()
      .then(function(){
        done();
      })
      .catch(done);
    });


    it('/ should create a new user', function(done){
      agentUtils.withJWT(server.post('/api/users'))
      .send({
        email    : '<EMAIL>',
        password : '<PASSWORD>'
      })
      .expect(200)
      .expect('Content-Type', /json/)
      .endAsync()
      .then(function(res){
        User.findByUsernameAsync('<EMAIL>')
        .then(function(user){
          expect(user).to.be.instanceof(Object);
          expect(user.email).to.eql('<EMAIL>');
          done();
        });
      })
      .catch(done);
    });

    it('/ should fail creation of a user with an already-existing email', function(done){
      agentUtils.withJWT(server.post('/api/users'))
      .send({
        email: '<EMAIL>',
        password: '<PASSWORD>'
      })
      .expect(500)
{'fraction_non_alphanumeric': 0.17827385631190065, 'fraction_numerical': 0.00793900330136771, 'mean_word_length': 2.410077727150898, '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': '643519', 'n_tokens_mistral': 4073, 'n_tokens_neox': 3953, 'n_words': 646}