Пример #1
0
    def test_subscribe(self):
        expected = {
            'hub.mode': 'subscribe',
            'hub.topic': 'fake feed url',
            'hub.callback': 'http://localhost/fake/notify/foo.com',
            'format': 'json',
            'retrieve': 'true',
        }
        item_a = {'permalinkUrl': 'A', 'content': 'a http://a.com a'}
        item_b = {'permalinkUrl': 'B', 'summary': 'b http://b.com b'}
        feed = json.dumps({'items': [item_a, {}, item_b]})
        self.expect_requests_post(superfeedr.PUSH_API_URL,
                                  feed,
                                  data=expected,
                                  auth=mox.IgnoreArg())
        self.mox.ReplayAll()

        superfeedr.subscribe(self.source, self.handler)
        self.assert_blogposts([
            BlogPost(id='A',
                     source=self.source.key,
                     feed_item=item_a,
                     unsent=['http://a.com/']),
            BlogPost(id='B',
                     source=self.source.key,
                     feed_item=item_b,
                     unsent=['http://b.com/']),
        ])
Пример #2
0
  def test_subscribe(self):
    expected = {
      'hub.mode': 'subscribe',
      'hub.topic': 'fake feed url',
      'hub.callback': 'http://localhost/fake/notify/foo.com',
      'format': 'json',
      'retrieve': 'true',
      }
    item_a = {'permalinkUrl': 'A', 'content': 'a http://a.com a'}
    item_b = {'permalinkUrl': 'B', 'summary': 'b http://b.com b'}
    feed = json.dumps({'items': [item_a, {}, item_b]})
    self.expect_requests_post(superfeedr.PUSH_API_URL, feed,
                              data=expected, auth=mox.IgnoreArg())
    self.mox.ReplayAll()

    superfeedr.subscribe(self.source, self.handler)

    posts = list(BlogPost.query())
    self.assert_entities_equal(
      [BlogPost(id='A', source=self.source.key, feed_item=item_a,
                unsent=['http://a.com']),
       BlogPost(id='B', source=self.source.key, feed_item=item_b,
                unsent=['http://b.com']),
       ], posts,
      ignore=('created', 'updated'))

    tasks = self.taskqueue_stub.GetTasks('propagate-blogpost')
    self.assert_equals([{'key': posts[0].key.urlsafe()},
                        {'key': posts[1].key.urlsafe()}],
                       [testutil.get_task_params(t) for t in tasks])
Пример #3
0
  def test_subscribe(self):
    expected = {
      'hub.mode': 'subscribe',
      'hub.topic': 'fake feed url',
      'hub.callback': 'http://localhost/fake/notify/foo.com',
      'format': 'json',
      'retrieve': 'true',
      }
    item_a = {'permalinkUrl': 'A', 'content': 'a http://a.com a'}
    item_b = {'permalinkUrl': 'B', 'summary': 'b http://b.com b'}
    feed = {'items': [item_a, {}, item_b]}
    self.expect_requests_post(superfeedr.PUSH_API_URL, feed,
                              data=expected, auth=mox.IgnoreArg())

    post_a = BlogPost(id='A', source=self.source.key, feed_item=item_a,
                      unsent=['http://a.com/'])
    post_b = BlogPost(id='B', source=self.source.key, feed_item=item_b,
                      unsent=['http://b.com/'])
    self.expect_task('propagate-blogpost', key=post_a)
    self.expect_task('propagate-blogpost', key=post_b)
    self.mox.ReplayAll()

    with self.app.test_request_context():
      superfeedr.subscribe(self.source)
      self.assert_blogposts([post_a, post_b])
Пример #4
0
    def test_subscribe(self):
        expected = {
            "hub.mode": "subscribe",
            "hub.topic": "fake feed url",
            "hub.callback": "http://localhost/fake/notify/foo.com",
            "format": "json",
            "retrieve": "true",
        }
        item_a = {"permalinkUrl": "A", "content": "a http://a.com a"}
        item_b = {"permalinkUrl": "B", "summary": "b http://b.com b"}
        feed = json.dumps({"items": [item_a, {}, item_b]})
        self.expect_requests_post(superfeedr.PUSH_API_URL, feed, data=expected, auth=mox.IgnoreArg())
        self.mox.ReplayAll()

        superfeedr.subscribe(self.source, self.handler)

        posts = list(BlogPost.query())
        self.assert_entities_equal(
            [
                BlogPost(id="A", source=self.source.key, feed_item=item_a, unsent=["http://a.com"]),
                BlogPost(id="B", source=self.source.key, feed_item=item_b, unsent=["http://b.com"]),
            ],
            posts,
            ignore=("created", "updated"),
        )

        tasks = self.taskqueue_stub.GetTasks("propagate-blogpost")
        self.assert_equals(
            [{"key": posts[0].key.urlsafe()}, {"key": posts[1].key.urlsafe()}],
            [testutil.get_task_params(t) for t in tasks],
        )
Пример #5
0
  def create_new(cls, user_url=None, **kwargs):
    """Creates and saves a new :class:`Source` and adds a poll task for it.

    Args:
      user_url: a string, optional. if provided, supersedes other urls when
        determining the author_url
      **kwargs: passed to :meth:`new()`

    Returns: newly created :class:`Source`
    """
    source = cls.new(**kwargs)
    if source is None:
      return None

    if not source.domain_urls:  # defer to the source if it already set this
      auth_entity = kwargs.get('auth_entity')
      if auth_entity and hasattr(auth_entity, 'user_json'):
        source.domain_urls, source.domains = source.urls_and_domains(
          auth_entity, user_url)
    logger.debug(f'URLs/domains: {source.domain_urls} {source.domains}')

    # check if this source already exists
    existing = source.key.get()
    if existing:
      # merge some fields
      source.features = set(source.features + existing.features)
      source.populate(**existing.to_dict(include=(
            'created', 'last_hfeed_refetch', 'last_poll_attempt', 'last_polled',
            'last_syndication_url', 'last_webmention_sent', 'superfeedr_secret',
            'webmention_endpoint')))
      verb = 'Updated'
    else:
      verb = 'Added'

    author_urls = source.get_author_urls()
    link = ('http://indiewebify.me/send-webmentions/?url=' + author_urls[0]
            if author_urls else 'http://indiewebify.me/#send-webmentions')
    feature = source.features[0] if source.features else 'listen'
    blurb = '%s %s. %s' % (
      verb, source.label(),
      'Try previewing a post from your web site!' if feature == 'publish'
      else '<a href="%s">Try a webmention!</a>' % link if feature == 'webmention'
      else "Refresh in a minute to see what we've found!")
    logger.info(f'{blurb} {source.bridgy_url()}')

    source.verify()
    if source.verified():
      flash(blurb)

    source.put()

    if 'webmention' in source.features:
      superfeedr.subscribe(source)

    if 'listen' in source.features and source.AUTO_POLL:
      util.add_poll_task(source, now=True)
      util.add_poll_task(source)

    return source
Пример #6
0
  def test_create_new_webmention(self):
    """We should subscribe to webmention sources in Superfeedr."""
    self.expect_requests_get('http://primary/', 'no webmention endpoint',
                             verify=False)
    self.mox.StubOutWithMock(superfeedr, 'subscribe')
    superfeedr.subscribe(mox.IsA(FakeSource), self.handler)

    self.mox.ReplayAll()
    FakeSource.create_new(self.handler, features=['webmention'],
                          domains=['primary/'], domain_urls=['http://primary/'])
Пример #7
0
    def test_create_new_webmention(self):
        """We should subscribe to webmention sources in Superfeedr."""
        self.expect_webmention_requests_get('http://primary/',
                                            'no webmention endpoint',
                                            verify=False)
        self.mox.StubOutWithMock(superfeedr, 'subscribe')
        superfeedr.subscribe(mox.IsA(FakeSource), self.handler)

        self.mox.ReplayAll()
        FakeSource.create_new(self.handler,
                              features=['webmention'],
                              domains=['primary/'],
                              domain_urls=['http://primary/'])
Пример #8
0
  def test_create_new_webmention(self):
    """We should subscribe to webmention sources in Superfeedr."""
    self.expect_webmention_requests_get('http://primary/', 'no webmention endpoint')
    self.mox.StubOutWithMock(superfeedr, 'subscribe')

    def check_source(source):
      assert isinstance(source, FakeSource)
      assert source.is_saved
      return True
    superfeedr.subscribe(mox.Func(check_source), self.handler)

    self.mox.ReplayAll()
    FakeSource.create_new(self.handler, features=['webmention'],
                          domains=['primary/'], domain_urls=['http://primary/'])
Пример #9
0
  def test_create_new_webmention(self):
    """We should subscribe to webmention sources in Superfeedr."""
    self.expect_webmention_requests_get('http://primary/', 'no webmention endpoint',
                                        verify=False)
    self.mox.StubOutWithMock(superfeedr, 'subscribe')

    def check_source(source):
      assert isinstance(source, FakeSource)
      assert source.is_saved
      return True
    superfeedr.subscribe(mox.Func(check_source), self.handler)

    self.mox.ReplayAll()
    FakeSource.create_new(self.handler, features=['webmention'],
                          domains=['primary/'], domain_urls=['http://primary/'])
Пример #10
0
  def test_subscribe(self):
    expected = {
      'hub.mode': 'subscribe',
      'hub.topic': 'fake feed url',
      'hub.callback': 'http://localhost/fake/notify/foo.com',
      'format': 'json',
      'retrieve': 'true',
      }
    item_a = {'permalinkUrl': 'A', 'content': 'a http://a.com a'}
    item_b = {'permalinkUrl': 'B', 'summary': 'b http://b.com b'}
    feed = json.dumps({'items': [item_a, {}, item_b]})
    self.expect_requests_post(superfeedr.PUSH_API_URL, feed,
                              data=expected, auth=mox.IgnoreArg())
    self.mox.ReplayAll()

    superfeedr.subscribe(self.source, self.handler)
    self.assert_blogposts(
      [BlogPost(id='A', source=self.source.key, feed_item=item_a,
                unsent=['http://a.com/']),
       BlogPost(id='B', source=self.source.key, feed_item=item_b,
                unsent=['http://b.com/']),
       ])
Пример #11
0
  def create_new(cls, handler, **kwargs):
    """Creates and saves a new Source and adds a poll task for it.

    Args:
      handler: the current RequestHandler
      **kwargs: passed to new()
    """
    source = cls.new(handler, **kwargs)
    if source is None:
      return None

    feature = source.features[0] if source.features else 'listen'

    if not source.domain_urls:
      # extract domain from the URL set on the user's profile, if any
      auth_entity = kwargs.get('auth_entity')
      if auth_entity and hasattr(auth_entity, 'user_json'):
        url, domain, ok = source._url_and_domain(auth_entity)
        if feature == 'publish' and not ok:
          if not url:
            handler.messages = {'Your %s profile is missing the website field. '
                                'Please add it and try again!' % cls.AS_CLASS.NAME}
          elif not domain:
            handler.messages = {'Could not parse the web site in your %s profile: '
                                '%s\n Please update it and try again!' %
                                (cls.AS_CLASS.NAME, url)}
          else:
            handler.messages = {"Could not connect to the web site in your %s profile: "
                                "%s\n Please update it and try again!" %
                                (cls.AS_CLASS.NAME, url)}
          return None

        if ok:
          if not source.domain_urls:
            source.domain_urls = [url]
          if not source.domains:
            source.domains = [domain]

    # check if this source already exists
    existing = source.key.get()
    if existing:
      # merge some fields
      source.features = set(source.features + existing.features)
      source.populate(**existing.to_dict(include=(
            'created', 'last_hfeed_fetch', 'last_poll_attempt', 'last_polled',
            'last_syndication_url', 'last_webmention_sent', 'superfeedr_secret')))
      verb = 'Updated'
    else:
      verb = 'Added'

    link = ('http://indiewebify.me/send-webmentions/?url=' + source.domain_urls[0]
            if source.domain_urls else 'http://indiewebify.me/#send-webmentions')
    blurb = '%s %s. %s' % (verb, source.label(), {
      'listen': "Refresh to see what we've found!",
      'publish': 'Try previewing a post from your web site!',
      'webmention': '<a href="%s">Try a webmention!</a>' % link,
      }.get(feature, ''))
    logging.info('%s %s', blurb, source.bridgy_url(handler))
    if not existing:
      util.email_me(subject=blurb, body=source.bridgy_url(handler))

    source.verify()
    if source.verified():
      handler.messages = {blurb}

    if 'webmention' in source.features:
      superfeedr.subscribe(source, handler)

    # TODO: ugh, *all* of this should be transactional
    source.put()

    if 'listen' in source.features:
      util.add_poll_task(source)

    return source
Пример #12
0
  def create_new(cls, handler, user_url=None, **kwargs):
    """Creates and saves a new Source and adds a poll task for it.

    Args:
      handler: the current RequestHandler
      user_url: a string, optional. if provided, supersedes other urls when
        determining the author_url
      **kwargs: passed to new()
    """
    source = cls.new(handler, **kwargs)
    if source is None:
      return None

    new_features = source.features or ['listen']
    if not source.domain_urls:  # defer to the source if it already set this
      auth_entity = kwargs.get('auth_entity')
      if auth_entity and hasattr(auth_entity, 'user_json'):
        source.domain_urls, source.domains = source._urls_and_domains(
          auth_entity, user_url)
        logging.debug('URLs/domains: %s %s', source.domain_urls, source.domains)
        if ('publish' in new_features and
            (not source.domain_urls or not source.domains)):
          handler.messages = {'No valid web sites found in your %s profile. '
                              'Please update it and try again!' % cls.GR_CLASS.NAME}
          return None

    # check if this source already exists
    existing = source.key.get()
    if existing:
      # merge some fields
      source.features = set(source.features + existing.features)
      source.populate(**existing.to_dict(include=(
            'created', 'last_hfeed_refetch', 'last_poll_attempt', 'last_polled',
            'last_syndication_url', 'last_webmention_sent', 'superfeedr_secret')))
      verb = 'Updated'
    else:
      verb = 'Added'

    author_urls = source.get_author_urls()
    link = ('http://indiewebify.me/send-webmentions/?url=' + author_urls[0]
            if author_urls else 'http://indiewebify.me/#send-webmentions')
    blurb = '%s %s. %s' % (verb, source.label(), {
      'listen': "Refresh in a minute to see what we've found!",
      'publish': 'Try previewing a post from your web site!',
      'webmention': '<a href="%s">Try a webmention!</a>' % link,
      }.get(new_features[0], ''))
    logging.info('%s %s', blurb, source.bridgy_url(handler))
    # uncomment to send email notification for each new user
    # if not existing:
    #   util.email_me(subject=blurb, body=source.bridgy_url(handler))

    source.verify()
    if source.verified():
      handler.messages = {blurb}

    if 'webmention' in source.features:
      superfeedr.subscribe(source, handler)

    # TODO: ugh, *all* of this should be transactional
    source.put()

    if 'listen' in source.features:
      util.add_poll_task(source, now=True)
      util.add_poll_task(source, countdown=source.poll_period().total_seconds())

    return source
Пример #13
0
    def create_new(cls, handler, user_url=None, **kwargs):
        """Creates and saves a new :class:`Source` and adds a poll task for it.

    Args:
      handler: the current :class:`webapp2.RequestHandler`
      user_url: a string, optional. if provided, supersedes other urls when
        determining the author_url
      **kwargs: passed to :meth:`new()`
    """
        source = cls.new(handler, **kwargs)
        if source is None:
            return None

        new_features = source.features or ['listen']
        if not source.domain_urls:  # defer to the source if it already set this
            auth_entity = kwargs.get('auth_entity')
            if auth_entity and hasattr(auth_entity, 'user_json'):
                source.domain_urls, source.domains = source._urls_and_domains(
                    auth_entity, user_url)
                logging.debug('URLs/domains: %s %s', source.domain_urls,
                              source.domains)
                if ('publish' in new_features
                        and (not source.domain_urls or not source.domains)):
                    handler.messages = {
                        'No valid web sites found in your %s profile. '
                        'Please update it and try again!' % cls.GR_CLASS.NAME
                    }
                    return None

        # check if this source already exists
        existing = source.key.get()
        if existing:
            # merge some fields
            source.features = set(source.features + existing.features)
            source.populate(**existing.to_dict(
                include=('created', 'last_hfeed_refetch', 'last_poll_attempt',
                         'last_polled', 'last_syndication_url',
                         'last_webmention_sent', 'superfeedr_secret')))
            verb = 'Updated'
        else:
            verb = 'Added'

        author_urls = source.get_author_urls()
        link = ('http://indiewebify.me/send-webmentions/?url=' + author_urls[0]
                if author_urls else 'http://indiewebify.me/#send-webmentions')
        blurb = '%s %s. %s' % (
            verb, source.label(), {
                'listen': "Refresh in a minute to see what we've found!",
                'publish': 'Try previewing a post from your web site!',
                'webmention': '<a href="%s">Try a webmention!</a>' % link,
            }.get(new_features[0], ''))
        logging.info('%s %s', blurb, source.bridgy_url(handler))
        # uncomment to send email notification for each new user
        # if not existing:
        #   util.email_me(subject=blurb, body=source.bridgy_url(handler))

        source.verify()
        if source.verified():
            handler.messages = {blurb}

        # TODO: ugh, *all* of this should be transactional
        source.put()

        if 'webmention' in source.features:
            superfeedr.subscribe(source, handler)

        if 'listen' in source.features:
            util.add_poll_task(source, now=True)
            util.add_poll_task(source,
                               countdown=source.poll_period().total_seconds())

        return source