Merge branch 'kerl/login_tests' into 'master'

Quelques tests pour les vues de login

See merge request klub-dev-ens/authens!6
This commit is contained in:
Ludovic Stephan 2020-06-12 16:15:27 +02:00
commit 4e9e2f62e8
3 changed files with 62 additions and 5 deletions

View file

@ -1,5 +1,5 @@
from unittest.mock import patch
from urllib.parse import quote as urlquote
from urllib.parse import quote
from django.conf import settings
from django.contrib.auth import get_user_model
@ -13,6 +13,38 @@ from authens.tests.cas_utils import FakeCASClient
UserModel = get_user_model()
class TestLoginViews(TestCase):
def test_cas_login_redirect(self):
url = reverse("authens:login.cas")
abs_url = "http://testserver{}".format(url)
client = Client()
# User request without ticket: redirect to the CAS login page.
response = client.get(url)
self.assertRedirects(
response,
"https://cas.eleves.ens.fr/login?service={}".format(quote(abs_url)),
fetch_redirect_response=False,
)
@patch("authens.backends.get_cas_client")
def test_cas_login_with_ticket(self, mock_cas_client):
# Make `get_cas_client` return a dummy CAS client that skips ticket verification
# and always log in a user with CAS login 'johndoe'.
mock_cas_client.return_value = FakeCASClient()
url = reverse("authens:login.cas")
client = Client()
# User request with a CAS ticket: validate the ticket.
response = client.get("{}?ticket=dummy-ticket".format(url), follow=True)
self.assertRedirects(response, settings.LOGIN_REDIRECT_URL)
def test_can_gen_login_switch(self):
response = Client().get(reverse("authens:login"))
self.assertEqual(response.status_code, 200)
class TestLogoutView(TestCase):
def test_regular_logout(self):
# Regular user (without a CAS account)
@ -32,7 +64,7 @@ class TestLogoutView(TestCase):
# Make `get_cas_client` return a dummy CAS client that skips ticket verification
# and always log in a user with CAS login 'johndoe'.
# This is only used for login.
mock_cas_client.return_value = FakeCASClient("johndoe", 2019)
mock_cas_client.return_value = FakeCASClient()
# CAS user
user = UserModel.objects.create_user(username="johndoe")
@ -48,7 +80,7 @@ class TestLogoutView(TestCase):
self.assertRedirects( # … and redirected to the CAS logout page.
response,
"https://cas.eleves.ens.fr/logout?service={}".format(
urlquote("http://testserver" + reverse("authens:login"))
quote("http://testserver/public")
),
fetch_redirect_response=False,
)

View file

@ -48,4 +48,5 @@ DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3"}}
ROOT_URLCONF = "tests.urls"
LOGIN_URL = reverse_lazy("authens:login")
LOGOUT_REDIRECT_URL = reverse_lazy("authens:login")
LOGIN_REDIRECT_URL = "/private"
LOGOUT_REDIRECT_URL = "/public"

View file

@ -1,4 +1,28 @@
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.urls import include, path
urlpatterns = [path("authens/", include("authens.urls"))]
# ---
# Two tiny views to easily test user authentication.
# ---
def public_view(request):
return HttpResponse("OK")
@login_required
def private_view(request):
return HttpResponse("OK")
# ---
# Urls: expose authens' urls + the above views.
# ---
urlpatterns = [
path("public", public_view),
path("private", private_view),
path("authens/", include("authens.urls")),
]