<commit_before>'use strict'
const { post } = require('./support/request')
const db = require('./support/db')
const { asyncWrap } = require('./support/nodeunit')
const app = require('./support/app')
const { HTTP_OK, HTTP_UNAUTHORIZED} = require('./support/const')

module.exports = {

  setUp: async function (test) {
    await db.init()
    await app.init()
    test()
  },

  /**
   * @api {post} /login Authenticate as user
   * @apiName Login
   * @apiGroup User
   * @apiParam {String} email
   * @apiParam {String} password
   * @apiSuccess (200) {String} Set-Cookie Authentication token 
   * 
   * @apiExample
   * POST /login HTTP/1.1 
   * Content-Type: application/json
   * 
   * {
   *  "email": "<EMAIL>",
   *  "password": "<PASSWORD>"
   * }
   * 
   */
  adminAuth: asyncWrap(async function (test) {

    const NUM_TESTS = 2
    test.expect(NUM_TESTS)

    let login = await post(null, '/login', {
      email: db.user.admin.email,
      password: <PASSWORD>
    })
      
    test.strictEqual(login.response.statusCode, HTTP_OK)
    test.equal(login.body, 'OK')

  }),

  testAuth: asyncWrap(async function (test) {

    const NUM_TESTS = 2
    test.expect(NUM_TESTS)

    let login = await post(null, '/login', {
      email: db.user.test.email,
      password: <PASSWORD>
    })

    test.strictEqual(login.response.statusCode, HTTP_OK)
    test.equal(login.body, 'OK')

  }),

  failAuth: asyncWrap(async function (test) {

    const NUM_TESTS = 2
    test.expect(NUM_TESTS)

    let login = await post(null, '/login', {
      email: '<EMAIL>',
      password: '<PASSWORD>'
    })

    test.strictEqual(login.response.statusCode, HTTP_UNAUTHORIZED)
    test.deepEqual(login.body.errors, [{
      code: HTTP_UNAUTHORIZED,
      detail: 'Authentication failed',
      status: 'Unauthorized',
      title: 'Not Authenticated'
    }])

  }),
  /**
   * @api {post} /oauth2/token Request OAuth token
   * @apiName OAuthToken
   * @apiGroup User
   * @apiParam {string} grant_type 'password'
   * @apiParam {String} email
   * @apiParam {String} password
   * 
   * @apiExample
   * POST /oauth2/token HTTP/1.1 
   * Content-Type: application/json
   * Authorization: Basic OWM1ZjhkMzItNGU5ZS00MjgxLTk0MzItNzk4ODBjMDBhMGU5OnRlc3R1c2Vy
   * 
   * {
   *  "grant_type": "password",
   *  "email": "<EMAIL>",
   *  "password": "<PASSWORD>"
   * }
   * 
   */
  oauthToken: asyncWrap(async function (test) {
    const NUM_TESTS = 2
    test.expect(NUM_TESTS)

    const token = await post({ 
      Authorization: 'Basic ' + Buffer(db.client.admin.id + ':' + db.client.admin.password).toString('base64')
    }, '/oauth2/token', {
      grant_type: 'password',
      password: <PASSWORD>,
      username: db.user.admin.email
    })

    test.strictEqual(token.response.statusCode, HTTP_OK)
    test.ok(token.body.access_token)

  })
}<commit_msg>added request password reset test<commit_after>'use strict'
const { post } = require('./support/request')
const db = require('./support/db')
const { asyncWrap } = require('./support/nodeunit')
const app = require('./support/app')
const { HTTP_OK, HTTP_UNAUTHORIZED} = require('./support/const')
const sinon = require('sinon')
const sandbox = sinon.createSandbox()

const BotServ = require('../api/Anope/BotServ')
const nodemailer = require('nodemailer')

const stub = {}

module.exports = {

  setUp: async function (test) {
    await db.init()
    await app.init()
    sandbox.stub(BotServ, 'say')
    stub.sendMail = sandbox.stub()
    sandbox.stub(nodemailer, 'createTransport').returns({
      sendMail: stub.sendMail
    })

    test()
  },
  tearDown: function (test) {
    sandbox.restore()
    test()
  },

  /**
   * @api {post} /login Authenticate as user
   * @apiName Login
   * @apiGroup User
   * @apiParam {String} email
   * @apiParam {String} password
   * @apiSuccess (200) {String} Set-Cookie Authentication token 
   * 
   * @apiExample
   * POST /login HTTP/1.1 
   * Content-Type: application/json
   * 
   * {
   *  "email": "<EMAIL>",
   *  "password": "<PASSWORD>"
   * }
   * 
   */
  adminAuth: asyncWrap(async function (test) {

    const NUM_TESTS = 2
    test.expect(NUM_TESTS)

    let login = await post(null, '/login', {
      email: db.user.admin.email,
      password: <PASSWORD>
    })
      
    test.strictEqual(login.response.statusCode, HTTP_OK)
    test.equal(login.body, 'OK')

  }),

  testAuth: asyncWrap(async function (test) {

    const NUM_TESTS = 2
    test.expect(NUM_TESTS)

    let login = await post(null, '/login', {
      email: db.user.test.email,
      password: <PASSWORD>
    })

    test.strictEqual(login.response.statusCode, HTTP_OK)
    test.equal(login.body, 'OK')

  }),

  failAuth: asyncWrap(async function (test) {

    const NUM_TESTS = 2
    test.expect(NUM_TESTS)

    let login = await post(null, '/login', {
      email: '<EMAIL>',
      password: '<PASSWORD>'
    })

    test.strictEqual(login.response.statusCode, HTTP_UNAUTHORIZED)
    test.deepEqual(login.body.errors, [{
      code: HTTP_UNAUTHORIZED,
      detail: 'Authentication failed',
      status: 'Unauthorized',
      title: 'Not Authenticated'
    }])

  }),
  /**
   * @api {post} /oauth2/token Request OAuth token
   * @apiName OAuthToken
   * @apiGroup User
   * @apiParam {string} grant_type 'password'
   * @apiParam {String} email
   * @apiParam {String} password
   * 
   * @apiExample
   * POST /oauth2/token HTTP/1.1 
   * Content-Type: application/json
   * Authorization: Basic OWM1ZjhkMzItNGU5ZS00MjgxLTk0MzItNzk4ODBjMDBhMGU5OnRlc3R1c2Vy
   * 
   * {
   *  "grant_type": "password",
   *  "email": "<EMAIL>",
   *  "password": "<PASSWORD>"
   * }
   * 
   */
  oauthToken: asyncWrap(async function (test) {
    const NUM_TESTS = 2
    test.expect(NUM_TESTS)

    const token = await post({ 
      Authorization: 'Basic ' + Buffer(db.client.admin.id + ':' + db.client.admin.password).toString('base64')
    }, '/oauth2/token', {
      grant_type: 'password',
      password: <PASSWORD>,
      username: db.user.admin.email
    })

    test.strictEqual(token.response.statusCode, HTTP_OK)
    test.ok(token.body.access_token)

  }),
  resetPassword: asyncWrap(async function (test) {
    const NUM_TESTS = 4
    test.expect(NUM_TESTS)

    const reset = await post(null, '/reset', {
      email: db.user.test.email
    })

    test.strictEqual(reset.response.statusCode, HTTP_OK)
    test.equal(reset.body, 'OK')
    test.strictEqual(BotServ.say.callCount, 1)
    test.strictEqual(stub.sendMail.callCount, 1)

  })
}
{'fraction_non_alphanumeric': 0.14710845221659763, 'fraction_numerical': 0.008436876821598405, 'mean_word_length': 2.9324487334137515, 'pattern_counts': {'":': 10, '<': 21, '<?xml version=': 0, '>': 21, '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': '565713', 'n_tokens_mistral': 2338, 'n_tokens_neox': 2256, 'n_words': 440}