def test_mapping_from_deeply_nested_attribute(self): mapping = { "attributes": { "address": { "openid": ["address.formatted.text.value"], "saml": ["postaladdress"] }, }, } data = { "address": { "formatted": { "text": { "value": ["100 Universal City Plaza, Hollywood CA 91608, USA"] } } } } converter = DataConverter(mapping) internal_repr = converter.to_internal("openid", data) external_repr = converter.from_internal("saml", internal_repr) assert external_repr["postaladdress"] == data["address"]["formatted"][ "text"]["value"]
class AddStaticAttributes(ResponseMicroService): """ Add static attributes to the responses. The path to the file describing the mapping (as YAML) of static attributes must be specified with the environment variable 'SATOSA_STATIC_ATTRIBUTES'. """ def __init__(self, internal_attributes): super(AddStaticAttributes, self).__init__() self.data_converter = DataConverter(internal_attributes) mapping_file = os.environ.get("SATOSA_STATIC_ATTRIBUTES") if not mapping_file: raise ValueError( "Could not find file containing mapping of static attributes.") with open(mapping_file) as f: self.static_attributes = yaml.safe_load(f) def process(self, context, data): all_attributes = data.get_attributes() all_attributes.update( self.data_converter.to_internal("saml", self.static_attributes)) data.add_attributes(all_attributes) return data
def test_to_internal_with_missing_attribute_value(self): mapping = { "attributes": { "mail": { "p1": ["emailaddress"], }, } } converter = DataConverter(mapping) internal_repr = converter.to_internal("p1", {}) assert not internal_repr
def test_to_internal_same_attribute_value_from_list_and_single_value(self, attribute_value): mapping = { "attributes": { "mail": { "foo": ["email"], }, }, } converter = DataConverter(mapping) internal_repr = converter.to_internal("foo", attribute_value) assert internal_repr["mail"] == ["*****@*****.**"]
def test_to_internal_with_missing_attribute_value(self): mapping = { "attributes": { "mail": { "p1": ["emailaddress"], }, } } converter = DataConverter(mapping) internal_repr = converter.to_internal("p1", {}) assert not internal_repr
def test_to_internal_same_attribute_value_from_list_and_single_value( self, attribute_value): mapping = { "attributes": { "mail": { "foo": ["email"], }, }, } converter = DataConverter(mapping) internal_repr = converter.to_internal("foo", attribute_value) assert internal_repr["mail"] == ["*****@*****.**"]
def test_map_one_source_attribute_to_multiple_internal_attributes(self): mapping = { "attributes": { "mail": { "p1": ["email"], }, "identifier": { "p1": ["email"], }, }, } converter = DataConverter(mapping) internal_repr = converter.to_internal("p1", {"email": ["*****@*****.**"]}) assert internal_repr == {"mail": ["*****@*****.**"], "identifier": ["*****@*****.**"]}
def test_to_internal_profile_missing_attribute_mapping(self): mapping = { "attributes": { "mail": { "foo": ["email"], }, "id": { "foo": ["id"], "bar": ["uid"], } }, } converter = DataConverter(mapping) internal_repr = converter.to_internal("bar", {"email": ["*****@*****.**"], "uid": ["uid"]}) assert "mail" not in internal_repr # no mapping for the 'mail' attribute in the 'bar' profile assert internal_repr["id"] == ["uid"]
def test_mapping_to_nested_attribute(self): mapping = { "attributes": { "address": { "openid": ["address.formatted"], "saml": ["postaladdress"] }, }, } data = { "postaladdress": ["100 Universal City Plaza, Hollywood CA 91608, USA"] } converter = DataConverter(mapping) internal_repr = converter.to_internal("saml", data) external_repr = converter.from_internal("openid", internal_repr) assert external_repr["address"]["formatted"] == data["postaladdress"]
def test_map_one_source_attribute_to_multiple_internal_attributes(self): mapping = { "attributes": { "mail": { "p1": ["email"], }, "identifier": { "p1": ["email"], }, }, } converter = DataConverter(mapping) internal_repr = converter.to_internal("p1", {"email": ["*****@*****.**"]}) assert internal_repr == { "mail": ["*****@*****.**"], "identifier": ["*****@*****.**"] }
def test_to_internal_profile_missing_attribute_mapping(self): mapping = { "attributes": { "mail": { "foo": ["email"], }, "id": { "foo": ["id"], "bar": ["uid"], } }, } converter = DataConverter(mapping) internal_repr = converter.to_internal("bar", { "email": ["*****@*****.**"], "uid": ["uid"] }) assert "mail" not in internal_repr # no mapping for the 'mail' attribute in the 'bar' profile assert internal_repr["id"] == ["uid"]
def test_multiple_source_attribute_values(self): mapping = { "attributes": { "mail": { "saml": ["mail", "emailAddress", "email"] }, }, } data = { "mail": ["*****@*****.**"], "email": ["*****@*****.**"], "emailAddress": ["*****@*****.**"], } expected = Counter(["*****@*****.**", "*****@*****.**", "*****@*****.**"]) converter = DataConverter(mapping) internal_repr = converter.to_internal("saml", data) assert Counter(internal_repr["mail"]) == expected external_repr = converter.from_internal("saml", internal_repr) assert Counter(external_repr[mapping["attributes"]["mail"]["saml"][0]]) == expected
class AddStaticAttributes(ResponseMicroService): """ Add static attributes to the responses. The path to the file describing the mapping (as YAML) of static attributes must be specified with the environment variable 'SATOSA_STATIC_ATTRIBUTES'. """ def __init__(self, internal_attributes): super(AddStaticAttributes, self).__init__() self.data_converter = DataConverter(internal_attributes) mapping_file = os.environ.get("SATOSA_STATIC_ATTRIBUTES") if not mapping_file: raise ValueError("Could not find file containing mapping of static attributes.") with open(mapping_file) as f: self.static_attributes = yaml.safe_load(f) def process(self, context, data): all_attributes = data.get_attributes() all_attributes.update(self.data_converter.to_internal("saml", self.static_attributes)) data.add_attributes(all_attributes) return data
def test_multiple_source_attribute_values(self): mapping = { "attributes": { "mail": { "saml": ["mail", "emailAddress", "email"] }, }, } data = { "mail": ["*****@*****.**"], "email": ["*****@*****.**"], "emailAddress": ["*****@*****.**"], } expected = Counter( ["*****@*****.**", "*****@*****.**", "*****@*****.**"]) converter = DataConverter(mapping) internal_repr = converter.to_internal("saml", data) assert Counter(internal_repr["mail"]) == expected external_repr = converter.from_internal("saml", internal_repr) assert Counter(external_repr[mapping["attributes"]["mail"]["saml"] [0]]) == expected