Mercurial > silorider
annotate tests/test_silos_twitter.py @ 18:a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
- This lets us properly handle various forms of linking.
- Add tests for processing posts with links.
- Fix configuration in tests.
- Basic error handling for processing posts.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 16 Sep 2018 21:16:20 -0700 |
parents | c199bd681e4e |
children | fb93d3fbff4e |
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') | |
18
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
15 cli.setFeedConfig('feed', feed) |
2 | 16 tweetmock.installTokens(cli, 'test') |
17 | |
18
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
18 ctx, _ = cli.run('process') |
2 | 19 assert ctx.cache.wasPosted('test', 'https://example.org/a-new-article') |
20 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
|
21 assert toot == ('A new article https://example.org/a-new-article', []) |
2 | 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') | |
18
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
31 cli.setFeedConfig('feed', feed) |
2 | 32 tweetmock.installTokens(cli, 'test') |
33 | |
18
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
34 ctx, _ = cli.run('process') |
2 | 35 assert ctx.cache.wasPosted('test', '/01234.html') |
36 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
|
37 assert toot == ("This is a quick update.", []) |
2 | 38 |
39 | |
40 def test_one_micropost_with_one_photo(cli, feedutil, tweetmock, monkeypatch): | |
41 feed = cli.createTempFeed(feedutil.makeFeed( | |
42 """<p class="p-name">This is a quick photo update.</p> | |
43 <div> | |
44 <a class="u-photo" href="/fullimg.jpg"><img src="/thumbimg.jpg"/></a> | |
45 </div> | |
46 <a class="u-url" href="/01234.html">permalink</a>""" | |
47 )) | |
48 | |
49 cli.appendSiloConfig('test', 'twitter', url='/blah') | |
18
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
50 cli.setFeedConfig('feed', feed) |
2 | 51 tweetmock.installTokens(cli, 'test') |
52 | |
18
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
53 ctx, _ = cli.run('process') |
2 | 54 |
55 assert ctx.cache.wasPosted('test', '/01234.html') | |
56 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
|
57 assert toot == ("This is a quick photo update.", ['/fullimg.jpg']) |
2 | 58 |
59 | |
60 def test_one_micropost_with_two_photos(cli, feedutil, tweetmock, monkeypatch): | |
61 feed = cli.createTempFeed(feedutil.makeFeed( | |
62 """<p class="p-name">This is a photo update with 2 photos.</p> | |
63 <div> | |
64 <a class="u-photo" href="/fullimg1.jpg"><img src="/thumbimg1.jpg"/></a> | |
65 <a class="u-photo" href="/fullimg2.jpg"><img src="/thumbimg2.jpg"/></a> | |
66 </div> | |
67 <a class="u-url" href="/01234.html">permalink</a>""" | |
68 )) | |
69 | |
70 cli.appendSiloConfig('test', 'twitter', url='/blah') | |
18
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
71 cli.setFeedConfig('feed', feed) |
2 | 72 tweetmock.installTokens(cli, 'test') |
73 | |
18
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
74 ctx, _ = cli.run('process') |
2 | 75 |
76 assert ctx.cache.wasPosted('test', '/01234.html') | |
77 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
|
78 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
|
79 ['/fullimg1.jpg', '/fullimg2.jpg']) |
2 | 80 |
81 | |
82 @pytest.fixture(scope='session') | |
83 def tweetmock(): | |
84 from silorider.silos.twitter import TwitterSilo | |
85 TwitterSilo._CLIENT_CLASS = TwitterMock | |
86 return TwitterMockUtil() | |
87 | |
88 | |
89 class TwitterMock: | |
90 def __init__(self, consumer_key, consumer_secret, | |
91 access_token_key, access_token_secret): | |
92 assert consumer_key == 'TEST_CLIENT_KEY' | |
93 assert consumer_secret == 'TEST_CLIENT_SECRET' | |
94 assert access_token_key == 'TEST_ACCESS_KEY' | |
95 assert access_token_secret == 'TEST_ACCESS_SECRET' | |
96 | |
97 self.tweets = [] | |
98 | |
99 def PostUpdate(self, tweet, media=None): | |
100 self.tweets.append((tweet, media)) | |
101 | |
102 | |
103 class TwitterMockUtil: | |
104 def installTokens(self, cli, silo_name): | |
105 def do_install_tokens(ctx): | |
106 ctx.cache.setCustomValue( | |
107 '%s_clienttoken' % silo_name, | |
108 'TEST_CLIENT_KEY,TEST_CLIENT_SECRET') | |
109 ctx.cache.setCustomValue( | |
110 '%s_accesstoken' % silo_name, | |
111 'TEST_ACCESS_KEY,TEST_ACCESS_SECRET') | |
112 | |
113 cli.preExecHook(do_install_tokens) |