mirror of
https://github.com/haiwen/seahub.git
synced 2025-08-31 22:54:11 +00:00
Merge pull request #1028 from haiwen/5.0-shib-nickname
[shib] Use display_name in shib_meta as nickname, fallback to givenna…
This commit is contained in:
79
tests/seahub/thirdpart/shibboleth/test_middleware.py
Normal file
79
tests/seahub/thirdpart/shibboleth/test_middleware.py
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
from mock import Mock
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
from django.test import RequestFactory
|
||||||
|
|
||||||
|
from seahub.profile.models import Profile
|
||||||
|
from seahub.test_utils import BaseTestCase
|
||||||
|
from shibboleth.middleware import ShibbolethRemoteUserMiddleware
|
||||||
|
|
||||||
|
settings.AUTHENTICATION_BACKENDS += (
|
||||||
|
'shibboleth.backends.ShibbolethRemoteUserBackend',
|
||||||
|
)
|
||||||
|
|
||||||
|
class ShibbolethRemoteUserMiddlewareTest(BaseTestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.middleware = ShibbolethRemoteUserMiddleware()
|
||||||
|
|
||||||
|
self.factory = RequestFactory()
|
||||||
|
|
||||||
|
# Create an instance of a GET request.
|
||||||
|
self.request = self.factory.get('/foo/')
|
||||||
|
# self.request = Mock()
|
||||||
|
|
||||||
|
self.request.user = self.user
|
||||||
|
self.request.user.is_authenticated = lambda: False
|
||||||
|
self.request.cloud_mode = False
|
||||||
|
self.request.session = {}
|
||||||
|
|
||||||
|
self.request.META = {}
|
||||||
|
self.request.META['REMOTE_USER'] = self.user.username
|
||||||
|
self.request.META['eppn'] = 'test eppn'
|
||||||
|
self.request.META['givenname'] = 'test_gname'
|
||||||
|
self.request.META['surname'] = 'test_sname'
|
||||||
|
|
||||||
|
# def test_can_process(self):
|
||||||
|
# assert len(Profile.objects.all()) == 0
|
||||||
|
|
||||||
|
# self.middleware.process_request(self.request)
|
||||||
|
|
||||||
|
# assert len(Profile.objects.all()) == 1
|
||||||
|
# assert self.request.shib_login is True
|
||||||
|
|
||||||
|
def test_make_profile_for_display_name(self):
|
||||||
|
assert len(Profile.objects.all()) == 0
|
||||||
|
|
||||||
|
self.middleware.make_profile(self.user, {
|
||||||
|
'display_name': 'display name',
|
||||||
|
'givenname': 'g',
|
||||||
|
'surname': 's',
|
||||||
|
'institution': 'i',
|
||||||
|
'contact_email': 'foo@foo.com'
|
||||||
|
})
|
||||||
|
|
||||||
|
assert len(Profile.objects.all()) == 1
|
||||||
|
assert Profile.objects.all()[0].nickname == 'display name'
|
||||||
|
|
||||||
|
def test_make_profile_for_givenname_surname(self):
|
||||||
|
assert len(Profile.objects.all()) == 0
|
||||||
|
|
||||||
|
self.middleware.make_profile(self.user, {
|
||||||
|
'givenname': 'g',
|
||||||
|
'surname': 's',
|
||||||
|
'institution': 'i',
|
||||||
|
'contact_email': 'foo@foo.com'
|
||||||
|
})
|
||||||
|
|
||||||
|
assert len(Profile.objects.all()) == 1
|
||||||
|
assert Profile.objects.all()[0].nickname == 'g s'
|
||||||
|
|
||||||
|
def test_make_profile_for_name_missing(self):
|
||||||
|
assert len(Profile.objects.all()) == 0
|
||||||
|
|
||||||
|
self.middleware.make_profile(self.user, {
|
||||||
|
'institution': 'i',
|
||||||
|
'contact_email': 'foo@foo.com'
|
||||||
|
})
|
||||||
|
|
||||||
|
assert len(Profile.objects.all()) == 1
|
||||||
|
assert Profile.objects.all()[0].nickname == ''
|
@@ -112,9 +112,14 @@ class ShibbolethRemoteUserMiddleware(RemoteUserMiddleware):
|
|||||||
Extrat nickname(givenname surname), contact_email, institution from
|
Extrat nickname(givenname surname), contact_email, institution from
|
||||||
Shib attributs, and add those to user profile.
|
Shib attributs, and add those to user profile.
|
||||||
"""
|
"""
|
||||||
|
# use `display_name` as nickname in shib_meta first
|
||||||
|
nickname = shib_meta.get('display_name', None)
|
||||||
|
if nickname is None:
|
||||||
|
# otherwise, fallback to givenname plus surname in shib_meta
|
||||||
givenname = shib_meta.get('givenname', '')
|
givenname = shib_meta.get('givenname', '')
|
||||||
surname = shib_meta.get('surname', '')
|
surname = shib_meta.get('surname', '')
|
||||||
nickname = "%s %s" % (givenname, surname)
|
nickname = "%s %s" % (givenname, surname)
|
||||||
|
|
||||||
institution = shib_meta.get('institution', None)
|
institution = shib_meta.get('institution', None)
|
||||||
contact_email = shib_meta.get('contact_email', None)
|
contact_email = shib_meta.get('contact_email', None)
|
||||||
|
|
||||||
@@ -122,7 +127,9 @@ class ShibbolethRemoteUserMiddleware(RemoteUserMiddleware):
|
|||||||
if not p:
|
if not p:
|
||||||
p = Profile(user=user.username)
|
p = Profile(user=user.username)
|
||||||
|
|
||||||
|
if nickname.strip(): # set nickname when it's not empty
|
||||||
p.nickname = nickname
|
p.nickname = nickname
|
||||||
|
|
||||||
if institution:
|
if institution:
|
||||||
p.institution = institution
|
p.institution = institution
|
||||||
if contact_email:
|
if contact_email:
|
||||||
|
Reference in New Issue
Block a user