本文整理汇总了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])