Added a setting to turn of the messages about the service

This commit is contained in:
Jorik Kraaikamp 2017-03-29 15:36:12 +02:00
parent 5410aee3d5
commit 224202c5c2
2 changed files with 86 additions and 19 deletions

View File

@ -295,6 +295,24 @@ class LoginTestCase(TestCase, BaseServicePattern, CanLogin):
) in response.content ) in response.content
) )
@override_settings(CAS_SHOW_SERVER_MESSAGES=False)
def test_view_login_get_allowed_service_no_message(self):
"""Request a ticket for an allowed service by an unauthenticated client"""
# get a bare new http client
client = Client()
# we are not authenticated and are asking for a ticket for https://www.example.com
# which is a valid service matched by self.service_pattern
response = client.get("/login?service=https://www.example.com")
# the login page should be displayed
self.assertEqual(response.status_code, 200)
# we warn the user why it need to authenticated
self.assertFalse(
(
b"Authentication required by service "
b"example (https://www.example.com)"
) in response.content
)
def test_view_login_get_denied_service(self): def test_view_login_get_denied_service(self):
"""Request a ticket for an denied service by an unauthenticated client""" """Request a ticket for an denied service by an unauthenticated client"""
# get a bare new http client # get a bare new http client
@ -306,6 +324,18 @@ class LoginTestCase(TestCase, BaseServicePattern, CanLogin):
# we warn the user that https://www.example.net is not an allowed service url # we warn the user that https://www.example.net is not an allowed service url
self.assertTrue(b"Service https://www.example.net not allowed" in response.content) self.assertTrue(b"Service https://www.example.net not allowed" in response.content)
@override_settings(CAS_SHOW_SERVER_MESSAGES=False)
def test_view_login_get_denied_service_no_message(self):
"""Request a ticket for an denied service by an unauthenticated client"""
# get a bare new http client
client = Client()
# we are not authenticated and are asking for a ticket for https://www.example.net
# which is NOT a valid service
response = client.get("/login?service=https://www.example.net")
self.assertEqual(response.status_code, 200)
# we warn the user that https://www.example.net is not an allowed service url
self.assertFalse(b"Service https://www.example.net not allowed" in response.content)
def test_view_login_get_auth_allowed_service(self): def test_view_login_get_auth_allowed_service(self):
"""Request a ticket for an allowed service by an authenticated client""" """Request a ticket for an allowed service by an authenticated client"""
# get a client that is already authenticated # get a client that is already authenticated
@ -505,6 +535,40 @@ class LoginTestCase(TestCase, BaseServicePattern, CanLogin):
# renewing authentication is done in the validate and serviceValidate views tests # renewing authentication is done in the validate and serviceValidate views tests
self.assertEqual(ticket.renew, True) self.assertEqual(ticket.renew, True)
@override_settings(CAS_SHOW_SERVER_MESSAGES=False)
def test_renew_message_disabled(self):
"""test the authentication renewal request from a service"""
# use the default test service
service = "https://www.example.com"
# get a client that is already authenticated
client = get_auth_client()
# ask for a ticket for the service but aks for authentication renewal
response = client.get("/login", {'service': service, 'renew': 'on'})
# we are ask to reauthenticate and tell the user why
self.assertEqual(response.status_code, 200)
self.assertFalse(
(
b"Authentication renewal required by "
b"service example (https://www.example.com)"
) in response.content
)
# get the form default parameter
params = copy_form(response.context["form"])
# set valid username/password
params["username"] = settings.CAS_TEST_USER
params["password"] = settings.CAS_TEST_PASSWORD
# the renew parameter from the form should be True
self.assertEqual(params["renew"], True)
# post the authentication request
response = client.post("/login", params)
# the request succed, a ticket is created and we are redirected to the service url
self.assertEqual(response.status_code, 302)
ticket_value = response['Location'].split('ticket=')[-1]
ticket = models.ServiceTicket.objects.get(value=ticket_value)
# the created ticket is marked has being gottent after a renew. Futher testing about
# renewing authentication is done in the validate and serviceValidate views tests
self.assertEqual(ticket.renew, True)
@override_settings(CAS_ENABLE_AJAX_AUTH=True) @override_settings(CAS_ENABLE_AJAX_AUTH=True)
def test_ajax_login_required(self): def test_ajax_login_required(self):
""" """

View File

@ -835,26 +835,29 @@ class LoginView(View, LogoutMixin):
# clean messages before leaving django # clean messages before leaving django
list(messages.get_messages(self.request)) list(messages.get_messages(self.request))
return HttpResponseRedirect(self.service) return HttpResponseRedirect(self.service)
if self.request.session.get("authenticated") and self.renew:
messages.add_message( if settings.CAS_SHOW_SERVICE_MESSAGES:
self.request, if self.request.session.get("authenticated") and self.renew:
messages.WARNING, messages.add_message(
_(u"Authentication renewal required by service %(name)s (%(url)s).") % self.request,
{'name': service_pattern.name, 'url': self.service} messages.WARNING,
) _(u"Authentication renewal required by service %(name)s (%(url)s).") %
else: {'name': service_pattern.name, 'url': self.service}
messages.add_message( )
self.request, else:
messages.WARNING, messages.add_message(
_(u"Authentication required by service %(name)s (%(url)s).") % self.request,
{'name': service_pattern.name, 'url': self.service} messages.WARNING,
) _(u"Authentication required by service %(name)s (%(url)s).") %
{'name': service_pattern.name, 'url': self.service}
)
except ServicePattern.DoesNotExist: except ServicePattern.DoesNotExist:
messages.add_message( if settings.CAS_SHOW_SERVICE_MESSAGES:
self.request, messages.add_message(
messages.ERROR, self.request,
_(u'Service %s not allowed') % self.service messages.ERROR,
) _(u'Service %s not allowed') % self.service
)
if self.ajax: if self.ajax:
data = { data = {
"status": "error", "status": "error",