Mercurial > silorider
comparison tests/test_silos_mastodon.py @ 18:a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
- This lets us properly handle various forms of linking.
- Add tests for processing posts with links.
- Fix configuration in tests.
- Basic error handling for processing posts.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 16 Sep 2018 21:16:20 -0700 |
parents | 27543b2e73b9 |
children | 53de06c2f97d |
comparison
equal
deleted
inserted
replaced
17:678278cb85b1 | 18:a921cc2306bc |
---|---|
11 </div> | 11 </div> |
12 <a class="u-url" href="https://example.org/a-new-article">permalink</a>""" | 12 <a class="u-url" href="https://example.org/a-new-article">permalink</a>""" |
13 )) | 13 )) |
14 | 14 |
15 cli.appendSiloConfig('test', 'mastodon', url='/blah') | 15 cli.appendSiloConfig('test', 'mastodon', url='/blah') |
16 cli.setFeedConfig('feed', feed) | |
16 mastmock.installTokens(cli, 'test') | 17 mastmock.installTokens(cli, 'test') |
17 | 18 |
18 ctx, _ = cli.run('process', feed) | 19 ctx, _ = cli.run('process') |
19 assert ctx.cache.wasPosted('test', 'https://example.org/a-new-article') | 20 assert ctx.cache.wasPosted('test', 'https://example.org/a-new-article') |
20 toot = ctx.silos[0].client.toots[0] | 21 toot = ctx.silos[0].client.toots[0] |
21 assert toot == ('A new article https://example.org/a-new-article', | 22 assert toot == ('A new article https://example.org/a-new-article', |
22 None, 'public') | 23 None, 'public') |
23 | 24 |
27 """<p class="p-name">This is a quick update.</p> | 28 """<p class="p-name">This is a quick update.</p> |
28 <a class="u-url" href="/01234.html">permalink</a>""" | 29 <a class="u-url" href="/01234.html">permalink</a>""" |
29 )) | 30 )) |
30 | 31 |
31 cli.appendSiloConfig('test', 'mastodon', url='/blah') | 32 cli.appendSiloConfig('test', 'mastodon', url='/blah') |
33 cli.setFeedConfig('feed', feed) | |
32 mastmock.installTokens(cli, 'test') | 34 mastmock.installTokens(cli, 'test') |
33 | 35 |
34 ctx, _ = cli.run('process', feed) | 36 ctx, _ = cli.run('process') |
35 assert ctx.cache.wasPosted('test', '/01234.html') | 37 assert ctx.cache.wasPosted('test', '/01234.html') |
36 toot = ctx.silos[0].client.toots[0] | 38 toot = ctx.silos[0].client.toots[0] |
37 assert toot == ("This is a quick update.", None, 'public') | 39 assert toot == ("This is a quick update.", None, 'public') |
38 | 40 |
39 | 41 |
45 </div> | 47 </div> |
46 <a class="u-url" href="/01234.html">permalink</a>""" | 48 <a class="u-url" href="/01234.html">permalink</a>""" |
47 )) | 49 )) |
48 | 50 |
49 cli.appendSiloConfig('test', 'mastodon', url='/blah') | 51 cli.appendSiloConfig('test', 'mastodon', url='/blah') |
52 cli.setFeedConfig('feed', feed) | |
50 mastmock.installTokens(cli, 'test') | 53 mastmock.installTokens(cli, 'test') |
51 | 54 |
52 with monkeypatch.context() as m: | 55 with monkeypatch.context() as m: |
53 import silorider.silos.mastodon | 56 import silorider.silos.mastodon |
54 mock_urllib(m) | 57 mock_urllib(m) |
55 m.setattr(silorider.silos.mastodon.MastodonSilo, '_media_callback', | 58 m.setattr(silorider.silos.mastodon.MastodonSilo, '_media_callback', |
56 _patched_media_callback) | 59 _patched_media_callback) |
57 ctx, _ = cli.run('process', feed) | 60 ctx, _ = cli.run('process') |
58 | 61 |
59 assert ctx.cache.wasPosted('test', '/01234.html') | 62 assert ctx.cache.wasPosted('test', '/01234.html') |
60 media = ctx.silos[0].client.media[0] | 63 media = ctx.silos[0].client.media[0] |
61 assert media == ('/retrieved/fullimg.jpg', 'image/jpeg', 1) | 64 assert media == ('/retrieved/fullimg.jpg', 'image/jpeg', 1) |
62 toot = ctx.silos[0].client.toots[0] | 65 toot = ctx.silos[0].client.toots[0] |
72 </div> | 75 </div> |
73 <a class="u-url" href="/01234.html">permalink</a>""" | 76 <a class="u-url" href="/01234.html">permalink</a>""" |
74 )) | 77 )) |
75 | 78 |
76 cli.appendSiloConfig('test', 'mastodon', url='/blah') | 79 cli.appendSiloConfig('test', 'mastodon', url='/blah') |
80 cli.setFeedConfig('feed', feed) | |
77 mastmock.installTokens(cli, 'test') | 81 mastmock.installTokens(cli, 'test') |
78 | 82 |
79 with monkeypatch.context() as m: | 83 with monkeypatch.context() as m: |
80 import silorider.silos.mastodon | 84 import silorider.silos.mastodon |
81 mock_urllib(m) | 85 mock_urllib(m) |
82 m.setattr(silorider.silos.mastodon.MastodonSilo, '_media_callback', | 86 m.setattr(silorider.silos.mastodon.MastodonSilo, '_media_callback', |
83 _patched_media_callback) | 87 _patched_media_callback) |
84 ctx, _ = cli.run('process', feed) | 88 ctx, _ = cli.run('process') |
85 | 89 |
86 assert ctx.cache.wasPosted('test', '/01234.html') | 90 assert ctx.cache.wasPosted('test', '/01234.html') |
87 media = ctx.silos[0].client.media[0] | 91 media = ctx.silos[0].client.media[0] |
88 assert media == ('/retrieved/fullimg1.jpg', 'image/jpeg', 1) | 92 assert media == ('/retrieved/fullimg1.jpg', 'image/jpeg', 1) |
89 media = ctx.silos[0].client.media[1] | 93 media = ctx.silos[0].client.media[1] |
90 assert media == ('/retrieved/fullimg2.jpg', 'image/jpeg', 2) | 94 assert media == ('/retrieved/fullimg2.jpg', 'image/jpeg', 2) |
91 toot = ctx.silos[0].client.toots[0] | 95 toot = ctx.silos[0].client.toots[0] |
92 assert toot == ("This is a photo update with 2 photos.", [1, 2], 'public') | 96 assert toot == ("This is a photo update with 2 photos.", [1, 2], 'public') |
97 | |
98 | |
99 def test_one_micropost_with_links(cli, feedutil, mastmock): | |
100 cli.appendSiloConfig('test', 'mastodon', url='/blah') | |
101 mastmock.installTokens(cli, 'test') | |
102 | |
103 feed = cli.createTempFeed(feedutil.makeFeed( | |
104 """<p class="p-name">This is a link: http://example.org/blah</p> | |
105 <a class="u-url" href="/01234.html">permalink</a>""")) | |
106 cli.setFeedConfig('feed', feed) | |
107 ctx, _ = cli.run('process') | |
108 toot = ctx.silos[0].client.toots[0] | |
109 assert toot == ("This is a link: http://example.org/blah", None, 'public') | |
110 | |
111 feed = cli.createTempFeed(feedutil.makeFeed( | |
112 """<p class="e-content">This is another link: <a href="http://example.org/blah">http://example.org/blah</a></p> | |
113 <a class="u-uri" href="/01234.html">permalink</a>""")) # NOQA | |
114 cli.setFeedConfig('feed', feed) | |
115 ctx, _ = cli.run('process') | |
116 toot = ctx.silos[0].client.toots[0] | |
117 assert toot == ("This is another link: http://example.org/blah", None, 'public') # NOQA | |
118 | |
119 feed = cli.createTempFeed(feedutil.makeFeed( | |
120 """<p class="e-content">This is yet <a href="http://example.org/blah">another link</a></p> | |
121 <a class="u-uri" href="/01234.html">permalink</a>""")) # NOQA | |
122 cli.setFeedConfig('feed', feed) | |
123 ctx, _ = cli.run('process') | |
124 toot = ctx.silos[0].client.toots[0] | |
125 assert toot == ("This is yet another link http://example.org/blah", None, 'public') # NOQA | |
93 | 126 |
94 | 127 |
95 def _patched_media_callback(self, tmpfile, mt): | 128 def _patched_media_callback(self, tmpfile, mt): |
96 return self.client.media_post(tmpfile, mt) | 129 return self.client.media_post(tmpfile, mt) |
97 | 130 |