Mercurial > silorider
annotate tests/test_silos_twitter.py @ 27:c898b4df0f29
Use context for html stripping, with support for custom URL sizes
For instance in Twitter URLs are 23 characters long since they use their
own URL shortening service. Without taking this into account, post lengths
would not be calculated correctly.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Wed, 19 Apr 2023 12:46:58 -0700 |
parents | fb93d3fbff4e |
children | 9e4eb3f2754e |
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 | |
25
fb93d3fbff4e
Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents:
18
diff
changeset
|
40 def test_one_micropost_with_mention(cli, feedutil, tweetmock): |
fb93d3fbff4e
Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents:
18
diff
changeset
|
41 feed = cli.createTempFeed(feedutil.makeFeed( |
fb93d3fbff4e
Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents:
18
diff
changeset
|
42 """<p class="p-name">Hey <a href="https://twitter.com/jack">Jacky</a> |
fb93d3fbff4e
Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents:
18
diff
changeset
|
43 you should fix your stuff!</p> |
fb93d3fbff4e
Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents:
18
diff
changeset
|
44 <a class="u-url" href="/01234.html">permalink</a>""" |
fb93d3fbff4e
Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents:
18
diff
changeset
|
45 )) |
fb93d3fbff4e
Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents:
18
diff
changeset
|
46 |
fb93d3fbff4e
Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents:
18
diff
changeset
|
47 cli.appendSiloConfig('test', 'twitter', url='/blah') |
fb93d3fbff4e
Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents:
18
diff
changeset
|
48 cli.setFeedConfig('feed', feed) |
fb93d3fbff4e
Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents:
18
diff
changeset
|
49 tweetmock.installTokens(cli, 'test') |
fb93d3fbff4e
Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents:
18
diff
changeset
|
50 |
fb93d3fbff4e
Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents:
18
diff
changeset
|
51 ctx, _ = cli.run('process') |
fb93d3fbff4e
Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents:
18
diff
changeset
|
52 assert ctx.cache.wasPosted('test', '/01234.html') |
fb93d3fbff4e
Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents:
18
diff
changeset
|
53 toot = ctx.silos[0].client.tweets[0] |
fb93d3fbff4e
Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents:
18
diff
changeset
|
54 assert toot == ("Hey @jack you should fix your stuff!", []) |
fb93d3fbff4e
Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents:
18
diff
changeset
|
55 |
fb93d3fbff4e
Support transforming twitter profile URLs into mentions.
Ludovic Chabant <ludovic@chabant.com>
parents:
18
diff
changeset
|
56 |
2 | 57 def test_one_micropost_with_one_photo(cli, feedutil, tweetmock, monkeypatch): |
58 feed = cli.createTempFeed(feedutil.makeFeed( | |
59 """<p class="p-name">This is a quick photo update.</p> | |
60 <div> | |
61 <a class="u-photo" href="/fullimg.jpg"><img src="/thumbimg.jpg"/></a> | |
62 </div> | |
63 <a class="u-url" href="/01234.html">permalink</a>""" | |
64 )) | |
65 | |
66 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
|
67 cli.setFeedConfig('feed', feed) |
2 | 68 tweetmock.installTokens(cli, 'test') |
69 | |
18
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
70 ctx, _ = cli.run('process') |
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 quick photo update.", ['/fullimg.jpg']) |
2 | 75 |
76 | |
77 def test_one_micropost_with_two_photos(cli, feedutil, tweetmock, monkeypatch): | |
78 feed = cli.createTempFeed(feedutil.makeFeed( | |
79 """<p class="p-name">This is a photo update with 2 photos.</p> | |
80 <div> | |
81 <a class="u-photo" href="/fullimg1.jpg"><img src="/thumbimg1.jpg"/></a> | |
82 <a class="u-photo" href="/fullimg2.jpg"><img src="/thumbimg2.jpg"/></a> | |
83 </div> | |
84 <a class="u-url" href="/01234.html">permalink</a>""" | |
85 )) | |
86 | |
87 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
|
88 cli.setFeedConfig('feed', feed) |
2 | 89 tweetmock.installTokens(cli, 'test') |
90 | |
18
a921cc2306bc
Do our own HTML parsing/stripping of micropost contents.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
91 ctx, _ = cli.run('process') |
2 | 92 |
93 assert ctx.cache.wasPosted('test', '/01234.html') | |
94 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
|
95 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
|
96 ['/fullimg1.jpg', '/fullimg2.jpg']) |
2 | 97 |
98 | |
99 @pytest.fixture(scope='session') | |
100 def tweetmock(): | |
101 from silorider.silos.twitter import TwitterSilo | |
102 TwitterSilo._CLIENT_CLASS = TwitterMock | |
103 return TwitterMockUtil() | |
104 | |
105 | |
106 class TwitterMock: | |
107 def __init__(self, consumer_key, consumer_secret, | |
108 access_token_key, access_token_secret): | |
109 assert consumer_key == 'TEST_CLIENT_KEY' | |
110 assert consumer_secret == 'TEST_CLIENT_SECRET' | |
111 assert access_token_key == 'TEST_ACCESS_KEY' | |
112 assert access_token_secret == 'TEST_ACCESS_SECRET' | |
113 | |
114 self.tweets = [] | |
115 | |
116 def PostUpdate(self, tweet, media=None): | |
117 self.tweets.append((tweet, media)) | |
118 | |
119 | |
120 class TwitterMockUtil: | |
121 def installTokens(self, cli, silo_name): | |
122 def do_install_tokens(ctx): | |
123 ctx.cache.setCustomValue( | |
124 '%s_clienttoken' % silo_name, | |
125 'TEST_CLIENT_KEY,TEST_CLIENT_SECRET') | |
126 ctx.cache.setCustomValue( | |
127 '%s_accesstoken' % silo_name, | |
128 'TEST_ACCESS_KEY,TEST_ACCESS_SECRET') | |
129 | |
130 cli.preExecHook(do_install_tokens) |