-{ argv }                    = require 'optimist'
-{ expect }                  = require "chai"
-{ env : { MONGO_URL } }     = process
-KONFIG                      = require('koding-config-manager').load("main.#{argv.c}")
-mongo                       = MONGO_URL or "mongodb://#{ KONFIG.mongo }"
-Bongo                       = require 'bongo'
-{ daisy }                   = Bongo
-JUser                       = require './user'
-JGroup                      = require './group'
-JAccount                    = require './account'
-JInvitation                 = require './invitation'
-JSession                    = require './session'
-{ generateRandomString
-  generateDummyClientData
+{ argv }                      = require 'optimist'
+{ expect }                    = require "chai"
+{ env : { MONGO_URL } }       = process
+KONFIG                        = require('koding-config-manager').load("main.#{argv.c}")
+mongo                         = MONGO_URL or "mongodb://#{ KONFIG.mongo }"
+Bongo                         = require 'bongo'
+{ daisy }                     = Bongo
+JUser                         = require './user'
+JGroup                        = require './group'
+JAccount                      = require './account'
+JSession                      = require './session'
+JInvitation                   = require './invitation'
+{ generateRandomEmail
+  generateDummyClient
+  generateRandomString
   generateDummyUserFormData } = require '../../../testhelper'
 
 
-bongo             = null
-adminClient       = null
-group             = null
+registerUserGenerateGroup = (adminClient, adminUserFormData, callback) ->
 
+  # create our admin user
+  JUser.convert adminClient, adminUserFormData, (err, data) ->
+    expect(err).to.not.exist
+    { account, newToken }   = data
+
+    # set credentials
+    adminClient.sessionToken        = newToken
+    adminClient.connection.delegate = account
+
+    # generate group creation data
+    groupData =
+      slug           : generateRandomString()
+      visibility     : 'visible'
+      title          : generateRandomString()
+      allowedDomains : [ 'koding.com' ]
+
+    # create the group
+    JGroup.create adminClient, groupData, account, (err, data) ->
+      group = data
+      adminClient.context.group = group.slug # set our new group's name
+      callback err, group
+
+
+# making sure we have mongo connection before tests
+beforeTests = -> before (done) ->
+
+  bongo = new Bongo
+    root   : __dirname
+    mongo  : mongo
+    models : ''
+
+  bongo.once 'dbClientReady', ->
+    done()
+
+
+# here we have actual tests
 runTests = ->
 
-  before (done) ->
-    adminClient = generateDummyClientData()
-    adminUserFormData = generateDummyUserFormData()
-
-    bongo = new Bongo
-      root   : __dirname
-      mongo  : mongo
-      models : '../../models'
-
-    bongo.once 'dbClientReady', ->
-
-      # creating a session before running tests
-      JSession.createSession (err, { session, account }) ->
-
-        adminClient.sessionToken        = session.token
-        adminClient.connection.delegate = account
-
-        # create our admin user
-        JUser.convert adminClient, adminUserFormData, (err, data) ->
-          expect(err).to.not.exist
-          { account, newToken }   = data
-
-          # set credentials
-          adminClient.sessionToken        = newToken
-          adminClient.connection.delegate = account
-
-          # generate group creation data
-          groupData =
-            slug           : generateRandomString()
-            visibility     : 'visible'
-            title          : generateRandomString()
-            allowedDomains : [ 'koding.com' ]
-
-          # create the group
-          JGroup.create adminClient, groupData, account, (err, data) ->
-            expect(err).to.not.exist
-
-            group = data
-            adminClient.context.group = group.slug # set our new group's name
-            done()
-
-
   describe 'workers.social.invitation.create', ->
-    client       = {}
-    userFormData = {}
-
-    beforeEach (done) ->
-      # create session data before each test
-      client = generateDummyClientData()
-      client.context.group = group.slug
-      userFormData = generateDummyUserFormData()
-
-      JSession.createSession (err, { session, account }) ->
-        expect(err).to.not.exist
-
-        client.sessionToken        = session.token
-        client.connection.delegate = account
-
-        done()
-
 
     describe 'if user is not a member yet', ->
 
       it 'should receive the invitation', (done) ->
+
+        group             = {}
+        client            = {}
+        adminClient       = {}
+        userFormData      = generateDummyUserFormData()
+        adminUserFormData = generateDummyUserFormData()
+
+
         queue = [
 
           ->
+            generateDummyClient { group : 'koding' }, (err, client_) ->
+              expect(err).to.not.exist
+              adminClient = client_
+              queue.next()
+
+          ->
+            # registering admin and generating a new group
+            registerUserGenerateGroup adminClient, adminUserFormData, (err, group_) ->
+              expect(err).to.not.exist
+              group = group_
+              queue.next()
+
+          ->
+            # generating new client object and setting it's group context
+            generateDummyClient { group : 'koding' }, (err, client_) ->
+              expect(err).to.not.exist
+              client = client_
+              client.context.group = group.slug
+              queue.next()
+
+          ->
+            # creating an invitation for the unregistered user
             invitationReq = {
               invitations:[
                 { email: userFormData.email }


        daisy queue

runTests()

              queue.next()

          ->
            # expecting invitation to exist
            JInvitation.one { email: userFormData.email, groupName: client.context.group }, (err, invitation) ->
              expect(err).to.not.exist
              expect(invitation).to.exist
              queue.next()

          -> done()

