Mercurial > silorider
annotate tests/test_silos_bluesky.py @ 64:d76063c61c6d
Fix wrong help message.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 26 Dec 2023 16:25:51 -0800 |
parents | c4dbbbb4990a |
children | dafbbf25bfc8 |
rev | line source |
---|---|
46 | 1 import pytest |
2 import atproto.xrpc_client.models as atprotomodels | |
3 from .mockutil import mock_urllib | |
4 | |
5 | |
6 def test_one_article(cli, feedutil, bskymock): | |
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', 'bluesky') | |
17 cli.setFeedConfig('feed', feed) | |
18 bskymock.installCredentials(cli, 'test') | |
19 | |
20 ctx, _ = cli.run('process') | |
21 assert ctx.cache.wasPosted('test', 'https://example.org/a-new-article') | |
22 post = ctx.silos[0].client.posts[0] | |
23 assert post == ('A new article https://example.org/a-new-article', | |
24 None, None) | |
25 | |
26 | |
27 def test_one_micropost(cli, feedutil, bskymock): | |
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', 'bluesky') | |
34 cli.setFeedConfig('feed', feed) | |
35 bskymock.installCredentials(cli, 'test') | |
36 | |
37 ctx, _ = cli.run('process') | |
38 assert ctx.cache.wasPosted('test', '/01234.html') | |
39 post = ctx.silos[0].client.posts[0] | |
40 assert post == ("This is a quick update.", None, None) | |
41 | |
42 | |
43 def test_one_micropost_with_one_photo(cli, feedutil, bskymock, 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', 'bluesky') | |
53 cli.setFeedConfig('feed', feed) | |
54 bskymock.installCredentials(cli, 'test') | |
55 | |
56 with monkeypatch.context() as m: | |
57 import silorider.silos.bluesky | |
58 mock_urllib(m) | |
58
d65f6dced79f
Fix media callback patches in unit tests
Ludovic Chabant <ludovic@chabant.com>
parents:
48
diff
changeset
|
59 m.setattr(silorider.silos.bluesky.BlueskySilo, 'mediaCallback', |
46 | 60 _patched_media_callback) |
61 ctx, _ = cli.run('process') | |
62 | |
63 assert ctx.cache.wasPosted('test', '/01234.html') | |
64 blob = ctx.silos[0].client.blobs[0] | |
65 assert blob == ('/retrieved/fullimg.jpg', None) | |
66 post = ctx.silos[0].client.posts[0] | |
63
c4dbbbb4990a
Fix Bluesky unit tests
Ludovic Chabant <ludovic@chabant.com>
parents:
58
diff
changeset
|
67 assert post[1].images[0].__test_index == 0 |
c4dbbbb4990a
Fix Bluesky unit tests
Ludovic Chabant <ludovic@chabant.com>
parents:
58
diff
changeset
|
68 embed = atprotomodels.AppBskyEmbedImages.Main(images=[ |
c4dbbbb4990a
Fix Bluesky unit tests
Ludovic Chabant <ludovic@chabant.com>
parents:
58
diff
changeset
|
69 _make_atproto_image('/retrieved/fullimg.jpg', test_index=0)]) |
46 | 70 assert post == ("This is a quick photo update.", embed, None) |
71 | |
72 | |
73 def test_one_micropost_with_two_photos(cli, feedutil, bskymock, monkeypatch): | |
74 feed = cli.createTempFeed(feedutil.makeFeed( | |
75 """<p class="p-name">This is a photo update with 2 photos.</p> | |
76 <div> | |
77 <a class="u-photo" href="/fullimg1.jpg"><img src="/thumbimg1.jpg"/></a> | |
78 <a class="u-photo" href="/fullimg2.jpg"><img src="/thumbimg2.jpg"/></a> | |
79 </div> | |
80 <a class="u-url" href="/01234.html">permalink</a>""" | |
81 )) | |
82 | |
83 cli.appendSiloConfig('test', 'bluesky') | |
84 cli.setFeedConfig('feed', feed) | |
85 bskymock.installCredentials(cli, 'test') | |
86 | |
87 with monkeypatch.context() as m: | |
88 import silorider.silos.bluesky | |
89 mock_urllib(m) | |
58
d65f6dced79f
Fix media callback patches in unit tests
Ludovic Chabant <ludovic@chabant.com>
parents:
48
diff
changeset
|
90 m.setattr(silorider.silos.bluesky.BlueskySilo, 'mediaCallback', |
46 | 91 _patched_media_callback) |
92 ctx, _ = cli.run('process') | |
93 | |
94 assert ctx.cache.wasPosted('test', '/01234.html') | |
95 blob = ctx.silos[0].client.blobs[0] | |
96 assert blob == ('/retrieved/fullimg1.jpg', None) | |
97 blob = ctx.silos[0].client.blobs[1] | |
98 assert blob == ('/retrieved/fullimg2.jpg', None) | |
99 post = ctx.silos[0].client.posts[0] | |
63
c4dbbbb4990a
Fix Bluesky unit tests
Ludovic Chabant <ludovic@chabant.com>
parents:
58
diff
changeset
|
100 embed = atprotomodels.AppBskyEmbedImages.Main(images=[ |
c4dbbbb4990a
Fix Bluesky unit tests
Ludovic Chabant <ludovic@chabant.com>
parents:
58
diff
changeset
|
101 _make_atproto_image('/retrieved/fullimg1.jpg', test_index=0), |
c4dbbbb4990a
Fix Bluesky unit tests
Ludovic Chabant <ludovic@chabant.com>
parents:
58
diff
changeset
|
102 _make_atproto_image('/retrieved/fullimg2.jpg', test_index=1)]) |
46 | 103 assert post == ("This is a photo update with 2 photos.", embed, None) |
104 | |
105 | |
106 def test_one_micropost_with_links(cli, feedutil, bskymock): | |
107 cli.appendSiloConfig('test', 'bluesky') | |
108 bskymock.installCredentials(cli, 'test') | |
109 | |
110 feed = cli.createTempFeed(feedutil.makeFeed( | |
111 """<p class="p-name">This is a link: http://example.org/blah</p> | |
112 <a class="u-url" href="/01234.html">permalink</a>""")) | |
113 | |
114 cli.setFeedConfig('feed', feed) | |
115 ctx, _ = cli.run('process') | |
116 post = ctx.silos[0].client.posts[0] | |
117 assert post[0] == "This is a link: http://example.org/blah" | |
118 assert post[2] == None | |
119 | |
120 feed = cli.createTempFeed(feedutil.makeFeed( | |
121 """<p class="e-content">This is another link: <a href="http://example.org/blah">http://example.org/blah</a></p> | |
122 <a class="u-url" href="/01234.html">permalink</a>""")) # NOQA | |
123 cli.setFeedConfig('feed', feed) | |
124 ctx, _ = cli.run('process') | |
125 post = ctx.silos[0].client.posts[0] | |
126 assert post[0] == "This is another link: http://example.org/blah" # NOQA | |
127 facet = _make_link_facet('http://example.org/blah', 22, 45) | |
128 assert post[2] == [facet] | |
129 | |
130 feed = cli.createTempFeed(feedutil.makeFeed( | |
131 """<p class="e-content">This is yet <a href="http://example.org/blah">another link</a></p> | |
132 <a class="u-url" href="/01234.html">permalink</a>""")) # NOQA | |
133 cli.setFeedConfig('feed', feed) | |
134 ctx, _ = cli.run('process') | |
135 post = ctx.silos[0].client.posts[0] | |
136 assert post[0] == "This is yet another link" # NOQA | |
137 facet = _make_link_facet('http://example.org/blah', 12, 24) | |
138 assert post[2] == [facet] | |
139 | |
140 | |
141 def _make_link_facet(url, start, end): | |
142 return atprotomodels.AppBskyRichtextFacet.Main( | |
143 features=[atprotomodels.AppBskyRichtextFacet.Link(uri=url)], | |
144 index=atprotomodels.AppBskyRichtextFacet.ByteSlice( | |
145 byteStart=start, byteEnd=end), | |
146 ) | |
147 | |
148 | |
149 def _patched_media_callback(self, tmpfile, mt, url, desc): | |
150 return self.client.upload_blob(tmpfile, desc) | |
151 | |
152 | |
153 @pytest.fixture(scope='session') | |
154 def bskymock(): | |
155 from silorider.silos.bluesky import BlueskySilo | |
156 BlueskySilo._CLIENT_CLASS = BlueskyMock | |
157 return BlueskyMockUtil() | |
158 | |
159 | |
63
c4dbbbb4990a
Fix Bluesky unit tests
Ludovic Chabant <ludovic@chabant.com>
parents:
58
diff
changeset
|
160 def _make_atproto_image(link, alt="", mime_type="image/jpg", size=100, test_index=None): |
c4dbbbb4990a
Fix Bluesky unit tests
Ludovic Chabant <ludovic@chabant.com>
parents:
58
diff
changeset
|
161 # atproto will validate models and that forces us to create |
c4dbbbb4990a
Fix Bluesky unit tests
Ludovic Chabant <ludovic@chabant.com>
parents:
58
diff
changeset
|
162 # an actual Image object. |
c4dbbbb4990a
Fix Bluesky unit tests
Ludovic Chabant <ludovic@chabant.com>
parents:
58
diff
changeset
|
163 # Not sure why we need to use model_validate here -- the simple |
c4dbbbb4990a
Fix Bluesky unit tests
Ludovic Chabant <ludovic@chabant.com>
parents:
58
diff
changeset
|
164 # constructor with keywords throws a validation error :( |
c4dbbbb4990a
Fix Bluesky unit tests
Ludovic Chabant <ludovic@chabant.com>
parents:
58
diff
changeset
|
165 blob_link = atprotomodels.blob_ref.BlobRefLink.model_validate({'$link': link}) |
c4dbbbb4990a
Fix Bluesky unit tests
Ludovic Chabant <ludovic@chabant.com>
parents:
58
diff
changeset
|
166 blob = atprotomodels.blob_ref.BlobRef(mime_type=mime_type, ref=blob_link, size=size) |
c4dbbbb4990a
Fix Bluesky unit tests
Ludovic Chabant <ludovic@chabant.com>
parents:
58
diff
changeset
|
167 img = atprotomodels.AppBskyEmbedImages.Image(alt=alt, image=blob) |
c4dbbbb4990a
Fix Bluesky unit tests
Ludovic Chabant <ludovic@chabant.com>
parents:
58
diff
changeset
|
168 if test_index is not None: |
c4dbbbb4990a
Fix Bluesky unit tests
Ludovic Chabant <ludovic@chabant.com>
parents:
58
diff
changeset
|
169 img.__test_index = test_index |
c4dbbbb4990a
Fix Bluesky unit tests
Ludovic Chabant <ludovic@chabant.com>
parents:
58
diff
changeset
|
170 return img |
c4dbbbb4990a
Fix Bluesky unit tests
Ludovic Chabant <ludovic@chabant.com>
parents:
58
diff
changeset
|
171 |
c4dbbbb4990a
Fix Bluesky unit tests
Ludovic Chabant <ludovic@chabant.com>
parents:
58
diff
changeset
|
172 |
46 | 173 class BlueskyMock: |
174 def __init__(self, base_url): | |
175 # base_url is unused here. | |
176 self.posts = [] | |
177 self.blobs = [] | |
178 | |
179 def login(self, email, password): | |
180 assert email == 'TEST_EMAIL' | |
181 assert password == 'TEST_PASSWORD' | |
182 | |
183 def upload_blob(self, tmpfile, desc): | |
63
c4dbbbb4990a
Fix Bluesky unit tests
Ludovic Chabant <ludovic@chabant.com>
parents:
58
diff
changeset
|
184 img = _make_atproto_image(tmpfile, test_index=len(self.blobs)) |
46 | 185 self.blobs.append((tmpfile, desc)) |
63
c4dbbbb4990a
Fix Bluesky unit tests
Ludovic Chabant <ludovic@chabant.com>
parents:
58
diff
changeset
|
186 return img |
46 | 187 |
48
486affad656e
Rewrite posting process with card system and more structured API
Ludovic Chabant <ludovic@chabant.com>
parents:
46
diff
changeset
|
188 def send_post(self, text, post_datetime=None, embed=None, facets=None): |
46 | 189 self.posts.append((text, embed, facets)) |
190 | |
191 | |
192 class BlueskyMockUtil: | |
193 def installCredentials(self, cli, silo_name): | |
194 def do_install_credentials(ctx): | |
195 ctx.cache.setCustomValue( | |
196 '%s_email' % silo_name, | |
197 'TEST_EMAIL') | |
198 ctx.cache.setCustomValue( | |
199 '%s_password' % silo_name, | |
200 'TEST_PASSWORD') | |
201 | |
202 cli.preExecHook(do_install_credentials) |