<commit_before>/**
 * API tests
 */

require('dotenv').config();
const config = require('../config');

const expect = require('chai').expect;
const express = require('express');
const bodyParser = require('body-parser');
const http = require('http');
const request = require('request');
const MongoClient = require('mongodb').MongoClient;

// use testing database
config.mongoUri = process.env.MONGO_URI_TEST;

const api = require('../api.js');
const apiPort = parseInt(process.env.PORT_WDS, 10) + 2;

const user = require('../user');

describe('Backend API', () => {
  before(done => {
    // connect to the database
    if (this.db) {
      return done();
    }
    MongoClient.connect(config.mongoUri, (err, db) => {
      expect(err).to.be.equal(null);
      this.db = db;
      done();
    });
  });

  before(() => {
    const app = express();
    app.use(bodyParser.urlencoded({ extended: true }));
    app.use('/', api(this.db));
    this.server = http.createServer(app).listen(apiPort);
    this.url = `http://localhost:${apiPort}`;
  });

  it('should connect to the database', () => {
      expect(this.db).to.be.ok;
  });

  it('should handle GET requests like ?t=some/task', done => {
    request.get(`${this.url}/?t=foo/bar`, (err, res, body) => {
      const result = JSON.parse(body);
      expect(result.url).to.be.equal('/foo/bar');
      done();
    });
  });
  it('should handle POST requests like ?t=some/task', done => {
    request.post(`${this.url}/?t=bar/baz`, (err, res, body) => {
      const result = JSON.parse(body);
      expect(result.url).to.be.equal('/bar/baz');
      done();
    });
  });

  describe('user login', () => {
    before(done => {
      // create a test user
      this.db.collection('users')
        .insert({
          uid: 1,
          email: '<EMAIL>',
          name: '<NAME>',
          pinHashed: user.hash(1234, config.userHashSalt)
        })
        .then(() => {
          done();
        });
    });
    it('should have a method for adding users');

    it('should handle bad logins', done => {
      request.post(`${this.url}/login`, { form: { pin: 1000 } }, (err, res, body) => {
        expect(JSON.parse(body).error).to.be.equal(true);
        done();
      });
    });

    it('should throttle bad logins');

    describe('good login', () => {
      before(done => {
        request.post(`${this.url}/login`, { form: { pin: 1234 } }, (err, res, body) => {
          expect(err).to.be.equal(null);
          this.goodLoginResult = JSON.parse(body);
          done();
        });
      });

      it('should return an error status of false', () => {
        expect(this.goodLoginResult.error).to.be.equal(false);
      });
      it('should return a token', () => {
        expect(this.goodLoginResult.api_key).to.be.a('string').of.length(40); // sha-1 length
      });
      it('should return a uid', () => {
        expect(this.goodLoginResult.uid).to.be.a('number');
      });
      it('should return a name', () => {
        expect(this.goodLoginResult.name).to.be.a('string').of.length.greaterThan(0);
      });
    });

    after(() => {<commit_msg>[added to test] 403 error on bad login<commit_after>/**
 * API tests
 */

require('dotenv').config();
const config = require('../config');

const expect = require('chai').expect;
const express = require('express');
const bodyParser = require('body-parser');
const http = require('http');
const request = require('request');
const MongoClient = require('mongodb').MongoClient;

// use testing database
config.mongoUri = process.env.MONGO_URI_TEST;

const api = require('../api.js');
const apiPort = parseInt(process.env.PORT_WDS, 10) + 2;

const user = require('../user');

describe('Backend API', () => {
  before(done => {
    // connect to the database
    if (this.db) {
      return done();
    }
    MongoClient.connect(config.mongoUri, (err, db) => {
      expect(err).to.be.equal(null);
      this.db = db;
      done();
    });
  });

  before(() => {
    const app = express();
    app.use(bodyParser.urlencoded({ extended: true }));
    app.use('/', api(this.db));
    this.server = http.createServer(app).listen(apiPort);
    this.url = `http://localhost:${apiPort}`;
  });

  it('should connect to the database', () => {
      expect(this.db).to.be.ok;
  });

  it('should handle GET requests like ?t=some/task', done => {
    request.get(`${this.url}/?t=foo/bar`, (err, res, body) => {
      const result = JSON.parse(body);
      expect(result.url).to.be.equal('/foo/bar');
      done();
    });
  });
  it('should handle POST requests like ?t=some/task', done => {
    request.post(`${this.url}/?t=bar/baz`, (err, res, body) => {
      const result = JSON.parse(body);
      expect(result.url).to.be.equal('/bar/baz');
      done();
    });
  });

  describe('user login', () => {
    before(done => {
      // create a test user
      this.db.collection('users')
        .insert({
          uid: 1,
          email: '<EMAIL>',
          name: '<NAME>',
          pinHashed: user.hash(1234, config.userHashSalt)
        })
        .then(() => {
          done();
        });
    });
    it('should have a method for adding users');

    it('should handle bad logins', done => {
      request.post(`${this.url}/login`, { form: { pin: 1000 } }, (err, res, body) => {
        expect(res.statusCode).to.be.equal(403);
        expect(JSON.parse(body).error).to.be.equal(true);
        done();
      });
    });

    it('should throttle bad logins');

    describe('good login', () => {
      before(done => {
        request.post(`${this.url}/login`, { form: { pin: 1234 } }, (err, res, body) => {
          expect(err).to.be.equal(null);
          this.goodLoginResult = JSON.parse(body);
          done();
        });
      });

      it('should return an error status of false', () => {
        expect(this.goodLoginResult.error).to.be.equal(false);
      });
      it('should return a token', () => {
        expect(this.goodLoginResult.api_key).to.be.a('string').of.length(40); // sha-1 length
      });
      it('should return a uid', () => {
        expect(this.goodLoginResult.uid).to.be.a('number');
      });
      it('should return a name', () => {
        expect(this.goodLoginResult.name).to.be.a('string').of.length.greaterThan(0);
      });
    });

    after(() => {
{'fraction_non_alphanumeric': 0.20427892234548337, 'fraction_numerical': 0.007290015847860539, 'mean_word_length': 2.5355742296918766, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 51, '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': '6563404', 'n_tokens_mistral': 2080, 'n_tokens_neox': 2015, 'n_words': 398}