<commit_before>var async = require('async');
var should = require('should');
var request = require('supertest');
var sequelize_fixtures =  require('sequelize-fixtures');

var app = require('../');
var db = require('../../db');
var expressApp = null;
var port = 3333;

var defaultPassword<PASSWORD> = "<PASSWORD>";

var users = [ {
                id: 1,
                username: "testAdmin",
                type: "admin"
              }, {
                id: 2,
                username: "testController",
                type: "controller"
              }, {
                id: 3,
                username: "testUser",
                type: "user"
              }, {
                id: 4,
                username: "killMe",
                type: "user"
              } ];


var authenticatedAdminAgent;
var authenticatedControllerAgent;
var authenticatedUserAgent;
var authenticatedDeadUserAgent;

var loginUser = function(username, cb) {
  var server = request.agent('http://localhost:' + port);
  server
    .post('/auth/login')
    .type('form')
    .send({ username: username })
    .send({ password: <PASSWORD> })
    .end(function(err, res) {
      cb(err, server);
    });
};

describe('HGOTS Web Server', function() {
  this.timeout(10000);
  
  
  beforeEach(function(done) {
    app.init(port, null, null, function() {
      app.start(function() {
        expressApp = app.getExpress();
        sequelize_fixtures.loadFixtures(require('./fixtures/test.json'), db, function() {
          async.map(["testAdmin", "testController", "testUser", "killMe"], loginUser, function(err, results) {
            if ( err ) throw err;

            authenticatedAdminAgent = results[0];
            authenticatedControllerAgent = results[1];
            authenticatedUserAgent = results[2];
            authenticatedDeadUserAgent = results[3];

            done();
          });
        });
      });
    });
  });
  
  afterEach(function(done) {
    app.stop();
    done();
  });
  
  it('should exist', function(done) {
    should.exist(expressApp);
    done();
  });
  
  it('should be listening at localhost:' + port, function(done) {
    request(expressApp)
      .get('/')
      .expect(200, done);
  });
  
  describe('GET Main /', function() {
    it('should respond with HTML on /', function(done) {
      request(expressApp)
        .get('/')
        .expect('Content-Type', /html/)
        .expect(200, done);
    });
  });
  
  describe('POST Validate Login /auth/login', function() {
    it('should redirect to login', function(done) {
      request(expressApp)
        .post('/auth/login')
        .expect(302, done);
    });
    
    it('should respond with 403 without credentials', function(done) {
      request(expressApp)
        .post('/auth/login')
        .type('form')
        .expect(302)
        .end(function(err, res) {
          res.header.location.should.include('/');
          done();
        });
    });
    
    it('should respond with 403 with wrong credentials', function(done) {
      request(expressApp)
        .post('/auth/login')
        .type('form')
        .send({ username: "failingUser" })
        .send({ password: "<PASSWORD>" })
        .expect(302)
        .end(function(err, res) {
          res.header.location.should.include('/');
          done();
        });
    });

    it('should respond with 403 with correct username but wrong pass', function(done) {
      request(expressApp)
        .post('/auth/login')
        .type('form')
        .send({ username: "testUser" })
        .send({ password: "<PASSWORD>" })
        .expect(302)
        .end(function(err, res) {
          res.header.location.should.include('/');
          done();
        });
    });
    
    it('should respond with 200 with correct credentials', function(done) {
      request(expressApp)
        .post('/auth/login')
        .type('form')
        .send({ username: "testUser" })
        .send({ password: <PASSWORD> })
        .expect(302)
        .end(function(err, res) {
          res.header.location.should.include('/app');
          done();
        });
    });
  });
  
  describe('GET App /app', function() {
    it('should redirect without credentials', function(done) {
      request(expressApp)
        .get('/app')
        .expect(302, done);
    });

    it('should stay with logged in user', function(done) {
      authenticatedUserAgent
        .get('/app')
        .expect(200, done);
    });
  });

  describe('API', function() {
    var prefix = '/api';
    describe('v1', function() {
      prefix += '/v1';
      describe('GET /users', function() {
        var url = prefix + '/users';
        
        it('should return all users to an admin', function(done) {
          authenticatedAdminAgent
            .get(url)
            .expect('Content-Type', /json/)
            .expect(200)
            .expect({users: users}, done);
        });
        
        it('should return all users to a controller', function(done) {
          authenticatedControllerAgent
            .get(url)
            .expect('Content-Type', /json/)
            .expect(200)
            .expect({users: users}, done);
        });
        
        it('should return "403: Forbidden" to a user', function(done) {
          authenticatedUserAgent
            .get(url)
            .expect(403, done);
        });
      });
      
      describe('POST /users', function() {
        var url = prefix + '/users';
        
        it('should allow an admin to create a new user', function(done) {
          authenticatedAdminAgent
            .post(url)
            .send({ username: "newTestUser1", password: <PASSWORD>, type: "user" })
            .expect('Content-Type', /json/)
            .expect(200, done);
        });
        
        it('should allow an admin to create a new controller', function(done) {
          authenticatedAdminAgent
            .post(url)
            .send({ username: "newTestController1", password: <PASSWORD>, type: "controller" })
            .expect('Content-Type', /json/)
            .expect(200, done);
        });
        
        it('should allow an admin to create a new admin', function(done) {
          authenticatedAdminAgent
            .post(url)
            .send({ username: "newTestAdmin1", password: <PASSWORD>, type: "admin" })
            .expect('Content-Type', /json/)
            .expect(200, done);
        });
        
        it('should allow a controller to create a new user', function(done) {
          authenticatedControllerAgent
            .post(url)
            .send({ username: "newTestUser2", password: <PASSWORD>, type: "user" })
            .expect('Content-Type', /json/)
            .expect(200, done);
        });
        
        it('should not allow a controller to create a new controller', function(done) {
          authenticatedControllerAgent
            .post(url)
            .send({ username: "newTestController2", password: <PASSWORD>, type: "controller" })
            .expect('Content-Type', /json/)
            .expect(403, done);
        });
        
        it('should not allow a controller to create a new admin', function(done) {
          authenticatedControllerAgent
            .post(url)
            .send({ username: "newTestAdmin2", password: <PASSWORD>, type: "admin" })
            .expect('Content-Type', /json/)
            .expect(403, done);
        });
        
        it('should not allow a normal user to create a new user', function(done) {
          authenticatedUserAgent
            .post(url)
            .send({ username: "newTestUser3", password: <PASSWORD>, type: "user" })
            .expect('Content-Type', /json/)
            .expect(403, done);
        });
        
        it('should not allow a normal user to create a new controller', function(done) {
          authenticatedUserAgent
            .post(url)
            .send({ username: "newTestController3", password: <PASSWORD>, type: "controller" })
            .expect('Content-Type', /json/)
            .expect(403, done);
        });
        
        it('should not allow a normal user to create a new admin', function(done) {
          authenticatedUserAgent
            .post(url)
            .send({ username: "newTestAdmin3", password: <PASSWORD>, type: "admin" })
            .expect('Content-Type', /json/)
            .expect(403, done);<commit_msg>Fixed tests.<commit_after>var async = require('async');
var should = require('should');
var request = require('supertest');
var sequelize_fixtures =  require('sequelize-fixtures');

var app = require('../');
var db = require('../../db');
var expressApp = null;
var port = 3333;

var defaultPassword<PASSWORD> = "<PASSWORD>";

var users = [ {
                id: 1,
                username: "testAdmin",
                type: "admin"
              }, {
                id: 2,
                username: "testController",
                type: "controller"
              }, {
                id: 3,
                username: "testUser",
                type: "user"
              }, {
                id: 4,
                username: "killMe",
                type: "user"
              } ];


var authenticatedAdminAgent;
var authenticatedControllerAgent;
var authenticatedUserAgent;
var authenticatedDeadUserAgent;

var loginUser = function(username, cb) {
  var server = request.agent('http://localhost:' + port);
  server
    .post('/auth/login')
    .type('form')
    .send({ username: username })
    .send({ password: <PASSWORD> })
    .end(function(err, res) {
      cb(err, server);
    });
};

describe('HGOTS Web Server', function() {
  this.timeout(10000);
  
  
  beforeEach(function(done) {
    app.init(port, null, null, function() {
      app.start(function() {
        expressApp = app.getExpress();
        sequelize_fixtures.loadFixtures(require('./fixtures/test.json'), db, function() {
          async.map(["testAdmin", "testController", "testUser", "killMe"], loginUser, function(err, results) {
            if ( err ) throw err;

            authenticatedAdminAgent = results[0];
            authenticatedControllerAgent = results[1];
            authenticatedUserAgent = results[2];
            authenticatedDeadUserAgent = results[3];

            done();
          });
        });
      });
    });
  });
  
  afterEach(function(done) {
    app.stop();
    done();
  });
  
  it('should exist', function(done) {
    should.exist(expressApp);
    done();
  });
  
  it('should be listening at localhost:' + port, function(done) {
    request(expressApp)
      .get('/')
      .expect(200, done);
  });
  
  describe('GET Main /', function() {
    it('should respond with HTML on /', function(done) {
      request(expressApp)
        .get('/')
        .expect('Content-Type', /html/)
        .expect(200, done);
    });
  });
  
  describe('POST Validate Login /auth/login', function() {
    it('should redirect to login', function(done) {
      request(expressApp)
        .post('/auth/login')
        .expect(302, done);
    });
    
    it('should respond with 403 without credentials', function(done) {
      request(expressApp)
        .post('/auth/login')
        .type('form')
        .expect(302)
        .end(function(err, res) {
          res.header.location.should.include('/');
          done();
        });
    });
    
    it('should respond with 403 with wrong credentials', function(done) {
      request(expressApp)
        .post('/auth/login')
        .type('form')
        .send({ username: "failingUser" })
        .send({ password: "<PASSWORD>" })
        .expect(302)
        .end(function(err, res) {
          res.header.location.should.include('/');
          done();
        });
    });

    it('should respond with 403 with correct username but wrong pass', function(done) {
      request(expressApp)
        .post('/auth/login')
        .type('form')
        .send({ username: "testUser" })
        .send({ password: "<PASSWORD>" })
        .expect(302)
        .end(function(err, res) {
          res.header.location.should.include('/');
          done();
        });
    });
    
    it('should respond with 200 with correct credentials', function(done) {
      request(expressApp)
        .post('/auth/login')
        .type('form')
        .send({ username: "testUser" })
        .send({ password: <PASSWORD> })
        .expect(302)
        .end(function(err, res) {
          res.header.location.should.include('/app');
          done();
        });
    });
  });
  
  describe('GET App /app', function() {
    it('should redirect without credentials', function(done) {
      request(expressApp)
        .get('/app')
        .expect(302, done);
    });

    it('should stay with logged in user', function(done) {
      authenticatedUserAgent
        .get('/app')
        .expect(200, done);
    });
  });

  describe('API', function() {
    var prefix = '/api';
    describe('v1', function() {
      prefix += '/v1';
      describe('GET /users', function() {
        var url = prefix + '/users';
        
        it('should return all users to an admin', function(done) {
          authenticatedAdminAgent
            .get(url)
            .expect('Content-Type', /json/)
            .expect(200)
            .expect({users: users}, done);
        });
        
        it('should return all users to a controller', function(done) {
          authenticatedControllerAgent
            .get(url)
            .expect('Content-Type', /json/)
            .expect(200)
            .expect({users: users}, done);
        });
        
        it('should return "403: Forbidden" to a user', function(done) {
          authenticatedUserAgent
            .get(url)
            .expect(403, done);
        });
      });
      
      describe('POST /users', function() {
        var url = prefix + '/users';
        
        it('should allow an admin to create a new user', function(done) {
          authenticatedAdminAgent
            .post(url)
            .send({user: { username: "newTestUser1", password: <PASSWORD>, type: "user" } })
            .expect('Content-Type', /json/)
            .expect(200, done);
        });
        
        it('should allow an admin to create a new controller', function(done) {
          authenticatedAdminAgent
            .post(url)
            .send({user: { username: "new<PASSWORD>", password: <PASSWORD>, type: "controller" } })
            .expect('Content-Type', /json/)
            .expect(200, done);
        });
        
        it('should allow an admin to create a new admin', function(done) {
          authenticatedAdminAgent
            .post(url)
            .send({user: { username: "newTestAdmin1", password: <PASSWORD>, type: "admin" } })
            .expect('Content-Type', /json/)
            .expect(200, done);
        });
        
        it('should allow a controller to create a new user', function(done) {
          authenticatedControllerAgent
            .post(url)
            .send({user: { username: "newTestUser2", password: <PASSWORD>, type: "user" } })
            .expect('Content-Type', /json/)
            .expect(200, done);
        });
        
        it('should not allow a controller to create a new controller', function(done) {
          authenticatedControllerAgent
            .post(url)
            .send({user: { username: "newTestController2", password: <PASSWORD>, type: "controller" } })
            .expect('Content-Type', /json/)
            .expect(403, done);
        });
        
        it('should not allow a controller to create a new admin', function(done) {
          authenticatedControllerAgent
            .post(url)
            .send({user: { username: "newTestAdmin2", password: <PASSWORD>, type: "admin" } })
            .expect('Content-Type', /json/)
            .expect(403, done);
        });
        
        it('should not allow a normal user to create a new user', function(done) {
          authenticatedUserAgent
            .post(url)
            .send({user: { username: "newTestUser3", password: <PASSWORD>, type: "user" } })
            .expect('Content-Type', /json/)
            .expect(403, done);
        });
        
        it('should not allow a normal user to create a new controller', function(done) {
          authenticatedUserAgent
            .post(url)
            .send({user: { username: "newTestController3", password: <PASSWORD>, type: "controller" } })
            .expect('Content-Type', /json/)
            .expect(403, done);
        });
        
        it('should not allow a normal user to create a new admin', function(done) {
          authenticatedUserAgent
            .post(url)
            .send({user: { username: "newTestAdmin3", password: <PASSWORD>, type: "admin" } })
            .expect('Content-Type', /json/)
            .expect(403, done);
{'fraction_non_alphanumeric': 0.1500386697602475, 'fraction_numerical': 0.012552799095722529, 'mean_word_length': 1.8808911739502998, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, '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': '315979', 'n_tokens_mistral': 4816, 'n_tokens_neox': 4599, 'n_words': 1112}