<commit_before>'use strict';

let chai = require('chai');
let chaiHttp = require('chai-http');
chai.use(chaiHttp);
let mongoose = require('mongoose');

const request = chai.request;
const expect = chai.expect;

process.env.NODE_ENV = 'test';

const TEST_DB_SERVER = 'mongodb://localhost/test_db';
process.env.DB_SERVER = TEST_DB_SERVER;

let app = require('../api/app');
let server;

let baseUrl = 'localhost:3000'
let testCourse = require('./testCourse');
let testUser = require('./testUser');

describe('testing routes', () => {
  before((done) => {
    server = app.listen(3000, () => {
      done();
    });
  });

  after((done) => {
    mongoose.connection.db.dropDatabase(() => {
      server.close(() => {
        mongoose.connection.close(() => {
          done();
        });
      });
    });
  });

  it('course: GET should respond with a 404 error when request sent for an unknown route', (done) => {
    request(baseUrl)
      .get('/')
      .end((err, res) => {
        expect(err).to.have.status(404);
        expect(res.text).to.include('Not Found');
        done();
      });
  });

  it('course: GET should respond with 200', (done) => {
    request(baseUrl)
      .get('/course')
      .end((err, res) => {
        expect(err).to.eql(null);
        expect(res).to.have.status(200);
        expect(res.text).to.include('success');
        done();
      });
  });

  it('course: POST should respond with new course', (done) => {
    request(baseUrl)
      .post('/course/create')
      .send(testCourse)
      .end((err, res) => {
        expect(err).to.eql(null);
        expect(res).to.have.status(200);
        expect(res.body).to.have.deep.property('name', '<NAME>');
        done();
      });
  });

  it('course: POST should respond with an error when required data is missing', (done) => {
    request(baseUrl)
      .post('/course/create')
      .send({ name: 'newcastle' })
      .end((err, res) => {
        expect(err).to.have.status(500);
        expect(res.text).to.include('Error creating course');
        done();
      });
  });

  it('user: POST to register a new user should respond with the user object', (done) => {
    request(baseUrl)
      .post('/user/register')
      .send(testUser)
      .end((err, res) => {
        expect(err).to.eql(null);
        expect(res.body).to.have.deep.property('username', 'testUser');
        done();
      });
  });

  it('user: POST to register an existing user should respond with an error', (done) => {
    request(baseUrl)
      .post('/user/register')
      .send(testUser)
      .end((err, res) => {
        expect(err).to.have.status(500);
        expect(res.text).to.include('Error creating user');
        done();
      });
  });

  it('user: POST to login should respond with the user\'s name', (done) => {
    request(baseUrl)
      .post('/user/login')
      .send({ emailOrUsername: testUser.username, password: <PASSWORD> })
      .end((err, res) => {
        expect(err).to.eql(null);
        expect(res.body).to.have.deep.property('firstName', testUser.firstName);
        done();
      });
  });

  it('user: POST to login should fail and respond with an error', (done) => {
    request(baseUrl)
      .post('/user/login')
      .send({ emailOrUsername: testUser.username, password: '<PASSWORD>' })
      .end((err, res) => {
        expect(err).to.have.status(400);
        expect(res.text).to.include('Incorrect username or password');
        done();
      });
  });
});<commit_msg>quick code cleanup in test.js<commit_after>'use strict';

let chai = require('chai');
let chaiHttp = require('chai-http');
chai.use(chaiHttp);
let mongoose = require('mongoose');

const request = chai.request;
const expect = chai.expect;

process.env.NODE_ENV = 'test';

const TEST_DB_SERVER = 'mongodb://localhost/test_db';
process.env.DB_SERVER = TEST_DB_SERVER;

let app = require('../api/app');
let server;

let baseUrl = 'localhost:3000'
let testCourse = require('./testCourse');
let testUser = require('./testUser');

describe('testing routes', () => {
  before((done) => {
    server = app.listen(3000, () => {
      done();
    });
  });

  after((done) => {
    mongoose.connection.db.dropDatabase(() => {
      server.close(() => {
        mongoose.connection.close(() => {
          done();
        });
      });
    });
  });

  it('course: GET should respond with a 404 error when request sent for an unknown route', (done) => {
    request(baseUrl)
      .get('/')
      .end((err, res) => {
        expect(err).to.have.status(404);
        expect(res.text).to.include('Not Found');
        done();
      });
  });

  it('course: GET should respond with 200', (done) => {
    request(baseUrl)
      .get('/course')
      .end((err, res) => {
        expect(res).to.have.status(200);
        expect(res.text).to.include('success');
        done();
      });
  });

  it('course: POST should respond with new course', (done) => {
    request(baseUrl)
      .post('/course/create')
      .send(testCourse)
      .end((err, res) => {
        expect(res).to.have.status(200);
        expect(res.body).to.have.deep.property('name', '<NAME>');
        done();
      });
  });

  it('course: POST should respond with an error when required data is missing', (done) => {
    request(baseUrl)
      .post('/course/create')
      .send({ name: 'newcastle' })
      .end((err, res) => {
        expect(err).to.have.status(500);
        expect(res.text).to.include('Error creating course');
        done();
      });
  });

  it('user: POST to register a new user should respond with the user object', (done) => {
    request(baseUrl)
      .post('/user/register')
      .send(testUser)
      .end((err, res) => {
        expect(res).to.have.status(200);
        expect(res.body).to.have.deep.property('username', 'testUser');
        done();
      });
  });

  it('user: POST to register an existing user should respond with an error', (done) => {
    request(baseUrl)
      .post('/user/register')
      .send(testUser)
      .end((err, res) => {
        expect(err).to.have.status(500);
        expect(res.text).to.include('Error creating user');
        done();
      });
  });

  it('user: POST to login should respond with the user\'s name', (done) => {
    request(baseUrl)
      .post('/user/login')
      .send({
        emailOrUsername: testUser.username,
        password: <PASSWORD> ,
      })
      .end((err, res) => {
        expect(res).to.have.status(200);
        expect(res.body).to.have.deep.property('firstName', testUser.firstName);
        done();
      });
  });

  it('user: POST to login should fail and respond with an error', (done) => {
    request(baseUrl)
      .post('/user/login')
      .send({
        emailOrUsername: testUser.username,
        password: '<PASSWORD>'
      })
      .end((err, res) => {
        expect(err).to.have.status(400);
        expect(res.text).to.include('Incorrect username or password');
        done();
      });
  });
});
{'fraction_non_alphanumeric': 0.18977892621303474, 'fraction_numerical': 0.010048808498420901, 'mean_word_length': 2.524026302478503, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 55, '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': '4686740', 'n_tokens_mistral': 2282, 'n_tokens_neox': 2197, 'n_words': 460}