         email     : email
         username  : username
 
-    createTeamRequestParams = generateCreateTeamRequestParams
-      body        :
-        email     : email
-        username  : anotherUsername
-
     queue = [
 
       ->

          queue.next()

      ->
        # expecting HTTP 400 from juser.convert
        expectedBody = 'Email is already in use!'
        request.post createTeamRequestParams, (err, res, body) ->
          expect(err)             .to.not.exist
          expect(res.statusCode)  .to.be.equal 400
          expect(body)            .to.be.equal expectedBody
          queue.next()

      -> done()

    ]

    daisy queue


  it 'should send HTTP 400 when username is not set', (done) ->

    expectedBody            = 'Errors were encountered during validation: username'
    createTeamRequestParams = generateCreateTeamRequestParams
      body        :
        username  : ''

    request.post createTeamRequestParams, (err, res, body) ->
      expect(err)             .to.not.exist
      expect(res.statusCode)  .to.be.equal 400
      expect(body)            .to.be.equal expectedBody
      done()


  it 'should send HTTP 400 when passwords does not match', (done) ->

    # sending different passwords
    createTeamRequestParams = generateCreateTeamRequestParams
      body              :
        password        : 'somePassword'
        passwordConfirm : 'anotherPassword'

    request.post createTeamRequestParams, (err, res, body) ->
      expect(err)             .to.not.exist
      expect(res.statusCode)  .to.be.equal 400
      expect(body)            .to.be.equal 'Passwords must match!'
      done()


  it 'should send HTTP 400 when agree is set as false', (done) ->

    expectedBody            = 'Errors were encountered during validation: agree'
    createTeamRequestParams = generateCreateTeamRequestParams
      body    :
        agree : 'false'

    request.post createTeamRequestParams, (err, res, body) ->
      expect(err)             .to.not.exist
      expect(res.statusCode)  .to.be.equal 400
      expect(body)            .to.be.equal expectedBody
      done()


  describe 'when teamAccessCode is provided', ->

    it 'should send HTTP 400 if teamAccessCode non-existent', (done) ->

      expectedBody            = 'Team Invitation is not found'
      createTeamRequestParams = generateCreateTeamRequestParams
        body             :
          teamAccessCode : 'someNonExistentTeamAccessCode'

      request.post createTeamRequestParams, (err, res, body) ->
        expect(err)             .to.not.exist
        expect(res.statusCode)  .to.be.equal 400
        expect(body)            .to.be.equal expectedBody
        done()


runTests()



  it 'should send HTTP 403 when slug is set as koding', (done) ->

    generateCreateTeamRequestParams {
      body : { slug : 'koding' }
    }, (createTeamRequestParams) ->

      expectedBody = "Sorry, Team URL 'koding.#{hostname}' is already in use"
      request.post createTeamRequestParams, (err, res, body) ->
        expect(err)             .to.not.exist
        expect(res.statusCode)  .to.be.equal 403
        expect(body)            .to.be.equal expectedBody
        done()


  it 'should send HTTP 403 when slug is set as an already used one', (done) ->

    slug                    = generateRandomString()

    queue = [

      ->
        generateCreateTeamRequestParams {
          body : { slug }
        }, (createTeamRequestParams) ->

          # expecting first team create request to be successful
          request.post createTeamRequestParams, (err, res, body) ->
            expect(err)             .to.not.exist
            expect(res.statusCode)  .to.be.equal 200
            queue.next()

      ->
        generateCreateTeamRequestParams {
          body : { slug }
        }, (createTeamRequestParams) ->

          # expecting second team crete request with the same slug to fail
          expectedBody = "Sorry, Team URL '#{slug}.#{hostname}' is already in use"
          request.post createTeamRequestParams, (err, res, body) ->
            expect(err)             .to.not.exist
            expect(body)            .to.be.equal expectedBody
            expect(res.statusCode)  .to.be.equal 403
            expect(body)            .to.be.equal expectedBody
            queue.next()

      -> done()

