Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_arxiv_urlize(self):
h = 'sosmooth.org'
app.config['SERVER_NAME'] = h
with app.app_context():
self.assertEqual(
urlize('http://example.com/'),
'<a href="http://example.com/">this http URL</a>',
'urlize (URL linking) 1/6')
self.assertEqual(
urlize('https://example.com/'),
'<a href="https://example.com/">this https URL</a>',
'urlize (URL linking) 2/6')
self.assertEqual(
urlize('ftp://example.com/'),
'<a href="ftp://example.com/">this ftp URL</a>',
'urlize (URL linking) 3/6')
self.assertEqual(
urlize('http://example.com/.hep-th/9901001'),
'<a href="http://example.com/.hep-th/9901001">this http URL</a>',
'urlize (URL linking) 4/6')
self.assertEqual(
urlize(
'http://projecteuclid.org/euclid.bj/1151525136'
),
'<a href="http://projecteuclid.org/euclid.bj/1151525136">this http URL</a>',
'urlize (URL linking) 6/6')
self.assertEqual(
urlize(' Correction to Bernoulli (2006), 12, 551--570 http://projecteuclid.org/euclid.bj/1151525136'),
Markup(' Correction to Bernoulli (2006), 12, 551--570 <a href="http://projecteuclid.org/euclid.bj/1151525136">this http URL</a>'),
'urlize (URL linking) 6/6')
f'<a href="https://arxiv.org/abs/hep-th/9901002">hep-th/9901002</a>.',
'followed by period')
self.assertEqual(
urlize('0702.0003.'),
f'<a href="https://arxiv.org/abs/0702.0003">0702.0003</a>.',
'followed by period')
self.assertEqual(
urlize('hep-th/9901001,hep-th/9901002'),
f'<a href="https://arxiv.org/abs/hep-th/9901001">hep-th/9901001</a>,<a href="https://arxiv.org/abs/hep-th/9901002">hep-th/9901002</a>',
'filter_urls_ids_escape (ID linking) 3/7')
self.assertEqual(
urlize('0702.0003, something'),
f'<a href="https://arxiv.org/abs/0702.0003">0702.0003</a>, something',
'followed by comma')
self.assertEqual(
urlize('(0702.0003) something'),
f'(<a href="https://arxiv.org/abs/0702.0003">0702.0003</a>) something',
'in parens')
'<a href="http://projecteuclid.org/euclid.bj/1151525136">this http URL</a>',
'urlize (URL linking) 6/6')
self.assertEqual(
urlize(' Correction to Bernoulli (2006), 12, 551--570 http://projecteuclid.org/euclid.bj/1151525136'),
Markup(' Correction to Bernoulli (2006), 12, 551--570 <a href="http://projecteuclid.org/euclid.bj/1151525136">this http URL</a>'),
'urlize (URL linking) 6/6')
# shouldn't match
self.assertEqual(
urlize('2448446.4710(5)'), '2448446.4710(5)',
'urlize (should not match) 1/9')
self.assertEqual(
urlize('HJD=2450274.4156+/-0.0009'),
'HJD=2450274.4156+/-0.0009',
'urlize (should not match) 2/9')
self.assertEqual(
urlize('T_min[HJD]=49238.83662(14)+0.146352739(11)E.'),
'T_min[HJD]=49238.83662(14)+0.146352739(11)E.',
'urlize (should not match) 3/9')
self.assertEqual(
urlize('Pspin=1008.3408s'), 'Pspin=1008.3408s',
'urlize (should not match) 4/9')
self.assertEqual(
urlize('2453527.87455^{+0.00085}_{-0.00091}'),
'2453527.87455^{+0.00085}_{-0.00091}',
'urlize (should not match) 5/9')
self.assertEqual(
urlize('2451435.4353'), '2451435.4353',
'urlize (should not match) 6/9')
self.assertEqual(
urlize('cond-mat/97063007'),
'<a href="https://arxiv.org/abs/cond-mat/9706300">cond-mat/9706300</a>7',
'urlize (should match) 7/9')
self.assertEqual(
urlize('ftp://example.com/'),
'<a href="ftp://example.com/">this ftp URL</a>',
'urlize (URL linking) 3/6')
self.assertEqual(
urlize('http://example.com/.hep-th/9901001'),
'<a href="http://example.com/.hep-th/9901001">this http URL</a>',
'urlize (URL linking) 4/6')
self.assertEqual(
urlize(
'http://projecteuclid.org/euclid.bj/1151525136'
),
'<a href="http://projecteuclid.org/euclid.bj/1151525136">this http URL</a>',
'urlize (URL linking) 6/6')
self.assertEqual(
urlize(' Correction to Bernoulli (2006), 12, 551--570 http://projecteuclid.org/euclid.bj/1151525136'),
Markup(' Correction to Bernoulli (2006), 12, 551--570 <a href="http://projecteuclid.org/euclid.bj/1151525136">this http URL</a>'),
'urlize (URL linking) 6/6')
# shouldn't match
self.assertEqual(
urlize('2448446.4710(5)'), '2448446.4710(5)',
'urlize (should not match) 1/9')
self.assertEqual(
urlize('HJD=2450274.4156+/-0.0009'),
'HJD=2450274.4156+/-0.0009',
'urlize (should not match) 2/9')
self.assertEqual(
urlize('T_min[HJD]=49238.83662(14)+0.146352739(11)E.'),
'T_min[HJD]=49238.83662(14)+0.146352739(11)E.',
'urlize (should not match) 3/9')
self.assertEqual(
urlize('Pspin=1008.3408s'), 'Pspin=1008.3408s',
def test_arxiv_id_urls_basic(self):
h = 'arxiv.org' # Totally bogus setup for testing, at least url_for returns something
app.config['SERVER_NAME'] = h
with app.app_context():
self.assertEqual(urlize('', ['arxiv_id']), '')
s = 'some text 134#%$$%&^^%*^&(()*_)_<>?:;[}}'
self.assertEqual(urlize(s), str(escape(s)),
'filters should return escaped text')
self.assertEqual(
urlize('hep-th/9901001'),
f'<a href="https://arxiv.org/abs/hep-th/9901001">hep-th/9901001</a>',
)
self.assertEqual(
urlize('hep-th/9901001 hep-th/9901002'),
f'<a href="https://arxiv.org/abs/hep-th/9901001">hep-th/9901001</a> <a href="https://arxiv.org/abs/hep-th/9901002">hep-th/9901002</a>'
)
def test_arxiv_id_urls_basic(self):
h = 'arxiv.org' # Totally bogus setup for testing, at least url_for returns something
app.config['SERVER_NAME'] = h
with app.app_context():
self.assertEqual(urlize('', ['arxiv_id']), '')
s = 'some text 134#%$$%&^^%*^&(()*_)_<>?:;[}}'
self.assertEqual(urlize(s), str(escape(s)),
'filters should return escaped text')
self.assertEqual(
urlize('hep-th/9901001'),
f'<a href="https://arxiv.org/abs/hep-th/9901001">hep-th/9901001</a>',
)
self.assertEqual(
urlize('hep-th/9901001 hep-th/9901002'),
f'<a href="https://arxiv.org/abs/hep-th/9901001">hep-th/9901001</a> <a href="https://arxiv.org/abs/hep-th/9901002">hep-th/9901002</a>'
)
def test_arxiv_id_urls_escaping(self):
h = 'sosmooth.org'
app.config['SERVER_NAME'] = h
with app.app_context():
ax_id = 'hep-th/9901002'
user_entered_txt = ' <div>div should be escaped</div>'
ex_txt = escape(user_entered_txt).__html__()
self.assertEqual(
urlize(ax_id + user_entered_txt),
f'<a href="https://arxiv.org/abs/hep-th/9901002">hep-th/9901002</a>{ex_txt}',
'Dealing with user entered text with html that should be escaped for safety'
)
jinja_escaped_txt = Markup(
' <div>div should already be escaped by jinja2</div>')
self.assertEqual(
urlize(ax_id + jinja_escaped_txt),
f'<a href="https://arxiv.org/abs/hep-th/9901002">hep-th/9901002</a>{jinja_escaped_txt}',
'Dealing with text that has been escaped by Jinja2 already')
def test_arxiv_id_urls_punct(self):
h = 'sosmooth.org'
app.config['SERVER_NAME'] = h
with app.app_context():
self.assertEqual(
urlize('hep-th/9901002.'),
f'<a href="https://arxiv.org/abs/hep-th/9901002">hep-th/9901002</a>.',
'followed by period')
self.assertEqual(
urlize('0702.0003.'),
f'<a href="https://arxiv.org/abs/0702.0003">0702.0003</a>.',
'followed by period')
self.assertEqual(
urlize('hep-th/9901001,hep-th/9901002'),
f'<a href="https://arxiv.org/abs/hep-th/9901001">hep-th/9901001</a>,<a href="https://arxiv.org/abs/hep-th/9901002">hep-th/9901002</a>',
'filter_urls_ids_escape (ID linking) 3/7')
self.assertEqual(
urlize('0702.0003, something'),
f'<a href="https://arxiv.org/abs/0702.0003">0702.0003</a>, something',
'followed by comma')
self.assertEqual(
urlize('(0702.0003) something'),
f'(<a href="https://arxiv.org/abs/0702.0003">0702.0003</a>) something',
'in parens')
'<a href="http://example.com/">this http URL</a>',
'urlize (URL linking) 1/6')
self.assertEqual(
urlize('https://example.com/'),
'<a href="https://example.com/">this https URL</a>',
'urlize (URL linking) 2/6')
self.assertEqual(
urlize('ftp://example.com/'),
'<a href="ftp://example.com/">this ftp URL</a>',
'urlize (URL linking) 3/6')
self.assertEqual(
urlize('http://example.com/.hep-th/9901001'),
'<a href="http://example.com/.hep-th/9901001">this http URL</a>',
'urlize (URL linking) 4/6')
self.assertEqual(
urlize(
'http://projecteuclid.org/euclid.bj/1151525136'
),
'<a href="http://projecteuclid.org/euclid.bj/1151525136">this http URL</a>',
'urlize (URL linking) 6/6')
self.assertEqual(
urlize(' Correction to Bernoulli (2006), 12, 551--570 http://projecteuclid.org/euclid.bj/1151525136'),
Markup(' Correction to Bernoulli (2006), 12, 551--570 <a href="http://projecteuclid.org/euclid.bj/1151525136">this http URL</a>'),
'urlize (URL linking) 6/6')
# shouldn't match
self.assertEqual(
urlize('2448446.4710(5)'), '2448446.4710(5)',
'urlize (should not match) 1/9')
self.assertEqual(
urlize('HJD=2450274.4156+/-0.0009'),
'HJD=2450274.4156+/-0.0009',
'urlize (should not match) 2/9')
def test_arxiv_id_urls_punct(self):
h = 'sosmooth.org'
app.config['SERVER_NAME'] = h
with app.app_context():
self.assertEqual(
urlize('hep-th/9901002.'),
f'<a href="https://arxiv.org/abs/hep-th/9901002">hep-th/9901002</a>.',
'followed by period')
self.assertEqual(
urlize('0702.0003.'),
f'<a href="https://arxiv.org/abs/0702.0003">0702.0003</a>.',
'followed by period')
self.assertEqual(
urlize('hep-th/9901001,hep-th/9901002'),
f'<a href="https://arxiv.org/abs/hep-th/9901001">hep-th/9901001</a>,<a href="https://arxiv.org/abs/hep-th/9901002">hep-th/9901002</a>',
'filter_urls_ids_escape (ID linking) 3/7')
self.assertEqual(
urlize('0702.0003, something'),
f'<a href="https://arxiv.org/abs/0702.0003">0702.0003</a>, something',
'followed by comma')
self.assertEqual(
urlize('(0702.0003) something'),