<commit_before>Bongo                               = require 'bongo'
koding                              = require './../bongo'

{ daisy }                           = Bongo
{ expect }                          = require 'chai'
{ Relationship }                    = require 'jraphical'
{ TeamHandlerHelper
  generateRandomEmail
  generateRandomString
  RegisterHandlerHelper }           = require '../../../testhelper'

{ generateGetTeamRequestParams
  generateJoinTeamRequestParams
  generateCreateTeamRequestParams } = TeamHandlerHelper

request                             = require 'request'
querystring                         = require 'querystring'

JUser                               = null
JGroup                              = null
JAccount                            = null
JSession                            = null
JInvitation                         = null


# here we have actual tests
runTests = -> describe 'server.handlers.getteam', ->

  beforeEach (done) ->

    # including models before each test case, requiring them outside of
    # tests suite is causing undefined errors
    { JUser
      JGroup
      JAccount
      JSession
      JInvitation } = koding.models

    done()


  it 'should send HTTP 404 if group does not exist using any method', (done) ->

    addRequestToQueue = (queue, method, requestParams) ->
      requestParams.method = method
      queue.push ->
        request requestParams, (err, res, body) ->
          expect(err)             .to.not.exist
          expect(res.statusCode)  .to.be.equal 404
          expect(body)            .to.be.equal 'no group found'
          queue.next()

    queue                 = []
    methods               = ['post', 'get', 'put', 'patch']
    groupSlug             = generateRandomString()
    getTeamRequestParams  = generateGetTeamRequestParams { groupSlug }

    for method in methods
      addRequestToQueue queue, method, getTeamRequestParams

    queue.push -> done()

    daisy queue


  it 'should send HTTP 200 if slug is valid using any method', (done) ->

    addRequestToQueue = (queue, method, requestParams) ->
      requestParams.method = method
      queue.push ->
        request requestParams, (err, res, body) ->
          expect(err)             .to.not.exist
          expect(res.statusCode)  .to.be.equal 200
          queue.next()

    queue                     = []
    methods                   = ['post', 'get', 'put', 'patch']
    groupSlug                 = generateRandomString()
    getTeamRequestParams      = generateGetTeamRequestParams { groupSlug }

    createTeamRequestParams   = generateCreateTeamRequestParams
      body    :
        slug  : groupSlug

    queue.push ->
      request.post createTeamRequestParams, (err, res, body) ->
        expect(err)             .to.not.exist
        expect(res.statusCode)  .to.be.equal 200
        queue.next()

    for method in methods
      addRequestToQueue queue, method, getTeamRequestParams

    queue.push -> done()

    daisy queue


runTests()
<commit_msg>moved request creation logic to foreach loop<commit_after>Bongo                               = require 'bongo'
koding                              = require './../bongo'

{ daisy }                           = Bongo
{ expect }                          = require 'chai'
{ Relationship }                    = require 'jraphical'
{ TeamHandlerHelper
  generateRandomEmail
  generateRandomString
  RegisterHandlerHelper }           = require '../../../testhelper'

{ generateGetTeamRequestParams
  generateJoinTeamRequestParams
  generateCreateTeamRequestParams } = TeamHandlerHelper

request                             = require 'request'
querystring                         = require 'querystring'

JUser                               = null
JGroup                              = null
JAccount                            = null
JSession                            = null
JInvitation                         = null


# here we have actual tests
runTests = -> describe 'server.handlers.getteam', ->

  beforeEach (done) ->

    # including models before each test case, requiring them outside of
    # tests suite is causing undefined errors
    { JUser
      JGroup
      JAccount
      JSession
      JInvitation } = koding.models

    done()


  it 'should send HTTP 404 if group does not exist using any method', (done) ->

    queue                 = []
    methods               = ['post', 'get', 'put', 'patch']
    groupSlug             = generateRandomString()

    methods.forEach (method) ->
      getTeamRequestParams  = generateGetTeamRequestParams { method, groupSlug }

      queue.push ->
        request getTeamRequestParams, (err, res, body) ->
          expect(err)             .to.not.exist
          expect(res.statusCode)  .to.be.equal 404
          expect(body)            .to.be.equal 'no group found'
          queue.next()

    queue.push -> done()

    daisy queue


  it 'should send HTTP 200 if slug is valid using any method', (done) ->

    queue                     = []
    methods                   = ['post', 'get', 'put', 'patch']
    groupSlug                 = generateRandomString()

    createTeamRequestParams   = generateCreateTeamRequestParams
      body    :
        slug  : groupSlug

    queue.push ->
      request.post createTeamRequestParams, (err, res, body) ->
        expect(err)             .to.not.exist
        expect(res.statusCode)  .to.be.equal 200
        queue.next()

    methods.forEach (method) ->
      getTeamRequestParams = generateGetTeamRequestParams { method, groupSlug }

      queue.push ->
        request getTeamRequestParams, (err, res, body) ->
          expect(err)             .to.not.exist
          expect(res.statusCode)  .to.be.equal 200
          queue.next()

    queue.push -> done()

    daisy queue


runTests()

{'fraction_non_alphanumeric': 0.08616546515619636, 'fraction_numerical': 0.005149330587023687, 'mean_word_length': 1.6013392857142856, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5387972', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1438, 'n_words': 394}