~ssweeny/friends/fix-lp1258657

« back to all changes in this revision

Viewing changes to friends/tests/test_shortener.py

  • Committer: Andrew Starr-Bochicchio
  • Date: 2013-04-16 17:51:32 UTC
  • mfrom: (160.11.29 trunk-next)
  • mto: This revision was merged to the branch mainline in revision 218.
  • Revision ID: a.starr.b@gmail.com-20130416175132-kurn5v12qr2rhfn1
Merge on lp:~super-friends/friends/trunk-next

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
import unittest
24
24
 
25
 
from friends.shorteners import isgd, ougd, linkeecom, lookup, tinyurlcom
 
25
from friends.utils.shorteners import Short
26
26
from friends.tests.mocks import FakeSoupMessage, mock
27
27
 
28
28
 
33
33
    @mock.patch('friends.utils.http.Soup.Message',
34
34
                FakeSoupMessage('friends.tests.data', 'short.dat'))
35
35
    def test_isgd(self):
36
 
        # Test the shortener.
37
36
        self.assertEqual(
38
 
            isgd.URLShortener().shorten('http://www.python.org'),
 
37
            Short('is.gd').make('http://www.python.org'),
39
38
            'http://sho.rt/')
40
39
 
41
 
    def test_isgd_protocol(self):
42
 
        self.assertEqual(isgd.URLShortener.name, 'is.gd')
43
 
        self.assertEqual(isgd.URLShortener.fqdn, 'http://is.gd')
44
 
 
45
40
    @mock.patch('friends.utils.http.Soup.Message',
46
41
                FakeSoupMessage('friends.tests.data', 'short.dat'))
47
42
    def test_ougd(self):
48
 
        # Test the shortener.
49
43
        self.assertEqual(
50
 
            ougd.URLShortener().shorten('http://www.python.org'),
 
44
            Short('ou.gd').make('http://www.python.org'),
51
45
            'http://sho.rt/')
52
46
 
53
 
    def test_ougd_protocol(self):
54
 
        self.assertEqual(ougd.URLShortener.name, 'ou.gd')
55
 
        self.assertEqual(ougd.URLShortener.fqdn, 'http://ou.gd')
56
 
 
57
47
    @mock.patch('friends.utils.http.Soup.Message',
58
48
                FakeSoupMessage('friends.tests.data', 'short.dat'))
59
49
    def test_linkeecom(self):
60
 
        # Test the shortener.
61
50
        self.assertEqual(
62
 
            linkeecom.URLShortener().shorten('http://www.python.org'),
 
51
            Short('linkee.com').make('http://www.python.org'),
63
52
            'http://sho.rt/')
64
53
 
65
 
    def test_linkeecom_protocol(self):
66
 
        self.assertEqual(linkeecom.URLShortener.name, 'linkee.com')
67
 
        self.assertEqual(linkeecom.URLShortener.fqdn, 'http://linkee.com')
68
 
 
69
54
    @mock.patch('friends.utils.http.Soup.Message',
70
55
                FakeSoupMessage('friends.tests.data', 'short.dat'))
71
56
    def test_tinyurlcom(self):
72
 
        # Test the shortener.
73
57
        self.assertEqual(
74
 
            tinyurlcom.URLShortener().shorten('http://www.python.org'),
 
58
            Short('tinyurl.com').make('http://www.python.org'),
75
59
            'http://sho.rt/')
76
60
 
77
 
    def test_tinyurlcom_protocol(self):
78
 
        self.assertEqual(tinyurlcom.URLShortener.name, 'tinyurl.com')
79
 
        self.assertEqual(tinyurlcom.URLShortener.fqdn, 'http://tinyurl.com')
80
 
 
81
61
    @mock.patch('friends.utils.http.Soup.Message',
82
 
                FakeSoupMessage('friends.tests.data', 'short.dat'))
83
 
    def test_enabled_lookup(self):
84
 
        # Look up an enabled shortener.
85
 
        shortener = lookup.lookup('tinyurl.com')
 
62
                FakeSoupMessage('friends.tests.data', 'durlme.dat'))
 
63
    def test_durlme(self):
86
64
        self.assertEqual(
87
 
            shortener.shorten('http://www.python.org'),
88
 
            'http://sho.rt/')
 
65
            Short('durl.me').make('http://www.python.org'),
 
66
            'http://durl.me/5o')
89
67
 
90
68
    def test_missing_or_disabled_lookup(self):
91
69
        # Looking up a non-existent or disabled shortener gives you one that
92
70
        # returns the original url back unchanged.
93
 
        shortener = lookup.lookup('dummy')
94
 
        self.assertEqual(
95
 
            shortener.shorten('http://www.python.org'),
 
71
        self.assertEqual(
 
72
            Short('nonexistant').make('http://www.python.org'),
 
73
            'http://www.python.org')
 
74
        self.assertEqual(
 
75
            Short().make('http://www.python.org'),
96
76
            'http://www.python.org')
97
77
 
98
78
    def test_is_shortened(self):
99
 
        # Test a URL that has been shortened.
100
 
        self.assertTrue(lookup.is_shortened('http://tinyurl.com/foo'))
101
 
        self.assertTrue(lookup.is_shortened('http://is.gd/foo'))
102
 
        self.assertTrue(lookup.is_shortened('http://linkee.com/foo'))
103
 
        self.assertTrue(lookup.is_shortened('http://ou.gd/foo'))
 
79
        # Test URLs that have been shortened.
 
80
        self.assertTrue(Short.already('http://tinyurl.com/foo'))
 
81
        self.assertTrue(Short.already('http://is.gd/foo'))
 
82
        self.assertTrue(Short.already('http://linkee.com/foo'))
 
83
        self.assertTrue(Short.already('http://ou.gd/foo'))
 
84
        self.assertTrue(Short.already('http://durl.me/foo'))
104
85
 
105
86
    def test_is_not_shortened(self):
106
87
        # Test a URL that has not been shortened.
107
 
        self.assertFalse(lookup.is_shortened('http://www.python.org/bar'))
108
 
 
109
 
    @mock.patch('friends.shorteners.base.Downloader')
110
 
    def test_urls_quoted_properly(self, dl_mock):
111
 
        lookup.lookup('tinyurl.com').shorten(
 
88
        self.assertFalse(Short.already('http://www.python.org/bar'))
 
89
 
 
90
    @mock.patch('friends.utils.shorteners.Downloader')
 
91
    def test_isgd_quoted_properly(self, dl_mock):
 
92
        Short('is.gd').make('http://example.com/~user/stuff/+things')
 
93
        dl_mock.assert_called_once_with(
 
94
            'http://is.gd/api.php?longurl=http%3A%2F%2Fexample.com'
 
95
            '%2F%7Euser%2Fstuff%2F%2Bthings')
 
96
 
 
97
    @mock.patch('friends.utils.shorteners.Downloader')
 
98
    def test_ougd_quoted_properly(self, dl_mock):
 
99
        Short('ou.gd').make('http://example.com/~user/stuff/+things')
 
100
        dl_mock.assert_called_once_with(
 
101
            'http://ou.gd/api.php?format=simple&action=shorturl&url='
 
102
            'http%3A%2F%2Fexample.com%2F%7Euser%2Fstuff%2F%2Bthings')
 
103
 
 
104
    @mock.patch('friends.utils.shorteners.Downloader')
 
105
    def test_linkeecom_quoted_properly(self, dl_mock):
 
106
        Short('linkee.com').make(
 
107
            'http://example.com/~user/stuff/+things')
 
108
        dl_mock.assert_called_once_with(
 
109
            'http://api.linkee.com/1.0/shorten?format=text&input='
 
110
            'http%3A%2F%2Fexample.com%2F%7Euser%2Fstuff%2F%2Bthings')
 
111
 
 
112
    @mock.patch('friends.utils.shorteners.Downloader')
 
113
    def test_tinyurl_quoted_properly(self, dl_mock):
 
114
        Short('tinyurl.com').make(
112
115
            'http://example.com/~user/stuff/+things')
113
116
        dl_mock.assert_called_once_with(
114
117
            'http://tinyurl.com/api-create.php?url=http%3A%2F%2Fexample.com'
115
118
            '%2F%7Euser%2Fstuff%2F%2Bthings')
 
119
 
 
120
    @mock.patch('friends.utils.shorteners.Downloader')
 
121
    def test_durlme_quoted_properly(self, dl_mock):
 
122
        dl_mock().get_string().strip.return_value = ''
 
123
        dl_mock.reset_mock()
 
124
        Short('durl.me').make(
 
125
            'http://example.com/~user/stuff/+things')
 
126
        dl_mock.assert_called_once_with(
 
127
            'http://durl.me/api/Create.do?type=json&longurl='
 
128
            'http%3A%2F%2Fexample.com%2F%7Euser%2Fstuff%2F%2Bthings')
 
129
 
 
130
    @mock.patch('friends.utils.shorteners.Downloader')
 
131
    def test_dont_over_shorten(self, dl_mock):
 
132
        Short('tinyurl.com').make('http://tinyurl.com/page_id')
 
133
        Short('linkee.com').make('http://ou.gd/page_id')
 
134
        Short('is.gd').make('http://is.gd/page_id')
 
135
        Short('ou.gd').make('http://linkee.com/page_id')
 
136
        self.assertEqual(dl_mock.call_count, 0)
 
137
 
 
138
    def test_find_all_in_string(self):
 
139
        shorter = Short()
 
140
        shorter.make = lambda url: 'zombo.com'
 
141
        self.assertEqual(
 
142
            'Welcome to zombo.com, anything is possible. '
 
143
            'You can do anything at zombo.com!',
 
144
            shorter.sub(
 
145
                'Welcome to http://example.com/really/really/long/url, '
 
146
                'anything is possible. You can do anything at '
 
147
                'http://example.com!'))