예제 #1
0
def test_user_rss_url_etag(session, client):
    entries = EntryFactory.create_batch(5)
    author1 = AuthorFactory()
    author1.entries.extend(entries)
    user = UserFactory(active=True)
    user.userfeed.private = False
    sub1 = Subscription(user=user, author=author1)
    sub1.save()

    h = Headers()
    h.add("If-None-Match", None)
    response1 = client.get(url_for("users.user_feed", user_id=user.id), headers=h)

    assert response1.status_code == 200
    etag = response1.headers.get("ETag")
    assert response1.data
    assert (
        response1.headers.get("Content-Type") == "application/atom+xml; charset=utf-8"
    )

    h.add("If-None-Match", etag)
    response2 = client.get(url_for("users.user_feed", user_id=user.id), headers=h)

    assert response2.status_code == 304
    etag2 = response2.headers.get("ETag")
    assert etag2 == etag
    assert not response2.data
예제 #2
0
    def test_subscription_add_period(self):
        user = UserFactory()
        author = AuthorFactory()
        sub = Subscription(user=user, author=author)
        self.assertFalse(sub.periods)
        sub.add_period(PERIOD.IMMEDIATE)
        self.assertIsNotNone(sub.periods)

        p = Period.query.filter_by(name=PERIOD.DAILY).first()
        sub.add_period(p)
        self.assertEqual(len(sub.periods), 2)
예제 #3
0
def test_subscription_add_period(session):
    user = UserFactory()
    author = AuthorFactory()
    sub = Subscription(user=user, author=author)
    assert sub.periods == []
    sub.add_period(PERIOD.IMMEDIATE)
    assert sub.periods is not None
    assert len(sub.periods) == 1

    p = Period.query.filter_by(name=PERIOD.DAILY).first()
    sub.add_period(p)
    assert len(sub.periods) == 2
예제 #4
0
def subscribe(author_id):
    """
    Create a Subscription to an author
    """
    author = Author.query.get_or_404(author_id)

    sub = (
        db.session.query(Subscription)
        .filter(
            Subscription.user_id == current_user.id, Subscription.author_id == author.id
        )
        .first()
    )

    if not sub:
        sub = Subscription(user=current_user, author=author, active=True)
        app.logger.info("%s created", sub)
    else:
        sub.active = True
        app.logger.info("%s set to active", sub)

    if not sub.periods:
        sub.add_period(PERIOD.DAILY)

    sub.save()

    update_user_rss.send(bp, users=[current_user])

    flash(
        f"Successfully created a {sub.period_string()} Email Subscription to {author.name}",
        ALERT.SUCCESS,
    )

    return redirect_back("authors.authors")
예제 #5
0
def subscribe(author_id):
    author = Author.query.get_or_404(author_id)
    sub = Subscription.query.filter(
        Subscription.user_id == current_user.id,
        Subscription.author_id == author.id).first()
    if not sub:
        sub = Subscription(user=current_user, author=author, active=True)
        app.logger.info(u'Subscription {0} created'.format(sub))
    else:
        sub.active = True
        app.logger.info(u'Subscription {0} set to active'.format(sub))
    if not sub.periods:
        sub.add_period(PERIOD.DAILY)
    sub.save()
    flash(u'Successfully created a {0} Email Subscription to {1}'.format(
        sub.period_string(), author.name), ALERT.SUCCESS)
    return redirect_back('authors.authors')
예제 #6
0
def test_update_multiple_entries(subhandler, session):
    user1 = UserFactory(active=True)
    user2 = UserFactory(active=True)
    user3 = UserFactory(active=True)
    user4 = UserFactory(active=True)

    author1 = AuthorFactory()

    sub1 = Subscription(user=user1, author=author1, active=True)
    sub2 = Subscription(user=user2, author=author1, active=True)
    sub3 = Subscription(user=user3, author=author1, active=False)
    sub4 = Subscription(user=user4, author=author1, active=True)

    p = Period.query.filter_by(name=PERIOD.IMMEDIATE).first()
    sub1.add_period(p)
    sub2.add_period(p)
    sub3.add_period(p)

    p2 = Period.query.filter_by(name=PERIOD.DAILY).first()
    sub4.add_period(p2)

    entry1 = EntryFactory()
    entry2 = EntryFactory()

    entry1.authors.append(author1)
    entry2.authors.append(author1)

    entries = [entry1, entry2]

    db.session.commit()

    with mail.record_messages() as outbox:
        subhandler.update(entries)

        assert len(outbox) == 2

        email_count = Email.query.count()
        assert email_count == 2

        user1email = Email.query.filter_by(address=user1.email).first()
        assert user1email.address == user1.email
        assert sorted(user1email.authors) == sorted(user1.authors)
        assert user1email.sent_at is not None

        user2email = Email.query.filter_by(address=user1.email).first()
        assert user2email.address == user1.email
        assert sorted(user2email.authors) == sorted(user1.authors)
        assert user2email.sent_at is not None

        user3email = Email.query.filter_by(address=user3.email).first()
        assert user3email is None

        user4email = Email.query.filter_by(address=user4.email).first()
        assert user4email is None
예제 #7
0
def create_dev_data(user):
    from faker import Factory as FakerFactory

    faker = FakerFactory.create()

    feed = Feed(
        topic="http://test.com/feed",
        hub="http://push.hub.com",
        site_url="http://test.com",
        title="Test Feed",
        description="A test feed",
        site_name="TestFeed.com",
        user=user,
    )
    db.session.add(feed)

    entry1 = Entry(
        title="Test Entry",
        guid="http://test.com/feed/12345345234",
        content=list_to_html_paragraphs(faker.paragraphs(nb=5)),
        published=datetime(2017, 1, 1),
        site="TestFeed.com",
    )
    db.session.add(entry1)

    entry2 = Entry(
        title="Another Test Entry",
        guid="http://test.com/feed/346546gsdfgd",
        content=list_to_html_paragraphs(faker.paragraphs()),
        published=datetime(2017, 2, 1),
        site="TestFeed.com",
    )
    db.session.add(entry2)

    author1 = Author(
        givenname="Testy",
        familyname="McTesterson",
        name="Testy McTesterson",
        email="*****@*****.**",
        url="http://test.com/authors/testy",
    )
    db.session.add(author1)

    author2 = Author(
        givenname="John",
        familyname="Doe",
        name="John Doe",
        email="*****@*****.**",
        url="http://test.com/authors/johndoe",
    )
    db.session.add(author2)

    entry1.add_authors([author1])
    entry2.add_authors([author1, author2])

    subscription = Subscription(user=user, author=author1, active=True)
    subscription.add_period(PERIOD.DAILY)
    subscription.add_period(PERIOD.IMMEDIATE)
    subscription.add_period(PERIOD.WEEKLY)
    subscription.add_period(PERIOD.MONTHLY)

    db.session.add(subscription)

    r1 = Recommended(author=author1, active=True)
    r2 = Recommended(author=author2, active=True)
    db.session.add(r1)
    db.session.add(r2)

    dailyPeriod = Period.query.filter_by(name=PERIOD.DAILY).first()
    email = Email(
        user=user,
        period=dailyPeriod,
        authors=[author1, author2],
        entries=[entry1, entry2],
        address=user.email,
    )
    db.session.add(email)

    db.session.commit()