<commit_before>const describe = require('mocha').describe
const it = require('mocha').it
const beforeEach = require('mocha').beforeEach
const afterEach = require('mocha').afterEach
const assert = require('chai').assert
const sinon = require('sinon')

const database = require('../lib/storage/database')
const route = require('../lib/http/route')
const receive = require('../handlers/receive/handler')

const eventReceiveSuccess = require('../events/receive-success.json')
const eventReceiveSuccessJson = require('../events/receive-success-json.json')
const eventBadEmail = require('../events/receive-bad-email.json')
const eventNoEmail = require('../events/receive-no-email.json')
const eventHoneypot = require('../events/receive-honeypot.json')

describe('receive', function () {
  var spy, stub
  beforeEach(function () {
    spy = sinon.spy(route, 'render')
    stub = sinon.stub(database, 'put').callsFake((data, callback) => callback())
  })
  afterEach(function () {
    spy.restore()
    stub.restore()
  })
  describe('success', function () {
    it('html', function () {
      routeRenderAssert('receive-success', eventReceiveSuccess, spy)
    })
    it('json', function () {
      routeRenderAssert('receive-success', eventReceiveSuccessJson, spy)
    })
  })
  describe('error', function () {
    it('bad email', function () {
      routeRenderAssert('receive-bad-email', eventBadEmail, spy)
    })
    it('no email', function () {
      routeRenderAssert('receive-no-email', eventNoEmail, spy)
    })
    it('honeypot', function () {
      routeRenderAssert('receive-honeypot', eventHoneypot, spy)
    })
  })
})

function routeRenderAssert (route, event, spy) {
  receive.handle(event, {}, sinon.stub())
  assert(spy.calledOnce)
  assert(spy.calledWith(route))
}<commit_msg>update receive test to evaluate on returned response object<commit_after>const describe = require('mocha').describe
const it = require('mocha').it
const beforeEach = require('mocha').beforeEach
const afterEach = require('mocha').afterEach
const assert = require('chai').assert
const sinon = require('sinon')

const database = require('../lib/storage/database')
const route = require('../lib/http/route')
const response = require('../lib/http/response')
const receive = require('../handlers/receive/handler')
const request = require('../handlers/receive/request')

const eventReceiveSuccess = require('../events/receive-success.json')
const eventReceiveSuccessJson = require('../events/receive-success-json.json')
const eventBadEmail = require('../events/receive-bad-email.json')
const eventNoEmail = require('../events/receive-no-email.json')
const eventHoneypot = require('../events/receive-honeypot.json')

describe('receive', function () {
  var spy, stub
  beforeEach(function () {
    spy = sinon.spy(response, 'build')
    stub = sinon.stub(database, 'put').callsFake((data, callback) => callback())
  })
  afterEach(function () {
    spy.restore()
    stub.restore()
  })
  describe('success', function () {
    it('html', function () {
      responseAssert('receive-success', eventReceiveSuccess, spy)
    })
    it('json', function () {
      responseAssert('receive-success', eventReceiveSuccessJson, spy)
    })
  })
  describe('error', function () {
    it('bad email', function () {
      responseAssert('receive-bad-email', eventBadEmail, spy)
    })
    it('no email', function () {
      responseAssert('receive-no-email', eventNoEmail, spy)
    })
    it('honeypot', function () {
      responseAssert('receive-honeypot', eventHoneypot, spy)
    })
  })
})

function responseAssert (type, event, spy) {
  let data = request.getParams(event)
  let routeDetails = route.getRouteDetails(type, data)
  receive.handle(event, {}, sinon.stub())
  assert(spy.calledOnce)
  let result = spy.firstCall.returnValue
  assert.equal(result.statusCode, routeDetails.statusCode)
  let expectedContentType = data['_format'] === 'json' ? 'application/json' : 'text/html'
  assert.equal(result.headers['Content-Type'], expectedContentType)
  assert.include(result.body, routeDetails.message)
}
{'fraction_non_alphanumeric': 0.16454144188327613, 'fraction_numerical': 0.0, 'mean_word_length': 4.489905787348587, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1824124', 'n_tokens_mistral': 1243, 'n_tokens_neox': 1196, 'n_words': 233}