Show More
@@ -1,216 +1,217 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | rhodecode.tests.test_libs |
|
4 | 4 | ~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
5 | 5 | |
|
6 | 6 | |
|
7 | 7 | Package for testing various lib/helper functions in rhodecode |
|
8 | 8 | |
|
9 | 9 | :created_on: Jun 9, 2011 |
|
10 | 10 | :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com> |
|
11 | 11 | :license: GPLv3, see COPYING for more details. |
|
12 | 12 | """ |
|
13 | 13 | # This program is free software: you can redistribute it and/or modify |
|
14 | 14 | # it under the terms of the GNU General Public License as published by |
|
15 | 15 | # the Free Software Foundation, either version 3 of the License, or |
|
16 | 16 | # (at your option) any later version. |
|
17 | 17 | # |
|
18 | 18 | # This program is distributed in the hope that it will be useful, |
|
19 | 19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20 | 20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21 | 21 | # GNU General Public License for more details. |
|
22 | 22 | # |
|
23 | 23 | # You should have received a copy of the GNU General Public License |
|
24 | 24 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
25 | 25 | from __future__ import with_statement |
|
26 | 26 | import unittest |
|
27 | 27 | import datetime |
|
28 | 28 | import hashlib |
|
29 | 29 | import mock |
|
30 | 30 | from rhodecode.tests import * |
|
31 | 31 | |
|
32 | 32 | proto = 'http' |
|
33 | 33 | TEST_URLS = [ |
|
34 | 34 | ('%s://127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'], |
|
35 | 35 | '%s://127.0.0.1' % proto), |
|
36 | 36 | ('%s://marcink@127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'], |
|
37 | 37 | '%s://127.0.0.1' % proto), |
|
38 | 38 | ('%s://marcink:pass@127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'], |
|
39 | 39 | '%s://127.0.0.1' % proto), |
|
40 | 40 | ('%s://127.0.0.1:8080' % proto, ['%s://' % proto, '127.0.0.1', '8080'], |
|
41 | 41 | '%s://127.0.0.1:8080' % proto), |
|
42 | 42 | ('%s://domain.org' % proto, ['%s://' % proto, 'domain.org'], |
|
43 | 43 | '%s://domain.org' % proto), |
|
44 | 44 | ('%s://user:pass@domain.org:8080' % proto, ['%s://' % proto, 'domain.org', |
|
45 | 45 | '8080'], |
|
46 | 46 | '%s://domain.org:8080' % proto), |
|
47 | 47 | ] |
|
48 | 48 | |
|
49 | 49 | proto = 'https' |
|
50 | 50 | TEST_URLS += [ |
|
51 | 51 | ('%s://127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'], |
|
52 | 52 | '%s://127.0.0.1' % proto), |
|
53 | 53 | ('%s://marcink@127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'], |
|
54 | 54 | '%s://127.0.0.1' % proto), |
|
55 | 55 | ('%s://marcink:pass@127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'], |
|
56 | 56 | '%s://127.0.0.1' % proto), |
|
57 | 57 | ('%s://127.0.0.1:8080' % proto, ['%s://' % proto, '127.0.0.1', '8080'], |
|
58 | 58 | '%s://127.0.0.1:8080' % proto), |
|
59 | 59 | ('%s://domain.org' % proto, ['%s://' % proto, 'domain.org'], |
|
60 | 60 | '%s://domain.org' % proto), |
|
61 | 61 | ('%s://user:pass@domain.org:8080' % proto, ['%s://' % proto, 'domain.org', |
|
62 | 62 | '8080'], |
|
63 | 63 | '%s://domain.org:8080' % proto), |
|
64 | 64 | ] |
|
65 | 65 | |
|
66 | 66 | |
|
67 | 67 | class TestLibs(unittest.TestCase): |
|
68 | 68 | |
|
69 | 69 | def test_uri_filter(self): |
|
70 | 70 | from rhodecode.lib.utils2 import uri_filter |
|
71 | 71 | |
|
72 | 72 | for url in TEST_URLS: |
|
73 | 73 | self.assertEqual(uri_filter(url[0]), url[1]) |
|
74 | 74 | |
|
75 | 75 | def test_credentials_filter(self): |
|
76 | 76 | from rhodecode.lib.utils2 import credentials_filter |
|
77 | 77 | |
|
78 | 78 | for url in TEST_URLS: |
|
79 | 79 | self.assertEqual(credentials_filter(url[0]), url[2]) |
|
80 | 80 | |
|
81 | 81 | def test_str2bool(self): |
|
82 | 82 | from rhodecode.lib.utils2 import str2bool |
|
83 | 83 | test_cases = [ |
|
84 | 84 | ('t', True), |
|
85 | 85 | ('true', True), |
|
86 | 86 | ('y', True), |
|
87 | 87 | ('yes', True), |
|
88 | 88 | ('on', True), |
|
89 | 89 | ('1', True), |
|
90 | 90 | ('Y', True), |
|
91 | 91 | ('yeS', True), |
|
92 | 92 | ('Y', True), |
|
93 | 93 | ('TRUE', True), |
|
94 | 94 | ('T', True), |
|
95 | 95 | ('False', False), |
|
96 | 96 | ('F', False), |
|
97 | 97 | ('FALSE', False), |
|
98 | 98 | ('0', False), |
|
99 | 99 | ('-1', False), |
|
100 | 100 | ('', False), ] |
|
101 | 101 | |
|
102 | 102 | for case in test_cases: |
|
103 | 103 | self.assertEqual(str2bool(case[0]), case[1]) |
|
104 | 104 | |
|
105 | 105 | def test_mention_extractor(self): |
|
106 | 106 | from rhodecode.lib.utils2 import extract_mentioned_users |
|
107 | 107 | sample = ( |
|
108 | 108 | "@first hi there @marcink here's my email marcin@email.com " |
|
109 | 109 | "@lukaszb check @one_more22 it pls @ ttwelve @D[] @one@two@three " |
|
110 | 110 | "@MARCIN @maRCiN @2one_more22 @john please see this http://org.pl " |
|
111 | 111 | "@marian.user just do it @marco-polo and next extract @marco_polo " |
|
112 | 112 | "user.dot hej ! not-needed maril@domain.org" |
|
113 | 113 | ) |
|
114 | 114 | |
|
115 | 115 | s = sorted([ |
|
116 | 116 | 'first', 'marcink', 'lukaszb', 'one_more22', 'MARCIN', 'maRCiN', 'john', |
|
117 | 117 | 'marian.user', 'marco-polo', 'marco_polo' |
|
118 | 118 | ], key=lambda k: k.lower()) |
|
119 | 119 | self.assertEqual(s, extract_mentioned_users(sample)) |
|
120 | 120 | |
|
121 | 121 | def test_age(self): |
|
122 | 122 | import calendar |
|
123 | 123 | from rhodecode.lib.utils2 import age |
|
124 | 124 | n = datetime.datetime.now() |
|
125 | 125 | delt = lambda *args, **kwargs: datetime.timedelta(*args, **kwargs) |
|
126 | prev_month = n.month - 1 if n.month != 1 else n.month - 2 | |
|
126 | 127 | self.assertEqual(age(n), u'just now') |
|
127 | 128 | self.assertEqual(age(n - delt(seconds=1)), u'1 second ago') |
|
128 | 129 | self.assertEqual(age(n - delt(seconds=60 * 2)), u'2 minutes ago') |
|
129 | 130 | self.assertEqual(age(n - delt(hours=1)), u'1 hour ago') |
|
130 | 131 | self.assertEqual(age(n - delt(hours=24)), u'1 day ago') |
|
131 | 132 | self.assertEqual(age(n - delt(hours=24 * 5)), u'5 days ago') |
|
132 |
self.assertEqual(age(n - delt(hours=24 * (calendar.mdays[ |
|
|
133 | self.assertEqual(age(n - delt(hours=24 * (calendar.mdays[prev_month]))), | |
|
133 | 134 | u'1 month ago') |
|
134 |
self.assertEqual(age(n - delt(hours=24 * (calendar.mdays[ |
|
|
135 | self.assertEqual(age(n - delt(hours=24 * (calendar.mdays[prev_month] + 2))), | |
|
135 | 136 | u'1 month and 2 days ago') |
|
136 | 137 | self.assertEqual(age(n - delt(hours=24 * 400)), u'1 year and 1 month ago') |
|
137 | 138 | |
|
138 | 139 | def test_age_in_future(self): |
|
139 | 140 | import calendar |
|
140 | 141 | from rhodecode.lib.utils2 import age |
|
141 | 142 | n = datetime.datetime.now() |
|
142 | 143 | delt = lambda *args, **kwargs: datetime.timedelta(*args, **kwargs) |
|
143 | 144 | self.assertEqual(age(n), u'just now') |
|
144 | 145 | self.assertEqual(age(n + delt(seconds=1)), u'in 1 second') |
|
145 | 146 | self.assertEqual(age(n + delt(seconds=60 * 2)), u'in 2 minutes') |
|
146 | 147 | self.assertEqual(age(n + delt(hours=1)), u'in 1 hour') |
|
147 | 148 | self.assertEqual(age(n + delt(hours=24)), u'in 1 day') |
|
148 | 149 | self.assertEqual(age(n + delt(hours=24 * 5)), u'in 5 days') |
|
149 | 150 | self.assertEqual(age(n + delt(hours=24 * (calendar.mdays[n.month]))), |
|
150 | 151 | u'in 1 month') |
|
151 | 152 | self.assertEqual(age(n + delt(hours=24 * (calendar.mdays[n.month] + 1))), |
|
152 | 153 | u'in 1 month and 1 day') |
|
153 | 154 | self.assertEqual(age(n + delt(hours=24 * 400)), u'in 1 year and 1 month') |
|
154 | 155 | |
|
155 | 156 | def test_tag_exctrator(self): |
|
156 | 157 | sample = ( |
|
157 | 158 | "hello pta[tag] gog [[]] [[] sda ero[or]d [me =>>< sa]" |
|
158 | 159 | "[requires] [stale] [see<>=>] [see => http://url.com]" |
|
159 | 160 | "[requires => url] [lang => python] [just a tag]" |
|
160 | 161 | "[,d] [ => ULR ] [obsolete] [desc]]" |
|
161 | 162 | ) |
|
162 | 163 | from rhodecode.lib.helpers import desc_stylize |
|
163 | 164 | res = desc_stylize(sample) |
|
164 | 165 | self.assertTrue('<div class="metatag" tag="tag">tag</div>' in res) |
|
165 | 166 | self.assertTrue('<div class="metatag" tag="obsolete">obsolete</div>' in res) |
|
166 | 167 | self.assertTrue('<div class="metatag" tag="stale">stale</div>' in res) |
|
167 | 168 | self.assertTrue('<div class="metatag" tag="lang">python</div>' in res) |
|
168 | 169 | self.assertTrue('<div class="metatag" tag="requires">requires => <a href="/url">url</a></div>' in res) |
|
169 | 170 | self.assertTrue('<div class="metatag" tag="tag">tag</div>' in res) |
|
170 | 171 | |
|
171 | 172 | def test_alternative_gravatar(self): |
|
172 | 173 | from rhodecode.lib.helpers import gravatar_url |
|
173 | 174 | _md5 = lambda s: hashlib.md5(s).hexdigest() |
|
174 | 175 | |
|
175 | 176 | def fake_conf(**kwargs): |
|
176 | 177 | from pylons import config |
|
177 | 178 | config['app_conf'] = {} |
|
178 | 179 | config['app_conf']['use_gravatar'] = True |
|
179 | 180 | config['app_conf'].update(kwargs) |
|
180 | 181 | return config |
|
181 | 182 | |
|
182 | 183 | class fake_url(): |
|
183 | 184 | @classmethod |
|
184 | 185 | def current(cls, *args, **kwargs): |
|
185 | 186 | return 'https://server.com' |
|
186 | 187 | |
|
187 | 188 | with mock.patch('pylons.url', fake_url): |
|
188 | 189 | fake = fake_conf(alternative_gravatar_url='http://test.com/{email}') |
|
189 | 190 | with mock.patch('pylons.config', fake): |
|
190 | 191 | from pylons import url |
|
191 | 192 | assert url.current() == 'https://server.com' |
|
192 | 193 | grav = gravatar_url(email_address='test@foo.com', size=24) |
|
193 | 194 | assert grav == 'http://test.com/test@foo.com' |
|
194 | 195 | |
|
195 | 196 | fake = fake_conf(alternative_gravatar_url='http://test.com/{email}') |
|
196 | 197 | with mock.patch('pylons.config', fake): |
|
197 | 198 | grav = gravatar_url(email_address='test@foo.com', size=24) |
|
198 | 199 | assert grav == 'http://test.com/test@foo.com' |
|
199 | 200 | |
|
200 | 201 | fake = fake_conf(alternative_gravatar_url='http://test.com/{md5email}') |
|
201 | 202 | with mock.patch('pylons.config', fake): |
|
202 | 203 | em = 'test@foo.com' |
|
203 | 204 | grav = gravatar_url(email_address=em, size=24) |
|
204 | 205 | assert grav == 'http://test.com/%s' % (_md5(em)) |
|
205 | 206 | |
|
206 | 207 | fake = fake_conf(alternative_gravatar_url='http://test.com/{md5email}/{size}') |
|
207 | 208 | with mock.patch('pylons.config', fake): |
|
208 | 209 | em = 'test@foo.com' |
|
209 | 210 | grav = gravatar_url(email_address=em, size=24) |
|
210 | 211 | assert grav == 'http://test.com/%s/%s' % (_md5(em), 24) |
|
211 | 212 | |
|
212 | 213 | fake = fake_conf(alternative_gravatar_url='{scheme}://{netloc}/{md5email}/{size}') |
|
213 | 214 | with mock.patch('pylons.config', fake): |
|
214 | 215 | em = 'test@foo.com' |
|
215 | 216 | grav = gravatar_url(email_address=em, size=24) |
|
216 | 217 | assert grav == 'https://server.com/%s/%s' % (_md5(em), 24) |
General Comments 0
You need to be logged in to leave comments.
Login now