<commit_before>var generateCrumb = require("../crumb"),
  Code = require('code'),
  Lab = require('lab'),
  lab = exports.lab = Lab.script(),
  describe = lab.experiment,
  before = lab.before,
  after = lab.after,
  beforeEach = lab.beforeEach,
  afterEach = lab.afterEach,
  it = lab.test,
  expect = Code.expect,
  nock = require("nock"),
  redis = require("../../../adapters/redis-sessions"),
  server,
  users = require('../../fixtures').users;

before(function(done) {
  require('../../mocks/server')(function(obj) {
    server = obj;
    done();
  });
});

after(function(done) {
  redis.flushdb();
  server.stop(done);
});

describe('Getting to the login page', function() {
  it('renders the login page if you are not already logged in', function(done) {
    var options = {
      url: '/login'
    };

    server.inject(options, function(resp) {
      expect(resp.statusCode).to.equal(200);
      var source = resp.request.response.source;
      expect(source.template).to.equal('user/login');
      done();
    });
  });

  it('redirects already authenticated users to their profile', function(done) {
    var mock = nock("https://user-api-example.com")
      .get("/user/bob")
      .reply(200, users.bob);

    var licenseMock = nock("https://license-api-example.com")
      .get("/customer/bob/stripe")
      .reply(404);

    var options = {
      url: '/login',
      credentials: users.bob
    };

    server.inject(options, function(resp) {
      mock.done();
      licenseMock.done();
      expect(resp.statusCode).to.equal(302);
      expect(resp.headers.location).to.equal('/~bob');
      done();
    });
  });

  it('renders an error if the cookie crumb is missing', function(done) {
    var options = {
      url: '/login',
      method: 'POST',
      payload: {}
    };

    server.inject(options, function(resp) {
      expect(resp.statusCode).to.equal(403);
      done();
    });
  });

  it('renders an error if one of the login fields is empty', function(done) {
    generateCrumb(server, function(crumb) {
      var options = {
        url: '/login',
        method: 'POST',
        payload: {
          crumb: crumb,
        },
        headers: {
          cookie: 'crumb=' + crumb
        }
      };

      server.inject(options, function(resp) {
        expect(resp.statusCode).to.equal(400);
        var source = resp.request.response.source;
        expect(source.template).to.equal('user/login');
        expect(source.context.error).to.contain({
          type: 'missing'
        });
        done();
      });
    });
  });

  it('renders an error if the username or password is incorrect', function(done) {

    var mock = nock("https://user-api-example.com")
      .post("/user/fakeboom/login", {<commit_msg>Correct test for actual displayable error<commit_after>var generateCrumb = require("../crumb"),
  Code = require('code'),
  Lab = require('lab'),
  lab = exports.lab = Lab.script(),
  describe = lab.experiment,
  before = lab.before,
  after = lab.after,
  beforeEach = lab.beforeEach,
  afterEach = lab.afterEach,
  it = lab.test,
  expect = Code.expect,
  nock = require("nock"),
  redis = require("../../../adapters/redis-sessions"),
  server,
  users = require('../../fixtures').users;

before(function(done) {
  require('../../mocks/server')(function(obj) {
    server = obj;
    done();
  });
});

after(function(done) {
  redis.flushdb();
  server.stop(done);
});

describe('Getting to the login page', function() {
  it('renders the login page if you are not already logged in', function(done) {
    var options = {
      url: '/login'
    };

    server.inject(options, function(resp) {
      expect(resp.statusCode).to.equal(200);
      var source = resp.request.response.source;
      expect(source.template).to.equal('user/login');
      done();
    });
  });

  it('redirects already authenticated users to their profile', function(done) {
    var mock = nock("https://user-api-example.com")
      .get("/user/bob")
      .reply(200, users.bob);

    var licenseMock = nock("https://license-api-example.com")
      .get("/customer/bob/stripe")
      .reply(404);

    var options = {
      url: '/login',
      credentials: users.bob
    };

    server.inject(options, function(resp) {
      mock.done();
      licenseMock.done();
      expect(resp.statusCode).to.equal(302);
      expect(resp.headers.location).to.equal('/~bob');
      done();
    });
  });

  it('renders an error if the cookie crumb is missing', function(done) {
    var options = {
      url: '/login',
      method: 'POST',
      payload: {}
    };

    server.inject(options, function(resp) {
      expect(resp.statusCode).to.equal(403);
      done();
    });
  });

  it('renders an error if one of the login fields is empty', function(done) {
    generateCrumb(server, function(crumb) {
      var options = {
        url: '/login',
        method: 'POST',
        payload: {
          crumb: crumb,
        },
        headers: {
          cookie: 'crumb=' + crumb
        }
      };

      server.inject(options, function(resp) {
        expect(resp.statusCode).to.equal(400);
        var source = resp.request.response.source;
        expect(source.template).to.equal('user/login');
        expect(source.context.error).to.equal("Username and password are required");
        done();
      });
    });
  });

  it('renders an error if the username or password is incorrect', function(done) {

    var mock = nock("https://user-api-example.com")
      .post("/user/fakeboom/login", {
{'fraction_non_alphanumeric': 0.1705637121624071, 'fraction_numerical': 0.0065252854812398045, 'mean_word_length': 2.5577047066408767, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 6, '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': '2460348', 'n_tokens_mistral': 1771, 'n_tokens_neox': 1707, 'n_words': 357}