<commit_before>{ daisy
  expect
  request
  generateUrl
  deepObjectExtend
  generateRandomString
  generateRandomUsername
  checkBongoConnectivity
  generateRequestParamsEncodeBody } = require '../../../../testhelper'

{ withConvertedUser }               = require '../../../../../workers/social/testhelper'
{ withConvertedUserAndApiToken }    = require '../../../../../workers/social/testhelper/models/apitokenhelper'

apiErrors                           = require './errors'
KodingLogger                        = require '../../../../models/kodinglogger'


generateLogRequestParams = (opts = {}, subdomain) ->

  params    =
    url     : generateUrl { route : '-/api/logs', subdomain }
    query   : {}
    headers : { Authorization : "Bearer #{generateRandomString()}" }

  requestParams = generateRequestParamsEncodeBody params, opts

  return requestParams


TESTUSERS =
  admin   : null
  regular : null

TESTLOG   = generateRandomString()

# use different scope on each test ~ GG
TESTSCOPE = KodingLogger.SCOPES[Math.round(Math.random() * KodingLogger.SCOPES.length)]

beforeTests = -> before (done) ->

  KodingLogger.connect()

  checkBongoConnectivity ->

    withConvertedUser (regular) ->
      TESTUSERS.regular = regular

      withConvertedUser { role: 'admin' }, (admin) ->
        TESTUSERS.admin = admin

        KodingLogger[TESTSCOPE] TESTUSERS.admin.group.slug, TESTLOG
        done()


runTests = -> describe 'server.handlers.api.logs', ->


  it 'should send HTTP 400 if provided api token is not valid', (done) ->

    logRequestParams = generateLogRequestParams()

    request.get logRequestParams, (err, res, body) ->
      expect(err).to.not.exist
      expect(res.statusCode).to.be.equal 400
      expect(JSON.parse body).to.be.deep.equal { error : apiErrors.invalidApiToken }

      done()


  it 'should send HTTP 401 if api token is not provided and there is no session', (done) ->

    logRequestParams = generateRequestParamsEncodeBody
      url            : generateUrl { route : '-/api/logs' }

    request.get logRequestParams, (err, res, body) ->
      expect(err).to.not.exist
      expect(res.statusCode).to.be.equal 401
      expect(JSON.parse body).to.be.deep.equal { error : apiErrors.unauthorizedRequest }

      done()


  it 'should send HTTP 401 if no api token provided and session not belongs to an admin', (done) ->

    { client } = TESTUSERS.regular

    logRequestParams = generateRequestParamsEncodeBody
      url      : generateUrl { route : '-/api/logs' }
      clientId : client.sessionToken

    request.get logRequestParams, (err, res, body) ->

      expect(err).to.not.exist
      expect(res.statusCode).to.be.equal 401
      expect(JSON.parse body).to.be.deep.equal { error : apiErrors.unauthorizedRequest }

      done()


  it 'should send HTTP 403 if group.isApiEnabled is not true', (done) ->

    # creating user, group, and api token
    options = { createGroup : yes, groupData : { isApiEnabled : yes } }
    withConvertedUserAndApiToken options, ({ client, userFormData, apiToken, group }) ->

      # setting api token availability false for the group
      group.modify client, { isApiEnabled: false }, (err) ->

        expect(err).to.not.exist

        logRequestParams = generateLogRequestParams
          headers : { Authorization : "Bearer #{apiToken.code}" }

        request.get logRequestParams, (err, res, body) ->
          expect(err).to.not.exist
          expect(res.statusCode).to.be.equal 403
          expect(JSON.parse body).to.be.deep.equal { error : apiErrors.apiIsDisabled }

          done()


  it 'should send HTTP 400 if provided token valid but not subdomain', (done) ->

    # creating user, group, and api token
    options = { createGroup : yes, groupData : { isApiEnabled : yes } }
    withConvertedUserAndApiToken options, ({ group, account, apiToken }) ->

      logRequestParams = generateLogRequestParams
        headers : { Authorization : "Bearer #{apiToken.code}" }

      request.get logRequestParams, (err, res, body) ->
        expect(err).to.not.exist
        expect(res.statusCode).to.be.equal 400
        expect(JSON.parse body).to.be.deep.equal {
          error: apiErrors.invalidRequestDomain
        }

        done()


  it 'should send HTTP 200 if provided token and request domain is valid', (done) ->

    # creating user, group, and api token
    options = { createGroup : yes, groupData : { isApiEnabled : yes } }
    withConvertedUserAndApiToken options, ({ group, account, apiToken }) ->

      logRequestParams = generateLogRequestParams
        headers : { Authorization : "Bearer #{apiToken.code}" }
      , group.slug

      request.get logRequestParams, (err, res, body) ->
        expect(err).to.not.exist
        expect(res.statusCode).to.be.equal 200
        expect(body).to.contain 'data'

        done()


  it 'should send HTTP 200 if token not provided but there is a valid session', (done) ->

    { client } = TESTUSERS.admin

    logRequestParams = generateRequestParamsEncodeBody
      url      : generateUrl { route : '-/api/logs' }
      clientId : client.sessionToken

    request.get logRequestParams, (err, res, body) ->

      expect(err).to.not.exist
      expect(res.statusCode).to.be.equal 200
      expect(body).to.contain 'data'

      done()


  it 'should send HTTP 200 and the result in data if session is valid', (done) ->

    { client, group } = TESTUSERS.admin

    logRequestParams = generateRequestParamsEncodeBody
      url      : generateUrl { route : "-/api/logs?q=#{TESTLOG}" }
      clientId : client.sessionToken

    identifier = KodingLogger.getIdentifier TESTSCOPE, group.slug

    request.get logRequestParams, (err, res, body) -><commit_msg>Api:Logs.test: create test team once and use it from cache<commit_after>{ daisy
  expect
  request
  generateUrl
  deepObjectExtend
  generateRandomString
  generateRandomUsername
  checkBongoConnectivity
  generateRequestParamsEncodeBody } = require '../../../../testhelper'

{ withConvertedUser }               = require '../../../../../workers/social/testhelper'
{ withConvertedUserAndApiToken }    = require '../../../../../workers/social/testhelper/models/apitokenhelper'

apiErrors                           = require './errors'
KodingLogger                        = require '../../../../models/kodinglogger'


generateLogRequestParams = (opts = {}, subdomain) ->

  params    =
    url     : generateUrl { route : '-/api/logs', subdomain }
    query   : {}
    headers : { Authorization : "Bearer #{generateRandomString()}" }

  requestParams = generateRequestParamsEncodeBody params, opts

  return requestParams


TESTUSERS =
  admin   : null
  regular : null
  team    : null

TESTLOG   = generateRandomString()

# use different scope on each test ~ GG
TESTSCOPE = KodingLogger.SCOPES[Math.round(Math.random() * KodingLogger.SCOPES.length)]


beforeTests = -> before (done) ->

  KodingLogger.connect()

  checkBongoConnectivity ->

    withConvertedUser (regular) ->
      TESTUSERS.regular = regular

      withConvertedUser { role: 'admin' }, (admin) ->
        TESTUSERS.admin = admin

        KodingLogger[TESTSCOPE] TESTUSERS.admin.group.slug, TESTLOG

        options = { createGroup : yes, groupData : { isApiEnabled : yes } }

        withConvertedUserAndApiToken options, (team) ->
          TESTUSERS.team = team

          done()


runTests = -> describe 'server.handlers.api.logs', ->


  it 'should send HTTP 400 if provided api token is not valid', (done) ->

    logRequestParams = generateLogRequestParams()

    request.get logRequestParams, (err, res, body) ->
      expect(err).to.not.exist
      expect(res.statusCode).to.be.equal 400
      expect(JSON.parse body).to.be.deep.equal { error : apiErrors.invalidApiToken }

      done()


  it 'should send HTTP 401 if api token is not provided and there is no session', (done) ->

    logRequestParams = generateRequestParamsEncodeBody
      url            : generateUrl { route : '-/api/logs' }

    request.get logRequestParams, (err, res, body) ->
      expect(err).to.not.exist
      expect(res.statusCode).to.be.equal 401
      expect(JSON.parse body).to.be.deep.equal { error : apiErrors.unauthorizedRequest }

      done()


  it 'should send HTTP 401 if no api token provided and session not belongs to an admin', (done) ->

    { client } = TESTUSERS.regular

    logRequestParams = generateRequestParamsEncodeBody
      url      : generateUrl { route : '-/api/logs' }
      clientId : client.sessionToken

    request.get logRequestParams, (err, res, body) ->

      expect(err).to.not.exist
      expect(res.statusCode).to.be.equal 401
      expect(JSON.parse body).to.be.deep.equal { error : apiErrors.unauthorizedRequest }

      done()


  it 'should send HTTP 403 if group.isApiEnabled is not true', (done) ->

    { client, apiToken, group } = TESTUSERS.team

    # setting api token availability false for the group
    group.modify client, { isApiEnabled: false }, (err) ->

      expect(err).to.not.exist

      logRequestParams = generateLogRequestParams
        headers : { Authorization : "Bearer #{apiToken.code}" }

      request.get logRequestParams, (err, res, body) ->
        expect(err).to.not.exist
        expect(res.statusCode).to.be.equal 403
        expect(JSON.parse body).to.be.deep.equal { error : apiErrors.apiIsDisabled }

        # revert api token availability to true for the group
        group.modify client, { isApiEnabled: true }, (err) ->
          expect(err).to.not.exist
          done()


  it 'should send HTTP 400 if provided token valid but not subdomain', (done) ->

    { client, apiToken, group } = TESTUSERS.team

    logRequestParams = generateLogRequestParams
      headers : { Authorization : "Bearer #{apiToken.code}" }

    request.get logRequestParams, (err, res, body) ->
      expect(err).to.not.exist
      expect(res.statusCode).to.be.equal 400
      expect(JSON.parse body).to.be.deep.equal {
        error: apiErrors.invalidRequestDomain
      }

      done()


  it 'should send HTTP 200 if provided token and request domain is valid', (done) ->

    { client, apiToken, group } = TESTUSERS.team

    logRequestParams = generateLogRequestParams
      headers : { Authorization : "Bearer #{apiToken.code}" }
    , group.slug

    request.get logRequestParams, (err, res, body) ->
      expect(err).to.not.exist
      expect(res.statusCode).to.be.equal 200
      expect(body).to.contain 'data'

      done()


  it 'should send HTTP 200 if token not provided but there is a valid session', (done) ->

    { client } = TESTUSERS.admin

    logRequestParams = generateRequestParamsEncodeBody
      url      : generateUrl { route : '-/api/logs' }
      clientId : client.sessionToken

    request.get logRequestParams, (err, res, body) ->

      expect(err).to.not.exist
      expect(res.statusCode).to.be.equal 200
      expect(body).to.contain 'data'

      done()


  it 'should send HTTP 200 and the result in data if session is valid', (done) ->

    { client, group } = TESTUSERS.admin

    logRequestParams = generateRequestParamsEncodeBody
      url      : generateUrl { route : "-/api/logs?q=#{TESTLOG}" }
      clientId : client.sessionToken

    identifier = KodingLogger.getIdentifier TESTSCOPE, group.slug

    request.get logRequestParams, (err, res, body) ->
{'fraction_non_alphanumeric': 0.1152386834906909, 'fraction_numerical': 0.007941410041471808, 'mean_word_length': 2.727063465965143, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 58, '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': '7313546', 'n_tokens_mistral': 3408, 'n_tokens_neox': 3180, 'n_words': 837}