 
     queue = [
 
-      ->
-        request.post registerRequestParams, (err, res, body) ->
-          expect(err)             .to.not.exist
-          expect(res.statusCode)  .to.be.equal 200
-          queue.next()
-
-      ->
-        request.post validateEmailRequestParams, (err, res, body) ->
-          expect(err)             .to.not.exist
-          expect(res.statusCode)  .to.be.equal 400
-          expect(body)            .to.be.equal 'Bad request'
-          queue.next()
-
-      -> done()
-
-    ]
-
-    daisy queue
+      (next) ->
+        request.post registerRequestParams, (err, res, body) ->
+          expect(err).to.not.exist
+          expect(res.statusCode).to.be.equal 200
+          next()
+
+      (next) ->
+        request.post validateEmailRequestParams, (err, res, body) ->
+          expect(err).to.not.exist
+          expect(res.statusCode).to.be.equal 400
+          expect(body).to.be.equal 'Bad request'
+          next()
+
+    ]
+
+    async.series queue, done
 
 
   it 'should send HTTP 400 if 2FA was activated for the account', (done) ->

    username  = generateRandomUsername()

    registerRequestParams = generateRegisterRequestParams
      body     :
        email  : email

    validateEmailRequestParams = generateValidateEmailRequestParams
      body     :
        email  : email

    queue = [

      ->
        # registering a new user
        request.post registerRequestParams, (err, res, body) ->
          expect(err)             .to.not.exist
          expect(res.statusCode)  .to.be.equal 200
          queue.next()

      ->
        # expecting email validation to fail using already registered email
        request.post validateEmailRequestParams, (err, res, body) ->
          expect(err)             .to.not.exist
          expect(res.statusCode)  .to.be.equal 400
          expect(body)            .to.be.equal 'Bad request'
          queue.next()

      -> done()

    ]

    daisy queue


  it 'should send HTTP 400 if dotted gmail address is in use', (done) ->

    email     = generateRandomEmail 'gmail.com'
    username  = generateRandomUsername()

    registerRequestParams = generateRegisterRequestParams
      body     :
        email  : email

    [username, host] = email.split '@'



    queue = [

      (next) ->
        # registering new user
        request.post registerRequestParams, (err, res, body) ->
          expect(err).to.not.exist
          expect(res.statusCode).to.be.equal 200
          next()

      (next) ->
        # expecting email with invalid password to fail
        request.post validateEmailRequestParams, (err, res, body) ->
          expect(err).to.not.exist
          expect(res.statusCode).to.be.equal 200
          expect(body).to.be.equal 'User is logged in!'
          next()

    ]

    async.series queue, done


  it 'should send HTTP 200 if email is valid and not in use', (done) ->

    cookieJar = request.jar()

    validateEmailRequestParams = generateValidateEmailRequestParams
      jar     : cookieJar
      body    :
        email : generateRandomEmail()

    url = validateEmailRequestParams.url

    request.post validateEmailRequestParams, (err, res, body) ->
      expect(err).to.not.exist
      expect(res.statusCode).to.be.equal 200
      # expecting clientId cookie to be set
      expect(cookieJar.getCookieString url).to.contain 'clientId'
      expect(body).to.be.equal 'true'
      done()


beforeTests()

runTests()
