django-allauth-cas/tests/test_flows.py

70 lines
2.2 KiB
Python
Raw Normal View History

2017-07-25 18:31:42 +02:00
# -*- coding: utf-8 -*-
from django.contrib import messages
from django.contrib.auth import get_user_model
from django.contrib.messages.api import get_messages
from django.contrib.messages.storage.base import Message
from django.test import Client, override_settings
2017-07-25 18:31:42 +02:00
from allauth_cas.test.testcases import CASTestCase
2017-07-25 18:31:42 +02:00
User = get_user_model()
class LogoutFlowTests(CASTestCase):
2017-07-25 18:31:42 +02:00
expected_msg_str = (
2017-12-29 18:19:01 +01:00
"To logout of The Provider, please close your browser, or visit this "
"<a href=\"/accounts/theid/logout/?next=%2Fredir%2F\">"
2017-07-26 17:24:20 +02:00
"link</a>."
2017-07-25 18:31:42 +02:00
)
def setUp(self):
self.client_cas_login(self.client)
2017-07-25 18:31:42 +02:00
def assertCASLogoutNotInMessages(self, response):
r_messages = get_messages(response.wsgi_request)
self.assertNotIn(
self.expected_msg_str,
(str(msg) for msg in r_messages),
)
self.assertTemplateNotUsed(
response,
2017-12-29 18:19:01 +01:00
'socialaccount/messages/suggest_caslogout.html',
2017-07-25 18:31:42 +02:00
)
@override_settings(SOCIALACCOUNT_PROVIDERS={
'theid': {
2017-12-29 18:19:01 +01:00
'MESSAGE_SUGGEST_CASLOGOUT_ON_LOGOUT': True,
'MESSAGE_SUGGEST_CASLOGOUT_ON_LOGOUT_LEVEL': messages.WARNING,
2017-07-25 18:31:42 +02:00
},
})
def test_message_on_logout(self):
"""
Message is sent to propose user to logout of CAS.
"""
2017-12-29 18:19:01 +01:00
r = self.client.post('/accounts/logout/?next=/redir/')
2017-07-25 18:31:42 +02:00
r_messages = get_messages(r.wsgi_request)
expected_msg = Message(messages.WARNING, self.expected_msg_str)
self.assertIn(expected_msg, r_messages)
2017-12-29 18:19:01 +01:00
self.assertTemplateUsed(
r, 'socialaccount/messages/suggest_caslogout.html')
2017-07-25 18:31:42 +02:00
def test_message_on_logout_disabled(self):
r = self.client.post('/accounts/logout/')
self.assertCASLogoutNotInMessages(r)
@override_settings(SOCIALACCOUNT_PROVIDERS={
2017-12-29 18:19:01 +01:00
'theid': {'MESSAGE_SUGGEST_CASLOGOUT_ON_LOGOUT': True},
2017-07-25 18:31:42 +02:00
})
2017-12-29 18:19:01 +01:00
def test_other_logout(self):
2017-07-25 18:31:42 +02:00
"""
The CAS logout message doesn't appear with other login methods.
"""
User.objects.create_user('user', '', 'user')
client = Client()
client.login(username='user', password='user')
2017-07-25 18:31:42 +02:00
r = client.post('/accounts/logout/')
2017-07-25 18:31:42 +02:00
self.assertCASLogoutNotInMessages(r)