Skip to content

Commit 3ec5e13

Browse files
author
Roland Hedberg
committed
Tests will not fail if you don't have a MondoDB running.
1 parent 108a386 commit 3ec5e13

File tree

2 files changed

+104
-90
lines changed

2 files changed

+104
-90
lines changed

tests/test_75_mongodb.py

+63-53
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from contextlib import closing
2+
from pymongo.errors import ConnectionFailure
23
from saml2 import BINDING_HTTP_POST
34
from saml2.authn_context import INTERNETPROTOCOLPASSWORD
45
from saml2.client import Saml2Client
@@ -20,62 +21,71 @@ def _eq(l1, l2):
2021

2122
def test_flow():
2223
sp = Saml2Client(config_file="servera_conf")
23-
with closing(Server(config_file="idp_conf_mdb")) as idp1:
24-
with closing(Server(config_file="idp_conf_mdb")) as idp2:
25-
# clean out database
26-
idp1.ident.mdb.db.drop()
27-
28-
# -- dummy request ---
29-
req_id, orig_req = sp.create_authn_request(idp1.config.entityid)
30-
31-
# == Create an AuthnRequest response
32-
33-
rinfo = idp1.response_args(orig_req, [BINDING_HTTP_POST])
34-
35-
#name_id = idp1.ident.transient_nameid("id12", rinfo["sp_entity_id"])
36-
resp = idp1.create_authn_response({"eduPersonEntitlement": "Short stop",
37-
"surName": "Jeter",
38-
"givenName": "Derek",
39-
"mail": "[email protected]",
40-
"title": "The man"},
41-
userid="jeter",
42-
authn=AUTHN,
43-
**rinfo)
44-
45-
# What's stored away is the assertion
46-
a_info = idp2.session_db.get_assertion(resp.assertion.id)
47-
# Make sure what I got back from MongoDB is the same as I put in
48-
assert a_info["assertion"] == resp.assertion
49-
50-
# By subject
51-
nid = resp.assertion.subject.name_id
52-
_assertion = idp2.session_db.get_assertions_by_subject(nid)
53-
assert len(_assertion) == 1
54-
assert _assertion[0] == resp.assertion
55-
56-
nids = idp2.ident.find_nameid("jeter")
57-
assert len(nids) == 1
24+
try:
25+
with closing(Server(config_file="idp_conf_mdb")) as idp1:
26+
with closing(Server(config_file="idp_conf_mdb")) as idp2:
27+
# clean out database
28+
idp1.ident.mdb.db.drop()
29+
30+
# -- dummy request ---
31+
req_id, orig_req = sp.create_authn_request(idp1.config.entityid)
32+
33+
# == Create an AuthnRequest response
34+
35+
rinfo = idp1.response_args(orig_req, [BINDING_HTTP_POST])
36+
37+
#name_id = idp1.ident.transient_nameid("id12", rinfo["sp_entity_id"])
38+
resp = idp1.create_authn_response(
39+
{
40+
"eduPersonEntitlement": "Short stop",
41+
"surName": "Jeter",
42+
"givenName": "Derek",
43+
"mail": "[email protected]",
44+
"title": "The man"},
45+
userid="jeter",
46+
authn=AUTHN,
47+
**rinfo)
48+
49+
# What's stored away is the assertion
50+
a_info = idp2.session_db.get_assertion(resp.assertion.id)
51+
# Make sure what I got back from MongoDB is the same as I put in
52+
assert a_info["assertion"] == resp.assertion
53+
54+
# By subject
55+
nid = resp.assertion.subject.name_id
56+
_assertion = idp2.session_db.get_assertions_by_subject(nid)
57+
assert len(_assertion) == 1
58+
assert _assertion[0] == resp.assertion
59+
60+
nids = idp2.ident.find_nameid("jeter")
61+
assert len(nids) == 1
62+
except ConnectionFailure:
63+
pass
5864

5965

6066
def test_eptid_mongo_db():
61-
edb = EptidMDB("secret", "idp")
62-
e1 = edb.get("idp_entity_id", "sp_entity_id", "user_id",
63-
"some other data")
64-
print e1
65-
assert e1.startswith("idp_entity_id!sp_entity_id!")
66-
e2 = edb.get("idp_entity_id", "sp_entity_id", "user_id",
67-
"some other data")
68-
assert e1 == e2
69-
70-
e3 = edb.get("idp_entity_id", "sp_entity_id", "user_2",
71-
"some other data")
72-
print e3
73-
assert e1 != e3
74-
75-
e4 = edb.get("idp_entity_id", "sp_entity_id2", "user_id",
76-
"some other data")
77-
assert e4 != e1
78-
assert e4 != e3
67+
try:
68+
edb = EptidMDB("secret", "idp")
69+
except ConnectionFailure:
70+
pass
71+
else:
72+
e1 = edb.get("idp_entity_id", "sp_entity_id", "user_id",
73+
"some other data")
74+
print e1
75+
assert e1.startswith("idp_entity_id!sp_entity_id!")
76+
e2 = edb.get("idp_entity_id", "sp_entity_id", "user_id",
77+
"some other data")
78+
assert e1 == e2
79+
80+
e3 = edb.get("idp_entity_id", "sp_entity_id", "user_2",
81+
"some other data")
82+
print e3
83+
assert e1 != e3
84+
85+
e4 = edb.get("idp_entity_id", "sp_entity_id2", "user_id",
86+
"some other data")
87+
assert e4 != e1
88+
assert e4 != e3
7989

8090

8191

tests/test_76_metadata_in_mdb.py

+41-37
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: utf-8 -*-
2+
from pymongo.errors import ConnectionFailure
23

34
__author__ = 'rolandh'
45

@@ -55,43 +56,46 @@ def test_metadata():
5556
mds.imp({"local": [full_path("swamid-2.0.xml")]})
5657
assert len(mds) == 1 # One source
5758

58-
export_mdstore_to_mongo_db(mds, "metadata", "test")
59-
60-
mdmdb = MetadataMDB(ONTS, ATTRCONV, "metadata", "test")
61-
# replace all metadata instances with this one
62-
mds.metadata = {"mongo_db": mdmdb}
63-
64-
idps = mds.with_descriptor("idpsso")
65-
assert idps.keys()
66-
idpsso = mds.single_sign_on_service(UMU_IDP)
67-
assert len(idpsso) == 1
68-
assert destinations(idpsso) == [
69-
'https://idp.umu.se/saml2/idp/SSOService.php']
70-
71-
_name = name(mds[UMU_IDP])
72-
assert _name == u'Ume\xe5 University'
73-
certs = mds.certs(UMU_IDP, "idpsso", "signing")
74-
assert len(certs) == 1
75-
76-
sps = mds.with_descriptor("spsso")
77-
assert len(sps) == 417
78-
79-
wants = mds.attribute_requirement('https://connect.sunet.se/shibboleth')
80-
assert wants["optional"] == []
81-
lnamn = [d_to_local_name(mds.attrc, attr) for attr in wants["required"]]
82-
assert _eq(lnamn, ['eduPersonPrincipalName', 'mail', 'givenName', 'sn',
83-
'eduPersonScopedAffiliation', 'eduPersonAffiliation'])
84-
85-
wants = mds.attribute_requirement(
86-
"https://gidp.geant.net/sp/module.php/saml/sp/metadata.php/default-sp")
87-
# Optional
88-
lnamn = [d_to_local_name(mds.attrc, attr) for attr in wants["optional"]]
89-
assert _eq(lnamn, ['displayName', 'commonName', 'schacHomeOrganization',
90-
'eduPersonAffiliation', 'schacHomeOrganizationType'])
91-
# Required
92-
lnamn = [d_to_local_name(mds.attrc, attr) for attr in wants["required"]]
93-
assert _eq(lnamn, ['eduPersonTargetedID', 'mail',
94-
'eduPersonScopedAffiliation'])
59+
try:
60+
export_mdstore_to_mongo_db(mds, "metadata", "test")
61+
except ConnectionFailure:
62+
pass
63+
else:
64+
mdmdb = MetadataMDB(ONTS, ATTRCONV, "metadata", "test")
65+
# replace all metadata instances with this one
66+
mds.metadata = {"mongo_db": mdmdb}
67+
68+
idps = mds.with_descriptor("idpsso")
69+
assert idps.keys()
70+
idpsso = mds.single_sign_on_service(UMU_IDP)
71+
assert len(idpsso) == 1
72+
assert destinations(idpsso) == [
73+
'https://idp.umu.se/saml2/idp/SSOService.php']
74+
75+
_name = name(mds[UMU_IDP])
76+
assert _name == u'Ume\xe5 University'
77+
certs = mds.certs(UMU_IDP, "idpsso", "signing")
78+
assert len(certs) == 1
79+
80+
sps = mds.with_descriptor("spsso")
81+
assert len(sps) == 417
82+
83+
wants = mds.attribute_requirement('https://connect.sunet.se/shibboleth')
84+
assert wants["optional"] == []
85+
lnamn = [d_to_local_name(mds.attrc, attr) for attr in wants["required"]]
86+
assert _eq(lnamn, ['eduPersonPrincipalName', 'mail', 'givenName', 'sn',
87+
'eduPersonScopedAffiliation', 'eduPersonAffiliation'])
88+
89+
wants = mds.attribute_requirement(
90+
"https://gidp.geant.net/sp/module.php/saml/sp/metadata.php/default-sp")
91+
# Optional
92+
lnamn = [d_to_local_name(mds.attrc, attr) for attr in wants["optional"]]
93+
assert _eq(lnamn, ['displayName', 'commonName', 'schacHomeOrganization',
94+
'eduPersonAffiliation', 'schacHomeOrganizationType'])
95+
# Required
96+
lnamn = [d_to_local_name(mds.attrc, attr) for attr in wants["required"]]
97+
assert _eq(lnamn, ['eduPersonTargetedID', 'mail',
98+
'eduPersonScopedAffiliation'])
9599

96100
if __name__ == "__main__":
97101
test_metadata()

0 commit comments

Comments
 (0)