<commit_before>'use strict'
const tap = require('tap')
const http = require('http')
const request = require('request')
const Bot = require('../')

tap.test('POST webhook with no signature check', (t) => {
  let bot = new Bot({
    token: 'foo'
  })

  bot.on('error', (err) => {
    t.error(err, 'bot instance returned error')
    t.end()
  })

  bot.on('message', (payload, reply) => {
    t.type(payload, 'object', 'payload should be an object')
    t.type(reply, 'function', 'reply convenience function should exist')
    t.equals(payload.message.text, 'Test äëï', 'correct message was sent')
  })

  let server = http.createServer(bot.middleware()).listen(0, () => {
    let address = server.address()

    request({
      url: `http://localhost:${address.port}`,
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: '{"object":"page","entry":[{"id":1751036168465324,"time":1460923697656,"messaging":[{"sender":{"id":982337261802700},"recipient":{"id":1751036168465324},"timestamp":1460923697635,"message":{"mid":"mid.1460923697625:5c96e8279b55505308","seq":614,"text":"Test \\u00e4\\u00eb\\u00ef"}}]}]}'
    }, (err, res, body) => {
      t.error(err, 'response should not error')
      t.equals(res.statusCode, 200, 'request should return status code 200')
      t.deepEquals(JSON.parse(body), { status: 'ok' }, 'response should be okay')
      t.end()
    })
  })

  t.tearDown(() => {
    server.close()
  })
})

tap.test('POST webhook with signature check', (t) => {
  let bot = new Bot({
    token: 'foo',
    app_secret: 'e6af24be1d683c8c911949f897eea1f6'
  })

  bot.on('error', (err) => {
    t.error(err, 'bot instance returned error')
    t.end()
  })

  bot.on('message', (payload, reply) => {
    t.type(payload, 'object', 'payload should be an object')
    t.type(reply, 'function', 'reply convenience function should exist')
    t.equals(payload.message.text, 'Test äëï', 'correct message was sent')
  })

  let server = http.createServer(bot.middleware()).listen(0, () => {
    let address = server.address()

    request({
      url: `http://localhost:${address.port}`,
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'x-hub-signature': 'sha1=f1a4569dcf02a9829a15696d949b386b7d6d0272'
      },
      body: '{"object":"page","entry":[{"id":1751036168465324,"time":1460923697656,"messaging":[{"sender":{"id":982337261802700},"recipient":{"id":1751036168465324},"timestamp":1460923697635,"message":{"mid":"mid.1460923697625:5c96e8279b55505308","seq":614,"text":"Test \\u00e4\\u00eb\\u00ef"}}]}]}'
    }, (err, res, body) => {
      t.error(err, 'response should not error')
      t.equals(res.statusCode, 200, 'request should return 200 status code')
      t.deepEquals(JSON.parse(body), { status: 'ok' }, 'response should be okay')
      t.end()
    })
  })

  t.tearDown(() => {
    server.close()
  })
})

tap.test('POST webhook with incorrect signature', (t) => {
  let bot = new Bot({
    token: 'foo',
    app_secret: 'e6af24be1d683c8c911949f897eea1f6'
  })

  bot.on('error', (err) => {
    t.equals(err.message, 'Message integrity check failed', 'correct error should be thrown')
  })

  let server = http.createServer(bot.middleware()).listen(0, () => {
    let address = server.address()

    request({
      url: `http://localhost:${address.port}`,
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'x-hub-signature': 'sha1=DONEZO'
      },
      body: '{"object":"page","entry":[{"id":1751036168465324,"time":1460923697656,"messaging":[{"sender":{"id":982337261802700},"recipient":{"id":1751036168465324},"timestamp":1460923697635,"message":{"mid":"mid.1460923697625:5c96e8279b55505308","seq":614,"text":"Test \\u00e4\\u00eb\\u00ef"}}]}]}'
    }, (err, res, body) => {
      t.error(err, 'response should not error')
      t.equals(res.statusCode, 200, 'request should return 200 status code')
      t.deepEquals(JSON.parse(body), { status: 'not ok', error: 'Message integrity check failed' }, 'response should return error')
      t.end()
    })
  })

  t.tearDown(() => {
    server.close()
  })
})

tap.test('POST webhook with postback', (t) => {
  let bot = new Bot({
    token: 'foo'
  })

  bot.on('error', (err) => {
    t.error(err, 'bot instance returned error')
    t.end()
  })

  bot.on('postback', (payload, reply) => {
    t.type(payload, 'object', 'payload should be an object')
    t.type(reply, 'function', 'reply convenience function should exist')
    t.equals(payload.postback.payload, 'Test äëï', 'correct postback payload was sent')
  })

  let server = http.createServer(bot.middleware()).listen(0, () => {
    let address = server.address()

    request({
      url: `http://localhost:${address.port}`,
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: '{"object":"page","entry":[{"id":1751036168465324,"time":1460923697656,"messaging":[{"sender":{"id":982337261802700},"recipient":{"id":1751036168465324},"timestamp":1460923697635,"postback":{"payload":"Test \\u00e4\\u00eb\\u00ef"}}]}]}'
    }, (err, res, body) => {
      t.error(err, 'response should not error')
      t.equals(res.statusCode, 200, 'request should return 200 status code')
      t.deepEquals(JSON.parse(body), { status: 'ok' }, 'response should be okay')
      t.end()
    })
  })

  t.tearDown(() => {
    server.close()
  })
})

tap.test('POST webhook with delivery receipt', (t) => {
  let bot = new Bot({
    token: 'foo'
  })

  bot.on('error', (err) => {
    t.error(err, 'bot instance returned error')
    t.end()
  })

  bot.on('postback', (payload, reply) => {
    t.type(payload, 'object', 'payload should be an object')
    t.type(reply, 'function', 'reply convenience function should exist')
    t.equals(payload.delivery.watermark, '1460923697635', 'correct delivery receipt watermark was sent')
  })

  let server = http.createServer(bot.middleware()).listen(0, () => {
    let address = server.address()

    request({
      url: `http://localhost:${address.port}`,
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: '{"object":"page","entry":[{"id":1751036168465324,"time":1460923697656,"messaging":[{"sender":{"id":982337261802700},"recipient":{"id":1751036168465324},"timestamp":1460923697635,"delivery":{"mid":"mid.1460923697625:5c96e8279b55505308","seq":614, "watermark": 1460923697635}}]}]}'
    }, (err, res, body) => {
      t.error(err, 'response should not error')
      t.equals(res.statusCode, 200, 'request should return 200 status code')
      t.deepEquals(JSON.parse(body), { status: 'ok' }, 'response should be okay')
      t.end()
    })
  })

  t.tearDown(() => {
    server.close()
  })
})
<commit_msg>Added tests for authentication callback<commit_after>'use strict'
const tap = require('tap')
const http = require('http')
const request = require('request')
const Bot = require('../')

tap.test('POST webhook with no signature check', (t) => {
  let bot = new Bot({
    token: 'foo'
  })

  bot.on('error', (err) => {
    t.error(err, 'bot instance returned error')
    t.end()
  })

  bot.on('message', (payload, reply) => {
    t.type(payload, 'object', 'payload should be an object')
    t.type(reply, 'function', 'reply convenience function should exist')
    t.equals(payload.message.text, 'Test äëï', 'correct message was sent')
  })

  let server = http.createServer(bot.middleware()).listen(0, () => {
    let address = server.address()

    request({
      url: `http://localhost:${address.port}`,
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: '{"object":"page","entry":[{"id":1751036168465324,"time":1460923697656,"messaging":[{"sender":{"id":982337261802700},"recipient":{"id":1751036168465324},"timestamp":1460923697635,"message":{"mid":"mid.1460923697625:5c96e8279b55505308","seq":614,"text":"Test \\u00e4\\u00eb\\u00ef"}}]}]}'
    }, (err, res, body) => {
      t.error(err, 'response should not error')
      t.equals(res.statusCode, 200, 'request should return status code 200')
      t.deepEquals(JSON.parse(body), { status: 'ok' }, 'response should be okay')
      t.end()
    })
  })

  t.tearDown(() => {
    server.close()
  })
})

tap.test('POST webhook with signature check', (t) => {
  let bot = new Bot({
    token: 'foo',
    app_secret: 'e6af24be1d683c8c911949f897eea1f6'
  })

  bot.on('error', (err) => {
    t.error(err, 'bot instance returned error')
    t.end()
  })

  bot.on('message', (payload, reply) => {
    t.type(payload, 'object', 'payload should be an object')
    t.type(reply, 'function', 'reply convenience function should exist')
    t.equals(payload.message.text, 'Test äëï', 'correct message was sent')
  })

  let server = http.createServer(bot.middleware()).listen(0, () => {
    let address = server.address()

    request({
      url: `http://localhost:${address.port}`,
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'x-hub-signature': 'sha1=f1a4569dcf02a9829a15696d949b386b7d6d0272'
      },
      body: '{"object":"page","entry":[{"id":1751036168465324,"time":1460923697656,"messaging":[{"sender":{"id":982337261802700},"recipient":{"id":1751036168465324},"timestamp":1460923697635,"message":{"mid":"mid.1460923697625:5c96e8279b55505308","seq":614,"text":"Test \\u00e4\\u00eb\\u00ef"}}]}]}'
    }, (err, res, body) => {
      t.error(err, 'response should not error')
      t.equals(res.statusCode, 200, 'request should return 200 status code')
      t.deepEquals(JSON.parse(body), { status: 'ok' }, 'response should be okay')
      t.end()
    })
  })

  t.tearDown(() => {
    server.close()
  })
})

tap.test('POST webhook with incorrect signature', (t) => {
  let bot = new Bot({
    token: 'foo',
    app_secret: 'e6af24be1d683c8c911949f897eea1f6'
  })

  bot.on('error', (err) => {
    t.equals(err.message, 'Message integrity check failed', 'correct error should be thrown')
  })

  let server = http.createServer(bot.middleware()).listen(0, () => {
    let address = server.address()

    request({
      url: `http://localhost:${address.port}`,
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'x-hub-signature': 'sha1=DONEZO'
      },
      body: '{"object":"page","entry":[{"id":1751036168465324,"time":1460923697656,"messaging":[{"sender":{"id":982337261802700},"recipient":{"id":1751036168465324},"timestamp":1460923697635,"message":{"mid":"mid.1460923697625:5c96e8279b55505308","seq":614,"text":"Test \\u00e4\\u00eb\\u00ef"}}]}]}'
    }, (err, res, body) => {
      t.error(err, 'response should not error')
      t.equals(res.statusCode, 200, 'request should return 200 status code')
      t.deepEquals(JSON.parse(body), { status: 'not ok', error: 'Message integrity check failed' }, 'response should return error')
      t.end()
    })
  })

  t.tearDown(() => {
    server.close()
  })
})

tap.test('POST webhook with postback', (t) => {
  let bot = new Bot({
    token: 'foo'
  })

  bot.on('error', (err) => {
    t.error(err, 'bot instance returned error')
    t.end()
  })

  bot.on('postback', (payload, reply) => {
    t.type(payload, 'object', 'payload should be an object')
    t.type(reply, 'function', 'reply convenience function should exist')
    t.equals(payload.postback.payload, 'Test äëï', 'correct postback payload was sent')
  })

  let server = http.createServer(bot.middleware()).listen(0, () => {
    let address = server.address()

    request({
      url: `http://localhost:${address.port}`,
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: '{"object":"page","entry":[{"id":1751036168465324,"time":1460923697656,"messaging":[{"sender":{"id":982337261802700},"recipient":{"id":1751036168465324},"timestamp":1460923697635,"postback":{"payload":"Test \\u00e4\\u00eb\\u00ef"}}]}]}'
    }, (err, res, body) => {
      t.error(err, 'response should not error')
      t.equals(res.statusCode, 200, 'request should return 200 status code')
      t.deepEquals(JSON.parse(body), { status: 'ok' }, 'response should be okay')
      t.end()
    })
  })

  t.tearDown(() => {
    server.close()
  })
})

tap.test('POST webhook with delivery receipt', (t) => {
  let bot = new Bot({
    token: 'foo'
  })

  bot.on('error', (err) => {
    t.error(err, 'bot instance returned error')
    t.end()
  })

  bot.on('postback', (payload, reply) => {
    t.type(payload, 'object', 'payload should be an object')
    t.type(reply, 'function', 'reply convenience function should exist')
    t.equals(payload.delivery.watermark, '1460923697635', 'correct delivery receipt watermark was sent')
  })

  let server = http.createServer(bot.middleware()).listen(0, () => {
    let address = server.address()

    request({
      url: `http://localhost:${address.port}`,
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: '{"object":"page","entry":[{"id":1751036168465324,"time":1460923697656,"messaging":[{"sender":{"id":982337261802700},"recipient":{"id":1751036168465324},"timestamp":1460923697635,"delivery":{"mid":"mid.1460923697625:5c96e8279b55505308","seq":614, "watermark": 1460923697635}}]}]}'
    }, (err, res, body) => {
      t.error(err, 'response should not error')
      t.equals(res.statusCode, 200, 'request should return 200 status code')
      t.deepEquals(JSON.parse(body), { status: 'ok' }, 'response should be okay')
      t.end()
    })
  })

  t.tearDown(() => {
    server.close()
  })
})

tap.test('POST webhook with authentication callback', (t) => {
  let bot = new Bot({
    token: 'foo'
  })

  bot.on('error', (err) => {
    t.error(err, 'bot instance returned error')
    t.end()
  })

  bot.on('authentication', (payload, reply) => {
    t.type(payload, 'object', 'authentication should be an object')
    t.type(reply, 'function', 'reply convenience function should exist')
    t.equals(payload.optin.ref, 'bar', 'correct data ref was sent')
  })

  let server = http.createServer(bot.middleware()).listen(0, () => {
    let address = server.address()

    request({
      url: `http://localhost:${address.port}`,
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: '{"object":"page","entry":[{"id":510249619162304,"time":1461167227231,"messaging":[{"sender":{"id":1066835436691078},"recipient":{"id":510249619162304},"timestamp":1461167227231,"optin":{"ref":"bar"}}]}]}'
    }, (err, res, body) => {
      t.error(err, 'response should not error')
      t.equals(res.statusCode, 200, 'request should return 200 status code')
      t.deepEquals(JSON.parse(body), { status: 'ok' }, 'response should be okay')
      t.end()
    })
  })

  t.tearDown(() => {
    server.close()
  })
})

{'fraction_non_alphanumeric': 0.1987231182795699, 'fraction_numerical': 0.09213709677419354, 'mean_word_length': 3.791049581455248, 'pattern_counts': {'":': 148, '<': 3, '<?xml version=': 0, '>': 67, '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': '736890', 'n_tokens_mistral': 6014, 'n_tokens_neox': 5099, 'n_words': 982}