2008-03-31
基于django0.96的自动测试功能的Login增强
0.96不支持client对象,先Login再提交数据。因此,参照trunk,改写了Client
def login_new(self, **credentials):
"""Set the Client to appear as if it has sucessfully logged into a site.
Returns True if login is possible; False if the provided credentials
are incorrect, or the user is inactive, or if the sessions framework is
not available.
"""
user = authenticate(**credentials)
if user and user.is_active and 'django.contrib.sessions' in settings.INSTALLED_APPS:
#engine = __import__(settings.SESSION_ENGINE, {}, {}, [''])
# Create a fake request to store login details
request = HttpRequest()
#request.session = engine.SessionStore()
from django.contrib.sessions.middleware import SessionMiddleware
SessionMiddleware().process_request( request )
login(request, user)
obj = Session.objects.get_new_session_object()
session_key = obj.session_key
# Set the cookie to represent the session
self.cookies[settings.SESSION_COOKIE_NAME] = session_key
self.cookies[settings.SESSION_COOKIE_NAME]['max-age'] = None
self.cookies[settings.SESSION_COOKIE_NAME]['path'] = '/'
self.cookies[settings.SESSION_COOKIE_NAME]['domain'] = settings.SESSION_COOKIE_DOMAIN
self.cookies[settings.SESSION_COOKIE_NAME]['secure'] = settings.SESSION_COOKIE_SECURE or None
self.cookies[settings.SESSION_COOKIE_NAME]['expires'] = None
# Save the session values
#request.session.save()
new_session = Session.objects.save(session_key, request.session._session,
datetime.datetime.now() + datetime.timedelta(seconds=settings.SESSION_COOKIE_AGE))
return True
else:
return False
发表评论
- 浏览: 5199 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
jboss rules brms和ruleAg ...
楼主能把完整的文档贴出来吗
-- by dong_ta -
jboss rules brms和ruleAg ...
我用代理怎么老是出现这种错误啊!麻烦给指点一下哈!我的版本一致. RuleAge ...
-- by chunchun_1010 -
jboss rules brms和ruleAg ...
我在创建新的Rule时,在"Add a condition to the rul ...
-- by myim -
我的第一个Ajax Wicket ...
个人感觉javaeye的圈子似乎有些冷清。我觉得可能跟javaeye即有圈子,又 ...
-- by tailorcai -
我的第一个Ajax Wicket ...
为啥不加入wicket的圈子,然后把这两篇文章放到圈子博客里呢
-- by fakechris






评论排行榜