本文整理汇总了Python中django.contrib.sessions.middleware.SessionMiddleware方法的典型用法代码示例。如果您正苦于以下问题:Python middleware.SessionMiddleware方法的具体用法?Python middleware.SessionMiddleware怎么用?Python middleware.SessionMiddleware使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.sessions.middleware
的用法示例。
在下文中一共展示了middleware.SessionMiddleware方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
# 需要导入模块: from django.contrib.sessions import middleware [as 别名]
# 或者: from django.contrib.sessions.middleware import SessionMiddleware [as 别名]
def setUp(self):
self.factory = RequestFactory()
self.sessions = SessionMiddleware()
self.messages = MessageMiddleware()
self.event1 = models.Event.objects.create(
datetime=today_noon,
targetamount=5,
timezone=pytz.timezone(getattr(settings, 'TIME_ZONE', 'America/Denver')),
)
self.run1 = models.SpeedRun.objects.create(
name='Test Run 1', run_time='0:45:00', setup_time='0:05:00', order=1
)
self.run2 = models.SpeedRun.objects.create(
name='Test Run 2', run_time='0:15:00', setup_time='0:05:00', order=2
)
if not User.objects.filter(username='admin').exists():
User.objects.create_superuser('admin', 'nobody@example.com', 'password')
示例2: test_login_no_ticket_stores_explicit_next
# 需要导入模块: from django.contrib.sessions import middleware [as 别名]
# 或者: from django.contrib.sessions.middleware import SessionMiddleware [as 别名]
def test_login_no_ticket_stores_explicit_next(settings):
"""
When there is an explicit next pointer, it gets stored in the cookie
"""
settings.CAS_STORE_NEXT = True
factory = RequestFactory()
request = factory.get('/login/', {'next': '/admin/'})
# Create a session object from the middleware
process_request_for_middleware(request, SessionMiddleware)
# Create a user object from middleware
process_request_for_middleware(request, AuthenticationMiddleware)
response = LoginView().get(request)
assert response.status_code == 302
assert 'CASNEXT' in request.session
assert request.session['CASNEXT'] == '/admin/'
示例3: test_logout_not_completely
# 需要导入模块: from django.contrib.sessions import middleware [as 别名]
# 或者: from django.contrib.sessions.middleware import SessionMiddleware [as 别名]
def test_logout_not_completely(django_user_model, settings):
"""
Test the case where the user logs out, without the logout_completely flag.
"""
settings.CAS_LOGOUT_COMPLETELY = False
factory = RequestFactory()
request = factory.get('/logout/')
# Create a session object from the middleware
process_request_for_middleware(request, SessionMiddleware)
user = django_user_model.objects.create_user('test@example.com', '')
assert user is not None
request.user = user
response = LogoutView().get(request)
assert response.status_code == 302
assert request.user.is_anonymous is True
示例4: test_logout_completely
# 需要导入模块: from django.contrib.sessions import middleware [as 别名]
# 或者: from django.contrib.sessions.middleware import SessionMiddleware [as 别名]
def test_logout_completely(django_user_model, settings):
"""
Test the case where the user logs out.
"""
settings.CAS_LOGOUT_COMPLETELY = True
factory = RequestFactory()
request = factory.get('/logout/')
# Create a session object from the middleware
process_request_for_middleware(request, SessionMiddleware)
user = django_user_model.objects.create_user('test@example.com', '')
assert user is not None
request.user = user
response = LogoutView().get(request)
assert response.status_code == 302
assert request.user.is_anonymous is True
示例5: test_add_user_tokens_signal
# 需要导入模块: from django.contrib.sessions import middleware [as 别名]
# 或者: from django.contrib.sessions.middleware import SessionMiddleware [as 别名]
def test_add_user_tokens_signal(self):
User = get_user_model()
user = User.objects.create(username=settings.COGNITO_TEST_USERNAME)
user.access_token = 'access_token_value'
user.id_token = 'id_token_value'
user.refresh_token = 'refresh_token_value'
user.backend = 'warrant.django.backend.CognitoBackend'
user.api_key = 'abcdefg'
user.api_key_id = 'ab-1234'
request = RequestFactory().get('/login')
middleware = SessionMiddleware()
middleware.process_request(request)
request.session.save()
signals.user_logged_in.send(sender=user.__class__, request=request, user=user)
self.assertEqual(request.session['ACCESS_TOKEN'], 'access_token_value')
self.assertEqual(request.session['ID_TOKEN'], 'id_token_value')
self.assertEqual(request.session['REFRESH_TOKEN'], 'refresh_token_value')
self.assertEqual(request.session['API_KEY'], 'abcdefg')
self.assertEqual(request.session['API_KEY_ID'], 'ab-1234')
示例6: test_login_default
# 需要导入模块: from django.contrib.sessions import middleware [as 别名]
# 或者: from django.contrib.sessions.middleware import SessionMiddleware [as 别名]
def test_login_default(self):
url = reverse(self.URL_NAME)
data = {
"username": "api",
"password": "api"
}
request = RequestFactory().post(url, data=data)
request.user = AnonymousUser()
middleware = SessionMiddleware()
middleware.process_request(request)
request.session.save()
response = views.login_user(request)
assert response.status_code == 302
assert response.url == reverse("productdb:home")
示例7: test_login_ignore_next_login_link
# 需要导入模块: from django.contrib.sessions import middleware [as 别名]
# 或者: from django.contrib.sessions.middleware import SessionMiddleware [as 别名]
def test_login_ignore_next_login_link(self):
url = reverse(self.URL_NAME)
data = {
"username": "api",
"password": "api"
}
request = RequestFactory().post(url + "?next=/productdb/login", data=data)
request.user = AnonymousUser()
middleware = SessionMiddleware()
middleware.process_request(request)
request.session.save()
response = views.login_user(request)
assert response.status_code == 302
assert response.url == reverse("productdb:home"), "Should ignore the redirect to the login link"
示例8: test_login_with_next_link
# 需要导入模块: from django.contrib.sessions import middleware [as 别名]
# 或者: from django.contrib.sessions.middleware import SessionMiddleware [as 别名]
def test_login_with_next_link(self):
url = reverse(self.URL_NAME)
data = {
"username": "api",
"password": "api"
}
request = RequestFactory().post(url + "?next=/xyz", data=data)
request.user = AnonymousUser()
middleware = SessionMiddleware()
middleware.process_request(request)
request.session.save()
response = views.login_user(request)
assert response.status_code == 302
assert response.url == "/xyz"
示例9: test_login_failed
# 需要导入模块: from django.contrib.sessions import middleware [as 别名]
# 或者: from django.contrib.sessions.middleware import SessionMiddleware [as 别名]
def test_login_failed(self):
url = reverse(self.URL_NAME)
data = {
"username": "api",
"password": "invalid password"
}
request = RequestFactory().post(url, data=data)
request.user = AnonymousUser()
middleware = SessionMiddleware()
middleware.process_request(request)
request.session.save()
response = views.login_user(request)
assert response.status_code == 200
assert "Login failed, invalid credentials" in response.content.decode()
示例10: test_authenticate
# 需要导入模块: from django.contrib.sessions import middleware [as 别名]
# 或者: from django.contrib.sessions.middleware import SessionMiddleware [as 别名]
def test_authenticate(self):
factory = RequestFactory()
middleware = SessionMiddleware()
anon = AnonymousUser()
request = factory.get("/foo")
middleware.process_request(request)
request.user = anon
user1 = get_user_model().objects.create_user(username="Finbar")
token = RequestToken.objects.create_token(
user=user1,
scope="foo",
max_uses=10,
login_mode=RequestToken.LOGIN_MODE_REQUEST,
)
token.authenticate(request)
self.assertEqual(request.user, user1)
request.user = get_user_model().objects.create_user(username="Hyde")
self.assertRaises(InvalidAudienceError, token.authenticate, request)
示例11: _get_request_with_session
# 需要导入模块: from django.contrib.sessions import middleware [as 别名]
# 或者: from django.contrib.sessions.middleware import SessionMiddleware [as 别名]
def _get_request_with_session(self, data):
"""
Why the song-and-dance with middleware? Well. RequestFactory() lets us
add a user to the request, e.g. in order to pass our ToURequired test,
but doesn't let us access the session by default; Client() lets us see
the session, but not add a user to the request. We need to pass our
access test *and* see the session, so we need to:
* use RequestFactory() to add a user to the request
* invoke SessionMiddleware to bring the session into being
* actually generate the response, so that form_valid is invoked,
since that is where the session key is added
If you were getting the sense that class-based views are sometimes
hostile to unit testing, you were right.
"""
request = RequestFactory().post(self.url, data=data, follow=True)
request.user = self.editor
middleware = SessionMiddleware()
middleware.process_request(request)
request.session.save()
_ = views.RequestApplicationView.as_view()(request)
return request
示例12: test_httponly_session_cookie
# 需要导入模块: from django.contrib.sessions import middleware [as 别名]
# 或者: from django.contrib.sessions.middleware import SessionMiddleware [as 别名]
def test_httponly_session_cookie(self):
request = RequestFactory().get('/')
response = HttpResponse('Session test')
middleware = SessionMiddleware()
# Simulate a request the modifies the session
middleware.process_request(request)
request.session['hello'] = 'world'
# Handle the response through the middleware
response = middleware.process_response(request, response)
self.assertIs(response.cookies[settings.SESSION_COOKIE_NAME]['httponly'], True)
self.assertIn(
cookies.Morsel._reserved['httponly'],
str(response.cookies[settings.SESSION_COOKIE_NAME])
)
示例13: test_no_httponly_session_cookie
# 需要导入模块: from django.contrib.sessions import middleware [as 别名]
# 或者: from django.contrib.sessions.middleware import SessionMiddleware [as 别名]
def test_no_httponly_session_cookie(self):
request = RequestFactory().get('/')
response = HttpResponse('Session test')
middleware = SessionMiddleware()
# Simulate a request the modifies the session
middleware.process_request(request)
request.session['hello'] = 'world'
# Handle the response through the middleware
response = middleware.process_response(request, response)
self.assertEqual(response.cookies[settings.SESSION_COOKIE_NAME]['httponly'], '')
self.assertNotIn(
cookies.Morsel._reserved['httponly'],
str(response.cookies[settings.SESSION_COOKIE_NAME])
)
示例14: test_session_delete_on_end
# 需要导入模块: from django.contrib.sessions import middleware [as 别名]
# 或者: from django.contrib.sessions.middleware import SessionMiddleware [as 别名]
def test_session_delete_on_end(self):
request = RequestFactory().get('/')
response = HttpResponse('Session test')
middleware = SessionMiddleware()
# Before deleting, there has to be an existing cookie
request.COOKIES[settings.SESSION_COOKIE_NAME] = 'abc'
# Simulate a request that ends the session
middleware.process_request(request)
request.session.flush()
# Handle the response through the middleware
response = middleware.process_response(request, response)
# The cookie was deleted, not recreated.
# A deleted cookie header looks like:
# Set-Cookie: sessionid=; expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0; Path=/
self.assertEqual(
'Set-Cookie: {}=""; expires=Thu, 01 Jan 1970 00:00:00 GMT; '
'Max-Age=0; Path=/'.format(
settings.SESSION_COOKIE_NAME,
),
str(response.cookies[settings.SESSION_COOKIE_NAME])
)
示例15: test_flush_empty_without_session_cookie_doesnt_set_cookie
# 需要导入模块: from django.contrib.sessions import middleware [as 别名]
# 或者: from django.contrib.sessions.middleware import SessionMiddleware [as 别名]
def test_flush_empty_without_session_cookie_doesnt_set_cookie(self):
request = RequestFactory().get('/')
response = HttpResponse('Session test')
middleware = SessionMiddleware()
# Simulate a request that ends the session
middleware.process_request(request)
request.session.flush()
# Handle the response through the middleware
response = middleware.process_response(request, response)
# A cookie should not be set.
self.assertEqual(response.cookies, {})
# The session is accessed so "Vary: Cookie" should be set.
self.assertEqual(response['Vary'], 'Cookie')