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

/**
 * Create simple function stub for testing
 */
function stub () {

  const func = function () {
    func.called += 1
  }
  func.called = 0
  return func
}

// stub all the external calls
const NickServ = require('../api/Anope/NickServ')
const HostServ = require('../api/Anope/NickServ')
const BotServ = require('../api/Anope/BotServ')

const orig = {
  register: NickServ.register,
  update: HostServ.update,
  say: BotServ.say
}

const stubs = {
  register: stub(),
  update: stub(),
  say: stub()
}


module.exports = {
  setUp: async function (test) {
    await db.init()
    await app.init()

    stubs.register.called = 0
    stubs.update.called = 0
    stubs.say.called = 0

    NickServ.register = stubs.register
    HostServ.update = stubs.update
    BotServ.say = stubs.say

    test()
  },
  tearDown: function (test) {
    NickServ.register = orig.register
    HostServ.update = orig.update
    BotServ.say = orig.say
    test()
  },
  /**
   * @api {post} /register Register new user
   * @apiName UserRegister
   * @apiGroup User
   * 
   * @apiExample
   * POST /register HTTP/1.1
   * Content-Type: application/json
   * 
   * {
   *  "email": "<EMAIL>",
   *  "name": "roland",
   *  "nickname": "ratty",
   *  "platform": "pc",
   *  "password": "<PASSWORD>"
   * }
   */
  registerNew: asyncWrap(async function (test) {
    const NUM_TESTS = 4
    test.expect(NUM_TESTS)

    const register = await post(null, '/register', {
      email: '<EMAIL>',
      platform: 'pc',
      name: 'roland',
      nickname: 'ratty',
      password: '<PASSWORD>'
    })
    test.strictEqual(register.response.statusCode, HTTP_OK)
    test.strictEqual(stubs.register.called, 1)
    test.strictEqual(stubs.update.called, 1)
    test.strictEqual(stubs.say.called, 1)

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

    const register = await post(null, '/register', {
      email: db.user.test.email
    })
    test.strictEqual(register.response.statusCode, HTTP_BAD_REQUEST)
    test.strictEqual(stubs.register.called, 0)
    test.strictEqual(stubs.update.called, 0)
    test.strictEqual(stubs.say.called, 0)

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

    const register = await post(null, '/register', {
      email: '<EMAIL>',
      platform: 'PC'
    })
    test.strictEqual(register.response.statusCode, HTTP_BAD_REQUEST)
    test.strictEqual(stubs.register.called, 0)
    test.strictEqual(stubs.update.called, 0)
    test.strictEqual(stubs.say.called, 0)

  }),
  /**
   * @api {get} /users find user
   * @apiName UserFind
   * @apiGroup User
   * 
   * @apiHeader {String} Cookie auth token
   * 
   * @apiExample
   * GET /users?email=<EMAIL> HTTP/1.1
   * Cookie: fuelrats:session=<KEY>; path=/; httponly;<commit_msg>migrated to sinon for stubbing<commit_after>'use strict'
const { post, get } = require('./support/request')
const db = require('./support/db')
const auth = require('./support/auth')
const { asyncWrap } = require('./support/nodeunit')
const app = require('./support/app')
const { HTTP_BAD_REQUEST, HTTP_OK } = require('./support/const')
const sinon = require('sinon')
const sandbox = sinon.createSandbox()


const NickServ = require('../api/Anope/NickServ')
const HostServ = require('../api/Anope/NickServ')
const BotServ = require('../api/Anope/BotServ')

module.exports = {
  setUp: async function (test) {
    await db.init()
    await app.init()

    // stub all the external calls
    sandbox.stub(NickServ, 'register')
    sandbox.stub(HostServ, 'update')
    sandbox.stub(BotServ, 'say')

    test()
  },
  tearDown: function (test) {
    sandbox.restore()
    test()
  },
  /**
   * @api {post} /register Register new user
   * @apiName UserRegister
   * @apiGroup User
   * 
   * @apiExample
   * POST /register HTTP/1.1
   * Content-Type: application/json
   * 
   * {
   *  "email": "<EMAIL>",
   *  "name": "roland",
   *  "nickname": "ratty",
   *  "platform": "pc",
   *  "password": "<PASSWORD>"
   * }
   */
  registerNew: asyncWrap(async function (test) {
    const NUM_TESTS = 4
    test.expect(NUM_TESTS)

    const register = await post(null, '/register', {
      email: '<EMAIL>',
      platform: 'pc',
      name: 'roland',
      nickname: 'ratty',
      password: '<PASSWORD>'
    })
    test.strictEqual(register.response.statusCode, HTTP_OK)
    test.strictEqual(NickServ.register.callCount, 1)
    test.strictEqual(HostServ.update.callCount, 1)
    test.strictEqual(BotServ.say.callCount, 1)

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

    const register = await post(null, '/register', {
      email: db.user.test.email
    })
    test.strictEqual(register.response.statusCode, HTTP_BAD_REQUEST)
    test.strictEqual(NickServ.register.callCount, 0)
    test.strictEqual(HostServ.update.callCount, 0)
    test.strictEqual(BotServ.say.callCount, 0)

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

    const register = await post(null, '/register', {
      email: '<EMAIL>',
      platform: 'PC'
    })
    test.strictEqual(register.response.statusCode, HTTP_BAD_REQUEST)
    test.strictEqual(NickServ.register.callCount, 0)
    test.strictEqual(HostServ.update.callCount, 0)
    test.strictEqual(BotServ.say.callCount, 0)

  }),
  /**
   * @api {get} /users find user
   * @apiName UserFind
   * @apiGroup User
   * 
   * @apiHeader {String} Cookie auth token
   * 
   * @apiExample
   * GET /users?email=<EMAIL> HTTP/1.1
   * Cookie: fuelrats:session=<KEY>; path=/; httponly;
{'fraction_non_alphanumeric': 0.14962593516209477, 'fraction_numerical': 0.006151288445552784, 'mean_word_length': 3.1008861622358554, 'pattern_counts': {'":': 10, '<': 17, '<?xml version=': 0, '>': 17, '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': '3982604', 'n_tokens_mistral': 2089, 'n_tokens_neox': 2013, 'n_words': 421}