def make_logged_in_client(username='test', password='test', is_superuser=True, recreate=False, groupname=None):
 try:
  user = User.objects.get(username=username)
  if recreate:
   user.delete()
   raise User.DoesNotExist
 except User.DoesNotExist:
  user = User.objects.create_user(username, (username + '@localhost'), password)
  user.is_superuser = is_superuser
  user.save()
 else:
  if (user.is_superuser != is_superuser):
   user.is_superuser = is_superuser
   user.save()
 if (groupname is not None):
  (group, created) = Group.objects.get_or_create(name=groupname)
  if (not user.groups.filter(name=group.name).exists()):
   user.groups.add(group)
   user.save()
 c = Client()
 ret = c.login(username=username, password=password)
 assert ret, ("Login failed (user '%s')." % username)
 return c

def make_logged_in_client ( username = 'test' , password = 'test' , is_superuser = True , recreate = False , groupname = None ) : try : user = User . objects . get ( username = username ) if recreate : user . delete ( ) raise User . DoesNotExist except User . DoesNotExist : user = User . objects . create_user ( username , ( username + '@localhost' ) , password ) user . is_superuser = is_superuser user . save ( ) else : if ( user . is_superuser != is_superuser ) : user . is_superuser = is_superuser user . save ( ) if ( groupname is not None ) : ( group , created ) = Group . objects . get_or_create ( name = groupname ) if ( not user . groups . filter ( name = group . name ) . exists ( ) ) : user . groups . add ( group ) user . save ( ) c = Client ( ) ret = c . login ( username = username , password = password ) assert ret , ( "Login failed (user '%s')." % username ) return c
create a client with a user already logged in .