comparison tests/test_silos_webmention.py @ 19:d3c4c5082bbc

Add Webmention silo.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 02 Oct 2018 22:22:31 -0700
parents
children
comparison
equal deleted inserted replaced
18:a921cc2306bc 19:d3c4c5082bbc
1 import unittest.mock
2 import requests
3
4
5 def test_one_article_no_mentions(cli, feedutil):
6 feed = cli.createTempFeed(feedutil.makeFeed(
7 """<h1 class="p-name">A new article</h1>
8 <div class="e-content">
9 <p>This is the abstract of the article.</p>
10 <p>Read more at <a class="u-url" href="https://example.org/a-new-article">permalink</a>.</p>
11 </div>
12 """ # NOQA
13 ))
14
15 cli.appendSiloConfig('test', 'webmention', url='/blah')
16 cli.setFeedConfig('feed', feed)
17
18 with unittest.mock.patch('requests.get') as mock_get, \
19 unittest.mock.patch('requests.post') as mock_post:
20 mock_get.side_effect = [
21 _MockResponse('')]
22 mock_post.side_effect = []
23 ctx, _ = cli.run('process')
24 assert mock_get.call_args_list[0][0] == ('https://example.org/a-new-article',) # NOQA
25
26
27 def test_one_article_one_mention(cli, feedutil):
28 feed = cli.createTempFeed(feedutil.makeFeed(
29 """<h1 class="p-name">A new article</h1>
30 <div class="e-content">
31 <p>This is the abstract of the article.</p>
32 <p>Read more at <a class="u-url" href="https://example.org/a-new-article">permalink</a>.</p>
33 </div>
34 """ # NOQA
35 ))
36
37 cli.appendSiloConfig('test', 'webmention', url='/blah')
38 cli.setFeedConfig('feed', feed)
39
40 with unittest.mock.patch('requests.get') as mock_get, \
41 unittest.mock.patch('requests.post') as mock_post:
42 mock_get.side_effect = [
43 _MockResponse("""
44 <p>This is a reply to <a href="https://other.org/article">another article<a>.</p>
45 """), # NOQA
46 _MockResponse("""
47 <html><head>
48 <link rel="webmention" href="https://other.org/webmention">
49 </head><body>
50 </body></html>""")]
51 mock_post.side_effect = [
52 _MockResponse('')]
53 ctx, _ = cli.run('process')
54 assert mock_get.call_args_list[0][0] == ('https://example.org/a-new-article',) # NOQA
55 assert mock_get.call_args_list[1][0] == ('https://other.org/article',) # NOQA
56 assert mock_post.call_args_list[0][0] == ('https://other.org/webmention',) # NOQA
57
58
59 class _MockResponse:
60 def __init__(self, txt):
61 self.status_code = requests.codes.ok
62 self.headers = {}
63 self.history = []
64 self.text = self.content = txt