def make_response_ax(
            self, schema="http://axschema.org/",
            fullname="Some User", nickname="someuser", email="*****@*****.**",
            first=None, last=None, verified=False):
        endpoint = OpenIDServiceEndpoint()
        message = Message(OPENID2_NS)
        attributes = [
            ("nickname", schema + "namePerson/friendly", nickname),
            ("fullname", schema + "namePerson", fullname),
            ("email", schema + "contact/email", email),
            ("account_verified",
             "http://ns.login.ubuntu.com/2013/validation/account",
             "token_via_email" if verified else "no")
            ]
        if first:
            attributes.append(
                ("first", "http://axschema.org/namePerson/first", first))
        if last:
            attributes.append(
                ("last", "http://axschema.org/namePerson/last", last))

        message.setArg(AX_NS, "mode", "fetch_response")
        for (alias, uri, value) in attributes:
            message.setArg(AX_NS, "type.%s" % alias, uri)
            message.setArg(AX_NS, "value.%s" % alias, value)
        return SuccessResponse(
            endpoint, message, signed_fields=message.toPostArgs().keys())
Exemplo n.º 2
0
 def create_sreg_response(self, fullname='', email='', identifier=''):
     message = Message(OPENID2_NS)
     message.setArg(SREG_NS, "fullname", fullname)
     message.setArg(SREG_NS, "email", email)
     endpoint = OpenIDServiceEndpoint()
     endpoint.display_identifier = identifier
     return SuccessResponse(endpoint, message, signed_fields=message.toPostArgs().keys())
Exemplo n.º 3
0
    def make_response_ax(self,
                         schema="http://axschema.org/",
                         fullname="Some User",
                         nickname="someuser",
                         email="*****@*****.**",
                         first=None,
                         last=None,
                         verified=False):
        endpoint = OpenIDServiceEndpoint()
        message = Message(OPENID2_NS)
        attributes = [("nickname", schema + "namePerson/friendly", nickname),
                      ("fullname", schema + "namePerson", fullname),
                      ("email", schema + "contact/email", email),
                      ("account_verified",
                       "http://ns.login.ubuntu.com/2013/validation/account",
                       "token_via_email" if verified else "no")]
        if first:
            attributes.append(
                ("first", "http://axschema.org/namePerson/first", first))
        if last:
            attributes.append(
                ("last", "http://axschema.org/namePerson/last", last))

        message.setArg(AX_NS, "mode", "fetch_response")
        for (alias, uri, value) in attributes:
            message.setArg(AX_NS, "type.%s" % alias, uri)
            message.setArg(AX_NS, "value.%s" % alias, value)
        return SuccessResponse(endpoint,
                               message,
                               signed_fields=message.toPostArgs().keys())
Exemplo n.º 4
0
 def create_sreg_response(self, fullname='', email='', identifier=''):
     message = Message(OPENID2_NS)
     message.setArg(SREG_NS, "fullname", fullname)
     message.setArg(SREG_NS, "email", email)
     endpoint = OpenIDServiceEndpoint()
     endpoint.display_identifier = identifier
     return SuccessResponse(endpoint,
                            message,
                            signed_fields=message.toPostArgs().keys())
Exemplo n.º 5
0
 def make_openid_response(self, sreg_args=None, teams_args=None):
     endpoint = self.make_fake_openid_endpoint(claimed_id='some-id')
     message = Message(OPENID2_NS)
     if sreg_args is not None:
         for key, value in sreg_args.items():
             message.setArg(SREG_NS, key, value)
     if teams_args is not None:
         for key, value in teams_args.items():
             message.setArg(TEAMS_NS, key, value)
     response = SuccessResponse(
         endpoint, message, signed_fields=message.toPostArgs().keys())
     return response
Exemplo n.º 6
0
    def test_extract_user_details_sreg(self):
        endpoint = OpenIDServiceEndpoint()
        message = Message(OPENID2_NS)
        message.setArg(SREG_NS, "nickname", "someuser")
        message.setArg(SREG_NS, "fullname", "Some User")
        message.setArg(SREG_NS, "email", "*****@*****.**")
        response = SuccessResponse(
            endpoint, message, signed_fields=message.toPostArgs().keys())

        data = self.backend._extract_user_details(response)
        self.assertEqual(data, {"nickname": "someuser",
                                "first_name": "Some",
                                "last_name": "User",
                                "email": "*****@*****.**"})
Exemplo n.º 7
0
    def test_extract_user_details_sreg(self):
        endpoint = OpenIDServiceEndpoint()
        message = Message(OPENID2_NS)
        message.setArg(SREG_NS, "nickname", "someuser")
        message.setArg(SREG_NS, "fullname", "Some User")
        message.setArg(SREG_NS, "email", "*****@*****.**")
        response = SuccessResponse(
            endpoint, message, signed_fields=message.toPostArgs().keys())

        data = self.backend._extract_user_details(response)
        self.assertEqual(data, {"nickname": "someuser",
                                "first_name": "Some",
                                "last_name": "User",
                                "email": "*****@*****.**"})
Exemplo n.º 8
0
    def make_openid_response(self, sreg_args=None, teams_args=None):
        endpoint = OpenIDServiceEndpoint()
        endpoint.claimed_id = 'some-id'

        message = Message(OPENID2_NS)
        if sreg_args is not None:
            for key, value in sreg_args.items():
                message.setArg(SREG_NS, key, value)
        if teams_args is not None:
            for key, value in teams_args.items():
                message.setArg(TEAMS_NS, key, value)
        response = SuccessResponse(endpoint,
                                   message,
                                   signed_fields=message.toPostArgs().keys())
        return response
Exemplo n.º 9
0
    def test_extract_user_details_ax(self):
        endpoint = OpenIDServiceEndpoint()
        message = Message(OPENID2_NS)
        attributes = [
            ("nickname", "http://axschema.org/namePerson/friendly", "someuser"),
            ("fullname", "http://axschema.org/namePerson", "Some User"),
            ("email", "http://axschema.org/contact/email", "*****@*****.**"),
            ]
        message.setArg(AX_NS, "mode", "fetch_response")
        for (alias, uri, value) in attributes:
            message.setArg(AX_NS, "type.%s" % alias, uri)
            message.setArg(AX_NS, "value.%s" % alias, value)
        response = SuccessResponse(
            endpoint, message, signed_fields=message.toPostArgs().keys())

        data = self.backend._extract_user_details(response)
        self.assertEqual(data, {"nickname": "someuser",
                                "first_name": "Some",
                                "last_name": "User",
                                "email": "*****@*****.**"})
Exemplo n.º 10
0
    def test_explicitOpenID11NSSerialzation(self):
        m = Message(THE_OTHER_OPENID1_NS, False)

        post_args = m.toPostArgs()
        self.assertEqual(post_args, {'openid.ns': THE_OTHER_OPENID1_NS})
Exemplo n.º 11
0
class EmptyMessageTest(unittest.TestCase):
    def setUp(self):
        self.msg = Message()

    def test_toPostArgs(self):
        self.assertEqual(self.msg.toPostArgs(), {})

    def test_toArgs(self):
        self.assertEqual(self.msg.toArgs(), {})

    def test_toKVForm(self):
        self.assertEqual(self.msg.toKVForm(), '')

    def test_toURLEncoded(self):
        self.assertEqual(self.msg.toURLEncoded(), '')

    def test_toURL(self):
        base_url = 'http://base.url/'
        self.assertEqual(self.msg.toURL(base_url), base_url)

    def test_getOpenID(self):
        self.assertIsNone(self.msg.getOpenIDNamespace())

    def test_getKeyOpenID(self):
        # Could reasonably return None instead of raising an
        # exception. I'm not sure which one is more right, since this
        # case should only happen when you're building a message from
        # scratch and so have no default namespace.
        warning_msg = "UndefinedOpenIDNamespace exception is deprecated."
        with ShouldWarn(DeprecationWarning(warning_msg)):
            warnings.simplefilter('always')
            self.assertRaises(UndefinedOpenIDNamespace, self.msg.getKey, OPENID_NS, 'foo')

    def test_getKeyBARE(self):
        self.assertEqual(self.msg.getKey(BARE_NS, 'foo'), 'foo')

    def test_getKeyNS1(self):
        self.assertIsNone(self.msg.getKey(OPENID1_NS, 'foo'))

    def test_getKeyNS2(self):
        self.assertIsNone(self.msg.getKey(OPENID2_NS, 'foo'))

    def test_getKeyNS3(self):
        self.assertIsNone(self.msg.getKey('urn:nothing-significant', 'foo'))

    def test_hasKey(self):
        # Could reasonably return False instead of raising an
        # exception. I'm not sure which one is more right, since this
        # case should only happen when you're building a message from
        # scratch and so have no default namespace.
        warning_msg = "UndefinedOpenIDNamespace exception is deprecated."
        with ShouldWarn(DeprecationWarning(warning_msg)):
            warnings.simplefilter('always')
            self.assertRaises(UndefinedOpenIDNamespace, self.msg.hasKey, OPENID_NS, 'foo')

    def test_hasKeyBARE(self):
        self.assertFalse(self.msg.hasKey(BARE_NS, 'foo'))

    def test_hasKeyNS1(self):
        self.assertFalse(self.msg.hasKey(OPENID1_NS, 'foo'))

    def test_hasKeyNS2(self):
        self.assertFalse(self.msg.hasKey(OPENID2_NS, 'foo'))

    def test_hasKeyNS3(self):
        self.assertFalse(self.msg.hasKey('urn:nothing-significant', 'foo'))

    def test_getAliasedArgSuccess(self):
        msg = Message.fromPostArgs({'openid.ns.test': 'urn://foo', 'openid.test.flub': 'bogus'})
        actual_uri = msg.getAliasedArg('ns.test', no_default)
        self.assertEqual("urn://foo", actual_uri)

    def test_getAliasedArgFailure(self):
        msg = Message.fromPostArgs({'openid.test.flub': 'bogus'})
        self.assertRaises(KeyError, msg.getAliasedArg, 'ns.test', no_default)

    def test_getArg(self):
        # Could reasonably return None instead of raising an
        # exception. I'm not sure which one is more right, since this
        # case should only happen when you're building a message from
        # scratch and so have no default namespace.
        warning_msg = "UndefinedOpenIDNamespace exception is deprecated."
        with ShouldWarn(DeprecationWarning(warning_msg)):
            warnings.simplefilter('always')
            self.assertRaises(UndefinedOpenIDNamespace, self.msg.getArg, OPENID_NS, 'foo')

    test_getArgBARE = mkGetArgTest(BARE_NS, 'foo')
    test_getArgNS1 = mkGetArgTest(OPENID1_NS, 'foo')
    test_getArgNS2 = mkGetArgTest(OPENID2_NS, 'foo')
    test_getArgNS3 = mkGetArgTest('urn:nothing-significant', 'foo')

    def test_getArgs(self):
        # Could reasonably return {} instead of raising an
        # exception. I'm not sure which one is more right, since this
        # case should only happen when you're building a message from
        # scratch and so have no default namespace.
        warning_msg = "UndefinedOpenIDNamespace exception is deprecated."
        with ShouldWarn(DeprecationWarning(warning_msg)):
            warnings.simplefilter('always')
            self.assertRaises(UndefinedOpenIDNamespace, self.msg.getArgs, OPENID_NS)

    def test_getArgsBARE(self):
        self.assertEqual(self.msg.getArgs(BARE_NS), {})

    def test_getArgsNS1(self):
        self.assertEqual(self.msg.getArgs(OPENID1_NS), {})

    def test_getArgsNS2(self):
        self.assertEqual(self.msg.getArgs(OPENID2_NS), {})

    def test_getArgsNS3(self):
        self.assertEqual(self.msg.getArgs('urn:nothing-significant'), {})

    def test_updateArgs(self):
        warning_msg = "UndefinedOpenIDNamespace exception is deprecated."
        with ShouldWarn(DeprecationWarning(warning_msg)):
            warnings.simplefilter('always')
            self.assertRaises(UndefinedOpenIDNamespace, self.msg.updateArgs, OPENID_NS, {'does not': 'matter'})

    def _test_updateArgsNS(self, ns):
        update_args = {
            'Camper van Beethoven': 'David Lowery',
            'Magnolia Electric Co.': 'Jason Molina',
        }

        self.assertEqual(self.msg.getArgs(ns), {})
        self.msg.updateArgs(ns, update_args)
        self.assertEqual(self.msg.getArgs(ns), update_args)

    def test_updateArgsBARE(self):
        self._test_updateArgsNS(BARE_NS)

    def test_updateArgsNS1(self):
        self._test_updateArgsNS(OPENID1_NS)

    def test_updateArgsNS2(self):
        self._test_updateArgsNS(OPENID2_NS)

    def test_updateArgsNS3(self):
        self._test_updateArgsNS('urn:nothing-significant')

    def test_setArg(self):
        warning_msg = "UndefinedOpenIDNamespace exception is deprecated."
        with ShouldWarn(DeprecationWarning(warning_msg)):
            warnings.simplefilter('always')
            self.assertRaises(UndefinedOpenIDNamespace, self.msg.setArg, OPENID_NS, 'does not', 'matter')

    def _test_setArgNS(self, ns):
        key = 'Camper van Beethoven'
        value = 'David Lowery'
        self.assertIsNone(self.msg.getArg(ns, key))
        self.msg.setArg(ns, key, value)
        self.assertEqual(self.msg.getArg(ns, key), value)

    def test_setArgBARE(self):
        self._test_setArgNS(BARE_NS)

    def test_setArgNS1(self):
        self._test_setArgNS(OPENID1_NS)

    def test_setArgNS2(self):
        self._test_setArgNS(OPENID2_NS)

    def test_setArgNS3(self):
        self._test_setArgNS('urn:nothing-significant')

    def test_setArgToNone(self):
        self.assertRaises(AssertionError, self.msg.setArg, OPENID1_NS, 'op_endpoint', None)

    def test_delArg(self):
        # Could reasonably raise KeyError instead of raising
        # UndefinedOpenIDNamespace. I'm not sure which one is more
        # right, since this case should only happen when you're
        # building a message from scratch and so have no default
        # namespace.
        warning_msg = "UndefinedOpenIDNamespace exception is deprecated."
        with ShouldWarn(DeprecationWarning(warning_msg)):
            warnings.simplefilter('always')
            self.assertRaises(UndefinedOpenIDNamespace, self.msg.delArg, OPENID_NS, 'key')

    def _test_delArgNS(self, ns):
        key = 'Camper van Beethoven'
        self.assertRaises(KeyError, self.msg.delArg, ns, key)

    def test_delArgBARE(self):
        self._test_delArgNS(BARE_NS)

    def test_delArgNS1(self):
        self._test_delArgNS(OPENID1_NS)

    def test_delArgNS2(self):
        self._test_delArgNS(OPENID2_NS)

    def test_delArgNS3(self):
        self._test_delArgNS('urn:nothing-significant')

    def test_isOpenID1(self):
        self.assertFalse(self.msg.isOpenID1())

    def test_isOpenID2(self):
        self.assertFalse(self.msg.isOpenID2())