<commit_before>'use strict';

const _        = require('lodash');
const mongoose = require('mongoose');
const request  = require('supertest');
const should   = require('should');
const nassert  = require('n-assert');
const app      = require('../../../src/app');

const User = mongoose.model('user');

describe('controllers / users', () => {
  describe('getUsers', () => {
    let initialUsers = [
      {
        _id: nassert.getObjectId(),
        name: 'user1',
        email: '<EMAIL>'
      },
      {
        _id: nassert.getObjectId(),
        name: 'user2',
        email: '<EMAIL>'
      },
      {
        _id: nassert.getObjectId(),
        name: 'user3',
        email: '<EMAIL>'
      }
    ];

    async function test({ expectedStatus, expectedBody }) {
      await User.create(initialUsers);

      return request(app)
        .get('/api/users/')
        .expect(expectedStatus)
        .expect('Content-Type', /json/)
        .expect(res => nassert.assert(_.sortBy(res.body, '_id'), _.sortBy(expectedBody, '_id')));
    }

    it('should return status 200 and list of users', () => {
      let expectedStatus = 200;
      let expectedBody = _.map(initialUsers, user => {
        let userCopy = _.cloneDeep(user);
        userCopy._id = user._id; // rewrite id after clone
        return userCopy;
      });

      return test({ expectedStatus, expectedBody });
    });
  });

  describe('getUserById', () => {
    let initialUsers = [
      {
        _id: nassert.getObjectId(),
        name: 'user1',
        email: '<EMAIL>'
      },
      {
        _id: nassert.getObjectId(),
        name: 'user2',
        email: '<EMAIL>'
      },
      {
        _id: nassert.getObjectId(),
        name: 'user3',
        email: '<EMAIL>'
      }
    ];

    async function test({ userId, expectedStatus, expectedBody }) {
      await User.create(initialUsers);

      return request(app)
        .get('/api/users/' + userId)
        .expect(expectedStatus)
        .expect('Content-Type', /json/)
        .expect(res => nassert.assert(res.body, expectedBody));
    }

    it('should return status 422 when req.params._id is invalid', () => {
      let userId = 'Invalid Id';
      let expectedStatus = 422;
      let expectedBody = {
        reason: '_id must be a valid ObjectId'
      };

      return test({ userId, expectedStatus, expectedBody });
    });

    it('should return status 404 when user is not found by req.params._id', () => {
      let userId = nassert.getObjectId();
      let expectedStatus = 404;
      let expectedBody = {
        reason: 'user is not found'
      };

      return test({ userId, expectedStatus, expectedBody });
    });

    it('should return status 200 and user', () => {
      let userId = initialUsers[0]._id;
      let expectedStatus = 200;
      let expectedBody = _.cloneDeep(initialUsers[0]);
      expectedBody._id = userId;

      return test({ userId, expectedStatus, expectedBody });
    });
  });

  describe('createUser', () => {
    let initialUsers = [
      {
        _id: nassert.getObjectId(),
        name: 'user1',
        email: '<EMAIL>'
      },
      {
        _id: nassert.getObjectId(),
        name: 'user2',
        email: '<EMAIL>'
      },
      {
        _id: nassert.getObjectId(),
        name: 'user3',
        email: '<EMAIL>'
      }
    ];

    async function test({ userData, expectedStatus, expectedBody }) {
      await User.create(initialUsers);

      return request(app)
        .post('/api/users')
        .send(userData)
        .expect(expectedStatus)
        .expect('Content-Type', /application\/json/)
        .expect(res => nassert.assert(res.body, expectedBody));
    }

    it('should return status 422 when req.body is empty', () => {
      let userData;
      let expectedStatus = 422;
      let expectedBody = {
        reason: 'name is required'
      };

      return test({ userData, expectedStatus, expectedBody });
    });

    it('should return status 422 when req.body.name is undefined', () => {
      let userData = {
        email: '<EMAIL>'
      };
      let expectedStatus = 422;
      let expectedBody = {
        reason: 'name is required'
      };

      return test({ userData, expectedStatus, expectedBody });
    });

    it('should return status 422 when req.body.email is undefined', () => {
      let userData = {
        name: 'new-user'
      };
      let expectedStatus = 422;
      let expectedBody = {
        reason: 'email is required'
      };

      return test({ userData, expectedStatus, expectedBody });
    });

    it('should return status 422 when req.body.email is not valid email', () => {
      let userData = {
        name: 'new-user',
        email: 'invalidEmail'
      };
      let expectedStatus = 422;
      let expectedBody = {
        reason: 'email must be a valid email address'
      };

      return test({ userData, expectedStatus, expectedBody });
    });

    it('should return status 200 and create a new user when req.body is valid', () => {
      let userData = {
        name: 'new-user',
        email: '<EMAIL>'
      };
      let expectedStatus = 201;
      let expectedBody = {
        _id: '_mock_',
        name: 'new-user',
        email: '<EMAIL>'
      };<commit_msg>fix UTs<commit_after>'use strict';

const _        = require('lodash');
const mongoose = require('mongoose');
const request  = require('supertest');
const should   = require('should');
const nassert  = require('n-assert');
const app      = require('../../../src/app');

const User = mongoose.model('user');

describe('controllers / users', () => {
  describe('getUsers', () => {
    let initialUsers = [
      {
        _id: nassert.getObjectId(),
        name: 'user1',
        email: '<EMAIL>'
      },
      {
        _id: nassert.getObjectId(),
        name: 'user2',
        email: '<EMAIL>'
      },
      {
        _id: nassert.getObjectId(),
        name: 'user3',
        email: '<EMAIL>'
      }
    ];

    async function test({ expectedStatus, expectedBody }) {
      await User.create(initialUsers);

      return request(app)
        .get('/api/users/')
        .expect(expectedStatus)
        .expect('Content-Type', /json/)
        .expect(res => nassert.assert(_.sortBy(res.body, 'userId'), _.sortBy(expectedBody, 'userId')));
    }

    it('should return status 200 and list of users', () => {
      let expectedStatus = 200;
      let expectedBody = _.map(initialUsers, user => {
        let userCopy = _.cloneDeep(user);
        userCopy.userId = user._id; // rewrite id after clone
        delete userCopy._id;
        return userCopy;
      });

      return test({ expectedStatus, expectedBody });
    });
  });

  describe('getUserById', () => {
    let initialUsers = [
      {
        _id: nassert.getObjectId(),
        name: 'user1',
        email: '<EMAIL>'
      },
      {
        _id: nassert.getObjectId(),
        name: 'user2',
        email: '<EMAIL>'
      },
      {
        _id: nassert.getObjectId(),
        name: 'user3',
        email: '<EMAIL>'
      }
    ];

    async function test({ userId, expectedStatus, expectedBody }) {
      await User.create(initialUsers);

      return request(app)
        .get('/api/users/' + userId)
        .expect(expectedStatus)
        .expect('Content-Type', /json/)
        .expect(res => nassert.assert(res.body, expectedBody));
    }

    it('should return status 422 when req.params._id is invalid', () => {
      let userId = 'Invalid Id';
      let expectedStatus = 422;
      let expectedBody = {
        reason: '_id must be a valid ObjectId'
      };

      return test({ userId, expectedStatus, expectedBody });
    });

    it('should return status 404 when user is not found by req.params._id', () => {
      let userId = nassert.getObjectId();
      let expectedStatus = 404;
      let expectedBody = {
        reason: 'user is not found'
      };

      return test({ userId, expectedStatus, expectedBody });
    });

    it('should return status 200 and user', () => {
      let userId = initialUsers[0]._id;
      let expectedStatus = 200;
      let expectedBody = _.cloneDeep(initialUsers[0]);
      expectedBody.userId = userId;
      delete expectedBody._id;

      return test({ userId, expectedStatus, expectedBody });
    });
  });

  describe('createUser', () => {
    let initialUsers = [
      {
        _id: nassert.getObjectId(),
        name: 'user1',
        email: '<EMAIL>'
      },
      {
        _id: nassert.getObjectId(),
        name: 'user2',
        email: '<EMAIL>'
      },
      {
        _id: nassert.getObjectId(),
        name: 'user3',
        email: '<EMAIL>'
      }
    ];

    async function test({ userData, expectedStatus, expectedBody }) {
      await User.create(initialUsers);

      return request(app)
        .post('/api/users')
        .send(userData)
        .expect(expectedStatus)
        .expect('Content-Type', /application\/json/)
        .expect(res => nassert.assert(res.body, expectedBody));
    }

    it('should return status 422 when req.body is empty', () => {
      let userData;
      let expectedStatus = 422;
      let expectedBody = {
        reason: 'name is required'
      };

      return test({ userData, expectedStatus, expectedBody });
    });

    it('should return status 422 when req.body.name is undefined', () => {
      let userData = {
        email: '<EMAIL>'
      };
      let expectedStatus = 422;
      let expectedBody = {
        reason: 'name is required'
      };

      return test({ userData, expectedStatus, expectedBody });
    });

    it('should return status 422 when req.body.email is undefined', () => {
      let userData = {
        name: 'new-user'
      };
      let expectedStatus = 422;
      let expectedBody = {
        reason: 'email is required'
      };

      return test({ userData, expectedStatus, expectedBody });
    });

    it('should return status 422 when req.body.email is not valid email', () => {
      let userData = {
        name: 'new-user',
        email: 'invalidEmail'
      };
      let expectedStatus = 422;
      let expectedBody = {
        reason: 'email must be a valid email address'
      };

      return test({ userData, expectedStatus, expectedBody });
    });

    it('should return status 200 and create a new user when req.body is valid', () => {
      let userData = {
        name: 'new-user',
        email: '<EMAIL>'
      };
      let expectedStatus = 201;
      let expectedBody = {
        userId: '_mock_',
        name: 'new-user',
        email: '<EMAIL>'
      };
{'fraction_non_alphanumeric': 0.14373755570304353, 'fraction_numerical': 0.012325779842609273, 'mean_word_length': 2.080607476635514, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 61, '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': '6948184', 'n_tokens_mistral': 3171, 'n_tokens_neox': 3040, 'n_words': 787}