           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
+
+      it 'standard user should be able to leave from all groups', (done) ->
+
+        withConvertedUser ({ client, account, userFormData }) ->
+
+          queue = [
+
+            ->
+              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 return -1', (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 unregistered account to return -1
            account.createSocialApiId (err, socialApiId) ->
              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

  describe '#leaveFromAllGroups()', ->


{ daisy
  expect
  withDummyClient
  withConvertedUser
  generateDummyClient
  generateRandomString
  checkBongoConnectivity } = require '../../../testhelper'

JUser    = require './user'
JGroup   = require './group'
JAccount = require './account'
JSession = require './session'


# making sure we have db connection before tests
beforeTests = -> before (done) ->

  checkBongoConnectivity done


# here we have actual tests
