0
|
1 import pytest
|
2
|
2 from .mockutil import mock_urllib
|
0
|
3
|
|
4
|
|
5 def test_one_article(cli, feedutil, mastmock):
|
|
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 text of the article.</p>
|
|
10 <p>It has 2 paragraphs.</p>
|
|
11 </div>
|
|
12 <a class="u-url" href="https://example.org/a-new-article">permalink</a>"""
|
|
13 ))
|
|
14
|
|
15 cli.appendSiloConfig('test', 'mastodon', url='/blah')
|
|
16 mastmock.installTokens(cli, 'test')
|
|
17
|
|
18 ctx, _ = cli.run('process', feed)
|
|
19 assert ctx.cache.wasPosted('test', 'https://example.org/a-new-article')
|
|
20 toot = ctx.silos[0].client.toots[0]
|
|
21 assert toot == ('A new article https://example.org/a-new-article',
|
|
22 None, 'public')
|
|
23
|
|
24
|
|
25 def test_one_micropost(cli, feedutil, mastmock):
|
|
26 feed = cli.createTempFeed(feedutil.makeFeed(
|
|
27 """<p class="p-name">This is a quick update.</p>
|
|
28 <a class="u-url" href="/01234.html">permalink</a>"""
|
|
29 ))
|
|
30
|
|
31 cli.appendSiloConfig('test', 'mastodon', url='/blah')
|
|
32 mastmock.installTokens(cli, 'test')
|
|
33
|
|
34 ctx, _ = cli.run('process', feed)
|
|
35 assert ctx.cache.wasPosted('test', '/01234.html')
|
|
36 toot = ctx.silos[0].client.toots[0]
|
|
37 assert toot == ("This is a quick update.", None, 'public')
|
|
38
|
|
39
|
|
40 def test_one_micropost_with_one_photo(cli, feedutil, mastmock, monkeypatch):
|
|
41 feed = cli.createTempFeed(feedutil.makeFeed(
|
|
42 """<p class="p-name">This is a quick photo update.</p>
|
|
43 <div>
|
|
44 <a class="u-photo" href="/fullimg.jpg"><img src="/thumbimg.jpg"/></a>
|
|
45 </div>
|
|
46 <a class="u-url" href="/01234.html">permalink</a>"""
|
|
47 ))
|
|
48
|
|
49 cli.appendSiloConfig('test', 'mastodon', url='/blah')
|
|
50 mastmock.installTokens(cli, 'test')
|
|
51
|
|
52 with monkeypatch.context() as m:
|
2
|
53 import silorider.silos.mastodon
|
|
54 mock_urllib(m)
|
|
55 m.setattr(silorider.silos.mastodon.MastodonSilo, '_media_callback',
|
|
56 _patched_media_callback)
|
0
|
57 ctx, _ = cli.run('process', feed)
|
2
|
58
|
0
|
59 assert ctx.cache.wasPosted('test', '/01234.html')
|
|
60 media = ctx.silos[0].client.media[0]
|
2
|
61 assert media == ('/retrieved/fullimg.jpg', 'image/jpeg', 1)
|
0
|
62 toot = ctx.silos[0].client.toots[0]
|
|
63 assert toot == ("This is a quick photo update.", [1], 'public')
|
|
64
|
|
65
|
|
66 def test_one_micropost_with_two_photos(cli, feedutil, mastmock, monkeypatch):
|
|
67 feed = cli.createTempFeed(feedutil.makeFeed(
|
|
68 """<p class="p-name">This is a photo update with 2 photos.</p>
|
|
69 <div>
|
|
70 <a class="u-photo" href="/fullimg1.jpg"><img src="/thumbimg1.jpg"/></a>
|
|
71 <a class="u-photo" href="/fullimg2.jpg"><img src="/thumbimg2.jpg"/></a>
|
|
72 </div>
|
|
73 <a class="u-url" href="/01234.html">permalink</a>"""
|
|
74 ))
|
|
75
|
|
76 cli.appendSiloConfig('test', 'mastodon', url='/blah')
|
|
77 mastmock.installTokens(cli, 'test')
|
|
78
|
|
79 with monkeypatch.context() as m:
|
2
|
80 import silorider.silos.mastodon
|
|
81 mock_urllib(m)
|
|
82 m.setattr(silorider.silos.mastodon.MastodonSilo, '_media_callback',
|
|
83 _patched_media_callback)
|
0
|
84 ctx, _ = cli.run('process', feed)
|
2
|
85
|
0
|
86 assert ctx.cache.wasPosted('test', '/01234.html')
|
|
87 media = ctx.silos[0].client.media[0]
|
2
|
88 assert media == ('/retrieved/fullimg1.jpg', 'image/jpeg', 1)
|
0
|
89 media = ctx.silos[0].client.media[1]
|
2
|
90 assert media == ('/retrieved/fullimg2.jpg', 'image/jpeg', 2)
|
0
|
91 toot = ctx.silos[0].client.toots[0]
|
|
92 assert toot == ("This is a photo update with 2 photos.", [1, 2], 'public')
|
|
93
|
|
94
|
2
|
95 def _patched_media_callback(self, tmpfile, mt):
|
|
96 return self.client.media_post(tmpfile, mt)
|
0
|
97
|
|
98
|
|
99 @pytest.fixture(scope='session')
|
|
100 def mastmock():
|
|
101 from silorider.silos.mastodon import MastodonSilo
|
|
102 MastodonSilo._CLIENT_CLASS = MastodonMock
|
|
103 return MastodonMockUtil()
|
|
104
|
|
105
|
|
106 class MastodonMock:
|
|
107 @staticmethod
|
|
108 def create_app(app_name, scopes, api_base_url):
|
|
109 return ('TEST_CLIENT_ID', 'TEST_CLIENT_SECRET')
|
|
110
|
|
111 def __init__(self, client_id, client_secret, access_token, api_base_url):
|
|
112 self.toots = []
|
|
113 self.media = []
|
|
114 self.next_mid = 1
|
|
115
|
|
116 def log_in(self, username, password, scopes):
|
|
117 return 'TEST_ACCESS_TOKEN'
|
|
118
|
|
119 def auth_request_url(self, scopes):
|
|
120 return 'https://example.org/auth'
|
|
121
|
|
122 def status_post(self, toot, media_ids=None, visibility=None):
|
|
123 self.toots.append((toot, media_ids, visibility))
|
|
124
|
|
125 def media_post(self, filename, mimetype):
|
|
126 mid = self.next_mid
|
|
127 self.next_mid += 1
|
|
128 self.media.append((filename, mimetype, mid))
|
|
129 return mid
|
|
130
|
|
131
|
|
132 class MastodonMockUtil:
|
|
133 def installTokens(self, cli, silo_name):
|
|
134 def do_install_tokens(ctx):
|
|
135 ctx.cache.setCustomValue(
|
|
136 '%s_clienttoken' % silo_name,
|
|
137 'TEST_CLIENT_ID,TEST_CLIENT_SECRET')
|
|
138 ctx.cache.setCustomValue(
|
|
139 '%s_accesstoken' % silo_name,
|
|
140 'TEST_ACCESS_TOKEN')
|
|
141
|
|
142 cli.preExecHook(do_install_tokens)
|