Fix non-CAS users case

This commit is contained in:
Antonin Reitz 2019-11-27 12:57:14 +01:00
parent c3d6e2b144
commit 89cb540f02
2 changed files with 31 additions and 13 deletions

View file

@ -166,12 +166,19 @@ class GenericLoginViewTests(TestCase):
self.client.login(username="team", password="team")
r = self.client.post(self.url)
self.assertRedirects(
r,
"https://cas.eleves.ens.fr/logout?service=http%3A%2F%2Ftestserver%2Fk-fet%2Flogin%2Fgeneric",
fetch_redirect_response=False,
)
profile = getattr(self.user, "profile", None)
if profile and profile.login_clipper:
self.assertRedirects(
r,
"https://cas.eleves.ens.fr/logout?service={}".format(self.url),
fetch_redirect_response=False,
)
else:
self.assertRedirects(
r,
"/logout?next=http%3A%2F%2Ftestserver{}".format(self.url),
fetch_redirect_response=False,
)
def test_notoken_not_team(self):
"""

View file

@ -72,15 +72,26 @@ class GenericLoginView(View):
# which is why the CAS logout URL with callback is constructed ad hoc,
# without relying on Django redirection to Django CAS.
generic_login_url = request.build_absolute_uri() # preserves next parameter
generic_login_qd = QueryDict(mutable=True)
generic_login_qd["service"] = generic_login_url
here_url = request.build_absolute_uri() # preserves next parameter
profile = getattr(request.user, "profile", None)
cas_server_url = django_settings.CAS_SERVER_URL
cas_logout_url = cas_server_url + "logout"
cas_callback_url = cas_logout_url + "?{}".format(generic_login_qd.urlencode())
if profile and profile.login_clipper:
generic_login_url = here_url
generic_login_qd = QueryDict(mutable=True)
generic_login_qd["service"] = generic_login_url
logout_url = cas_callback_url
cas_server_url = django_settings.CAS_SERVER_URL
cas_logout_url = cas_server_url + "logout"
cas_callback_url = cas_logout_url + "?{}".format(
generic_login_qd.urlencode()
)
logout_url = cas_callback_url
else:
logout_url = reverse("cof-logout")
logout_qd = QueryDict(mutable=True)
logout_qd["next"] = here_url
logout_url += "?{}".format(logout_qd.urlencode(safe="/"))
resp = redirect(logout_url)
resp.set_signed_cookie(self.TOKEN_COOKIE_NAME, token.token, httponly=True)