Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if include_link == gr_source.INCLUDE_LINK:
content += ' - %s' % obj['url']
ret = {
'id': 'fake id',
'url': 'http://fake/url',
'content': content,
'granary_message': 'granary message',
}
if verb == 'rsvp-yes':
ret['type'] = 'post'
images = self._images(obj)
if images:
ret['images'] = images
return gr_source.creation_result(ret)
if obj.get('verb') == 'like':
return gr_source.creation_result(
abort=True, error_plain='Cannot publish likes',
error_html='Cannot publish likes')
content = self._content_for_create(obj, ignore_formatting=ignore_formatting)
if include_link == gr_source.INCLUDE_LINK:
content += ' - %s' % obj['url']
content = 'preview of ' + content
images = self._images(obj)
if images:
content += ' with images %s' % ','.join(images)
return gr_source.creation_result(description=content)
""")
self.expect_requests_get('http://orig.domain/baz', """
<article>
A fantastically well-written article
</article>
""")
self.source.gr_source.create({
'verb': 'like',
'displayName': 'liked this',
'url': 'http://foo.com/bar',
'object': [{'url': 'http://orig.domain/baz'}],
'objectType': 'activity',
}, include_link=gr_source.INCLUDE_LINK, ignore_formatting=False). \
AndReturn(gr_source.creation_result({
'url': 'http://fake/url',
'id': 'http://fake/url',
'content': 'liked this',
}))
self.mox.ReplayAll()
self.assert_created('')
import copy
import socket
from oauth_dropins.webutil import testutil
from oauth_dropins.webutil.util import json_dumps, json_loads
from granary import instagram
from granary import source
from granary.tests import test_facebook
from granary.tests import test_instagram
from granary.tests import test_twitter
import api, app
class FakeSource(source.Source):
NAME = 'Fake'
DOMAIN = 'fa.ke'
BASE_URL = 'http://fa.ke/'
def __init__(self, **kwargs):
pass
class HandlerTest(testutil.HandlerTest):
activities = [{'foo': '☕ bar'}]
def setUp(self):
super(HandlerTest, self).setUp()
self.mox.StubOutWithMock(FakeSource, 'get_activities_response')
api.Handler.get.cache_clear()
# mirror some methods from webutil.testutil
from oauth_dropins import handlers as oauth_handlers
from oauth_dropins.webutil.testutil import get_task_eta, get_task_params
from oauth_dropins.webutil.util import json_dumps, json_loads
import requests
import util
NOW = datetime.datetime.utcnow()
class FakeAuthEntity(BaseAuth):
user_json = ndb.TextProperty()
class FakeGrSource(gr_source.Source):
"""Fake granary source class.
Attributes:
activities, like, reaction, share, event, rsvp, etag, search_results,
last_search_query, blocked_ids
"""
NAME = 'FakeSource'
DOMAIN = 'fa.ke'
last_search_query = None
search_results = []
def user_url(self, id):
return 'http://fa.ke/' + id
def user_to_actor(self, user):
def preview_create(self, obj, include_link=gr_source.OMIT_LINK,
ignore_formatting=False):
if obj.get('verb') == 'like':
return gr_source.creation_result(
abort=True, error_plain='Cannot publish likes',
error_html='Cannot publish likes')
content = self._content_for_create(obj, ignore_formatting=ignore_formatting)
if include_link == gr_source.INCLUDE_LINK:
content += ' - %s' % obj['url']
content = 'preview of ' + content
images = self._images(obj)
if images:
content += ' with images %s' % ','.join(images)
def create(self, obj, include_link=gr_source.OMIT_LINK,
ignore_formatting=False):
verb = obj.get('verb')
type = obj.get('objectType')
if verb == 'like':
return gr_source.creation_result(
abort=True, error_plain='Cannot publish likes',
error_html='Cannot publish likes')
if 'content' not in obj:
return gr_source.creation_result(
abort=False, error_plain='No content',
error_html='No content')
if type == 'comment':
base_url = self.base_object(obj).get('url')
if not base_url:
return gr_source.creation_result(
def test_blocks_rate_limited(self):
self.mox.StubOutWithMock(FakeSource, 'get_blocklist')
FakeSource.get_blocklist().AndRaise(source.RateLimited('foo', partial=[]))
self.mox.ReplayAll()
resp = app.application.get_response('/fake/123/@blocks/')
self.assertEqual(429, resp.status_int)
def test_load_blocklist_rate_limited(self):
source = FakeSource(id='x')
self.mox.StubOutWithMock(source.gr_source, 'get_blocklist_ids')
source.gr_source.get_blocklist_ids().AndRaise(
gr_source.RateLimited(partial=[4, 5]))
self.mox.ReplayAll()
source.load_blocklist()
self.assertEqual([4, 5], source.blocked_ids)
def setUp(self):
super(PublishTest, self).setUp()
publish.SOURCE_NAMES['fake'] = testutil.FakeSource
publish.SOURCE_DOMAINS['fa.ke'] = testutil.FakeSource
self.auth_entity = testutil.FakeAuthEntity(id='0123456789')
self.source = testutil.FakeSource(
id='foo.com', features=['publish'], domains=['foo.com'],
domain_urls=['http://foo.com/'], auth_entity=self.auth_entity.key)
self.source.put()
self.oauth_state = {
'source_url': 'http://foo.com/bar',
'target_url': 'https://brid.gy/publish/fake',
'source_key': self.source.key.urlsafe().decode(),
'include_link': gr_source.INCLUDE_LINK,
}
self.post_html = '<article class="h-entry"><p class="e-content">%s</p></article>'
self.backlink = '\n<a href="http://localhost/publish/fake"></a>'