Skip to content

Commit c04ba65

Browse files
author
Roland Hedberg
committed
Merge pull request #158 from erickt/master
Fix all the tests
2 parents 3680d61 + ff5cb7d commit c04ba65

18 files changed

+559
-539
lines changed

src/saml2/ident.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,4 +334,5 @@ def handle_manage_name_id_request(self, name_id, new_id=None,
334334
return name_id
335335

336336
def close(self):
337-
self.db.close()
337+
if hasattr(self.db, 'close'):
338+
self.db.close()

src/saml2/server.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -145,28 +145,32 @@ def init_config(self, stype="idp"):
145145
raise Exception("Couldn't open identity database: %s" %
146146
(dbspec,))
147147

148-
_domain = self.config.getattr("domain", "idp")
149-
if _domain:
150-
self.ident.domain = _domain
151-
152-
self.ident.name_qualifier = self.config.entityid
148+
try:
149+
_domain = self.config.getattr("domain", "idp")
150+
if _domain:
151+
self.ident.domain = _domain
153152

154-
dbspec = self.config.getattr("edu_person_targeted_id", "idp")
155-
if not dbspec:
156-
pass
157-
else:
158-
typ = dbspec[0]
159-
addr = dbspec[1]
160-
secret = dbspec[2]
161-
if typ == "shelve":
162-
self.eptid = EptidShelve(secret, addr)
163-
elif typ == "mongodb":
164-
from saml2.mongo_store import EptidMDB
153+
self.ident.name_qualifier = self.config.entityid
165154

166-
self.eptid = EptidMDB(secret, database=addr,
167-
collection="eptid")
155+
dbspec = self.config.getattr("edu_person_targeted_id", "idp")
156+
if not dbspec:
157+
pass
168158
else:
169-
self.eptid = Eptid(secret)
159+
typ = dbspec[0]
160+
addr = dbspec[1]
161+
secret = dbspec[2]
162+
if typ == "shelve":
163+
self.eptid = EptidShelve(secret, addr)
164+
elif typ == "mongodb":
165+
from saml2.mongo_store import EptidMDB
166+
167+
self.eptid = EptidMDB(secret, database=addr,
168+
collection="eptid")
169+
else:
170+
self.eptid = Eptid(secret)
171+
except Exception:
172+
self.ident.close()
173+
raise
170174

171175
def wants(self, sp_entity_id, index=None):
172176
""" Returns what attributes the SP requires and which are optional
@@ -681,3 +685,6 @@ def create_ecp_authn_request_response(self, acs_url, identity,
681685
soap_envelope = soapenv.Envelope(header=header, body=body)
682686

683687
return "%s" % soap_envelope
688+
689+
def close(self):
690+
self.ident.close()

tests/test_37_entity_categories.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from contextlib import closing
12
from saml2 import saml, sigver
23
from saml2 import md
34
from saml2 import config
@@ -150,18 +151,17 @@ def test_filter_ava5():
150151

151152

152153
def test_idp_policy_filter():
153-
idp = Server("idp_conf_ec")
154+
with closing(Server("idp_conf_ec")) as idp:
155+
ava = {"givenName": ["Derek"], "sn": ["Jeter"],
156+
"mail": ["[email protected]"], "c": ["USA"],
157+
"eduPersonTargetedID": "foo!bar!xyz",
158+
"norEduPersonNIN": "19800101134"}
154159

155-
ava = {"givenName": ["Derek"], "sn": ["Jeter"],
156-
"mail": ["[email protected]"], "c": ["USA"],
157-
"eduPersonTargetedID": "foo!bar!xyz",
158-
"norEduPersonNIN": "19800101134"}
159-
160-
policy = idp.config.getattr("policy", "idp")
161-
ava = policy.filter(ava, "urn:mace:example.com:saml:roland:sp", idp.metadata)
160+
policy = idp.config.getattr("policy", "idp")
161+
ava = policy.filter(ava, "urn:mace:example.com:saml:roland:sp", idp.metadata)
162162

163-
print ava
164-
assert ava.keys() == ["eduPersonTargetedID"] # because no entity category
163+
print ava
164+
assert ava.keys() == ["eduPersonTargetedID"] # because no entity category
165165

166166
if __name__ == "__main__":
167167
test_idp_policy_filter()

tests/test_40_sigver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ def test_xbox():
438438
)
439439

440440
sigass = sec.sign_statement(assertion, class_name(assertion),
441-
key_file="pki/mykey.pem", node_id=assertion.id)
441+
key_file=full_path("test.key"), node_id=assertion.id)
442442

443443
_ass0 = saml.assertion_from_string(sigass)
444444

@@ -457,7 +457,7 @@ def test_xbox():
457457
assers = extension_elements_to_elements(_seass.extension_elements,
458458
[saml, samlp])
459459

460-
sign_cert_file = "pki/mycert.pem"
460+
sign_cert_file = full_path("test.pem")
461461

462462
for ass in assers:
463463
_ass = "%s" % ass

tests/test_41_response.py

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4+
from contextlib import closing
5+
46
from saml2 import config
57
from saml2.authn_context import INTERNETPROTOCOLPASSWORD
68

@@ -10,7 +12,9 @@
1012
from saml2.response import AuthnResponse
1113
from saml2.sigver import SignatureError
1214

13-
FALSE_ASSERT_SIGNED = "saml_false_signed.xml"
15+
from pathutils import full_path
16+
17+
FALSE_ASSERT_SIGNED = full_path("saml_false_signed.xml")
1418

1519
TIMESLACK = 20000000 # Roughly +- 12 month
1620

@@ -32,38 +36,38 @@ def _eq(l1, l2):
3236

3337
class TestResponse:
3438
def setup_class(self):
35-
server = Server("idp_conf")
36-
name_id = server.ident.transient_nameid(
37-
"urn:mace:example.com:saml:roland:sp", "id12")
38-
39-
self._resp_ = server.create_authn_response(
40-
IDENTITY,
41-
"id12", # in_response_to
42-
"http://lingon.catalogix.se:8087/",
43-
# consumer_url
44-
"urn:mace:example.com:saml:roland:sp",
45-
# sp_entity_id
46-
name_id=name_id)
47-
48-
self._sign_resp_ = server.create_authn_response(
49-
IDENTITY,
50-
"id12", # in_response_to
51-
"http://lingon.catalogix.se:8087/", # consumer_url
52-
"urn:mace:example.com:saml:roland:sp", # sp_entity_id
53-
name_id=name_id,
54-
sign_assertion=True)
55-
56-
self._resp_authn = server.create_authn_response(
57-
IDENTITY,
58-
"id12", # in_response_to
59-
"http://lingon.catalogix.se:8087/", # consumer_url
60-
"urn:mace:example.com:saml:roland:sp", # sp_entity_id
61-
name_id=name_id,
62-
authn=AUTHN)
63-
64-
conf = config.SPConfig()
65-
conf.load_file("server_conf")
66-
self.conf = conf
39+
with closing(Server("idp_conf")) as server:
40+
name_id = server.ident.transient_nameid(
41+
"urn:mace:example.com:saml:roland:sp", "id12")
42+
43+
self._resp_ = server.create_authn_response(
44+
IDENTITY,
45+
"id12", # in_response_to
46+
"http://lingon.catalogix.se:8087/",
47+
# consumer_url
48+
"urn:mace:example.com:saml:roland:sp",
49+
# sp_entity_id
50+
name_id=name_id)
51+
52+
self._sign_resp_ = server.create_authn_response(
53+
IDENTITY,
54+
"id12", # in_response_to
55+
"http://lingon.catalogix.se:8087/", # consumer_url
56+
"urn:mace:example.com:saml:roland:sp", # sp_entity_id
57+
name_id=name_id,
58+
sign_assertion=True)
59+
60+
self._resp_authn = server.create_authn_response(
61+
IDENTITY,
62+
"id12", # in_response_to
63+
"http://lingon.catalogix.se:8087/", # consumer_url
64+
"urn:mace:example.com:saml:roland:sp", # sp_entity_id
65+
name_id=name_id,
66+
authn=AUTHN)
67+
68+
conf = config.SPConfig()
69+
conf.load_file("server_conf")
70+
self.conf = conf
6771

6872
def test_1(self):
6973
xml_response = ("%s" % (self._resp_,))

tests/test_42_enc.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from contextlib import closing
12
from saml2.authn_context import INTERNETPROTOCOLPASSWORD
23
from saml2.server import Server
34
from saml2.sigver import pre_encryption_part, ASSERT_XPATH, EncryptError
@@ -30,13 +31,13 @@ def test_pre_enc():
3031

3132

3233
def test_reshuffle_response():
33-
server = Server("idp_conf")
34-
name_id = server.ident.transient_nameid(
35-
"urn:mace:example.com:saml:roland:sp", "id12")
34+
with closing(Server("idp_conf")) as server:
35+
name_id = server.ident.transient_nameid(
36+
"urn:mace:example.com:saml:roland:sp", "id12")
3637

37-
resp_ = server.create_authn_response(
38-
IDENTITY, "id12", "http://lingon.catalogix.se:8087/",
39-
"urn:mace:example.com:saml:roland:sp", name_id=name_id)
38+
resp_ = server.create_authn_response(
39+
IDENTITY, "id12", "http://lingon.catalogix.se:8087/",
40+
"urn:mace:example.com:saml:roland:sp", name_id=name_id)
4041

4142
resp2 = pre_encrypt_assertion(resp_)
4243

@@ -45,22 +46,22 @@ def test_reshuffle_response():
4546

4647

4748
def test_enc1():
48-
server = Server("idp_conf")
49-
name_id = server.ident.transient_nameid(
50-
"urn:mace:example.com:saml:roland:sp", "id12")
49+
with closing(Server("idp_conf")) as server:
50+
name_id = server.ident.transient_nameid(
51+
"urn:mace:example.com:saml:roland:sp", "id12")
5152

52-
resp_ = server.create_authn_response(
53-
IDENTITY, "id12", "http://lingon.catalogix.se:8087/",
54-
"urn:mace:example.com:saml:roland:sp", name_id=name_id)
53+
resp_ = server.create_authn_response(
54+
IDENTITY, "id12", "http://lingon.catalogix.se:8087/",
55+
"urn:mace:example.com:saml:roland:sp", name_id=name_id)
5556

5657
statement = pre_encrypt_assertion(resp_)
5758

58-
tmpl = "enc_tmpl.xml"
59+
tmpl = full_path("enc_tmpl.xml")
5960
# tmpl_file = open(tmpl, "w")
6061
# tmpl_file.write("%s" % pre_encryption_part())
6162
# tmpl_file.close()
6263

63-
data = "pre_enc.xml"
64+
data = full_path("pre_enc.xml")
6465
# data_file = open(data, "w")
6566
# data_file.write("%s" % statement)
6667
# data_file.close()
@@ -82,13 +83,13 @@ def test_enc1():
8283
def test_enc2():
8384
crypto = CryptoBackendXmlSec1(xmlsec_path)
8485

85-
server = Server("idp_conf")
86-
name_id = server.ident.transient_nameid(
87-
"urn:mace:example.com:saml:roland:sp", "id12")
86+
with closing(Server("idp_conf")) as server:
87+
name_id = server.ident.transient_nameid(
88+
"urn:mace:example.com:saml:roland:sp", "id12")
8889

89-
resp_ = server.create_authn_response(
90-
IDENTITY, "id12", "http://lingon.catalogix.se:8087/",
91-
"urn:mace:example.com:saml:roland:sp", name_id=name_id)
90+
resp_ = server.create_authn_response(
91+
IDENTITY, "id12", "http://lingon.catalogix.se:8087/",
92+
"urn:mace:example.com:saml:roland:sp", name_id=name_id)
9293

9394
enc_resp = crypto.encrypt_assertion(resp_, full_path("pubkey.pem"),
9495
pre_encryption_part())

tests/test_44_authnresp.py

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3+
from contextlib import closing
34
from saml2.authn_context import INTERNETPROTOCOLPASSWORD
45

56
from saml2.server import Server
@@ -28,37 +29,37 @@ def _eq(l1, l2):
2829

2930
class TestAuthnResponse:
3031
def setup_class(self):
31-
server = Server(dotname("idp_conf"))
32-
name_id = server.ident.transient_nameid(
33-
"urn:mace:example.com:saml:roland:sp","id12")
34-
35-
self._resp_ = server.create_authn_response(
36-
IDENTITY,
37-
"id12", # in_response_to
38-
"http://lingon.catalogix.se:8087/", # consumer_url
39-
"urn:mace:example.com:saml:roland:sp", # sp_entity_id
40-
name_id=name_id,
41-
authn=AUTHN)
42-
43-
self._sign_resp_ = server.create_authn_response(
44-
IDENTITY,
45-
"id12", # in_response_to
46-
"http://lingon.catalogix.se:8087/", # consumer_url
47-
"urn:mace:example.com:saml:roland:sp", # sp_entity_id
48-
name_id=name_id, sign_assertion=True,
49-
authn=AUTHN)
50-
51-
self._resp_authn = server.create_authn_response(
52-
IDENTITY,
53-
"id12", # in_response_to
54-
"http://lingon.catalogix.se:8087/", # consumer_url
55-
"urn:mace:example.com:saml:roland:sp", # sp_entity_id
56-
name_id=name_id,
57-
authn=AUTHN)
58-
59-
self.conf = config_factory("sp", dotname("server_conf"))
60-
self.conf.only_use_keys_in_metadata = False
61-
self.ar = authn_response(self.conf, "http://lingon.catalogix.se:8087/")
32+
with closing(Server(dotname("idp_conf"))) as server:
33+
name_id = server.ident.transient_nameid(
34+
"urn:mace:example.com:saml:roland:sp","id12")
35+
36+
self._resp_ = server.create_authn_response(
37+
IDENTITY,
38+
"id12", # in_response_to
39+
"http://lingon.catalogix.se:8087/", # consumer_url
40+
"urn:mace:example.com:saml:roland:sp", # sp_entity_id
41+
name_id=name_id,
42+
authn=AUTHN)
43+
44+
self._sign_resp_ = server.create_authn_response(
45+
IDENTITY,
46+
"id12", # in_response_to
47+
"http://lingon.catalogix.se:8087/", # consumer_url
48+
"urn:mace:example.com:saml:roland:sp", # sp_entity_id
49+
name_id=name_id, sign_assertion=True,
50+
authn=AUTHN)
51+
52+
self._resp_authn = server.create_authn_response(
53+
IDENTITY,
54+
"id12", # in_response_to
55+
"http://lingon.catalogix.se:8087/", # consumer_url
56+
"urn:mace:example.com:saml:roland:sp", # sp_entity_id
57+
name_id=name_id,
58+
authn=AUTHN)
59+
60+
self.conf = config_factory("sp", dotname("server_conf"))
61+
self.conf.only_use_keys_in_metadata = False
62+
self.ar = authn_response(self.conf, "http://lingon.catalogix.se:8087/")
6263

6364
def test_verify_1(self):
6465
xml_response = "%s" % (self._resp_,)
@@ -128,4 +129,4 @@ def test_verify_w_authn(self):
128129
if __name__ == "__main__":
129130
t = TestAuthnResponse()
130131
t.setup_class()
131-
t.test_verify_1()
132+
t.test_verify_1()

0 commit comments

Comments
 (0)