<commit_before>const app = require('../../app');
const request = require('supertest');
const {
  resetUsersTable,
  exampleUser,
} = require('../helpers');
const { createNewUser } = require('../../server/models/helpers');
const httpServer = require('http').createServer(app);

before(() => {
  httpServer.listen('3003');
});

beforeEach((done) => {
  resetUsersTable(done);
});

after(() => {
  httpServer.close();
});

describe('------ USERS ENDPOINTS: ------', () => {
  describe('POST /users/register', () => {
    it('can receive a POST to /users/register to create a user', (done) => {
      request(app)
        .post('/users/register')
        .send(exampleUser)
        .end((err, res) => {
          if (err) {
            return done(err);
          }
          res.statusCode.should.equal(200);
          res.body.should.have.keys(
            'id',
            'firstName',
            'lastName',
            'emailAddress',
            'password' // eslint-disable-line comma-dangle
          );
          return done();
        });
    });
    it('should not create a new user if the user with the same email already exists', (done) => {
      createNewUser(exampleUser)
        .then(() => {
          request(app)
            .post('/users/register')
            .send(exampleUser)
            .end((err, response) => {
              if (err) {
                return done(err);
              }
              response.statusCode.should.equal(200);
              response.text.should.equal(`A user with the email: ${ exampleUser.emailAddress } already exists`);
              return done();
            });
        })
        .catch(error => done(error));
    });
    it('should send 400 error if data is missing from request', (done) => {
      request(app)
        .post('/users/register')
        .send({
          firstName: '',
          lastName: 'boo',
          emailAddress: '<EMAIL>',
          emailAddressConfirmation: '<EMAIL>',
          password: 'password',
          passwordConfirmation: 'password',
        })
        .end((err, res) => {
          if (err) {
            return done(err);
          }
          res.statusCode.should.equal(400);
          res.body[0].msg.should.equal('First name is required');
          return done();
        });
    });
    it('should send 400 error if email is invalid', (done) => {
      request(app)
        .post('/users/register')
        .send({
          firstName: 'foo',
          lastName: 'boo',
          emailAddress: 'email.com',
          emailAddressConfirmation: 'email.com',
          password: 'password',
          passwordConfirmation: 'password',
        })
        .end((err, res) => {
          if (err) {
            return done(err);
          }
          res.statusCode.should.equal(400);
          res.body[0].msg.should.equal('Email is not valid');
          return done();
        });
    });
    it('should send 400 error if passwords do not match', (done) => {
      request(app)
        .post('/users/register')
        .send({
          firstName: 'foo',
          lastName: 'boo',
          emailAddress: '<EMAIL>',
          emailAddressConfirmation: '<EMAIL>',
          password: 'password',
          passwordConfirmation: '<PASSWORD>',
        })
        .end((err, res) => {
          if (err) {
            return done(err);
          }
          res.statusCode.should.equal(400);
          res.body[0].msg.should.equal('Passwords do not match');
          return done();
        });
    });
    it('should send 400 error if emails do not match', (done) => {
      request(app)
        .post('/users/register')
        .send({
          firstName: 'foo',
          lastName: 'boo',
          emailAddress: '<EMAIL>',
          emailAddressConfirmation: '<EMAIL>',
          password: 'password',
          passwordConfirmation: 'password',
        })
        .end((err, res) => {
          if (err) {
            return done(err);
          }
          res.statusCode.should.equal(400);
          res.body[0].msg.should.equal('Emails do not match');
          return done();
        });
    });
  });

  describe('POST /users/login', () => {
    it('should send a 200 with the user information if login is successful', (done) => {
      createNewUser(exampleUser)
        .then(() => {
          request(app)
            .post('/users/login')
            .send({
              username: exampleUser.emailAddress,
              password: <PASSWORD>,
            })
            .end((err, res) => {
              if (err) {
                return done(err);
              }
              res.statusCode.should.equal(200);
              res.body.should.have.keys(
                'id',
                'firstName',
                'lastName',
                'emailAddress',
                'password',
                'createdAt',
                'updatedAt' // eslint-disable-line comma-dangle
              );
              return done();
            });
        }).catch(error => done(error));
    });
    it('should send a 401 with an error message if user is not found', (done) => {
      request(app)
        .post('/users/login')
        .send({
          username: exampleUser.emailAddress,
          password: <PASSWORD>,
        })
        .end((err, res) => {
          if (err) {
            return done(err);
          }
          res.statusCode.should.equal(401);
          res.body.message.should.equal('A user by that email address does not exist.');
          return done();
        });
    });
    it('should send a 401 with an error message for an incorrectPassword', (done) => {
      createNewUser(exampleUser)
        .then(() => {
          request(app)
            .post('/users/login')
            .send({
              username: exampleUser.emailAddress,
              password: '<PASSWORD>',
            })
            .end((err, res) => {
              if (err) {
                return done(err);
              }
              res.statusCode.should.equal(401);
              res.body.message.should.equal('Incorrect password for the email address provided.');
              return done();
            });
        }).catch(error => done(error));
    });
  });

  describe('POST /users/login', () => {
    it('should log the user out and send a 200 with a message', (done) => {
      request(app)
        .get('/users/logout')
        .end((err, res) => {
          if (err) {
            return done(err);
          }
          res.statusCode.should.equal(200);
          res.text.should.equal('Log out successful');
          return done();
        });
    });
  });

  describe('PATCH /users/:id', () => {
    it('can receive a PATCH to /users/:id to edit an objective', (done) => {
      createNewUser(exampleUser)
        .then(() => {
          request(app)
            .patch('/users/1')
            .send({ firstName: 'boo' })<commit_msg>corrected error in test description<commit_after>const app = require('../../app');
const request = require('supertest');
const {
  resetUsersTable,
  exampleUser,
} = require('../helpers');
const { createNewUser } = require('../../server/models/helpers');
const httpServer = require('http').createServer(app);

before(() => {
  httpServer.listen('3003');
});

beforeEach((done) => {
  resetUsersTable(done);
});

after(() => {
  httpServer.close();
});

describe('------ USERS ENDPOINTS: ------', () => {
  describe('POST /users/register', () => {
    it('can receive a POST to /users/register to create a user', (done) => {
      request(app)
        .post('/users/register')
        .send(exampleUser)
        .end((err, res) => {
          if (err) {
            return done(err);
          }
          res.statusCode.should.equal(200);
          res.body.should.have.keys(
            'id',
            'firstName',
            'lastName',
            'emailAddress',
            'password' // eslint-disable-line comma-dangle
          );
          return done();
        });
    });
    it('should not create a new user if the user with the same email already exists', (done) => {
      createNewUser(exampleUser)
        .then(() => {
          request(app)
            .post('/users/register')
            .send(exampleUser)
            .end((err, response) => {
              if (err) {
                return done(err);
              }
              response.statusCode.should.equal(200);
              response.text.should.equal(`A user with the email: ${ exampleUser.emailAddress } already exists`);
              return done();
            });
        })
        .catch(error => done(error));
    });
    it('should send 400 error if data is missing from request', (done) => {
      request(app)
        .post('/users/register')
        .send({
          firstName: '',
          lastName: 'boo',
          emailAddress: '<EMAIL>',
          emailAddressConfirmation: '<EMAIL>',
          password: 'password',
          passwordConfirmation: 'password',
        })
        .end((err, res) => {
          if (err) {
            return done(err);
          }
          res.statusCode.should.equal(400);
          res.body[0].msg.should.equal('First name is required');
          return done();
        });
    });
    it('should send 400 error if email is invalid', (done) => {
      request(app)
        .post('/users/register')
        .send({
          firstName: 'foo',
          lastName: 'boo',
          emailAddress: 'email.com',
          emailAddressConfirmation: 'email.com',
          password: 'password',
          passwordConfirmation: 'password',
        })
        .end((err, res) => {
          if (err) {
            return done(err);
          }
          res.statusCode.should.equal(400);
          res.body[0].msg.should.equal('Email is not valid');
          return done();
        });
    });
    it('should send 400 error if passwords do not match', (done) => {
      request(app)
        .post('/users/register')
        .send({
          firstName: 'foo',
          lastName: 'boo',
          emailAddress: '<EMAIL>',
          emailAddressConfirmation: '<EMAIL>',
          password: 'password',
          passwordConfirmation: '<PASSWORD>',
        })
        .end((err, res) => {
          if (err) {
            return done(err);
          }
          res.statusCode.should.equal(400);
          res.body[0].msg.should.equal('Passwords do not match');
          return done();
        });
    });
    it('should send 400 error if emails do not match', (done) => {
      request(app)
        .post('/users/register')
        .send({
          firstName: 'foo',
          lastName: 'boo',
          emailAddress: '<EMAIL>',
          emailAddressConfirmation: '<EMAIL>',
          password: 'password',
          passwordConfirmation: 'password',
        })
        .end((err, res) => {
          if (err) {
            return done(err);
          }
          res.statusCode.should.equal(400);
          res.body[0].msg.should.equal('Emails do not match');
          return done();
        });
    });
  });

  describe('POST /users/login', () => {
    it('should send a 200 with the user information if login is successful', (done) => {
      createNewUser(exampleUser)
        .then(() => {
          request(app)
            .post('/users/login')
            .send({
              username: exampleUser.emailAddress,
              password: <PASSWORD>,
            })
            .end((err, res) => {
              if (err) {
                return done(err);
              }
              res.statusCode.should.equal(200);
              res.body.should.have.keys(
                'id',
                'firstName',
                'lastName',
                'emailAddress',
                'password',
                'createdAt',
                'updatedAt' // eslint-disable-line comma-dangle
              );
              return done();
            });
        }).catch(error => done(error));
    });
    it('should send a 401 with an error message if user is not found', (done) => {
      request(app)
        .post('/users/login')
        .send({
          username: exampleUser.emailAddress,
          password: <PASSWORD>,
        })
        .end((err, res) => {
          if (err) {
            return done(err);
          }
          res.statusCode.should.equal(401);
          res.body.message.should.equal('A user by that email address does not exist.');
          return done();
        });
    });
    it('should send a 401 with an error message for an incorrectPassword', (done) => {
      createNewUser(exampleUser)
        .then(() => {
          request(app)
            .post('/users/login')
            .send({
              username: exampleUser.emailAddress,
              password: '<PASSWORD>',
            })
            .end((err, res) => {
              if (err) {
                return done(err);
              }
              res.statusCode.should.equal(401);
              res.body.message.should.equal('Incorrect password for the email address provided.');
              return done();
            });
        }).catch(error => done(error));
    });
  });

  describe('POST /users/login', () => {
    it('should log the user out and send a 200 with a message', (done) => {
      request(app)
        .get('/users/logout')
        .end((err, res) => {
          if (err) {
            return done(err);
          }
          res.statusCode.should.equal(200);
          res.text.should.equal('Log out successful');
          return done();
        });
    });
  });

  describe('PATCH /users/:id', () => {
    it('can receive a PATCH to /users/:id to edit a users information', (done) => {
      createNewUser(exampleUser)
        .then(() => {
          request(app)
            .patch('/users/1')
            .send({ firstName: 'boo' })
{'fraction_non_alphanumeric': 0.15522863975650408, 'fraction_numerical': 0.009131096456264946, 'mean_word_length': 1.5593471810089021, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 95, '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': '5420823', 'n_tokens_mistral': 3866, 'n_tokens_neox': 3765, 'n_words': 847}