Mercurial > silorider
annotate tests/test_silos_twitter.py @ 15:cb1dc5c864d8 0.2.0
Specify URLs in the config file instead of in the CLI.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 29 Jul 2018 23:59:39 -0700 |
parents | c199bd681e4e |
children | a921cc2306bc |
rev | line source |
---|---|
2 | 1 import pytest |
2 | |
3 | |
4 def test_one_article(cli, feedutil, tweetmock): | |
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', 'twitter', url='/blah') | |
15 tweetmock.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.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 | 21 |
22 | |
23 def test_one_micropost(cli, feedutil, tweetmock): | |
24 feed = cli.createTempFeed(feedutil.makeFeed( | |
25 """<p class="p-name">This is a quick update.</p> | |
26 <a class="u-url" href="/01234.html">permalink</a>""" | |
27 )) | |
28 | |
29 cli.appendSiloConfig('test', 'twitter', url='/blah') | |
30 tweetmock.installTokens(cli, 'test') | |
31 | |
32 ctx, _ = cli.run('process', feed) | |
33 assert ctx.cache.wasPosted('test', '/01234.html') | |
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 | 36 |
37 | |
38 def test_one_micropost_with_one_photo(cli, feedutil, tweetmock, monkeypatch): | |
39 feed = cli.createTempFeed(feedutil.makeFeed( | |
40 """<p class="p-name">This is a quick photo update.</p> | |
41 <div> | |
42 <a class="u-photo" href="/fullimg.jpg"><img src="/thumbimg.jpg"/></a> | |
43 </div> | |
44 <a class="u-url" href="/01234.html">permalink</a>""" | |
45 )) | |
46 | |
47 cli.appendSiloConfig('test', 'twitter', url='/blah') | |
48 tweetmock.installTokens(cli, 'test') | |
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 | 51 |
52 assert ctx.cache.wasPosted('test', '/01234.html') | |
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 | 55 |
56 | |
57 def test_one_micropost_with_two_photos(cli, feedutil, tweetmock, monkeypatch): | |
58 feed = cli.createTempFeed(feedutil.makeFeed( | |
59 """<p class="p-name">This is a photo update with 2 photos.</p> | |
60 <div> | |
61 <a class="u-photo" href="/fullimg1.jpg"><img src="/thumbimg1.jpg"/></a> | |
62 <a class="u-photo" href="/fullimg2.jpg"><img src="/thumbimg2.jpg"/></a> | |
63 </div> | |
64 <a class="u-url" href="/01234.html">permalink</a>""" | |
65 )) | |
66 | |
67 cli.appendSiloConfig('test', 'twitter', url='/blah') | |
68 tweetmock.installTokens(cli, 'test') | |
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 | 71 |
72 assert ctx.cache.wasPosted('test', '/01234.html') | |
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 | 76 |
77 | |
78 @pytest.fixture(scope='session') | |
79 def tweetmock(): | |
80 from silorider.silos.twitter import TwitterSilo | |
81 TwitterSilo._CLIENT_CLASS = TwitterMock | |
82 return TwitterMockUtil() | |
83 | |
84 | |
85 class TwitterMock: | |
86 def __init__(self, consumer_key, consumer_secret, | |
87 access_token_key, access_token_secret): | |
88 assert consumer_key == 'TEST_CLIENT_KEY' | |
89 assert consumer_secret == 'TEST_CLIENT_SECRET' | |
90 assert access_token_key == 'TEST_ACCESS_KEY' | |
91 assert access_token_secret == 'TEST_ACCESS_SECRET' | |
92 | |
93 self.tweets = [] | |
94 | |
95 def PostUpdate(self, tweet, media=None): | |
96 self.tweets.append((tweet, media)) | |
97 | |
98 | |
99 class TwitterMockUtil: | |
100 def installTokens(self, cli, silo_name): | |
101 def do_install_tokens(ctx): | |
102 ctx.cache.setCustomValue( | |
103 '%s_clienttoken' % silo_name, | |
104 'TEST_CLIENT_KEY,TEST_CLIENT_SECRET') | |
105 ctx.cache.setCustomValue( | |
106 '%s_accesstoken' % silo_name, | |
107 'TEST_ACCESS_KEY,TEST_ACCESS_SECRET') | |
108 | |
109 cli.preExecHook(do_install_tokens) |