forked from snarfed/bridgy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordpress_rest_test.py
107 lines (90 loc) · 4.02 KB
/
wordpress_rest_test.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
# coding=utf-8
"""Unit tests for wordpress_rest.py.
"""
__author__ = ['Ryan Barrett <bridgy@ryanb.org>']
import json
import mox
import urllib
import appengine_config
from appengine_config import HTTP_TIMEOUT
from models import BlogPost
from activitystreams.oauth_dropins.wordpress_rest import WordPressAuth
import wordpress_rest
from wordpress_rest import WordPress
import testutil
class WordPressTest(testutil.HandlerTest):
def setUp(self):
super(WordPressTest, self).setUp()
self.auth_entity = WordPressAuth(id='my.wp.com',
user_json=json.dumps({
'display_name': 'Ryan',
'username': 'ry',
'avatar_URL': 'http://ava/tar'}),
blog_id='123',
blog_url='http://my.wp.com/',
access_token_str='my token')
self.auth_entity.put()
self.wp = WordPress(id='my.wp.com',
auth_entity=self.auth_entity.key,
url='http://my.wp.com/',
domains=['my.wp.com'])
def test_new(self):
self.expect_urlopen(
'https://public-api.wordpress.com/rest/v1/sites/123?pretty=true',
json.dumps({}))
self.mox.ReplayAll()
w = WordPress.new(self.handler, auth_entity=self.auth_entity)
self.assertEquals(self.auth_entity.key, w.auth_entity)
self.assertEquals('my.wp.com', w.key.id())
self.assertEquals('Ryan', w.name)
self.assertEquals(['http://my.wp.com/'], w.domain_urls)
self.assertEquals(['my.wp.com'], w.domains)
self.assertEquals('http://ava/tar', w.picture)
def test_new_with_site_domain(self):
self.expect_urlopen(
'https://public-api.wordpress.com/rest/v1/sites/123?pretty=true',
json.dumps({'ID': 123, 'URL': 'https://vanity.domain/'}))
self.mox.ReplayAll()
w = WordPress.new(self.handler, auth_entity=self.auth_entity)
self.assertEquals('vanity.domain', w.key.id())
self.assertEquals('https://vanity.domain/', w.url)
self.assertEquals(['https://vanity.domain/', 'http://my.wp.com/'],
w.domain_urls)
self.assertEquals(['vanity.domain', 'my.wp.com'], w.domains)
def test_create_comment_with_slug_lookup(self):
self.expect_urlopen(
'https://public-api.wordpress.com/rest/v1/sites/123/posts/'
'slug:the-slug?pretty=true',
json.dumps({'ID': 456}))
self.expect_urlopen(
'https://public-api.wordpress.com/rest/v1/sites/123/posts/'
'456/replies/new?pretty=true',
json.dumps({'ID': 789, 'ok': 'sgtm'}),
data=urllib.urlencode({'content': '<a href="http://who">who</a>: foo bar'}))
self.mox.ReplayAll()
resp = self.wp.create_comment('http://primary/post/123999/the-slug?asdf',
'who', 'http://who', 'foo bar')
self.assertEquals({'id': 789, 'ok': 'sgtm'}, resp)
def test_create_comment_with_unicode_chars(self):
self.expect_urlopen(
'https://public-api.wordpress.com/rest/v1/sites/123/posts/'
'123/replies/new?pretty=true',
json.dumps({}),
data=urllib.urlencode({
'content': '<a href="http://who">Degenève</a>: foo Degenève bar'}))
self.mox.ReplayAll()
resp = self.wp.create_comment('http://primary/post/123', u'Degenève',
'http://who', u'foo Degenève bar')
def test_create_comment_gives_up_on_invalid_input_error(self):
# see https://github.com/snarfed/bridgy/issues/161
self.expect_urlopen(
'https://public-api.wordpress.com/rest/v1/sites/123/posts/'
'123/replies/new?pretty=true',
json.dumps({'error': 'invalid_input'}),
status=400,
data=urllib.urlencode({'content': '<a href="http://who">name</a>: foo'}))
self.mox.ReplayAll()
resp = self.wp.create_comment('http://primary/post/123', 'name',
'http://who', 'foo')
# shouldn't raise an exception
self.assertEquals({'error': 'invalid_input'}, resp)