Skip to content

Commit

Permalink
Fix & test realm test view
Browse files Browse the repository at this point in the history
  • Loading branch information
np5 committed Oct 31, 2024
1 parent 1e7d616 commit 2c91130
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
4 changes: 2 additions & 2 deletions server/realms/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
path('realms/<uuid:pk>/update/', views.UpdateRealmView.as_view(), name='update'),

# SSO test views
path('<uuid:pk>/test/', views.TestRealmView.as_view(), name='test'),
path('<uuid:pk>/sessions/<uuid:ras_pk>/', views.RealmAuthenticationSessionView.as_view(),
path('realms/<uuid:pk>/test/', views.TestRealmView.as_view(), name='test'),
path('realms/<uuid:pk>/sessions/<uuid:ras_pk>/', views.RealmAuthenticationSessionView.as_view(),
name='authentication_session'),
]
2 changes: 1 addition & 1 deletion server/realms/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def get_context_data(self, **kwargs):

# realm groups
ctx["mapped_realm_groups"] = sorted(
get_realm_user_mapped_realm_groups(realm_user),
get_realm_user_mapped_realm_groups(realm_user) or [],
key=lambda g: g.display_name
)
ctx["mapped_realm_group_count"] = len(ctx["mapped_realm_groups"])
Expand Down
39 changes: 37 additions & 2 deletions tests/server_realms/test_realm_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
from accounts.models import User
from realms.backends.registry import backend_classes
from realms.models import Realm, RealmAuthenticationSession, RealmGroupMapping, RoleMapping
from .utils import (force_group, force_realm, force_realm_group,
force_realm_group_mapping, force_role_mapping,
from .utils import (force_group, force_realm, force_realm_authentication_session,
force_realm_group, force_realm_group_mapping, force_role_mapping,
force_realm_user, force_user)


Expand Down Expand Up @@ -1138,3 +1138,38 @@ def test_realm_user_one_zentral_no_user_link(self):
self.assertTemplateUsed(response, "realms/realmuser_detail.html")
self.assertNotContains(response, user.get_absolute_url())
self.assertContains(response, "Zentral user (1)")

# test realm

def test_realm_permission_denied(self):
realm = force_realm(enabled_for_login=True)
self.login()
response = self.client.post(reverse("realms:test", args=(realm.pk,)))
self.assertEqual(response.status_code, 403)

def test_realm(self):
realm = force_realm(enabled_for_login=True)
self.login("realms.view_realm")
response = self.client.post(reverse("realms:test", args=(realm.pk,)))
ras = realm.realmauthenticationsession_set.first()
self.assertRedirects(response, reverse("realms_public:ldap_login", args=(realm.pk, ras.pk)))

# authentication session

def test_authentication_session_login_redirect(self):
ras = force_realm_authentication_session()
self.login_redirect("test", ras.realm.pk)

def test_authentication_session_permission_denied(self):
ras = force_realm_authentication_session()
self.login()
response = self.client.get(reverse("realms:authentication_session", args=(ras.realm.pk, ras.pk)))
self.assertEqual(response.status_code, 403)

def test_authentication_session(self):
ras = force_realm_authentication_session()
self.login("realms.view_realm")
response = self.client.get(reverse("realms:authentication_session", args=(ras.realm.pk, ras.pk)))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "realms/realmauthenticationsession_detail.html")
self.assertContains(response, "IdP claims")
13 changes: 12 additions & 1 deletion tests/server_realms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from django.contrib.auth.models import Group
from django.utils.crypto import get_random_string
from accounts.models import User
from realms.models import (Realm, RealmEmail, RealmGroup, RealmGroupMapping,
from realms.models import (Realm, RealmAuthenticationSession,
RealmEmail, RealmGroup, RealmGroupMapping,
RealmUser, RealmUserGroupMembership, RoleMapping)


Expand Down Expand Up @@ -123,3 +124,13 @@ def force_user(username=None, email=None, active=True, remote=False, service_acc
user.set_password(get_random_string(12))
user.save()
return user


def force_realm_authentication_session(callback="realms.utils.test_callback"):
realm = force_realm(enabled_for_login=True)
_, realm_user = force_realm_user(realm=realm)
return RealmAuthenticationSession.objects.create(
realm=realm,
user=realm_user,
callback=callback,
)

0 comments on commit 2c91130

Please sign in to comment.