~gary/python-openid/python-openid-2.2.1-patched

« back to all changes in this revision

Viewing changes to openid/test/test_xri.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2007-11-30 02:46:28 UTC
  • mfrom: (1.1.1 pyopenid-2.0)
  • Revision ID: launchpad@pqm.canonical.com-20071130024628-qktwsew3383iawmq
[rs=SteveA] upgrade to python-openid-2.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from unittest import TestCase
 
2
from openid.yadis import xri
 
3
 
 
4
class XriDiscoveryTestCase(TestCase):
 
5
    def test_isXRI(self):
 
6
        i = xri.identifierScheme
 
7
        self.failUnlessEqual(i('=john.smith'), 'XRI')
 
8
        self.failUnlessEqual(i('@smiths/john'), 'XRI')
 
9
        self.failUnlessEqual(i('smoker.myopenid.com'), 'URI')
 
10
        self.failUnlessEqual(i('xri://=john'), 'XRI')
 
11
 
 
12
 
 
13
class XriEscapingTestCase(TestCase):
 
14
    def test_escaping_percents(self):
 
15
        self.failUnlessEqual(xri.escapeForIRI('@example/abc%2Fd/ef'),
 
16
                             '@example/abc%252Fd/ef')
 
17
 
 
18
 
 
19
    def test_escaping_xref(self):
 
20
        # no escapes
 
21
        esc = xri.escapeForIRI
 
22
        self.failUnlessEqual('@example/foo/(@bar)', esc('@example/foo/(@bar)'))
 
23
        # escape slashes
 
24
        self.failUnlessEqual('@example/foo/(@bar%2Fbaz)',
 
25
                             esc('@example/foo/(@bar/baz)'))
 
26
        self.failUnlessEqual('@example/foo/(@bar%2Fbaz)/(+a%2Fb)',
 
27
                             esc('@example/foo/(@bar/baz)/(+a/b)'))
 
28
        # escape query ? and fragment #
 
29
        self.failUnlessEqual('@example/foo/(@baz%3Fp=q%23r)?i=j#k',
 
30
                             esc('@example/foo/(@baz?p=q#r)?i=j#k'))
 
31
 
 
32
 
 
33
 
 
34
class XriTransformationTestCase(TestCase):
 
35
    def test_to_iri_normal(self):
 
36
        self.failUnlessEqual(xri.toIRINormal('@example'), 'xri://@example')
 
37
 
 
38
    try:
 
39
        unichr(0x10000)
 
40
    except ValueError:
 
41
        # bleh narrow python build
 
42
        def test_iri_to_url(self):
 
43
            s = u'l\xa1m'
 
44
            expected = 'l%C2%A1m'
 
45
            self.failUnlessEqual(xri.iriToURI(s), expected)
 
46
    else:
 
47
        def test_iri_to_url(self):
 
48
            s = u'l\xa1m\U00101010n'
 
49
            expected = 'l%C2%A1m%F4%81%80%90n'
 
50
            self.failUnlessEqual(xri.iriToURI(s), expected)
 
51
 
 
52
 
 
53
 
 
54
class CanonicalIDTest(TestCase):
 
55
    def mkTest(providerID, canonicalID, isAuthoritative):
 
56
        def test(self):
 
57
            result = xri.providerIsAuthoritative(providerID, canonicalID)
 
58
            format = "%s providing %s, expected %s"
 
59
            message = format % (providerID, canonicalID, isAuthoritative)
 
60
            self.failUnlessEqual(isAuthoritative, result, message)
 
61
 
 
62
        return test
 
63
 
 
64
    test_equals = mkTest('=', '=!698.74D1.A1F2.86C7', True)
 
65
    test_atOne = mkTest('@!1234', '@!1234!ABCD', True)
 
66
    test_atTwo = mkTest('@!1234!5678', '@!1234!5678!ABCD', True)
 
67
 
 
68
    test_atEqualsFails = mkTest('@!1234', '=!1234!ABCD', False)
 
69
    test_tooDeepFails = mkTest('@!1234', '@!1234!ABCD!9765', False)
 
70
    test_atEqualsAndTooDeepFails = mkTest('@!1234!ABCD', '=!1234', False)
 
71
    test_differentBeginningFails = mkTest('=!BABE', '=!D00D', False)
 
72
 
 
73
class TestGetRootAuthority(TestCase):
 
74
    def mkTest(the_xri, expected_root):
 
75
        def test(self):
 
76
            actual_root = xri.rootAuthority(the_xri)
 
77
            self.failUnlessEqual(actual_root, xri.XRI(expected_root))
 
78
        return test
 
79
 
 
80
    test_at = mkTest("@foo", "@")
 
81
    test_atStar = mkTest("@foo*bar", "@")
 
82
    test_atStarStar = mkTest("@*foo*bar", "@")
 
83
    test_atWithPath = mkTest("@foo/bar", "@")
 
84
    test_bangBang = mkTest("!!990!991", "!")
 
85
    test_bang = mkTest("!1001!02", "!")
 
86
    test_equalsStar = mkTest("=foo*bar", "=")
 
87
    test_xrefPath = mkTest("(example.com)/foo", "(example.com)")
 
88
    test_xrefStar = mkTest("(example.com)*bar/foo", "(example.com)")
 
89
    test_uriAuth = mkTest("baz.example.com/foo", "baz.example.com")
 
90
    test_uriAuthPort = mkTest("baz.example.com:8080/foo",
 
91
                              "baz.example.com:8080")
 
92
 
 
93
    # Looking at the ABNF in XRI Syntax 2.0, I don't think you can
 
94
    # have example.com*bar.  You can do (example.com)*bar, but that
 
95
    # would mean something else.
 
96
    ##("example.com*bar/(=baz)", "example.com*bar"),
 
97
    ##("baz.example.com!01/foo", "baz.example.com!01"),
 
98
 
 
99
if __name__ == '__main__':
 
100
    import unittest
 
101
    unittest.main()