annotate tests/test_silos_twitter.py @ 7:32c80c295769

Make CLI arguments easier to pass.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 29 Jul 2018 19:49:11 -0700
parents c199bd681e4e
children a921cc2306bc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import pytest
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 def test_one_article(cli, feedutil, tweetmock):
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 feed = cli.createTempFeed(feedutil.makeFeed(
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 """<h1 class="p-name">A new article</h1>
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 <div class="e-content">
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 <p>This is the text of the article.</p>
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 <p>It has 2 paragraphs.</p>
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 </div>
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11 <a class="u-url" href="https://example.org/a-new-article">permalink</a>"""
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 ))
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 cli.appendSiloConfig('test', 'twitter', url='/blah')
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 tweetmock.installTokens(cli, 'test')
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 ctx, _ = cli.run('process', feed)
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 assert ctx.cache.wasPosted('test', 'https://example.org/a-new-article')
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 toot = ctx.silos[0].client.tweets[0]
4
c199bd681e4e Twitter API accepts direct URLs for media.
Ludovic Chabant <ludovic@chabant.com>
parents: 2
diff changeset
20 assert toot == ('A new article https://example.org/a-new-article', [])
2
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23 def test_one_micropost(cli, feedutil, tweetmock):
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 feed = cli.createTempFeed(feedutil.makeFeed(
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 """<p class="p-name">This is a quick update.</p>
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 <a class="u-url" href="/01234.html">permalink</a>"""
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 ))
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 cli.appendSiloConfig('test', 'twitter', url='/blah')
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 tweetmock.installTokens(cli, 'test')
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 ctx, _ = cli.run('process', feed)
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 assert ctx.cache.wasPosted('test', '/01234.html')
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 toot = ctx.silos[0].client.tweets[0]
4
c199bd681e4e Twitter API accepts direct URLs for media.
Ludovic Chabant <ludovic@chabant.com>
parents: 2
diff changeset
35 assert toot == ("This is a quick update.", [])
2
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 def test_one_micropost_with_one_photo(cli, feedutil, tweetmock, monkeypatch):
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 feed = cli.createTempFeed(feedutil.makeFeed(
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 """<p class="p-name">This is a quick photo update.</p>
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 <div>
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 <a class="u-photo" href="/fullimg.jpg"><img src="/thumbimg.jpg"/></a>
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43 </div>
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44 <a class="u-url" href="/01234.html">permalink</a>"""
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45 ))
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47 cli.appendSiloConfig('test', 'twitter', url='/blah')
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 tweetmock.installTokens(cli, 'test')
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49
4
c199bd681e4e Twitter API accepts direct URLs for media.
Ludovic Chabant <ludovic@chabant.com>
parents: 2
diff changeset
50 ctx, _ = cli.run('process', feed)
2
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 assert ctx.cache.wasPosted('test', '/01234.html')
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53 toot = ctx.silos[0].client.tweets[0]
4
c199bd681e4e Twitter API accepts direct URLs for media.
Ludovic Chabant <ludovic@chabant.com>
parents: 2
diff changeset
54 assert toot == ("This is a quick photo update.", ['/fullimg.jpg'])
2
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57 def test_one_micropost_with_two_photos(cli, feedutil, tweetmock, monkeypatch):
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 feed = cli.createTempFeed(feedutil.makeFeed(
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 """<p class="p-name">This is a photo update with 2 photos.</p>
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60 <div>
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 <a class="u-photo" href="/fullimg1.jpg"><img src="/thumbimg1.jpg"/></a>
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62 <a class="u-photo" href="/fullimg2.jpg"><img src="/thumbimg2.jpg"/></a>
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63 </div>
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
64 <a class="u-url" href="/01234.html">permalink</a>"""
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
65 ))
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 cli.appendSiloConfig('test', 'twitter', url='/blah')
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68 tweetmock.installTokens(cli, 'test')
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
69
4
c199bd681e4e Twitter API accepts direct URLs for media.
Ludovic Chabant <ludovic@chabant.com>
parents: 2
diff changeset
70 ctx, _ = cli.run('process', feed)
2
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72 assert ctx.cache.wasPosted('test', '/01234.html')
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 toot = ctx.silos[0].client.tweets[0]
4
c199bd681e4e Twitter API accepts direct URLs for media.
Ludovic Chabant <ludovic@chabant.com>
parents: 2
diff changeset
74 assert toot == ("This is a photo update with 2 photos.",
c199bd681e4e Twitter API accepts direct URLs for media.
Ludovic Chabant <ludovic@chabant.com>
parents: 2
diff changeset
75 ['/fullimg1.jpg', '/fullimg2.jpg'])
2
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
77
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78 @pytest.fixture(scope='session')
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79 def tweetmock():
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80 from silorider.silos.twitter import TwitterSilo
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
81 TwitterSilo._CLIENT_CLASS = TwitterMock
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
82 return TwitterMockUtil()
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
83
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 class TwitterMock:
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86 def __init__(self, consumer_key, consumer_secret,
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
87 access_token_key, access_token_secret):
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88 assert consumer_key == 'TEST_CLIENT_KEY'
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89 assert consumer_secret == 'TEST_CLIENT_SECRET'
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
90 assert access_token_key == 'TEST_ACCESS_KEY'
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91 assert access_token_secret == 'TEST_ACCESS_SECRET'
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
93 self.tweets = []
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
94
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95 def PostUpdate(self, tweet, media=None):
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96 self.tweets.append((tweet, media))
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
98
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
99 class TwitterMockUtil:
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
100 def installTokens(self, cli, silo_name):
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
101 def do_install_tokens(ctx):
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
102 ctx.cache.setCustomValue(
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103 '%s_clienttoken' % silo_name,
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
104 'TEST_CLIENT_KEY,TEST_CLIENT_SECRET')
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
105 ctx.cache.setCustomValue(
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
106 '%s_accesstoken' % silo_name,
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
107 'TEST_ACCESS_KEY,TEST_ACCESS_SECRET')
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
108
27543b2e73b9 Add Twitter silo.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
109 cli.preExecHook(do_install_tokens)