def signal_connection(): notification_received.connect(when_notification_received) entries_added.connect(when_entries_added) update_user_rss.connect(when_update_user_rss) authors_entries_added.connect(when_authors_entries_added) yield notification_received.disconnect(when_notification_received) entries_added.disconnect(when_entries_added) update_user_rss.disconnect(when_update_user_rss) authors_entries_added.connect(when_authors_entries_added)
def test_create_feed(self): notification_received.connect(when_notification_received) url = 'davidbeath.com' feed = subscriber.create_feed(url) self.assertEqual(feed.topic, 'https://davidbeath.com/rss.xml') self.assertEqual(feed.hub, 'http://push.superfeedr.com') self.assertEqual(feed.title, 'David Beath') self.assertIsNotNone(feed.description) entries = Entry.query.filter_by(feed_id=feed.id).all() self.assertEqual(len(entries), 10)
def test_notification_path(self): with open(TEST_FILES_DIR + 'boingboing.xml') as f: data = f.read() user = UserFactory(active=True) feed = FeedFactory(status=STATUS.SUBSCRIBED) author = AuthorFactory(name='Cory Doctorow', givenname='Cory', familyname='Doctorow', email='*****@*****.**') sub = SubscriptionFactory(user=user, author=author, active=True) sub.add_period(PERIOD.IMMEDIATE) db.session.commit() h = hmac.new(bytes(feed.secret, 'UTF-8'), digestmod=hashlib.sha1) h.update(data.encode('UTF-8')) digest = h.hexdigest() sig = "sha1=" + digest headers = {} headers['X-Hub-Signature'] = sig headers['content-type'] = 'application/rss+xml' headers['Link'] = str(LinkHeader([Link(feed.hub, rel="hub"), Link(feed.topic, rel="self")])) with self.app.test_client() as c: notification_received.connect(when_notification_received) entries_added.connect(when_entries_added) update_user_rss.connect(when_update_user_rss) with mail.record_messages() as outbox: response = c.post(get_public_url(feed), headers=headers, data=data) self.assertEqual(response.status_code, 200) authors = db.session.query(Author).all() self.assertEqual(len(authors), 6) entries = db.session.query(Entry).all() self.assertEqual(len(entries), 30) self.assertIs(type(entries[0].content), str) self.assertEqual(len(outbox), 1) emails = Email.query.all() self.assertEqual(len(emails), 1) self.assertEqual(emails[0].address, user.email)
def capture_signal_notification_received(): recorded = [] def assert_received(sender, feed, content_type, content, **kwargs): assert sender assert feed assert content_type assert content recorded.append((feed, content)) notification_received.connect(assert_received) try: yield recorded finally: notification_received.disconnect(assert_received)
def test_notification(self): feed = FeedFactory(status=STATUS.SUBSCRIBED) db.session.commit() h = hmac.new(bytes(feed.secret, 'UTF-8'), digestmod=hashlib.sha1) h.update(jsondata.encode('UTF-8')) digest = h.hexdigest() sig = "sha1=" + digest headers = {} headers['X-Hub-Signature'] = sig headers['content-type'] = 'application/json' headers['Link'] = str(LinkHeader([Link(feed.hub, rel="hub"), Link(feed.topic, rel="self")])) with self.app.test_client() as c: notification_received.connect(when_notification_received) response = c.post(get_public_url(feed), headers=headers, data=jsondata) self.assertEqual(response.status_code, 200) self.assertAlmostEqual(feed.last_update_received, datetime.utcnow(), delta=timedelta(seconds=1)) self.assertAlmostEqual(feed.updated_on, datetime.utcnow(), delta=timedelta(seconds=1)) self.assertEqual(feed.title, 'A wonderful feed') entryCount = Entry.query.count() self.assertEqual(entryCount, 2) entries = Entry.query.all() self.assertIsNotNone(entries) self.assertEqual(len(entries), 2) entry1 = next((e for e in entries if e.link == "http://domain.tld/entry/1"), None) self.assertIsNotNone(entry1) self.assertEqual(entry1.link, "http://domain.tld/entry/1")
def test_notification_rss(self): feed = FeedFactory(status=STATUS.SUBSCRIBED) db.session.commit() with open(TEST_FILES_DIR + 'rss.xml') as f: data = f.read() h = hmac.new(bytes(feed.secret, 'UTF-8'), digestmod=hashlib.sha1) h.update(data.encode('UTF-8')) digest = h.hexdigest() sig = "sha1=" + digest headers = {} headers['X-Hub-Signature'] = sig headers['content-type'] = 'application/rss+xml' headers['Link'] = str(LinkHeader([Link(feed.hub, rel="hub"), Link(feed.topic, rel="self")])) with self.app.test_client() as c: notification_received.connect(when_notification_received) response = c.post(get_public_url(feed), headers=headers, data=data) self.assertEqual(response.status_code, 200) self.assertAlmostEqual(feed.last_update_received, datetime.utcnow(), delta=timedelta(seconds=1)) self.assertAlmostEqual(feed.updated_on, datetime.utcnow(), delta=timedelta(seconds=1)) self.assertEqual(feed.title, 'David Beath') entryCount = Entry.query.count() self.assertEqual(entryCount, 10)
def test_subscribe_succeeded_with_content(session): topic = 'http://test.com/feed' hub = 'http://push.superfeedr.com' notification_received.connect(when_notification_received) with open(TEST_FILES_DIR + 'rss.xml') as f: data = f.read() responses.add(responses.POST, hub, body=data, status=200, content_type='application/rss+xml') result = subscriber.subscribe(topic, hub, find_feed=False) assert result[0] == 200 feed = Feed.query.filter_by(topic=topic).first() assert feed.title == 'David Beath' assert feed.status == STATUS.PENDING_SUB entryCount = Entry.query.count() assert entryCount == 10
def register_pubsub_blueprint_and_signals(app): app.register_blueprint(pubsubhubbub_blueprint) print("Connecting 'when_notification_received' to 'notification-received' signal") notification_received.connect(when_notification_received)
def signal_notification_received(): notification_received.connect(when_notification_received) yield notification_received notification_received.disconnect(when_notification_received)
def register_ingestion_blueprint_and_signals(app): """Registers blueprint to app and connects signals""" app.register_blueprint(ingestion_blueprint) notification_received.connect(when_notification_received)
def test_json_notification_path(self): with open(TEST_FILES_DIR + 'notification.json') as f: data = f.read() user = UserFactory(active=True) feed = FeedFactory(status=STATUS.SUBSCRIBED, topic='http://testfeed.test') author = AuthorFactory(name='Testy McTesterson', givenname='Testy', familyname='McTesterson') sub = SubscriptionFactory(user=user, author=author, active=True) sub.add_period(PERIOD.IMMEDIATE) db.session.commit() h = hmac.new(bytes(feed.secret, 'UTF-8'), digestmod=hashlib.sha1) h.update(data.encode('UTF-8')) digest = h.hexdigest() sig = "sha1=" + digest headers = {} headers['X-Hub-Signature'] = sig headers['content-type'] = 'application/json' headers['Link'] = str(LinkHeader([Link(feed.hub, rel='hub'), Link(feed.topic, rel='self')])) with self.app.test_client() as c: notification_received.connect(when_notification_received) entries_added.connect(when_entries_added) update_user_rss.connect(when_update_user_rss) with mail.record_messages() as outbox: response = c.post(get_public_url(feed), headers=headers, data=data) self.assertEqual(response.status_code, 200) authors = db.session.query(Author).all() self.assertEqual(len(authors), 3) author2 = Author.query.filter_by(name='John Doe').first() self.assertEqual(author2.name, 'John Doe') self.assertEqual(author2.givenname, 'John') self.assertEqual(author2.familyname, 'Doe') author3 = Author.query.filter_by(name=u'Tĕstá ĀũʈĥőŘ').first() self.assertEqual(author3.name, u'Tĕstá ĀũʈĥőŘ') self.assertEqual(author3.givenname, u'Tĕstá') self.assertEqual(author3.familyname, u'ĀũʈĥőŘ') entries = Entry.query.all() self.assertEqual(len(entries), 2) self.assertIs(type(entries[0].content), str) self.assertIs(type(entries[1].content), str) entry2 = Entry.query.filter_by(guid='domain.tld/2015-12-02').first() self.assertEqual(entry2.content, u'This is the second entry, it contains unicode Tĕstá ĀũʈĥőŘ') self.assertEqual(len(outbox), 1) emails = Email.query.all() self.assertEqual(len(emails), 1) self.assertEqual(emails[0].address, user.email)