def test_fetch_feed_not_modified_last_modified(
    session, capture_signal_notification_received
):
    feed = Feed(topic="http://test.com/feed.rss")
    content_hash = "adfasdfasd"
    feed.content_hash = content_hash
    modified = datetime(2017, 6, 6, 12, 0, 0)

    responses.add(
        responses.GET,
        feed.topic,
        body=None,
        status=304,
        adding_headers={"last-modified": create_modified_header(modified)},
        content_type="application/rss+xml",
    )

    result = FeedFetcher.fetch_feed(feed)
    assert result is False
    assert feed.last_status_code == 304
    assert not feed.etag
    assert feed.last_modified == modified
    assert feed.content_hash == content_hash
    assert feed.next_scheduled_update > Feed.current_timestamp()

    assert len(capture_signal_notification_received) == 0

    stats = FeedStats.query.filter_by(feed_id=feed.id).first()
    assert stats
    assert stats.status_code == 304
    assert stats.new_entries == 0
    assert stats.updated_entries == 0
    assert stats.type == FetchType.PULL
    assert stats.latency > 0
    assert stats.parse_time == 0
def test_get_scheduled_feeds(session):
    timestamp = Feed.current_timestamp()

    feed_to_fetch_1 = FeedFactory(next_scheduled_update=timestamp - 1000)
    feed_to_fetch_2 = FeedFactory(next_scheduled_update=timestamp - 100000)
    feed_future_fetch = FeedFactory(next_scheduled_update=timestamp + 100)
    feed_is_push = FeedFactory(next_scheduled_update=timestamp - 1000, is_push=True)
    feed_is_inactive = FeedFactory(next_scheduled_update=timestamp - 1000, active=False)

    to_fetch = FeedFetcher.get_scheduled_feeds()
    assert len(to_fetch) == 3
    TestCase().assertCountEqual(
        to_fetch, [feed_to_fetch_1, feed_to_fetch_2, feed_is_push]
    )

    feed_to_fetch_2.next_scheduled_update = timestamp + 1000
    feed_to_fetch_3 = FeedFactory(next_scheduled_update=timestamp - 100)

    to_fetch = FeedFetcher.get_scheduled_feeds()
    assert len(to_fetch) == 3
    TestCase().assertCountEqual(
        to_fetch, [feed_to_fetch_1, feed_to_fetch_3, feed_is_push]
    )

    to_fetch = FeedFetcher.get_scheduled_feeds(1)
    assert len(to_fetch) == 1
def test_fetch_feed_no_new_content(session, capture_signal_notification_received):
    feed = Feed(topic="http://test.com/feed.rss")

    body = "<rss></rss>"
    feed.new_content(body)

    responses.add(
        responses.GET,
        feed.topic,
        body=body,
        status=200,
        content_type="application/rss+xml",
    )

    result = FeedFetcher.fetch_feed(feed)
    assert result is False
    assert feed.last_status_code == 200
    assert not feed.etag
    assert not feed.last_modified
    assert feed.next_scheduled_update > Feed.current_timestamp()

    assert len(capture_signal_notification_received) == 0

    stats = FeedStats.query.filter_by(feed_id=feed.id).first()
    assert stats
    assert stats.status_code == 200
    assert stats.new_entries == 0
    assert stats.updated_entries == 0
    assert stats.type == FetchType.PULL
    assert stats.latency > 0
    assert stats.parse_time == 0
def test_fetch_feed(session, capture_signal_notification_received):
    feed = Feed(topic="http://test.com/feed.rss")

    body = "<rss></rss>"

    responses.add(
        responses.GET,
        feed.topic,
        body=body,
        status=200,
        content_type="application/rss+xml",
    )

    result = FeedFetcher.fetch_feed(feed)
    assert result is True
    assert feed.last_status_code == 200
    assert not feed.etag
    assert not feed.last_modified
    assert feed.next_scheduled_update > Feed.current_timestamp()

    captured_feed, captured_content = capture_signal_notification_received[0]
    assert captured_feed.topic == feed.topic
    assert captured_content == body
Exemple #5
0
def test_feed_set_next_scheduled_update(session):
    timestamp = Feed.current_timestamp()

    feed = FeedFactory()
    assert not feed.last_fetch
    assert not feed.next_scheduled_update

    feed.set_next_scheduled_update()
    assert feed.next_scheduled_update > timestamp
    assert feed.next_scheduled_update < timestamp + 4000

    feed.last_fetch = datetime.utcnow()
    feed.set_next_scheduled_update()
    assert feed.next_scheduled_update > timestamp
    assert feed.next_scheduled_update < timestamp + 4000

    feed.last_fetch = datetime.utcnow() + timedelta(days=1)
    feed.set_next_scheduled_update()
    assert feed.next_scheduled_update > timestamp

    feed.last_fetch = datetime.utcnow()
    feed.set_next_scheduled_update(10000)
    assert feed.next_scheduled_update > timestamp + 9500
    assert feed.next_scheduled_update < timestamp + 11500
Exemple #6
0
    def index(self):
        current_time = datetime.utcnow()
        dayago = current_time - timedelta(days=1)
        weekago = current_time - timedelta(weeks=1)
        monthago = current_time - timedelta(days=31)
        yearago = current_time - timedelta(days=365)

        immediate_subs = get_count(
            db.session.query(Subscription.id)
            .join(Period, Subscription.periods)
            .filter(Period.name == PERIOD.IMMEDIATE, Subscription.active == True)
        )
        daily_subs = get_count(
            db.session.query(Subscription.id)
            .join(Period, Subscription.periods)
            .filter(Period.name == PERIOD.DAILY, Subscription.active == True)
        )
        weekly_subs = get_count(
            db.session.query(Subscription.id)
            .join(Period, Subscription.periods)
            .filter(Period.name == PERIOD.WEEKLY, Subscription.active == True)
        )
        monthly_subs = get_count(
            db.session.query(Subscription.id)
            .join(Period, Subscription.periods)
            .filter(Period.name == PERIOD.MONTHLY, Subscription.active == True)
        )

        active_users = get_count(db.session.query(User.id).filter_by(active=True))
        admin_users = get_count(
            db.session.query(User.id)
            .join(Role, User.roles)
            .filter(Role.name == "admin")
        )

        entries_last_day = get_count(
            db.session.query(Entry).filter(Entry.published > dayago)
        )
        entries_last_week = get_count(
            db.session.query(Entry).filter(Entry.published > weekago)
        )
        entries_last_month = get_count(
            db.session.query(Entry).filter(Entry.published > monthago)
        )
        entries_last_year = get_count(
            db.session.query(Entry).filter(Entry.published > yearago)
        )

        authors_subscribed = get_count(
            db.session.query(Author)
            .join(Subscription)
            .filter(Subscription.active == True)
        )

        subscribed_feeds = get_count(
            db.session.query(Feed).filter(Feed.status == STATUS.SUBSCRIBED)
        )
        unsubscribed_feeds = get_count(
            db.session.query(Feed).filter(Feed.status == STATUS.UNSUBSCRIBED)
        )
        default_hub_feeds = get_count(
            db.session.query(Feed).filter(Feed.hub == app.config["DEFAULT_HUB"])
        )
        fetch_feeds = get_count(
            db.session.query(Feed.id).filter(
                Feed.status == STATUS.FETCH, Feed.active == True
            )
        )

        daily_emails = get_count(
            db.session.query(Email)
            .join(Period, Email.period)
            .filter(Period.name == PERIOD.DAILY)
        )
        weekly_emails = get_count(
            db.session.query(Email)
            .join(Period, Email.period)
            .filter(Period.name == PERIOD.WEEKLY)
        )
        monthly_emails = get_count(
            db.session.query(Email)
            .join(Period, Email.period)
            .filter(Period.name == PERIOD.MONTHLY)
        )

        current_timestamp = Feed.current_timestamp()

        return self.render(
            "admin/home.html",
            userCount=get_count(db.session.query(User)),
            feedCount=get_count(db.session.query(Feed)),
            entryCount=get_count(db.session.query(Entry)),
            authorCount=get_count(db.session.query(Author)),
            subscriptionCount=get_count(db.session.query(Subscription)),
            emailCount=get_count(db.session.query(Email)),
            daily_emails=daily_emails,
            weekly_emails=weekly_emails,
            monthly_emails=monthly_emails,
            immediate_subs=immediate_subs,
            daily_subs=daily_subs,
            weekly_subs=weekly_subs,
            monthly_subs=monthly_subs,
            active_users=active_users,
            admin_users=admin_users,
            entries_last_year=entries_last_year,
            entries_last_day=entries_last_day,
            entries_last_week=entries_last_week,
            entries_last_month=entries_last_month,
            authors_subscribed=authors_subscribed,
            subscribed_feeds=subscribed_feeds,
            unsubscribed_feeds=unsubscribed_feeds,
            default_hub_feeds=default_hub_feeds,
            fetch_feeds=fetch_feeds,
            current_timestamp=current_timestamp,
            current_datetime=datetime.utcnow(),
        )