<commit_before>import expect from 'expect';
import request from 'supertest';
import app from '../app';
import models from '../models';
import { doBeforeAll, doBeforeEach, populateUsers } from './seeders/testHooks';
import { seedUsers, seedGroups, generateAuth, tokens } from './seeders/seed';

describe('Middleware functions:', () => {
  // doBeforeAll();
  describe('isValidUsername Middleware', () => {
    it('does not allow a user signup without a username', (done) => {
      request(app)
      .post('/api/user/signup')
      .send({
        email: '<EMAIL>',
        password: '<PASSWORD>'
      })
      .expect(400)
      .end((err, res) => {
        if(err) {
          return done(err);
        }
        expect(res.body.error).toBe('Username is required.');
        done();
      });
    });
    it('does not allow a user signup with invalid username', (done) => {
      request(app)
      .post('/api/user/signup')
      .send({
        username: 'user0.3',
        email: '<EMAIL>',
        password: '<PASSWORD>'
      })
      .expect(400)
      .end((err, res) => {
        if(err) {
          return done(err);
        }
        expect(res.body.error).toBe('Invalid Username format.');
        done();
      });
    });
  });

  describe('isTaken Middleware', () => {
    it('does not allow users signup without email', (done) => {
      request(app)
      .post('/api/user/signup')
      .send({
        username: 'user0',
        password: '<PASSWORD>'
      })
      .expect(400)
      .end((err, res) => {
        if(err) {
          return done(err);
        }
        expect(res.body.error).toBe('Email is required.');
        done();
      });
    });
    it('does not allow users signup without password', (done) => {
      request(app)
      .post('/api/user/signup')
      .send({
        username: 'user0',
        email: '<EMAIL>'
      })
      .expect(400)
      .end((err, res) => {
        if(err) {
          return done(err);
        }
        expect(res.body.error).toBe('Password is required.');
        done();
      });
    });
  });

  describe('isGroupMember Middleware', () => {
    it('should not allow users access a group route without passing a group id', (done) => {
      request(app)
      .get(`/api/group/ /users`)
      .set('x-auth', tokens[2])
      .expect(400)
      .end((err, res) => {
        if (err) {
          return done(err);
        }
        expect(res.body.error).toBe('GroupId must be provided');
        done();
      });
    });
    it('should not allow users access a group route with a non-existent group id', (done) => {
      request(app)
      .get(`/api/group/56/users`)
      .set('x-auth', tokens[2])
      .expect(404)
      .end((err, res) => {
        if (err) {
          return done(err);
        }
        expect(res.body.error).toBe('Specified group does not exist');
        done();
      });
    });
    it('should not allow users access a group they do not belong to', (done) => {
      request(app)
      .get(`/api/group/${seedGroups[1].id}/users`)
      .set('x-auth', tokens[1])
      .expect(401)
      .end((err, res) => {
        expect(res.body.error).toBe('You must belong to a group to interact with it');
        done();
      })
      });
    });
  });

  describe('Authenticate Middleware: ', () => {
    it('POST /api/signup route should be accessible to unauthenticated users', (done) => {
      request(app)
      .post('/api/user/signup')
      .send({
        username: 'user1',
        password: '<PASSWORD>',
        email: '<EMAIL>'
      })
      .expect(201)
      .end((err, res) => {
        if (err) {
          return done(err);
        }
        expect(res.body.user.id).toExist;
        expect(res.body.user.username).toBe('user1');
        expect(res.body.user.email).toBe('<EMAIL>');
        done();
      });
    });
    it('POST /api/user/me route should not be accessible to unauthenticated users', (done) => {
      request(app)
      .get('/api/user/me')
      .expect(401)
      .end((err, res) => {
        if (err) {
          return done(err);
        }
        expect(res.body.id).toNotExist;
        expect(res.body.error).toBe('You need to signup or login first');
        done();
      });
    });
    it('POST /api/user/me route should not be accessible to users with incorrect token', (done) => {
      const token = generateAuth(99);
      request(app)
      .get('/api/user/me')
      .set('x-auth', token)
      .expect(401)
      .end((err, res) => {
        if (err) {
          return done(err);
        }
        expect(res.body.id).toNotExist;
        expect(res.body.error).toBe('User could not be verifed. Signup or login first');
        done();
      })
    });
    it('POST /api/user/me route should not be accessible to users with invalid token', (done) => {
      const token = '<PASSWORD>';
      request(app)
      .get('/api/user/me')
      .set('x-auth', token)
      .expect(401)
      .end((err, res) => {
        if (err) {
          return done(err);
        }
        expect(res.body.id).toNotExist;
        done();
      })
    });
  });
<commit_msg>Fix test failing on Travis<commit_after>import expect from 'expect';
import request from 'supertest';
import app from '../app';
import models from '../models';
import { doBeforeAll, doBeforeEach, populateUsers } from './seeders/testHooks';
import { seedUsers, seedGroups, generateAuth, tokens } from './seeders/seed';

describe('Middleware functions:', () => {
  // doBeforeAll();
  describe('isValidUsername Middleware', () => {
    it('does not allow a user signup without a username', (done) => {
      request(app)
      .post('/api/user/signup')
      .send({
        email: '<EMAIL>',
        password: '<PASSWORD>'
      })
      .expect(400)
      .end((err, res) => {
        if(err) {
          return done(err);
        }
        expect(res.body.error).toBe('Username is required.');
        done();
      });
    });
    it('does not allow a user signup with invalid username', (done) => {
      request(app)
      .post('/api/user/signup')
      .send({
        username: 'user0.3',
        email: '<EMAIL>',
        password: '<PASSWORD>'
      })
      .expect(400)
      .end((err, res) => {
        if(err) {
          return done(err);
        }
        expect(res.body.error).toBe('Invalid Username format.');
        done();
      });
    });
  });

  describe('isTaken Middleware', () => {
    it('does not allow users signup without email', (done) => {
      request(app)
      .post('/api/user/signup')
      .send({
        username: 'user0',
        password: '<PASSWORD>'
      })
      .expect(400)
      .end((err, res) => {
        if(err) {
          return done(err);
        }
        expect(res.body.error).toBe('Email is required.');
        done();
      });
    });
    it('does not allow users signup without password', (done) => {
      request(app)
      .post('/api/user/signup')
      .send({
        username: 'user0',
        email: '<EMAIL>'
      })
      .expect(400)
      .end((err, res) => {
        if(err) {
          return done(err);
        }
        expect(res.body.error).toBe('Password is required.');
        done();
      });
    });
  });

  describe('isGroupMember Middleware', () => {
    it('should not allow users access a group route without passing a group id', (done) => {
      request(app)
      .get(`/api/group/ /users`)
      .set('x-auth', tokens[2])
      .expect(400)
      .end((err, res) => {
        if (err) {
          return done(err);
        }
        expect(res.body.error).toBe('GroupId must be provided');
        done();
      });
    });
    it('should not allow users access a group route with a non-existent group id', (done) => {
      request(app)
      .get(`/api/group/56/users`)
      .set('x-auth', tokens[2])
      .expect(404)
      .end((err, res) => {
        if (err) {
          return done(err);
        }
        expect(res.body.error).toBe('Specified group does not exist');
        done();
      });
    });
    it('should not allow users access a group they do not belong to', (done) => {
      request(app)
      .get(`/api/group/${seedGroups[1].id}/users`)
      .set('x-auth', tokens[1])
      .expect(401)
      .end((err, res) => {
        expect(res.body.error).toBe('You must belong to a group to interact with it');
        done();
      });
    });
  });

  describe('Authenticate Middleware: ', () => {
    it('POST /api/signup route should be accessible to unauthenticated users', (done) => {
      request(app)
      .post('/api/user/signup')
      .send({
        username: 'user1',
        password: '<PASSWORD>',
        email: '<EMAIL>'
      })
      .expect(201)
      .end((err, res) => {
        if (err) {
          return done(err);
        }
        expect(res.body.user.id).toExist;
        expect(res.body.user.username).toBe('user1');
        expect(res.body.user.email).toBe('<EMAIL>');
        done();
      });
    });
    it('POST /api/user/me route should not be accessible to unauthenticated users', (done) => {
      request(app)
      .get('/api/user/me')
      .expect(401)
      .end((err, res) => {
        if (err) {
          return done(err);
        }
        expect(res.body.id).toNotExist;
        expect(res.body.error).toBe('You need to signup or login first');
        done();
      });
    });
    it('POST /api/user/me route should not be accessible to users with incorrect token', (done) => {
      const token = generateAuth(99);
      request(app)
      .get('/api/user/me')
      .set('x-auth', token)
      .expect(401)
      .end((err, res) => {
        if (err) {
          return done(err);
        }
        expect(res.body.id).toNotExist;
        expect(res.body.error).toBe('User could not be verifed. Signup or login first');
        done();
      })
    });
    it('POST /api/user/me route should not be accessible to users with invalid token', (done) => {
      const token = '123';
      request(app)
      .get('/api/user/me')
      .set('x-auth', token)
      .expect(401)
      .end((err, res) => {
        if (err) {
          return done(err);
        }
        expect(res.body.id).toNotExist;
        done();
      })
    });
  });
});
{'fraction_non_alphanumeric': 0.17716689493249854, 'fraction_numerical': 0.009489336724711406, 'mean_word_length': 1.9042613636363637, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 76, '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': '4127308', 'n_tokens_mistral': 3243, 'n_tokens_neox': 3195, 'n_words': 734}