/
test_blogger.py
141 lines (116 loc) 路 5.73 KB
/
test_blogger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# coding=utf-8
"""Unit tests for blogger.py.
"""
import mox
import urllib
import urlparse
import appengine_config
from gdata.blogger import data
from gdata.blogger.client import BloggerClient
from gdata.client import RequestError
from oauth_dropins.blogger_v2 import BloggerV2Auth
import blogger
from blogger import Blogger
import util
import testutil
class BloggerTest(testutil.HandlerTest):
def setUp(self):
super(BloggerTest, self).setUp()
self.auth_entity = BloggerV2Auth(name='name',
blog_ids=['111'],
blog_hostnames=['my.blawg'],
picture_url='http://pic')
self.client = self.mox.CreateMock(BloggerClient)
self.comment = data.Comment()
self.comment.id = util.Struct(
text='tag:blogger.com,1999:blog-111.post-222.comment-333')
self.comment.to_string = lambda: '<foo></foo>'
def expect_get_posts(self):
post = data.BlogPost()
post.id = util.Struct(text='tag:blogger.com,1999:blog-111.post-222')
feed = data.BlogFeed()
feed.entry = [post]
def check_path(query):
return query.custom_parameters['path'] == '/path/to/post'
self.client.get_posts('111', query=mox.Func(check_path)).AndReturn(feed)
def test_new(self):
b = Blogger.new(self.handler, auth_entity=self.auth_entity)
self.assertEquals(self.auth_entity.key, b.auth_entity)
self.assertEquals('name', b.name)
self.assertEquals(['http://my.blawg/'], b.domain_urls)
self.assertEquals(['my.blawg'], b.domains)
self.assertEquals('http://pic', b.picture)
def test_new_oauth_dropins_error(self):
"""Blogger is special cased in oauth-dropins: when login succeeds but then
an authenticated API call fails, it returns an empty auth entity key, which
we can differentiate from a user decline because oauth-dropins can't
currently intercept Blogger declines.
"""
resp = blogger.application.get_response('/blogger/oauth_handler')
self.assertEquals(302, resp.status_int)
location = urlparse.urlparse(resp.headers['Location'])
self.assertEquals('/', location.path)
self.assertIn("Couldn't fetch your blogs", urllib.unquote(location.fragment))
self.assertEquals(0, BloggerV2Auth.query().count())
self.assertEquals(0, Blogger.query().count())
def test_oauth_handler_no_blogs(self):
self.auth_entity = BloggerV2Auth(id='123', name='name', picture_url='pic',
blogs_atom='x', user_atom='y', creds_json='z')
self.auth_entity.put()
resp = blogger.application.get_response(
'/blogger/oauth_handler?auth_entity=%s' % self.auth_entity.key.urlsafe())
self.assertEquals(302, resp.status_int)
location = urlparse.urlparse(resp.headers['Location'])
self.assertEquals('/', location.path)
self.assertIn("Couldn't fetch your blogs", urllib.unquote(location.fragment))
def test_new_no_blogs(self):
self.auth_entity.blog_hostnames = []
self.assertIsNone(Blogger.new(self.handler, auth_entity=self.auth_entity))
self.assertIn('Blogger blog not found', next(iter(self.handler.messages)))
def test_create_comment(self):
self.expect_get_posts()
self.client.add_comment('111', '222', '<a href="http://who">who</a>: foo bar'
).AndReturn(self.comment)
self.mox.ReplayAll()
b = Blogger.new(self.handler, auth_entity=self.auth_entity)
resp = b.create_comment('http://blawg/path/to/post', 'who', 'http://who',
'foo bar', client=self.client)
self.assert_equals({'id': '333', 'response': '<foo></foo>'}, resp)
def test_create_comment_with_unicode_chars(self):
# TODO: this just checks the arguments passed to client.add_comment(). we
# should test that the blogger client itself encodes as UTF-8.
self.expect_get_posts()
prefix = u'<a href="http://who">Degen猫ve</a>: '
content = prefix + 'x' * (blogger.MAX_COMMENT_LENGTH - len(prefix) - 3) + '...'
self.client.add_comment('111', '222', content).AndReturn(self.comment)
self.mox.ReplayAll()
b = Blogger.new(self.handler, auth_entity=self.auth_entity)
resp = b.create_comment('http://blawg/path/to/post', u'Degen猫ve', 'http://who',
'x' * blogger.MAX_COMMENT_LENGTH, client=self.client)
self.assert_equals({'id': '333', 'response': '<foo></foo>'}, resp)
def test_create_too_long_comment(self):
"""Blogger caps HTML comment length at 4096 chars."""
self.expect_get_posts()
self.client.add_comment(
'111', '222', u'<a href="http://who">Degen猫ve</a>: foo Degen猫ve bar'
).AndReturn(self.comment)
self.mox.ReplayAll()
b = Blogger.new(self.handler, auth_entity=self.auth_entity)
resp = b.create_comment('http://blawg/path/to/post', u'Degen猫ve', 'http://who',
u'foo Degen猫ve bar', client=self.client)
self.assert_equals({'id': '333', 'response': '<foo></foo>'}, resp)
def test_create_comment_gives_up_on_internal_error_bX2i87au(self):
# see https://github.com/snarfed/bridgy/issues/175
self.expect_get_posts()
self.client.add_comment('111', '222', '<a href="http://who">who</a>: foo bar'
).AndRaise(RequestError('500, Internal error: bX-2i87au'))
self.mox.ReplayAll()
b = Blogger.new(self.handler, auth_entity=self.auth_entity)
resp = b.create_comment('http://blawg/path/to/post', 'who', 'http://who',
'foo bar', client=self.client)
# the key point is that create_comment doesn't raise an exception
self.assert_equals({'error': '500, Internal error: bX-2i87au'}, resp)
def test_feed_url(self):
self.assertEquals(
'http://my.blawg/feeds/posts/default',
Blogger.new(self.handler, auth_entity=self.auth_entity).feed_url())