Skip to content

Commit

Permalink
test: add test for Django admin logout view
Browse files Browse the repository at this point in the history
Refs: HP-2280
  • Loading branch information
charn committed Mar 14, 2024
1 parent ffef69f commit 0a8b21a
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
22 changes: 21 additions & 1 deletion helusers/tests/settings.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
from helusers.defaults import SOCIAL_AUTH_PIPELINE # noqa: F401

USE_TZ = True

SECRET_KEY = "secret"

DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}}

INSTALLED_APPS = (
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"helusers.apps.HelusersConfig",
"helusers.apps.HelusersAdminConfig",
"social_django",
"helusers.tests",
)

AUTH_USER_MODEL = "tests.User"

SESSION_SERIALIZER = "helusers.sessions.TunnistamoOIDCSerializer"

AUTHENTICATION_BACKENDS = [
"helusers.tunnistamo_oidc.TunnistamoOIDCAuth",
"django.contrib.auth.backends.ModelBackend",
]
LOGIN_REDIRECT_URL = "/"
LOGOUT_REDIRECT_URL = "/"

OIDC_API_TOKEN_AUTH = {
"AUDIENCE": "test_audience",
"ISSUER": ["https://test_issuer_1", "https://test_issuer_2"],
Expand All @@ -22,9 +37,13 @@
"OIDC_CONFIG_EXPIRATION_TIME": 2,
}

SOCIAL_AUTH_TUNNISTAMO_KEY = "test-client-id"
SOCIAL_AUTH_TUNNISTAMO_SECRET = "iamyoursecret"
SOCIAL_AUTH_TUNNISTAMO_OIDC_ENDPOINT = "https://test_issuer_1"

HELUSERS_BACK_CHANNEL_LOGOUT_ENABLED = True

ROOT_URLCONF = "helusers.urls"
ROOT_URLCONF = "helusers.tests.urls"

DEFAULT_AUTO_FIELD = "django.db.models.AutoField"

Expand All @@ -37,6 +56,7 @@
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.request",
Expand Down
61 changes: 61 additions & 0 deletions helusers/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import pytest
from django.contrib.auth import get_user_model
from django.urls import reverse
from pytest_django.asserts import assertContains
from social_django.models import UserSocialAuth

from helusers.tunnistamo_oidc import TunnistamoOIDCAuth


@pytest.fixture
def admin_user():
return get_user_model().objects.create_superuser(
username="admin",
email="[email protected]",
first_name="Admin",
password="super-duper-test",
)


def test_admin_index(client):
response = client.get(reverse("admin:index"), follow=True)

assert response.status_code == 200
assertContains(
response, "Kirjaudu sisään Helsingin kaupungin työntekijän tunnuksella"
)


@pytest.mark.django_db
def test_admin_app_name(client, admin_user):
"""The App name in the admin index page"""
client.login(username="admin", password="super-duper-test")

response = client.get(reverse("admin:index"))

assert response.status_code == 200
assertContains(response, "Helsinki Users")


@pytest.mark.django_db
def test_django_admin_logout(client, admin_user):
"""Test that the Django admin logout works.
helusers.admin_site.AdminSite expects the end session url to be in the session
for using the helusers.views.LogoutView
"""
expected_url = "http://example.com/redirected_after_logout"
UserSocialAuth.objects.create(user=admin_user, provider=TunnistamoOIDCAuth.name)
client.login(username="admin", password="super-duper-test")
session = client.session
session["social_auth_end_session_url"] = expected_url
session.save()

response = client.get(reverse("admin:index"))
assert response.status_code == 200
assert response.wsgi_request.user.is_authenticated

response = client.post(reverse("admin:logout"))
assert response.status_code == 302
assert response.url == expected_url
assert response.wsgi_request.user.is_anonymous
9 changes: 9 additions & 0 deletions helusers/tests/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.conf.urls import include
from django.contrib import admin
from django.urls import path

urlpatterns = [
path("admin/", admin.site.urls),
path("pysocial/", include("social_django.urls", namespace="social")),
path("helauth/", include("helusers.urls")),
]
1 change: 1 addition & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pytest-cov
pytest-django
pytest-mock
responses
social-auth-app-django

0 comments on commit 0a8b21a

Please sign in to comment.