 
       it 'should return -1', (done) ->
 
-        client        = null
-        account       = null
-        userFormData  = generateDummyUserFormData()
-
-        queue = [
-
-          ->
-            # generating dummy client
-            generateDummyClient { group : 'koding' }, (err, client_) ->
+        withDummyClient ({ client, account }) ->
+          # expecting unregistered account to return -1
+          account.createSocialApiId (err, socialApiId) ->
+            expect(err).to.not.exist
+            expect(socialApiId).to.be.equal -1
+            done()
+
+
+
+    describe 'when account type is not unregistered', ->
+
+      it 'should return socialApiId if socialApiId is already set', (done) ->
+
+        withConvertedUser ({ client, account }) ->
+          socialApiId = '12345'
+
+          queue = [
+
+            ->
+              # setting social api id
+              account.update { $set : { socialApiId : socialApiId } }, (err) ->
+                expect(err).to.not.exist
+                queue.next()
+
+            ->
+              # expecting createsocialApiId method to return accountId
+              account.createSocialApiId (err, socialApiId_) ->
+                expect(err).to.not.exist
+                expect(socialApiId_).to.be.equal socialApiId
+                queue.next()
+
+            -> done()
+
+          ]
+
+          daisy queue
+
+
+      it 'should create social api id if account\'s socialApiId is not set', (done) ->
+
+        withConvertedUser ({ client, account }) ->
+
+          queue = [
+
+            ->
+              # unsetting account's socialApiId
+              account.socialApiId = null
+              account.update { $unset : { 'socialApiId' : 1 } }, (err) ->
+                expect(err).to.not.exist
+                expect(account.getAt 'socialApiId').to.not.exist
+                queue.next()
+
+            ->
+              # creating new social api id
+              account.createSocialApiId (err, socialApiId_) ->
+                expect(err).to.not.exist
+                expect(socialApiId_).to.exist
+
+                # expecting account's social api id to be set
+                expect(account.getAt 'socialApiId').to.exist
+                expect(account.socialApiId).to.exist
+                queue.next()
+
+            -> done()
+
+          ]
+
+          daisy queue
+
+
+  describe '#fetchMyPermissions()', ->
+
+    describe 'when group does not exist', ->
+
+      it 'should return error', (done) ->
+
+        withDummyClient { group : 'someInvalidGroup' }, ({ client, account }) ->
+          # expecting error when client's group does not exist
+          account.fetchMyPermissions client, (err, permissions) ->
+            expect(err?.message).to.be.equal 'group not found'
+            done()
+
+
+    describe 'when group exists', ->
+
+      describe 'if account is valid', ->
+
+        it 'should return client\'s permissions', (done) ->
+
+          withDummyClient ({ client, account }) ->
+            # expecting to be able to get permissions
+            account.fetchMyPermissions client, (err, permissions) ->
               expect(err).to.not.exist
-              client  = client_
-              account = client.connection.delegate
-              queue.next()
-
-          ->
-            # expecting unregistered account to return -1
-            account.createSocialApiId (err, socialApiId) ->
+              expect(permissions).to.exist
+              expect(permissions).to.be.an 'object'
+              done()
+
+
+      describe 'if group slug is not defined', ->
+
+        it 'should set the slug as koding and return permissions', (done) ->
+
+          withDummyClient ({ client, account }) ->
+            # expecting to be able to get permissions
+            account.fetchMyPermissions client, (err, permissions) ->
               expect(err)          .to.not.exist
-              expect(socialApiId)  .to.be.equal -1
-              queue.next()
-
-          -> done()
-
-        ]
-
-        daisy queue
-
-
-    describe 'when account type is not unregistered', ->
-
-      it 'should return socialApiId if socialApiId is already set', (done) ->
-
-        client        = null
-        account       = null
-        socialApiId   = '12345'
-        userFormData  = generateDummyUserFormData()
-
-        queue = [
-
-          ->
-            # generating dummy client
-            generateDummyClient { group : 'koding' }, (err, client_) ->
-              expect(err).to.not.exist
-              client  = client_
-              account = client.connection.delegate
-              queue.next()
-
-          ->
-            # registering user
-            JUser.convert client, userFormData, (err, data) ->
-              expect(err).to.not.exist
-              # set credentials
-              { account, newToken }      = data
-              client.sessionToken        = newToken
-              client.connection.delegate = account
-              queue.next()
-
-          ->
-            # setting social api id
-            account.update { $set : { socialApiId : socialApiId } }, (err) ->
-              expect(err).to.not.exist
-              queue.next()
-
-          ->
-            # expecting createsocialApiId method to return accountId
-            account.createSocialApiId (err, socialApiId_) ->
-              expect(err)          .to.not.exist
-              expect(socialApiId_) .to.be.equal socialApiId
-              queue.next()
-
-          -> done()
-
-        ]
-
-        daisy queue
-
-
-      it 'should create social api id if account\'s socialApiId is not set', (done) ->
-
-        client        = null
-        account       = null
-        userFormData  = generateDummyUserFormData()
-
-        queue = [
-
-          ->
-            # generating dummy client
-            generateDummyClient { group : 'koding' }, (err, client_) ->
-              expect(err).to.not.exist
-              client  = client_
-              account = client.connection.delegate
-              queue.next()
-
-          ->
-            # registering user
-            JUser.convert client, userFormData, (err, data) ->
-              expect(err).to.not.exist
-              # set credentials
-              { account, newToken }      = data
-              client.sessionToken        = newToken
-              client.connection.delegate = account
-              queue.next()
-
-          ->
-            # unsetting account's socialApiId
-            account.socialApiId = null
-            account.update { $unset : { 'socialApiId' : 1 } }, (err) ->
-              expect(err)                          .to.not.exist
-              expect(account.getAt 'socialApiId')  .to.not.exist
-              queue.next()
-
-          ->
-            # creating new social api id
-            account.createSocialApiId (err, socialApiId_) ->
-              expect(err)           .to.not.exist
-              expect(socialApiId_)  .to.exist
-              queue.next()
-
-          ->
-            # expecting account's social api id to be set
-            expect(account.getAt 'socialApiId')  .to.exist
-            expect(account.socialApiId)          .to.exist
-            queue.next()
-
-          -> done()
-
-        ]
-
-        daisy queue
-
-
-  describe '#fetchMyPermissions()', ->
-
-    describe 'when group does not exist', ->
-
-      it 'should return error', (done) ->
-
-        client        = null
-        account       = null
-        userFormData  = generateDummyUserFormData()
-
-        queue = [
-
-          ->
-            # generating dummy client
-            generateDummyClient { group : 'someInvalidGroup' }, (err, client_) ->
-              expect(err).to.not.exist
-              client  = client_
-              account = client.connection.delegate
-              queue.next()
-
-          ->
-            # expecting error when client's group does not exist
-            account.fetchMyPermissions client, (err, permissions) ->
-              expect(err?.message).to.be.equal 'group not found'
-              queue.next()
-
-          -> done()
-
-        ]
-
-        daisy queue
-
-
-    describe 'when group exists', ->
-
-      describe 'if account is valid', ->
-
-        it 'should return client\'s permissions', (done) ->
-
-          client        = null
-          account       = null
-          userFormData  = generateDummyUserFormData()
-
-          queue = [
-
-            ->
-              # generating dummy client
-              generateDummyClient { group : 'koding' }, (err, client_) ->
-                expect(err).to.not.exist
-                client  = client_
-                account = client.connection.delegate
-                queue.next()
-
-            ->
-              # expecting to be able to get permissions
-              account.fetchMyPermissions client, (err, permissions) ->
-                expect(err)          .to.not.exist
-                expect(permissions)  .to.exist
-                expect(permissions)  .to.be.an 'object'
-                queue.next()
-
-            -> done()
-
-          ]
-
-          daisy queue
-
-      describe 'if group slug is not defined', ->
-
-        it 'should set the slug as koding and return permissions', (done) ->
-
-          client        = null
-          account       = null
-          userFormData  = generateDummyUserFormData()
-
-          queue = [
-
-            ->
-              # generating dummy client
-              generateDummyClient { group : 'koding' }, (err, client_) ->
-                expect(err).to.not.exist
-                client               = client_
-                client.context.group = null
-                account              = client.connection.delegate
-                queue.next()
-
-            ->
-              # expecting to be able to get permissions
-              account.fetchMyPermissions client, (err, permissions) ->
-                expect(err)          .to.not.exist
-                expect(permissions)  .to.exist
-                expect(permissions)  .to.be.an 'object'
-                queue.next()
-
-            -> done()
-
-          ]
-
-          daisy queue
+              expect(permissions)  .to.exist
+              expect(permissions)  .to.be.an 'object'
+              done()
 
   describe '#leaveFromAllGroups()', ->
 

          expect(groups).to.have.length.above(1)
          done()

      it 'standart user should be able to leave from all groups', (done) ->
        client       = null
        account      = null
        userFormData = generateDummyUserFormData()

        queue = [

          ->
            generateDummyClient { group : 'koding' }, (err, client_) ->
              expect(err).to.not.exist
              client = client_
              queue.next()

          ->
            # registering admin client
            JUser.convert client, userFormData, (err, data) ->
              expect(err).to.not.exist
              { account, newToken } = data

              client.sessionToken        = newToken
              client.connection.delegate = account
              client.context.group       = 'koding'
              queue.next()

          ->
            JUser = require './user'
            JUser.addToGroup account, group.slug, userFormData.email, null, (err) ->
              expect(err).to.not.exist
              queue.next()

          ->
            account.fetchAllParticipatedGroups client, (err, groups) ->
              expect(err).to.not.exist
              expect(groups).to.have.length.above(1)
              queue.next()

          ->
            account.leaveFromAllGroups client, (err) ->
              expect(err).to.not.exist
              queue.next()

          ->
            account.fetchAllParticipatedGroups client, (err, groups) ->
              expect(err).to.not.exist
              expect(groups).to.have.length(1)
              queue.next()

          ->
            done()

        ]

        daisy queue

beforeTests()



    it 'should pass error if fields contain invalid key', (done) ->

      withConvertedUser ({ client, account }) ->
        fields = { someInvalidField : 'someInvalidField' }
        account.modify client, fields, (err) ->
          expect(err?.message).to.be.equal 'Modify fields is not valid'
          done()


    it 'should update given fields correctly', (done) ->

      withConvertedUser ({ client, account }) ->

        fields =
          'profile.about'     : 'newAbout'
          'profile.lastName'  : 'newLastName'
          'profile.firstName' : 'newFirstName'

        # expecting account to be modified
        account.modify client, fields, (err, data) ->
          expect(err).to.not.exist

          # expecting account's values to be changed
          for key, value of fields
            expect(account.getAt key).to.be.equal value
          done()


  describe '#createSocialApiId()', ->
