Mercurial > silorider
annotate tests/test_silos_mastodon.py @ 69:dafbbf25bfc8
Upgrade bluesky silo to atproto 0.0.35 and unit tests.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 26 Dec 2023 16:55:22 -0800 |
parents | d65f6dced79f |
children |
rev | line source |
---|---|
69
dafbbf25bfc8
Upgrade bluesky silo to atproto 0.0.35 and unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
58
diff
changeset
|
1 import os.path |
0 | 2 import pytest |
2 | 3 from .mockutil import mock_urllib |
0 | 4 |
5 | |
6 def test_one_article(cli, feedutil, mastmock): | |
7 feed = cli.createTempFeed(feedutil.makeFeed( | |
8 """<h1 class="p-name">A new article</h1> | |
9 <div class="e-content"> | |
10 <p>This is the text of the article.</p> | |
11 <p>It has 2 paragraphs.</p> | |
12 </div> | |
13 <a class="u-url" href="https://example.org/a-new-article">permalink</a>""" | |
14 )) | |
15 | |
16 cli.appendSiloConfig('test', 'mastodon', url='/blah') | |
18
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
17 cli.setFeedConfig('feed', feed) |
0 | 18 mastmock.installTokens(cli, 'test') |
19 | |
18
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
20 ctx, _ = cli.run('process') |
0 | 21 assert ctx.cache.wasPosted('test', 'https://example.org/a-new-article') |
22 toot = ctx.silos[0].client.toots[0] | |
23 assert toot == ('A new article https://example.org/a-new-article', | |
24 None, 'public') | |
25 | |
26 | |
27 def test_one_micropost(cli, feedutil, mastmock): | |
28 feed = cli.createTempFeed(feedutil.makeFeed( | |
29 """<p class="p-name">This is a quick update.</p> | |
30 <a class="u-url" href="/01234.html">permalink</a>""" | |
31 )) | |
32 | |
33 cli.appendSiloConfig('test', 'mastodon', url='/blah') | |
18
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
34 cli.setFeedConfig('feed', feed) |
0 | 35 mastmock.installTokens(cli, 'test') |
36 | |
18
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
37 ctx, _ = cli.run('process') |
0 | 38 assert ctx.cache.wasPosted('test', '/01234.html') |
39 toot = ctx.silos[0].client.toots[0] | |
40 assert toot == ("This is a quick update.", None, 'public') | |
41 | |
42 | |
43 def test_one_micropost_with_one_photo(cli, feedutil, mastmock, monkeypatch): | |
44 feed = cli.createTempFeed(feedutil.makeFeed( | |
45 """<p class="p-name">This is a quick photo update.</p> | |
46 <div> | |
47 <a class="u-photo" href="/fullimg.jpg"><img src="/thumbimg.jpg"/></a> | |
48 </div> | |
49 <a class="u-url" href="/01234.html">permalink</a>""" | |
50 )) | |
51 | |
52 cli.appendSiloConfig('test', 'mastodon', url='/blah') | |
18
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
53 cli.setFeedConfig('feed', feed) |
0 | 54 mastmock.installTokens(cli, 'test') |
55 | |
56 with monkeypatch.context() as m: | |
2 | 57 import silorider.silos.mastodon |
58 mock_urllib(m) | |
69
dafbbf25bfc8
Upgrade bluesky silo to atproto 0.0.35 and unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
58
diff
changeset
|
59 m.setattr(os.path, 'getsize', lambda path: 42) |
58
d65f6dced79f
Fix media callback patches in unit tests
Ludovic Chabant <ludovic@chabant.com>
parents:
35
diff
changeset
|
60 m.setattr(silorider.silos.mastodon.MastodonSilo, 'mediaCallback', |
2 | 61 _patched_media_callback) |
18
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
62 ctx, _ = cli.run('process') |
2 | 63 |
0 | 64 assert ctx.cache.wasPosted('test', '/01234.html') |
65 media = ctx.silos[0].client.media[0] | |
2 | 66 assert media == ('/retrieved/fullimg.jpg', 'image/jpeg', 1) |
0 | 67 toot = ctx.silos[0].client.toots[0] |
68 assert toot == ("This is a quick photo update.", [1], 'public') | |
69 | |
70 | |
71 def test_one_micropost_with_two_photos(cli, feedutil, mastmock, monkeypatch): | |
72 feed = cli.createTempFeed(feedutil.makeFeed( | |
73 """<p class="p-name">This is a photo update with 2 photos.</p> | |
74 <div> | |
75 <a class="u-photo" href="/fullimg1.jpg"><img src="/thumbimg1.jpg"/></a> | |
76 <a class="u-photo" href="/fullimg2.jpg"><img src="/thumbimg2.jpg"/></a> | |
77 </div> | |
78 <a class="u-url" href="/01234.html">permalink</a>""" | |
79 )) | |
80 | |
81 cli.appendSiloConfig('test', 'mastodon', url='/blah') | |
18
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
82 cli.setFeedConfig('feed', feed) |
0 | 83 mastmock.installTokens(cli, 'test') |
84 | |
85 with monkeypatch.context() as m: | |
2 | 86 import silorider.silos.mastodon |
87 mock_urllib(m) | |
69
dafbbf25bfc8
Upgrade bluesky silo to atproto 0.0.35 and unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
58
diff
changeset
|
88 m.setattr(os.path, 'getsize', lambda path: 42) |
58
d65f6dced79f
Fix media callback patches in unit tests
Ludovic Chabant <ludovic@chabant.com>
parents:
35
diff
changeset
|
89 m.setattr(silorider.silos.mastodon.MastodonSilo, 'mediaCallback', |
2 | 90 _patched_media_callback) |
18
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
91 ctx, _ = cli.run('process') |
2 | 92 |
0 | 93 assert ctx.cache.wasPosted('test', '/01234.html') |
94 media = ctx.silos[0].client.media[0] | |
2 | 95 assert media == ('/retrieved/fullimg1.jpg', 'image/jpeg', 1) |
0 | 96 media = ctx.silos[0].client.media[1] |
2 | 97 assert media == ('/retrieved/fullimg2.jpg', 'image/jpeg', 2) |
0 | 98 toot = ctx.silos[0].client.toots[0] |
99 assert toot == ("This is a photo update with 2 photos.", [1, 2], 'public') | |
100 | |
101 | |
18
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
102 def test_one_micropost_with_links(cli, feedutil, mastmock): |
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
103 cli.appendSiloConfig('test', 'mastodon', url='/blah') |
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
104 mastmock.installTokens(cli, 'test') |
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
105 |
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
106 feed = cli.createTempFeed(feedutil.makeFeed( |
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
107 """<p class="p-name">This is a link: http://example.org/blah</p> |
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
108 <a class="u-url" href="/01234.html">permalink</a>""")) |
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
109 cli.setFeedConfig('feed', feed) |
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
110 ctx, _ = cli.run('process') |
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
111 toot = ctx.silos[0].client.toots[0] |
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
112 assert toot == ("This is a link: http://example.org/blah", None, 'public') |
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
113 |
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
114 feed = cli.createTempFeed(feedutil.makeFeed( |
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
115 """<p class="e-content">This is another link: <a href="http://example.org/blah">http://example.org/blah</a></p> |
30
53de06c2f97d
Fix Mastodon tests, add some logging for media attachments
Ludovic Chabant <ludovic@chabant.com>
parents:
18
diff
changeset
|
116 <a class="u-url" href="/01234.html">permalink</a>""")) # NOQA |
18
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
117 cli.setFeedConfig('feed', feed) |
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
118 ctx, _ = cli.run('process') |
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
119 toot = ctx.silos[0].client.toots[0] |
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
120 assert toot == ("This is another link: http://example.org/blah", None, 'public') # NOQA |
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
121 |
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
122 feed = cli.createTempFeed(feedutil.makeFeed( |
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
123 """<p class="e-content">This is yet <a href="http://example.org/blah">another link</a></p> |
30
53de06c2f97d
Fix Mastodon tests, add some logging for media attachments
Ludovic Chabant <ludovic@chabant.com>
parents:
18
diff
changeset
|
124 <a class="u-url" href="/01234.html">permalink</a>""")) # NOQA |
18
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
125 cli.setFeedConfig('feed', feed) |
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
126 ctx, _ = cli.run('process') |
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
127 toot = ctx.silos[0].client.toots[0] |
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
128 assert toot == ("This is yet another link http://example.org/blah", None, 'public') # NOQA |
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
129 |
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
130 |
35
7965adc14569
Handle alt-attributes for images
Ludovic Chabant <ludovic@chabant.com>
parents:
30
diff
changeset
|
131 def _patched_media_callback(self, tmpfile, mt, url, desc): |
2 | 132 return self.client.media_post(tmpfile, mt) |
0 | 133 |
134 | |
135 @pytest.fixture(scope='session') | |
136 def mastmock(): | |
137 from silorider.silos.mastodon import MastodonSilo | |
138 MastodonSilo._CLIENT_CLASS = MastodonMock | |
139 return MastodonMockUtil() | |
140 | |
141 | |
142 class MastodonMock: | |
143 @staticmethod | |
144 def create_app(app_name, scopes, api_base_url): | |
145 return ('TEST_CLIENT_ID', 'TEST_CLIENT_SECRET') | |
146 | |
147 def __init__(self, client_id, client_secret, access_token, api_base_url): | |
148 self.toots = [] | |
149 self.media = [] | |
150 self.next_mid = 1 | |
151 | |
152 def log_in(self, username, password, scopes): | |
153 return 'TEST_ACCESS_TOKEN' | |
154 | |
155 def auth_request_url(self, scopes): | |
156 return 'https://example.org/auth' | |
157 | |
158 def status_post(self, toot, media_ids=None, visibility=None): | |
159 self.toots.append((toot, media_ids, visibility)) | |
160 | |
161 def media_post(self, filename, mimetype): | |
162 mid = self.next_mid | |
163 self.next_mid += 1 | |
164 self.media.append((filename, mimetype, mid)) | |
165 return mid | |
166 | |
167 | |
168 class MastodonMockUtil: | |
169 def installTokens(self, cli, silo_name): | |
170 def do_install_tokens(ctx): | |
171 ctx.cache.setCustomValue( | |
172 '%s_clienttoken' % silo_name, | |
173 'TEST_CLIENT_ID,TEST_CLIENT_SECRET') | |
174 ctx.cache.setCustomValue( | |
175 '%s_accesstoken' % silo_name, | |
176 'TEST_ACCESS_TOKEN') | |
177 | |
178 cli.preExecHook(do_install_tokens) |