Skip to content

Commit

Permalink
Add tests for full name support
Browse files Browse the repository at this point in the history
  • Loading branch information
amercader committed Feb 17, 2021
1 parent 03cb5eb commit 91f7ccf
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
11 changes: 10 additions & 1 deletion ckanext/saml2auth/tests/responses/unsigned0.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,19 @@
<saml:Attribute Name="email" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
<saml:AttributeValue xsi:type="xs:string">[email protected]</saml:AttributeValue>
</saml:Attribute>
<saml:Attribute Name="firstname" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
<saml:AttributeValue>John</saml:AttributeValue>
</saml:Attribute>
<saml:Attribute Name="lastname" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
<saml:AttributeValue>Smith</saml:AttributeValue>
</saml:Attribute>
<saml:Attribute Name="fullname" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
<saml:AttributeValue>John Smith (Operations)</saml:AttributeValue>
</saml:Attribute>
<saml:Attribute Name="eduPersonAffiliation" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
<saml:AttributeValue xsi:type="xs:string">users</saml:AttributeValue>
<saml:AttributeValue xsi:type="xs:string">examplerole1</saml:AttributeValue>
</saml:Attribute>
</saml:AttributeStatement>
</saml:Assertion>
</samlp:Response>
</samlp:Response>
73 changes: 73 additions & 0 deletions ckanext/saml2auth/tests/test_blueprint_get_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import os
import pytest

from ckan import model

from saml2.xmldsig import SIG_RSA_SHA256
from saml2.xmldsig import DIGEST_SHA256
from saml2.saml import NAMEID_FORMAT_ENTITY
Expand Down Expand Up @@ -338,3 +340,74 @@ def test_signed_not_encrypted_assertion(self, app):
}
response = app.post(url=url, params=data)
assert_equal(200, response.status_code)

@pytest.mark.ckan_config(u'ckanext.saml2auth.entity_id', u'urn:gov:gsa:SAML:2.0.profiles:sp:sso:test:entity')
@pytest.mark.ckan_config(u'ckanext.saml2auth.idp_metadata.location', u'local')
@pytest.mark.ckan_config(u'ckanext.saml2auth.idp_metadata.local_path', os.path.join(extras_folder, 'provider0', 'idp.xml'))
@pytest.mark.ckan_config(u'ckanext.saml2auth.want_response_signed', u'False')
@pytest.mark.ckan_config(u'ckanext.saml2auth.want_assertions_signed', u'False')
@pytest.mark.ckan_config(u'ckanext.saml2auth.want_assertions_or_response_signed', u'False')
def test_user_fullname_using_first_last_name(self, app):

# read about saml2 responses: https://www.samltool.com/generic_sso_res.php
unsigned_response_file = os.path.join(responses_folder, 'unsigned0.xml')
unsigned_response = open(unsigned_response_file).read()
# parse values
context = {
'entity_id': 'urn:gov:gsa:SAML:2.0.profiles:sp:sso:test:entity',
'destination': 'http://test.ckan.net/acs',
'recipient': 'http://test.ckan.net/acs',
'issue_instant': datetime.now().isoformat()
}
t = Template(unsigned_response)
final_response = t.render(**context)

encoded_response = self._b4_encode_string(final_response)
url = '/acs'

data = {
'SAMLResponse': encoded_response
}
response = app.post(url=url, params=data)
assert_equal(200, response.status_code)

user = model.User.by_email('[email protected]')[0]

assert user.fullname == 'John Smith'

@pytest.mark.ckan_config(u'ckanext.saml2auth.entity_id', u'urn:gov:gsa:SAML:2.0.profiles:sp:sso:test:entity')
@pytest.mark.ckan_config(u'ckanext.saml2auth.idp_metadata.location', u'local')
@pytest.mark.ckan_config(u'ckanext.saml2auth.idp_metadata.local_path', os.path.join(extras_folder, 'provider0', 'idp.xml'))
@pytest.mark.ckan_config(u'ckanext.saml2auth.want_response_signed', u'False')
@pytest.mark.ckan_config(u'ckanext.saml2auth.want_assertions_signed', u'False')
@pytest.mark.ckan_config(u'ckanext.saml2auth.want_assertions_or_response_signed', u'False')
@pytest.mark.ckan_config(u'ckanext.saml2auth.user_fullname', u'fullname')
@pytest.mark.ckan_config(u'ckanext.saml2auth.user_firstname', None)
@pytest.mark.ckan_config(u'ckanext.saml2auth.user_lastname', None)
def test_user_fullname_using_fullname(self, app):

# read about saml2 responses: https://www.samltool.com/generic_sso_res.php
unsigned_response_file = os.path.join(responses_folder, 'unsigned0.xml')
unsigned_response = open(unsigned_response_file).read()
# parse values
context = {
'entity_id': 'urn:gov:gsa:SAML:2.0.profiles:sp:sso:test:entity',
'destination': 'http://test.ckan.net/acs',
'recipient': 'http://test.ckan.net/acs',
'issue_instant': datetime.now().isoformat()
}
t = Template(unsigned_response)
final_response = t.render(**context)

encoded_response = self._b4_encode_string(final_response)
url = '/acs'

data = {
'SAMLResponse': encoded_response
}
response = app.post(url=url, params=data)
assert_equal(200, response.status_code)

user = model.User.by_email('[email protected]')[0]

assert user.fullname == 'John Smith (Operations)'
4 changes: 2 additions & 2 deletions test.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use = config:../ckan/test-core.ini

ckanext.saml2auth.idp_metadata.location = local
ckanext.saml2auth.idp_metadata.local_path = /path/to/idp.xml
ckanext.saml2auth.user_firstname = name
ckanext.saml2auth.user_firstname = firstname
ckanext.saml2auth.user_lastname = lastname
ckanext.saml2auth.user_email = email

Expand Down Expand Up @@ -54,4 +54,4 @@ level = NOTSET
formatter = generic

[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s] %(message)s
format = %(asctime)s %(levelname)-5.5s [%(name)s] %(message)s

0 comments on commit 91f7ccf

Please sign in to comment.