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