<commit_before>/* eslint no-unused-expressions: 0 */
import chaiHTTP from 'chai-http';
import sinon from 'sinon';
import chai from 'chai';
import models from '../../models';
import server from '../../index';
import dummyData from '../dummy.json';

chai.use(chaiHTTP);
let token, token2;
const expect = chai.expect;
const {
  validGroup,
  anotherValidGroup,
  twitter,
  emptyName,
  invalidName
} = dummyData.Groups;

describe('/api/group', () => {
  const user = dummyData.Users.thirdValidUser;
  before(() => {
    return models.sequelize.truncate({ cascade: true })
    .then(() => {
      return models.User.create(user)
      .then(() => {
        return chai.request(server)
        .post('/api/user/signin')
        .send({
          username: user.username,
          password: <PASSWORD>
        })
        .then((res) => {
          expect(res).to.have.status(200);
          expect(res).to.be.json;
          token = res.body.token;
        })
        .catch((err) => {
          throw err;
        });
      });
    });
  });
  it('should take an id, name and description and create a group',
  (done) => {
    chai.request(server)
    .post('/api/group')
    .set('Authorization', `Bearer ${token}`)
    .send(validGroup)
    .end((err, res) => {
      expect(res).to.have.status(201);
      expect(res).to.be.json;
      expect(res.body.group.id).to.equal(validGroup.id);
      expect(res.body.group.name).to.equal(validGroup.name);
      expect(res.body.group.description).to.equal(validGroup.description);
      expect(res.body.group).to.have.own.property('createdAt');
      return done();
    });
  });
  it('should define id if not passed in request', (done) => {
    chai.request(server)
    .post('/api/group')
    .set('Authorization', `Bearer ${token}`)
    .send(twitter)
    .end((err, res) => {
      expect(res).to.have.status(201);
      expect(res).to.be.json;
      expect(res.body.group.id).to.exist;
      expect(res.body.group.name).to.equal(twitter.name);
      expect(res.body.group.description).to.equal(twitter.description);
      return done();
    });
  });
  it('should return validation error if id exists', (done) => {
    chai.request(server)
    .post('/api/group')
    .set('Authorization', `Bearer ${token}`)
    .send({
      id: validGroup.id,
      name: 'newGroup'
    })
    .end((err, res) => {
      expect(res).to.have.status(400);
      expect(res).to.be.json;
      expect(res.body.error).to.equal('id already exists!');
      return done();
    });
  });
  it('should return validation error if name exists', (done) => {
    chai.request(server)
    .post('/api/group')
    .set('Authorization', `Bearer ${token}`)
    .send({
      id: anotherValidGroup.id,
      name: validGroup.name
    })
    .end((err, res) => {
      expect(res).to.have.status(400);
      expect(res).to.be.json;
      expect(res.body.error).to
      .equal('You have an existing group Nations_pledge');
      return done();
    });
  });
  it('should return validation error if name is empty', (done) => {
    chai.request(server)
    .post('/api/group')
    .set('Authorization', `Bearer ${token}`)
    .send(emptyName)
    .end((err, res) => {
      expect(res).to.have.status(400);
      expect(res).to.be.json;
      expect(res.body.error).to.have
      .string('Group name cannot be an empty string');
      expect(res.body.error).to.have
      .string('Name can contain only letters, numbers and underscores');
      return done();
    });
  });
  it('should return validation error if name is not alphanumeric',
  (done) => {
    chai.request(server)
    .post('/api/group')
    .set('Authorization', `Bearer ${token}`)
    .send(invalidName)
    .end((err, res) => {
      expect(res).to.have.status(400);
      expect(res).to.be.json;
      expect(res.body.error).to.have
      .string('Name can contain only letters, numbers and underscores');
      return done();
    });
  });
  it('should return error if `create group` fails', (done) => {
    const stub = sinon.stub(models.Group, 'create');
    stub.rejects();
    chai.request(server)
    .post('/api/group')
    .set('Authorization', `Bearer ${token}`)
    .send({
      name: 'errorOut',
      description: 'Group cannot be created'
    })
    .end((err, res) => {
      stub.restore();
      expect(res).to.have.status(500);
      expect(res).to.be.json;
      expect(res.body.error).to.equal('Exception 500! Operation failed.');
      return done();
    });
  });
});

describe('/api/group/:groupId/user', () => {
  const userToBeAdded = dummyData.Users.anotherValidUser;
  const anotherAuthUser = dummyData.Users.validUser;
  before(() => {
    return models.User.bulkCreate([
      userToBeAdded,
      anotherAuthUser
    ], {
      validate: true,
      individualHooks: true
    })
    .then(() => {
      return chai.request(server)
      .post('/api/user/signin')
      .send({
        username: anotherAuthUser.username,
        password: <PASSWORD>
      })
      .then((res) => {
        expect(res).to.have.status(200);
        expect(res).to.be.json;
        token2 = res.body.token;
      })
      .catch((err) => {
        throw err;
      });
    });
  });
  it('should add a user to a group', (done) => {
    chai.request(server)
    .post(`/api/group/${validGroup.id}/user`)
    .set('Authorization', `Bearer ${token}`)
    .send({
      username: userToBeAdded.username
    })
    .end((err, res) => {
      expect(res).to.have.status(200);
      return done();
    });
  });
  it('should return error if `add user` fails', (done) => {
    const stub = sinon.stub(models.Group.prototype, 'addUser');
    stub.rejects();
    chai.request(server)
    .post(`/api/group/${validGroup.id}/user`)
    .set('Authorization', `Bearer ${token}`)
    .send({
      username: userToBeAdded.username<commit_msg>[test] use mock to test error handling<commit_after>/* eslint no-unused-expressions: 0 */
import chaiHTTP from 'chai-http';
import sinon from 'sinon';
import chai from 'chai';
import models from '../../models';
import server from '../../index';
import dummyData from '../dummy.json';
import AdhocModelService from '../../services/AdhocModelService';

chai.use(chaiHTTP);
let token, token2;
const expect = chai.expect;
const {
  validGroup,
  anotherValidGroup,
  twitter,
  emptyName,
  invalidName
} = dummyData.Groups;

describe('/api/group', () => {
  const user = dummyData.Users.thirdValidUser;
  before(() => {
    return models.sequelize.truncate({ cascade: true })
    .then(() => {
      return models.User.create(user)
      .then(() => {
        return chai.request(server)
        .post('/api/user/signin')
        .send({
          username: user.username,
          password: <PASSWORD>
        })
        .then((res) => {
          expect(res).to.have.status(200);
          expect(res).to.be.json;
          token = res.body.token;
        })
        .catch((err) => {
          throw err;
        });
      });
    });
  });
  it('should take an id, name and description and create a group',
  (done) => {
    chai.request(server)
    .post('/api/group')
    .set('Authorization', `Bearer ${token}`)
    .send(validGroup)
    .end((err, res) => {
      expect(res).to.have.status(201);
      expect(res).to.be.json;
      expect(res.body.group.id).to.equal(validGroup.id);
      expect(res.body.group.name).to.equal(validGroup.name);
      expect(res.body.group.description).to.equal(validGroup.description);
      expect(res.body.group).to.have.own.property('createdAt');
      return done();
    });
  });
  it('should define id if not passed in request', (done) => {
    chai.request(server)
    .post('/api/group')
    .set('Authorization', `Bearer ${token}`)
    .send(twitter)
    .end((err, res) => {
      expect(res).to.have.status(201);
      expect(res).to.be.json;
      expect(res.body.group.id).to.exist;
      expect(res.body.group.name).to.equal(twitter.name);
      expect(res.body.group.description).to.equal(twitter.description);
      return done();
    });
  });
  it('should return validation error if id exists', (done) => {
    chai.request(server)
    .post('/api/group')
    .set('Authorization', `Bearer ${token}`)
    .send({
      id: validGroup.id,
      name: 'newGroup'
    })
    .end((err, res) => {
      expect(res).to.have.status(400);
      expect(res).to.be.json;
      expect(res.body.error).to.equal('id already exists!');
      return done();
    });
  });
  it('should return validation error if name exists', (done) => {
    chai.request(server)
    .post('/api/group')
    .set('Authorization', `Bearer ${token}`)
    .send({
      id: anotherValidGroup.id,
      name: validGroup.name
    })
    .end((err, res) => {
      expect(res).to.have.status(400);
      expect(res).to.be.json;
      expect(res.body.error).to
      .equal('You have an existing group Nations_pledge');
      return done();
    });
  });
  it('should return validation error if name is empty', (done) => {
    chai.request(server)
    .post('/api/group')
    .set('Authorization', `Bearer ${token}`)
    .send(emptyName)
    .end((err, res) => {
      expect(res).to.have.status(400);
      expect(res).to.be.json;
      expect(res.body.error).to.have
      .string('Group name cannot be an empty string');
      expect(res.body.error).to.have
      .string('Name can contain only letters, numbers and underscores');
      return done();
    });
  });
  it('should return validation error if name is not alphanumeric',
  (done) => {
    chai.request(server)
    .post('/api/group')
    .set('Authorization', `Bearer ${token}`)
    .send(invalidName)
    .end((err, res) => {
      expect(res).to.have.status(400);
      expect(res).to.be.json;
      expect(res.body.error).to.have
      .string('Name can contain only letters, numbers and underscores');
      return done();
    });
  });
  it('should return error if `create group` fails', (done) => {
    const stub = sinon.stub(models.Group, 'create');
    stub.rejects();
    chai.request(server)
    .post('/api/group')
    .set('Authorization', `Bearer ${token}`)
    .send({
      name: 'errorOut',
      description: 'Group cannot be created'
    })
    .end((err, res) => {
      stub.restore();
      expect(res).to.have.status(500);
      expect(res).to.be.json;
      expect(res.body.error).to.equal('Exception 500! Operation failed.');
      return done();
    });
  });
});

describe('/api/group/:groupId/user', () => {
  const userToBeAdded = dummyData.Users.anotherValidUser;
  const anotherAuthUser = dummyData.Users.validUser;
  before(() => {
    return models.User.bulkCreate([
      userToBeAdded,
      anotherAuthUser
    ], {
      validate: true,
      individualHooks: true
    })
    .then(() => {
      return chai.request(server)
      .post('/api/user/signin')
      .send({
        username: anotherAuthUser.username,
        password: <PASSWORD>
      })
      .then((res) => {
        expect(res).to.have.status(200);
        expect(res).to.be.json;
        token2 = res.body.token;
      })
      .catch((err) => {
        throw err;
      });
    });
  });
  it('should add a user to a group', (done) => {
    chai.request(server)
    .post(`/api/group/${validGroup.id}/user`)
    .set('Authorization', `Bearer ${token}`)
    .send({
      username: userToBeAdded.username
    })
    .end((err, res) => {
      expect(res).to.have.status(200);
      return done();
    });
  });
  it('should return error if `add user` fails', (done) => {
    const stub = sinon.stub(AdhocModelService, 'returnModelInstance');
    stub.rejects();
    chai.request(server)
    .post(`/api/group/${validGroup.id}/user`)
    .set('Authorization', `Bearer ${token}`)
    .send({
      username: userToBeAdded.username
{'fraction_non_alphanumeric': 0.18180269211109218, 'fraction_numerical': 0.006133924007497018, 'mean_word_length': 2.5746041412911085, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 63, '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': '660132', 'n_tokens_mistral': 3889, 'n_tokens_neox': 3843, 'n_words': 737}