Esempio n. 1
0
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)
Esempio n. 2
0
    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)
Esempio n. 3
0
def test_listen_for_entries_added(subhandler, session):
    user = UserFactory(active=True)
    author = AuthorFactory()
    entry = EntryFactory()
    entry.authors.append(author)
    sub = SubscriptionFactory(user=user, author=author, active=True)
    sub.add_period(PERIOD.IMMEDIATE)
    db.session.add(sub)
    db.session.commit()

    class TestSender:
        def __init__(self, entries):
            self.entries = entries

    with mail.record_messages() as outbox:
        entries_added.connect(when_entries_added)
        sender = TestSender([entry])
        assert len(sender.entries) == 1
        entries_added.send(sender, entries=[entry])

        assert len(outbox) == 1
Esempio n. 4
0
def signal_entries_added():
    entries_added.connect(when_entries_added)

    yield entries_added

    entries_added.disconnect(when_entries_added)
Esempio n. 5
0
def register_subscription_blueprint_and_signals(app):
    """Registers blueprint to app and connects signals"""
    app.register_blueprint(subscription_blueprint)
    entries_added.connect(when_entries_added)
Esempio n. 6
0
def register_subscription_blueprint_and_signals(app):
    app.register_blueprint(subscription_blueprint)
    print("Connecting 'when_entries_added' to 'entries-added' signal")
    entries_added.connect(when_entries_added)
Esempio n. 7
0
    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)