def test_getExtensionArgs_nonempty(self): aliases = NamespaceMap() aliases.addAlias(self.type_a, self.alias_a) msg = ax.StoreRequest(aliases=aliases) msg.setValues(self.type_a, ['foo', 'bar']) args = msg.getExtensionArgs() expected_args = { 'mode': 'store_request', 'type.' + self.alias_a: self.type_a, 'count.' + self.alias_a: '2', 'value.%s.1' % (self.alias_a, ): 'foo', 'value.%s.2' % (self.alias_a, ): 'bar', } self.failUnlessEqual(expected_args, args)
def test_getExtensionArgs_nonempty(self): aliases = NamespaceMap() aliases.addAlias(self.type_a, self.alias_a) msg = ax.StoreRequest(aliases=aliases) msg.setValues(self.type_a, ['foo', 'bar']) args = msg.getExtensionArgs() expected_args = { 'mode':'store_request', 'type.' + self.alias_a: self.type_a, 'count.' + self.alias_a: '2', 'value.%s.1' % (self.alias_a,):'foo', 'value.%s.2' % (self.alias_a,):'bar', } self.failUnlessEqual(expected_args, args)
def getExtensionArgs(self): """Get the serialized form of this attribute fetch request. @returns: The fetch request message parameters @rtype: {unicode:unicode} """ aliases = NamespaceMap() required = [] if_available = [] ax_args = self._newArgs() for type_uri, attribute in self.requested_attributes.iteritems(): if attribute.alias is None: alias = aliases.add(type_uri) else: # This will raise an exception when the second # attribute with the same alias is added. I think it # would be better to complain at the time that the # attribute is added to this object so that the code # that is adding it is identified in the stack trace, # but it's more work to do so, and it won't be 100% # accurate anyway, since the attributes are # mutable. So for now, just live with the fact that # we'll learn about the error later. # # The other possible approach is to hide the error and # generate a new alias on the fly. I think that would # probably be bad. alias = aliases.addAlias(type_uri, attribute.alias) if attribute.required: required.append(alias) else: if_available.append(alias) if attribute.count != 1: ax_args['count.' + alias] = str(attribute.count) ax_args['type.' + alias] = type_uri if required: ax_args['required'] = ','.join(required) if if_available: ax_args['if_available'] = ','.join(if_available) return ax_args
def parseExtensionArgs(self, ax_args): """Parse attribute exchange key/value arguments into this object. @param ax_args: The attribute exchange fetch_response arguments, with namespacing removed. @type ax_args: {unicode:unicode} @returns: None @raises ValueError: If the message has bad values for particular fields @raises KeyError: If the namespace mapping is bad or required arguments are missing """ self._checkMode(ax_args) aliases = NamespaceMap() for key, value in ax_args.iteritems(): if key.startswith('type.'): type_uri = value alias = key[5:] checkAlias(alias) aliases.addAlias(type_uri, alias) for type_uri, alias in aliases.iteritems(): try: count_s = ax_args['count.' + alias] except KeyError: value = ax_args['value.' + alias] if value == u'': values = [] else: values = [value] else: count = int(count_s) values = [] for i in range(1, count + 1): value_key = 'value.%s.%d' % (alias, i) value = ax_args[value_key] values.append(value) self.data[type_uri] = values
class ToTypeURIsTest(unittest.TestCase): def setUp(self): self.aliases = NamespaceMap() def test_empty(self): for empty in [None, '']: uris = ax.toTypeURIs(self.aliases, empty) self.failUnlessEqual([], uris) def test_undefined(self): self.failUnlessRaises( KeyError, ax.toTypeURIs, self.aliases, 'http://janrain.com/') def test_one(self): uri = 'http://janrain.com/' alias = 'openid_hackers' self.aliases.addAlias(uri, alias) uris = ax.toTypeURIs(self.aliases, alias) self.failUnlessEqual([uri], uris) def test_two(self): uri1 = 'http://janrain.com/' alias1 = 'openid_hackers' self.aliases.addAlias(uri1, alias1) uri2 = 'http://jyte.com/' alias2 = 'openid_hack' self.aliases.addAlias(uri2, alias2) uris = ax.toTypeURIs(self.aliases, ','.join([alias1, alias2])) self.failUnlessEqual([uri1, uri2], uris)
class ToTypeURIsTest(unittest.TestCase): def setUp(self): self.aliases = NamespaceMap() def test_empty(self): for empty in [None, '']: uris = ax.toTypeURIs(self.aliases, empty) self.failUnlessEqual([], uris) def test_undefined(self): self.failUnlessRaises(KeyError, ax.toTypeURIs, self.aliases, 'http://janrain.com/') def test_one(self): uri = 'http://janrain.com/' alias = 'openid_hackers' self.aliases.addAlias(uri, alias) uris = ax.toTypeURIs(self.aliases, alias) self.failUnlessEqual([uri], uris) def test_two(self): uri1 = 'http://janrain.com/' alias1 = 'openid_hackers' self.aliases.addAlias(uri1, alias1) uri2 = 'http://jyte.com/' alias2 = 'openid_hack' self.aliases.addAlias(uri2, alias2) uris = ax.toTypeURIs(self.aliases, ','.join([alias1, alias2])) self.failUnlessEqual([uri1, uri2], uris)
def _getExtensionKVArgs(self, aliases=None): """Get the extension arguments for the key/value pairs contained in this message. @param aliases: An alias mapping. Set to None if you don't care about the aliases for this request. """ if aliases is None: aliases = NamespaceMap() ax_args = {} for type_uri, values in self.data.iteritems(): alias = aliases.add(type_uri) ax_args['type.' + alias] = type_uri ax_args['count.' + alias] = str(len(values)) for i, value in enumerate(values): key = 'value.%s.%d' % (alias, i + 1) ax_args[key] = value return ax_args
def parseExtensionArgs(self, ax_args): """Given attribute exchange arguments, populate this FetchRequest. @param ax_args: Attribute Exchange arguments from the request. As returned from L{Message.getArgs<openid.message.Message.getArgs>}. @type ax_args: dict @raises KeyError: if the message is not consistent in its use of namespace aliases. @raises NotAXMessage: If ax_args does not include an Attribute Exchange mode. @raises AXError: If the data to be parsed does not follow the attribute exchange specification. At least when 'if_available' or 'required' is not specified for a particular attribute type. """ # Raises an exception if the mode is not the expected value self._checkMode(ax_args) aliases = NamespaceMap() for key, value in ax_args.iteritems(): if key.startswith('type.'): alias = key[5:] type_uri = value aliases.addAlias(type_uri, alias) count_key = 'count.' + alias count_s = ax_args.get(count_key) if count_s: try: count = int(count_s) if count <= 0: raise AXError( "Count %r must be greater than zero, got %r" % ( count_key, count_s, )) except ValueError: if count_s != UNLIMITED_VALUES: raise AXError("Invalid count value for %r: %r" % ( count_key, count_s, )) count = count_s else: count = 1 self.add(AttrInfo(type_uri, alias=alias, count=count)) required = toTypeURIs(aliases, ax_args.get('required')) for type_uri in required: self.requested_attributes[type_uri].required = True if_available = toTypeURIs(aliases, ax_args.get('if_available')) all_type_uris = required + if_available for type_uri in aliases.iterNamespaceURIs(): if type_uri not in all_type_uris: raise AXError('Type URI %r was in the request but not ' 'present in "required" or "if_available"' % (type_uri, )) self.update_url = ax_args.get('update_url')
def getExtensionArgs(self): """Serialize this object into arguments in the attribute exchange namespace @returns: The dictionary of unqualified attribute exchange arguments that represent this fetch_response. @rtype: {unicode;unicode} """ aliases = NamespaceMap() zero_value_types = [] if self.request is not None: # Validate the data in the context of the request (the # same attributes should be present in each, and the # counts in the response must be no more than the counts # in the request) for type_uri in self.data: if type_uri not in self.request: raise KeyError( 'Response attribute not present in request: %r' % (type_uri,)) for attr_info in self.request.iterAttrs(): # Copy the aliases from the request so that reading # the response in light of the request is easier if attr_info.alias is None: aliases.add(attr_info.type_uri) else: aliases.addAlias(attr_info.type_uri, attr_info.alias) try: values = self.data[attr_info.type_uri] except KeyError: values = [] zero_value_types.append(attr_info) if (attr_info.count != UNLIMITED_VALUES) and \ (attr_info.count < len(values)): raise AXError( 'More than the number of requested values were ' 'specified for %r' % (attr_info.type_uri,)) kv_args = self._getExtensionKVArgs(aliases) # Add the KV args into the response with the args that are # unique to the fetch_response ax_args = self._newArgs() # For each requested attribute, put its type/alias and count # into the response even if no data were returned. for attr_info in zero_value_types: alias = aliases.getAlias(attr_info.type_uri) kv_args['type.' + alias] = attr_info.type_uri kv_args['count.' + alias] = '0' update_url = ((self.request and self.request.update_url) or self.update_url) if update_url: ax_args['update_url'] = update_url ax_args.update(kv_args) return ax_args
def setUp(self): self.aliases = NamespaceMap()
def __init__(self): self.openid1 = False self.namespaces = NamespaceMap()
def getExtensionArgs(self): """Serialize this object into arguments in the attribute exchange namespace @returns: The dictionary of unqualified attribute exchange arguments that represent this fetch_response. @rtype: {unicode;unicode} """ aliases = NamespaceMap() zero_value_types = [] if self.request is not None: # Validate the data in the context of the request (the # same attributes should be present in each, and the # counts in the response must be no more than the counts # in the request) for type_uri in self.data: if type_uri not in self.request: raise KeyError( 'Response attribute not present in request: %r' % (type_uri, )) for attr_info in self.request.iterAttrs(): # Copy the aliases from the request so that reading # the response in light of the request is easier if attr_info.alias is None: aliases.add(attr_info.type_uri) else: aliases.addAlias(attr_info.type_uri, attr_info.alias) try: values = self.data[attr_info.type_uri] except KeyError: values = [] zero_value_types.append(attr_info) if (attr_info.count != UNLIMITED_VALUES) and \ (attr_info.count < len(values)): raise AXError( 'More than the number of requested values were ' 'specified for %r' % (attr_info.type_uri, )) kv_args = self._getExtensionKVArgs(aliases) # Add the KV args into the response with the args that are # unique to the fetch_response ax_args = self._newArgs() # For each requested attribute, put its type/alias and count # into the response even if no data were returned. for attr_info in zero_value_types: alias = aliases.getAlias(attr_info.type_uri) kv_args['type.' + alias] = attr_info.type_uri kv_args['count.' + alias] = '0' update_url = ((self.request and self.request.update_url) or self.update_url) if update_url: ax_args['update_url'] = update_url ax_args.update(kv_args) return ax_args
def parseExtensionArgs(self, ax_args): """Given attribute exchange arguments, populate this FetchRequest. @param ax_args: Attribute Exchange arguments from the request. As returned from L{Message.getArgs<openid.message.Message.getArgs>}. @type ax_args: dict @raises KeyError: if the message is not consistent in its use of namespace aliases. @raises NotAXMessage: If ax_args does not include an Attribute Exchange mode. @raises AXError: If the data to be parsed does not follow the attribute exchange specification. At least when 'if_available' or 'required' is not specified for a particular attribute type. """ # Raises an exception if the mode is not the expected value self._checkMode(ax_args) aliases = NamespaceMap() for key, value in ax_args.iteritems(): if key.startswith('type.'): alias = key[5:] type_uri = value aliases.addAlias(type_uri, alias) count_key = 'count.' + alias count_s = ax_args.get(count_key) if count_s: try: count = int(count_s) if count <= 0: raise AXError("Count %r must be greater than zero, got %r" % (count_key, count_s,)) except ValueError: if count_s != UNLIMITED_VALUES: raise AXError("Invalid count value for %r: %r" % (count_key, count_s,)) count = count_s else: count = 1 self.add(AttrInfo(type_uri, alias=alias, count=count)) required = toTypeURIs(aliases, ax_args.get('required')) for type_uri in required: self.requested_attributes[type_uri].required = True if_available = toTypeURIs(aliases, ax_args.get('if_available')) all_type_uris = required + if_available for type_uri in aliases.iterNamespaceURIs(): if type_uri not in all_type_uris: raise AXError( 'Type URI %r was in the request but not ' 'present in "required" or "if_available"' % (type_uri,)) self.update_url = ax_args.get('update_url')