<commit_before>var chai = require('chai');
var chaiHttp = require('chai-http');
var should = chai.should();

var config = require('config');
var Promise = require('bluebird');
var crypto = require('crypto');

var umpack = require('./helpers/umpack');
var utils = require('./helpers/utils');
var mongoose = require('mongoose');
var ObjectId = require('mongodb').ObjectID;

var usersCollection = 'users';
var rolesCollection = 'roleactions';
var username = 'test';
var password = '<PASSWORD>';

chai.use(chaiHttp);
global.Promise = Promise;

describe('service API', function() {

  var app = require('./helpers/app');

  before(function() {

    return new Promise(function(resolve, reject) {

        resolve(mongoose.connection.db.dropCollection(
          rolesCollection));

      })
      .then(function() {

        return mongoose.connection.collection(rolesCollection).insert({
          "name": "user",
          "actions": [{
            "_id": new ObjectId("58a301b880a92f3930ebfef4"),
            "pattern": "/um/*",
            "name": "um",
            "verbDelete": true,
            "verbPost": true,
            "verbPut": true,
            "verbGet": true
          }],
          "__v": 0
        });

      });

  });

  beforeEach(function() {

    return mongoose.connection.db.collection(usersCollection).remove();

  });

});

function saveRecordWithParameters(metadata, isActivated, roles) {
  if (isActivated === null || isActivated === undefined) isActivated = true;

  if (!roles) roles = ['user'];

  return mongoose.connection.collection(usersCollection).insert({
    metaData: metadata,
    userName: username,
    password: utils.passwordHash(password),
    email: "<EMAIL>",
    isActivated: isActivated,
    roles: roles,
    '__v': 0
  });
}<commit_msg>cover login<commit_after>var chai = require('chai');
var chaiHttp = require('chai-http');
var should = chai.should();

var config = require('config');
var Promise = require('bluebird');
var crypto = require('crypto');

var umpack = require('./helpers/umpack');
var utils = require('./helpers/utils');
var mongoose = require('mongoose');
var ObjectId = require('mongodb').ObjectID;

var usersCollection = 'users';
var rolesCollection = 'roleactions';
var username = 'test';
var password = '<PASSWORD>';

chai.use(chaiHttp);
global.Promise = Promise;

describe('service API', function() {

  var app = require('./helpers/app');

  before(function() {

    return new Promise(function(resolve, reject) {

        resolve(mongoose.connection.db.dropCollection(
          rolesCollection));

      })
      .then(function() {

        return mongoose.connection.collection(rolesCollection).insert({
          "name": "user",
          "actions": [{
            "_id": new ObjectId("58a301b880a92f3930ebfef4"),
            "pattern": "/um/*",
            "name": "um",
            "verbDelete": true,
            "verbPost": true,
            "verbPut": true,
            "verbGet": true
          }],
          "__v": 0
        });

      });

  });

  beforeEach(function() {

    return mongoose.connection.db.collection(usersCollection).remove();

  });

  describe('POST /login', function () {

    it('should return token', function () {
      return utils.saveRecordWithParameters()
        .then(utils.login)
        .then(function (res) {
          res.should.have.status(200);

          should.exist(res.text);
        });
    });

    it('should return USER_NOT_EXISTS when no user by that username', function () {
      var promise = utils.login();

      return utils.shouldBeBadRequest(promise, 605);
    });

    it('should return USER_NOT_ACTIVE when user is not activated', function () {
      var promise = utils.saveRecordWithParameters({}, false)
        .then(utils.login);

      return utils.shouldBeBadRequest(promise, 601);
    });

    it('should return WRONG_USER_CREDENTIALS on incorrect password', function () {
      var promise =  mongoose.connection.db.collection(usersCollection)
        .insert({
          userName: username,
          password: utils.passwordHash('<PASSWORD>'),
          isActivated: true,
          roles: ['user']
        })
        .then(utils.login);

      return utils.shouldBeBadRequest(promise, 603);
    });
  });

});
{'fraction_non_alphanumeric': 0.15301572400844873, 'fraction_numerical': 0.010560901196902136, 'mean_word_length': 2.8640072529465095, 'pattern_counts': {'":': 20, '<': 7, '<?xml version=': 0, '>': 7, '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': '2588360', 'n_tokens_mistral': 1274, 'n_tokens_neox': 1240, 'n_words': 270}