Skip to content

Commit 35ef2ac

Browse files
committed
fixed mock usage; got 3 of 5 relme tests working
1 parent 08bba7f commit 35ef2ac

File tree

5 files changed

+47
-51
lines changed

5 files changed

+47
-51
lines changed

ronkyuu/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
__email__ = 'bear@bear.im'
99
__copyright__ = 'Copyright (c) 2013-2016 by Mike Taylor and Kartik Prabhu'
1010
__license__ = 'MIT'
11-
__version__ = '0.3.3'
11+
__version__ = '0.3.4'
1212
__url__ = 'https://github.com/bear/ronkyuu'
1313
__download_url__ = 'https://pypi.python.org/pypi/ronkyuu'
1414
__description__ = 'Webmention Manager'

ronkyuu/relme.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
IndieWeb Rel=Me Tools
77
"""
88

9-
from tools import normalizeURL
9+
from tools import normalizeURL, cleanURL
1010

1111
import requests
1212
from urlparse import urlparse
1313
from bs4 import BeautifulSoup
1414

1515

16+
_html_parser = 'html5lib' # 'html.parser', 'lxml', 'lxml-xml'
17+
18+
1619
# see http://microformats.org/wiki/rel-me for the spec
1720
#
1821
# With a profile URL http://bear.im and a resource URL http://twitter.com/bear
@@ -71,15 +74,14 @@ def findRelMe(sourceURL):
7174
'url': sourceURL
7275
}
7376
if r.status_code == requests.codes.ok:
74-
dom = BeautifulSoup(r.text, 'html.parser')
75-
77+
dom = BeautifulSoup(r.text, _html_parser)
7678
for link in dom.find_all('a', rel='me'):
7779
rel = link.get('rel')
7880
href = link.get('href')
7981
if rel is not None and href is not None:
8082
url = urlparse(href)
8183
if url is not None and url.scheme in ('http', 'https'):
82-
result['relme'].append(href)
84+
result['relme'].append(cleanURL(href))
8385
return result
8486

8587

ronkyuu/tools.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
import json
1111
import re
12+
from urlparse import urlsplit, urlunsplit
1213

1314
import requests
1415

@@ -53,6 +54,11 @@ def getURLChain(targetURL):
5354
ok = False
5455
return (ok, chain)
5556

57+
def cleanURL(targetURL):
58+
scheme, netloc, path, query, fragment = urlsplit(targetURL)
59+
if len(path) == 0:
60+
path = '/'
61+
return urlunsplit((scheme, netloc, path, query, fragment))
5662

5763
def normalizeURL(targetURL):
5864
result = None

setup.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ def extract_metaitem(meta):
3838
license=extract_metaitem('license'),
3939
packages=find_packages(exclude=('tests', 'docs')),
4040
platforms=['Any'],
41+
install_requires=[
42+
'requests',
43+
'beautifulsoup',
44+
'mf2py',
45+
'html5lib',
46+
'lxml',
47+
],
4148
classifiers=[
4249
'Development Status :: 4 - Beta',
4350
'Intended Audience :: Developers',

tests/test_relme.py

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,91 +5,72 @@
55
"""
66

77
import unittest
8-
from httmock import urlmatch, HTTMock
8+
from httmock import all_requests, HTTMock
99

1010
from ronkyuu import findRelMe, confirmRelMe
11+
from ronkyuu.tools import cleanURL
1112

1213

13-
profile_url = "https://bear.im"
14-
redirect_url = "http://code-bear.com"
15-
twitter_url = "https://twitter.com/bear"
16-
other_url = "https://tantek.com"
14+
profile_url = cleanURL("https://bear.im")
15+
redirect_url = cleanURL("http://code-bear.com")
16+
twitter_url = cleanURL("https://twitter.com/bear")
17+
t_co_url = cleanURL('https://t.co/ZK4BFjSq66')
18+
other_url = cleanURL("https://tantek.com")
1719

1820
profile_html = ''.join(open('./tests/data/relme_bear.html').readlines())
1921
twitter_html = ''.join(open('./tests/data/relme_twitter_bear.html').readlines())
2022
other_html = ''.join(open('./tests/data/relme_tantek.html').readlines())
2123

22-
@urlmatch(netloc=r'https://bear\.im$')
23-
def bear_im_mock(url, request):
24-
return profile_html
25-
26-
@urlmatch(netloc=r'(.*\.)?twitter\.com$')
27-
def twitter_mock(url, request):
28-
return twitter_html
29-
30-
@urlmatch(netloc=r'(.*\.)?tantek\.com$')
31-
def other_mock(url, request):
24+
@all_requests
25+
def _mock(url, request):
26+
if 'code-bear.com' in url:
27+
return profile_html
28+
if 'bear.im' in url:
29+
return profile_html
30+
if 'twitter.com' in url:
31+
return twitter_html
3232
return other_html
3333

3434
class TestProfileURLParsing(unittest.TestCase):
3535
def runTest(self):
36-
with HTTMock(bear_im_mock):
36+
with HTTMock(_mock):
3737
relme = findRelMe(profile_url)
3838
assert len(relme['relme']) > 0
39-
assert 'https://bear.im' in relme['relme']
39+
assert profile_url in relme['relme']
4040

4141
class TestTargetURLParsing(unittest.TestCase):
4242
def runTest(self):
43-
with HTTMock(twitter_mock):
43+
with HTTMock(_mock):
4444
relme = findRelMe(twitter_url)
4545
assert len(relme['relme']) > 0
46-
assert 'https://t.co/ZK4BFjSq66' in relme['relme']
46+
assert t_co_url in relme['relme']
4747

4848
class TestConfirmRelMeSimple(unittest.TestCase):
4949
"""Test a relme case where the resource URL directly maps
5050
to the profile URL's relme list
5151
"""
5252
def runTest(self):
53-
with HTTMock(bear_im_mock):
53+
with HTTMock(_mock):
5454
relmeProfile = findRelMe(profile_url)
55-
assert len(relmeProfile['relme']) > 0
56-
assert 'https://bear.im' in relmeProfile['relme']
57-
58-
with HTTMock(twitter_mock):
59-
relmeTarget = findRelMe(twitter_url)
60-
assert len(relmeTarget['relme']) > 0
61-
assert 'https://t.co/ZK4BFjSq66' in relmeTarget['relme']
55+
relmeTarget = findRelMe(twitter_url)
6256

63-
assert confirmRelMe(profile_url, twitter_url, relmeProfile['relme'], relmeTarget['relme'])
57+
assert confirmRelMe(profile_url, twitter_url, relmeProfile['relme'], relmeTarget['relme'])
6458

6559
class TestInvalidRelMeSimple(unittest.TestCase):
6660
def runTest(self):
67-
with HTTMock(bear_im_mock):
61+
with HTTMock(_mock):
6862
relmeProfile = findRelMe(profile_url)
69-
assert len(relmeProfile['relme']) > 0
70-
assert 'https://bear.im' in relmeProfile['relme']
71-
72-
with HTTMock(other_mock):
7363
relmeOtherProfile = findRelMe(other_url)
74-
assert len(relmeOtherProfile['relme']) > 0
75-
assert 'https://twitter.com/t' in relmeOtherProfile['relme']
76-
77-
assert not confirmRelMe(profile_url, other_url, relmeProfile['relme'], relmeOtherProfile['relme'])
64+
assert not confirmRelMe(profile_url, other_url, relmeProfile['relme'], relmeOtherProfile['relme'])
7865

7966
class TestConfirmRelMeRedirect(unittest.TestCase):
8067
"""Test a relme case where the resource URL in-directly maps
8168
to the profile URL's relme list because the profile URL itself
8269
is a redirect.
8370
"""
8471
def runTest(self):
85-
with HTTMock(bear_im_mock):
72+
with HTTMock(_mock):
8673
relmeProfile = findRelMe(redirect_url)
87-
assert len(relmeProfile['relme']) > 0
88-
assert 'https://bear.im' in relmeProfile['relme']
89-
90-
with HTTMock(twitter_mock):
91-
relmeTarget = findRelMe(twitter_url)
92-
assert len(relmeTarget['relme']) > 0
93-
assert 'https://t.co/ZK4BFjSq66' in relmeTarget['relme']
74+
relmeTarget = findRelMe(twitter_url)
9475

95-
assert confirmRelMe(profile_url, twitter_url, relmeProfile['relme'], relmeTarget['relme'])
76+
assert confirmRelMe(profile_url, twitter_url, relmeProfile['relme'], relmeTarget['relme'])

0 commit comments

Comments
 (0)