<commit_before>'use strict';
const expect = require('chai').expect;
const request = require('request');
const fs = require('fs');
//start app
require('../index.js');

describe('simple tests', function(){
  it('is on', function(done){
    request.get('http://localhost:3000/', function(err, response, body){
      expect(err).to.not.be.ok;
      expect(response.statusCode).to.equal(200);
      done();
    });

  });
  it('insert users to db', function(done){
    request.post('http://localhost:3000/users', function(err, response, body){
      expect(err).to.not.be.ok;
      expect(response.statusCode).to.equal(200);
      done();
    });

  });
  it('update user to db', function(done){
    request.put('http://localhost:3000/users/test', function(err, response, body){
      expect(err).to.not.be.ok;
      expect(response.statusCode).to.equal(200);
      done();
    });

  });
  it('remove user from db', function(done){
    request.delete('http://localhost:3000/users/test', function(err, response, body){
      expect(err).to.not.be.ok;
      expect(response.statusCode).to.equal(200);
      done();
    });

  });
  it('call fun', function(done){
    request.post('http://localhost:3000/functions/notify', function(err, response, body){
      expect(err).to.not.be.ok;
      expect(response.statusCode).to.equal(200);
      done();
    });

  });
});

describe("db ops", function(){
  let db = require('../databaseAdapter.js')("mongodb://localhost:27017/dev");
  let user1 = {email:"<EMAIL>",name:"name",phone:"0123456789"};
  let user2 = {email:"<EMAIL>",name:"name2",phone:"0123456789"};

  it('insert', function(done){
    return db.insert([user1, user2])
    .then(()=>done());
  });

  it('find', function(done){
    db.find(user1.email)
    .then(user=>{
      expect(user.name).to.equal(user1.name);
      return db.find("<EMAIL>");
    })
    .then(user=>{
      expect(user).to.not.be.ok;
      done();
    }).catch(err=>{
      if(err) done(err);
      else done(new Error("something wrong"));
    });
  });

  it('delete', function(done){
    db.delete(user1.email)
    .then(result=>{
      expect(result.deletedCount).to.equal(1);
      return db.delete(user2.email);
    })
    .then(result=>{
      expect(result.deletedCount).to.equal(1);
      return db.delete("<EMAIL>");
    })
    .then(result=>{
      expect(result.deletedCount).to.equal(0);
      done();
    }).catch(err=>{
      if(err) done(err);
      else done(new Error("something wrong"));
    });
  });

});

describe('notify hooks', function(){
  it('email', function(done){
    const fileStr = fs.readFileSync('./test/config.json', 'utf8');
    const config = JSON.parse(fileStr);
    if(!config.sendgrid) {
      done(new Error("no email keys"));
    }
    const emailSender = require('../emailAdapter.js')(config.sendgrid);
    emailSender.send(config.testerEmail)
    .then(result=>{
      expect(result.message).equal.to('success');
      done();
    })<commit_msg>fix test<commit_after>'use strict';
const expect = require('chai').expect;
const request = require('request');
const fs = require('fs');
//start app
require('../index.js');

describe('simple tests', function(){
  it('is on', function(done){
    request.get('http://localhost:3000/', function(err, response, body){
      expect(err).to.not.be.ok;
      expect(response.statusCode).to.equal(200);
      done();
    });

  });
  it('insert users to db', function(done){
    request.post('http://localhost:3000/users', function(err, response, body){
      expect(err).to.not.be.ok;
      expect(response.statusCode).to.equal(200);
      done();
    });

  });
  it('update user to db', function(done){
    request.put('http://localhost:3000/users/test', function(err, response, body){
      expect(err).to.not.be.ok;
      expect(response.statusCode).to.equal(200);
      done();
    });

  });
  it('remove user from db', function(done){
    request.delete('http://localhost:3000/users/test', function(err, response, body){
      expect(err).to.not.be.ok;
      expect(response.statusCode).to.equal(200);
      done();
    });

  });
  it('call fun', function(done){
    request.post('http://localhost:3000/functions/notify', function(err, response, body){
      expect(err).to.not.be.ok;
      expect(response.statusCode).to.equal(200);
      done();
    });

  });
});

describe("db ops", function(){
  let db = require('../databaseAdapter.js')("mongodb://localhost:27017/dev");
  let user1 = {email:"<EMAIL>",name:"name",phone:"0123456789"};
  let user2 = {email:"<EMAIL>",name:"name2",phone:"0123456789"};

  it('insert', function(done){
    return db.insert([user1, user2])
    .then(()=>done());
  });

  it('find', function(done){
    db.find(user1.email)
    .then(user=>{
      expect(user.name).to.equal(user1.name);
      return db.find("<EMAIL>");
    })
    .then(user=>{
      expect(user).to.not.be.ok;
      done();
    }).catch(err=>{
      if(err) done(err);
      else done(new Error("something wrong"));
    });
  });

  it('delete', function(done){
    db.delete(user1.email)
    .then(result=>{
      expect(result.deletedCount).to.equal(1);
      return db.delete(user2.email);
    })
    .then(result=>{
      expect(result.deletedCount).to.equal(1);
      return db.delete("<EMAIL>");
    })
    .then(result=>{
      expect(result.deletedCount).to.equal(0);
      done();
    }).catch(err=>{
      if(err) done(err);
      else done(new Error("something wrong"));
    });
  });

});

describe('notify hooks', function(){
  it('email', function(done){
    const fileStr = fs.readFileSync('./test/config.json', 'utf8');
    const config = JSON.parse(fileStr);
    if(!config.sendgrid) {
      done(new Error("no email keys"));
    }
    const emailSender = require('../emailAdapter.js')(config.sendgrid);
    emailSender.send(config.testerEmail)
    .then(result=>{
      expect(result.message).to.equal('success');
      done();
    })
{'fraction_non_alphanumeric': 0.20983274201723265, 'fraction_numerical': 0.024666328771751986, 'mean_word_length': 3.2960812772133528, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 29, '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': '6665441', 'n_tokens_mistral': 2048, 'n_tokens_neox': 1941, 'n_words': 307}