本文整理匯總了Python中django.contrib.auth.signals.user_logged_out方法的典型用法代碼示例。如果您正苦於以下問題:Python signals.user_logged_out方法的具體用法?Python signals.user_logged_out怎麽用?Python signals.user_logged_out使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.contrib.auth.signals
的用法示例。
在下文中一共展示了signals.user_logged_out方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_delete_user_logged_out_signal
# 需要導入模塊: from django.contrib.auth import signals [as 別名]
# 或者: from django.contrib.auth.signals import user_logged_out [as 別名]
def test_delete_user_logged_out_signal(self):
"""Send the user_logged_out signal if a user deletes their Auth Token."""
handler = MagicMock()
signals.user_logged_out.connect(handler)
someday = timezone.now() + datetime.timedelta(days=1)
user = UserFactory.create()
token = AuthTokenFactory.create(user=user, expires=someday)
# Custom auth header containing token
auth = 'Token ' + token.key
request = self.create_request(
'delete',
user=user,
HTTP_AUTHORIZATION=auth,
)
response = self.view_class.as_view()(request)
handler.assert_called_once_with(
signal=signals.user_logged_out,
sender=views.GetAuthToken,
request=response.renderer_context['request'],
user=user,
)
示例2: test_refresh_fails_for_already_signed_in_user
# 需要導入模塊: from django.contrib.auth import signals [as 別名]
# 或者: from django.contrib.auth.signals import user_logged_out [as 別名]
def test_refresh_fails_for_already_signed_in_user(self, mock_random_string):
mock_random_string.return_value = 'examplestring'
# Mutable to log which users get logged out.
logged_out_users = []
# Register a signal on 'user_logged_out' so we can
# update 'logged_out_users'.
@receiver(user_logged_out)
def logged_out(sender, user=None, **kwargs):
logged_out_users.append(user)
client = ClientWithUser()
# First confirm that the home page is a public page.
resp = client.get('/')
# At least security doesn't kick you out.
self.assertEqual(resp.status_code, 404)
# Also check that this page doesn't force you to redirect
# to authenticate.
resp = client.get('/mdo_fake_view/')
self.assertEqual(resp.status_code, 200)
client.login(username=self.user.username, password='password')
# Set expiration to some time in the past
session = client.session
session['oidc_id_token_expiration'] = time.time() - 100
session['_auth_user_backend'] = 'mozilla_django_oidc.auth.OIDCAuthenticationBackend'
session.save()
# Confirm that now you're forced to authenticate again.
resp = client.get('/mdo_fake_view/')
self.assertEqual(resp.status_code, 302)
self.assertTrue(
'http://example.com/authorize' in resp.url and
'prompt=none' in resp.url
)
# Now suppose the user goes there and something goes wrong.
# For example, the user might have become "blocked" or the 2FA
# verficiation has expired and needs to be done again.
resp = client.get('/callback/', {
'error': 'login_required',
'error_description': 'Multifactor authentication required',
})
self.assertEqual(resp.status_code, 302)
self.assertEqual(resp.url, '/')
# Since the user in 'client' doesn't change, we have to use other
# queues to assert that the user got logged out properly.
# The session gets flushed when you get signed out.
# This is the only decent way to know the user lost all
# request.session and
self.assertTrue(not client.session.items())
# The signal we registered should have fired for this user.
self.assertEqual(client.user, logged_out_users[0])