<commit_before>const controller = require('../../../model/user/user-controller');

describe('User Controller', () => {
  let r;
  const resStub = {
    status: () => ({ json: (obj) => Promise.resolve(obj) }),
  };
  it('catches error on findByEmail', async () => {
    controller.model.findOne = jest.fn(() => Promise.reject(new Error('bad')));
    r = await controller.findByEmail({ body: { email: '' } }, resStub);
    expect(r.message).toBe('bad');
  });
  it('returns 400 on findByEmail', async () => {
    controller.model.findOne = jest.fn(() => Promise.resolve());
    r = await controller.findByEmail({ body: { email: '' } }, resStub);
    expect(r.message).toBe('wrong email');
  });
  it('returns 400 on pswdreset', async () => {
    r = await controller.pswdreset({ body: { email: '' } }, resStub);
    expect(r.message).toBe('Password is not min 8 characters');
  });
  it('returns 400 on pswdreset with weak password', async () => {
    r = await controller.pswdreset({ body: { password: '<PASSWORD>', email: '' } }, resStub);
    expect(r.message).toBe('Password is not min 8 characters');
  });
  it('returns 500 on pswdreset with failure to encrypt', async () => {
    controller.model.encryptPswd = jest.fn(() => Promise.reject(new Error('bad')));
    r = await controller.pswdreset({ body: { password: '<PASSWORD>', email: '' } }, resStub);
    expect(r.message).toBe('bad');
  });
  it('returns 200 on pswdreset', async () => {
    controller.model.encryptPswd = jest.fn(() => Promise.resolve('encryptedpasswordhere'));
    controller.authFindOneAndUpdate = jest.fn(() => Promise.resolve(true));
    r = await controller.pswdreset({ body: { password: '<PASSWORD>', email: '' } }, resStub);
    expect(r).toBe(true);
  });
  it('returns 500 on resetpswd', async () => {
    controller.model.findOneAndUpdate = jest.fn(() => Promise.reject(new Error('bad')));
    r = await controller.resetpswd({ body: { password: '<PASSWORD>', email: '' } }, resStub);
    expect(r.message).toBe('bad');
  });
  it('catches error on findOne when validateChangeEmail', async () => {
    controller.model.findOne = jest.fn(() => Promise.reject(new Error('bad')));
    try { await controller.validateChangeEmail({ body: { changeemail: '' } }); } catch (e) { expect(e.message).toBe('bad'); }
  });
  it('catches error on validateChangeEmail when changeemail', async () => {
    controller.validateChangeEmail = jest.fn(() => Promise.reject(new Error('bad')));
    r = await controller.changeemail({ body: { changeemail: '<EMAIL>' } }, resStub);
    expect(r.message).toBe('bad');
  });
  it('catches error on findOneAndUpdate when changeemail', async () => {
    controller.validateChangeEmail = jest.fn(() => Promise.resolve(true));
    controller.model.findOneAndUpdate = jest.fn(() => Promise.reject(new Error('bad')));
    r = await controller.changeemail({ body: { changeemail: '<EMAIL>' } }, resStub);
    expect(r.message).toBe('bad');
  });
});<commit_msg>tests<commit_after>const controller = require('../../../model/user/user-controller');

describe('User Controller', () => {
  let r;
  const resStub = {
    status: () => ({ json: (obj) => Promise.resolve(obj) }),
  };
  it('catches error on findByEmail', async () => {
    controller.model.findOne = jest.fn(() => Promise.reject(new Error('bad')));
    r = await controller.findByEmail({ body: { email: '' } }, resStub);
    expect(r.message).toBe('bad');
  });
  it('returns 400 on findByEmail', async () => {
    controller.model.findOne = jest.fn(() => Promise.resolve());
    r = await controller.findByEmail({ body: { email: '' } }, resStub);
    expect(r.message).toBe('wrong email');
  });
  it('returns 400 on pswdreset', async () => {
    r = await controller.pswdreset({ body: { email: '' } }, resStub);
    expect(r.message).toBe('Password is not min 8 characters');
  });
  it('returns 400 on pswdreset with weak password', async () => {
    r = await controller.pswdreset({ body: { password: '<PASSWORD>', email: '' } }, resStub);
    expect(r.message).toBe('Password is not min 8 characters');
  });
  it('returns 500 on pswdreset with failure to encrypt', async () => {
    controller.model.encryptPswd = jest.fn(() => Promise.reject(new Error('bad')));
    r = await controller.pswdreset({ body: { password: '<PASSWORD>', email: '' } }, resStub);
    expect(r.message).toBe('bad');
  });
  it('returns 200 on pswdreset', async () => {
    controller.model.encryptPswd = jest.fn(() => Promise.resolve('encryptedpasswordhere'));
    controller.authFindOneAndUpdate = jest.fn(() => Promise.resolve(true));
    r = await controller.pswdreset({ body: { password: '<PASSWORD>', email: '' } }, resStub);
    expect(r).toBe(true);
  });
  it('returns 500 on resetpswd', async () => {
    controller.model.findOneAndUpdate = jest.fn(() => Promise.reject(new Error('bad')));
    r = await controller.resetpswd({ body: { password: '<PASSWORD>', email: '' } }, resStub);
    expect(r.message).toBe('bad');
  });
  it('catches error on findOne when validateChangeEmail', async () => {
    controller.model.findOne = jest.fn(() => Promise.reject(new Error('bad')));
    try { await controller.validateChangeEmail({ body: { changeemail: '' } }); } catch (e) { expect(e.message).toBe('bad'); }
  });
  it('catches error on validateChangeEmail when changeemail', async () => {
    controller.validateChangeEmail = jest.fn(() => Promise.reject(new Error('bad')));
    r = await controller.changeemail({ body: { changeemail: '<EMAIL>' } }, resStub);
    expect(r.message).toBe('bad');
  });
  it('catches error on findOneAndUpdate when changeemail', async () => {
    controller.validateChangeEmail = jest.fn(() => Promise.resolve(true));
    controller.model.findOneAndUpdate = jest.fn(() => Promise.reject(new Error('bad')));
    r = await controller.changeemail({ body: { changeemail: '<EMAIL>' } }, resStub);
    expect(r.message).toBe('bad');
  });
  it('return 401 on finishLogin', async () => {
    r = await controller.finishLogin(resStub, false, {});
    expect(r.message).toBe('Wrong password');
  });
  it('return 500 on finishLogin', async () => {
    controller.model.findByIdAndUpdate = jest.fn(() => Promise.reject(new Error('bad')));
    r = await controller.finishLogin(resStub, true, {});
    expect(r.message).toBe('bad');
  });
});
{'fraction_non_alphanumeric': 0.20190174326465926, 'fraction_numerical': 0.007290015847860539, 'mean_word_length': 4.156045751633987, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 64, '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': '2750654', 'n_tokens_mistral': 1955, 'n_tokens_neox': 1934, 'n_words': 433}