2
|
1 import pytest
|
|
2 from .mockutil import mock_urllib
|
|
3
|
|
4
|
|
5 def test_one_article(cli, feedutil, tweetmock):
|
|
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', 'twitter', url='/blah')
|
|
16 tweetmock.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.tweets[0]
|
|
21 assert toot == ('A new article https://example.org/a-new-article', None)
|
|
22
|
|
23
|
|
24 def test_one_micropost(cli, feedutil, tweetmock):
|
|
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', 'twitter', url='/blah')
|
|
31 tweetmock.installTokens(cli, 'test')
|
|
32
|
|
33 ctx, _ = cli.run('process', feed)
|
|
34 assert ctx.cache.wasPosted('test', '/01234.html')
|
|
35 toot = ctx.silos[0].client.tweets[0]
|
|
36 assert toot == ("This is a quick update.", None)
|
|
37
|
|
38
|
|
39 def test_one_micropost_with_one_photo(cli, feedutil, tweetmock, 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', 'twitter', url='/blah')
|
|
49 tweetmock.installTokens(cli, 'test')
|
|
50
|
|
51 with monkeypatch.context() as m:
|
|
52 import silorider.silos.twitter
|
|
53 mock_urllib(m)
|
|
54 m.setattr(silorider.silos.twitter.TwitterSilo, '_media_callback',
|
|
55 _patched_media_callback)
|
|
56 ctx, _ = cli.run('process', feed)
|
|
57
|
|
58 assert ctx.cache.wasPosted('test', '/01234.html')
|
|
59 media = ctx.silos[0].client.media[0]
|
|
60 assert media == ('/retrieved/fullimg.jpg', 1)
|
|
61 toot = ctx.silos[0].client.tweets[0]
|
|
62 assert toot == ("This is a quick photo update.", [1])
|
|
63
|
|
64
|
|
65 def test_one_micropost_with_two_photos(cli, feedutil, tweetmock, monkeypatch):
|
|
66 feed = cli.createTempFeed(feedutil.makeFeed(
|
|
67 """<p class="p-name">This is a photo update with 2 photos.</p>
|
|
68 <div>
|
|
69 <a class="u-photo" href="/fullimg1.jpg"><img src="/thumbimg1.jpg"/></a>
|
|
70 <a class="u-photo" href="/fullimg2.jpg"><img src="/thumbimg2.jpg"/></a>
|
|
71 </div>
|
|
72 <a class="u-url" href="/01234.html">permalink</a>"""
|
|
73 ))
|
|
74
|
|
75 cli.appendSiloConfig('test', 'twitter', url='/blah')
|
|
76 tweetmock.installTokens(cli, 'test')
|
|
77
|
|
78 with monkeypatch.context() as m:
|
|
79 import silorider.silos.twitter
|
|
80 mock_urllib(m)
|
|
81 m.setattr(silorider.silos.twitter.TwitterSilo, '_media_callback',
|
|
82 _patched_media_callback)
|
|
83 ctx, _ = cli.run('process', feed)
|
|
84
|
|
85 assert ctx.cache.wasPosted('test', '/01234.html')
|
|
86 media = ctx.silos[0].client.media[0]
|
|
87 assert media == ('/retrieved/fullimg1.jpg', 1)
|
|
88 media = ctx.silos[0].client.media[1]
|
|
89 assert media == ('/retrieved/fullimg2.jpg', 2)
|
|
90 toot = ctx.silos[0].client.tweets[0]
|
|
91 assert toot == ("This is a photo update with 2 photos.", [1, 2])
|
|
92
|
|
93
|
|
94 def _patched_media_callback(self, tmpfile, mt):
|
|
95 return self.client.UploadMediaChunked(tmpfile)
|
|
96
|
|
97
|
|
98 @pytest.fixture(scope='session')
|
|
99 def tweetmock():
|
|
100 from silorider.silos.twitter import TwitterSilo
|
|
101 TwitterSilo._CLIENT_CLASS = TwitterMock
|
|
102 return TwitterMockUtil()
|
|
103
|
|
104
|
|
105 class TwitterMock:
|
|
106 def __init__(self, consumer_key, consumer_secret,
|
|
107 access_token_key, access_token_secret):
|
|
108 assert consumer_key == 'TEST_CLIENT_KEY'
|
|
109 assert consumer_secret == 'TEST_CLIENT_SECRET'
|
|
110 assert access_token_key == 'TEST_ACCESS_KEY'
|
|
111 assert access_token_secret == 'TEST_ACCESS_SECRET'
|
|
112
|
|
113 self.tweets = []
|
|
114 self.media = []
|
|
115 self.next_mid = 1
|
|
116
|
|
117 def PostUpdate(self, tweet, media=None):
|
|
118 self.tweets.append((tweet, media))
|
|
119
|
|
120 def UploadMediaChunked(self, filename):
|
|
121 mid = self.next_mid
|
|
122 self.next_mid += 1
|
|
123 self.media.append((filename, mid))
|
|
124 return mid
|
|
125
|
|
126
|
|
127 class TwitterMockUtil:
|
|
128 def installTokens(self, cli, silo_name):
|
|
129 def do_install_tokens(ctx):
|
|
130 ctx.cache.setCustomValue(
|
|
131 '%s_clienttoken' % silo_name,
|
|
132 'TEST_CLIENT_KEY,TEST_CLIENT_SECRET')
|
|
133 ctx.cache.setCustomValue(
|
|
134 '%s_accesstoken' % silo_name,
|
|
135 'TEST_ACCESS_KEY,TEST_ACCESS_SECRET')
|
|
136
|
|
137 cli.preExecHook(do_install_tokens)
|