 
 generateFakeClient = (options, callback) ->
 
-  {clientId, groupName, section} = options
+  { clientId, groupName, section } = options
 
   fakeClient    =
     context       :


  { JSession, JAccount } = bongo.models

  JSession.fetchSession clientId, (err, response)->

    return handleError err, callback  if err

    if not response or not response.session
      return handleError new Error "Session is not set", callback

    { session } = response
    { username, groupName } = session

    JAccount.one { "profile.nickname": username }, (err, account) ->
      # we can ignore err here
      prepareFakeClient fakeClient, {groupName, session, username, account}
      return callback null, fakeClient, session

prepareFakeClient = (fakeClient, options) ->
  {groupName, session, username, account, sessionToken} = options

  {JAccount}      = bongo.models

  unless account
    account         = new JAccount
    account.profile = nickname: username
    account.type    = 'unregistered'

  fakeClient.sessionToken = sessionToken ? session.clientId

  # set username into context
  fakeClient.context or= {}
  fakeClient.context.group = groupName or fakeClient.context.group
  fakeClient.context.user  = session.username or fakeClient.context.user

  # create connection property
  fakeClient.connection or= {}
  fakeClient.connection.delegate  = account or fakeClient.connection.delegate
  fakeClient.connection.groupName = groupName or fakeClient.connection.groupName

  fakeClient.impersonating = session.impersonating or false


module.exports = { generateFakeClient: generateFakeClientFromReq, updateCookie}

  if not clientId and req.pendingCookies?.clientId
    clientId = req.pendingCookies.clientId

  generateFakeClient { clientId, groupName, section }, (err, fakeClient, session) ->

    return callback err  if err

    { delegate } = fakeClient.connection

    updateCookie req, res, session

