Ejemplo n.º 1
0
def test_subscription_remove_period(subhandler, client, session):
    # users = User.query.all()
    # pprint(users)

    password = '******'
    user = UserFactory(active=True, password=password)
    author = AuthorFactory()
    sub = SubscriptionFactory(user=user, author=author, active=True)
    sub.add_period(PERIOD.DAILY)

    # with client as c:
    assert sub.has_period(PERIOD.DAILY)
    assert len(sub.periods) == 1

    h = Headers()
    auth = requests.auth._basic_auth_str(user.email, password)
    h.add('Authorization', auth)
    url = url_for('subscriptions.remove_period') + '?period=Daily' + '&subscription_id=' + str(sub.id)

    response = client.post(url, headers=h)

    assert response.status_code == 302

    assert len(sub.periods) == 0
    assert sub.has_period(PERIOD.DAILY) is False
Ejemplo n.º 2
0
def test_update_no_active_users(subhandler, session):
    user = UserFactory(active=False)
    author = AuthorFactory()
    entry = EntryFactory()
    entry.authors.append(author)
    sub = SubscriptionFactory(user=user, author=author, active=True)
    sub.add_period(PERIOD.IMMEDIATE)

    with mail.record_messages() as outbox:
        with pytest.raises(NoneFoundError):
            subhandler.update([entry])
        assert len(outbox) == 0
Ejemplo 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
Ejemplo n.º 4
0
def test_cron_update_single_user(subhandler, client, session):
    # users = User.query.all()
    # pprint(users)


    user = UserFactory(active=True)
    user2 = UserFactory(active=True)
    author = AuthorFactory()
    now = datetime.utcnow()
    entry1 = EntryFactory(published=now - timedelta(hours=5))
    entry2 = EntryFactory(published=now - timedelta(hours=3))
    entry3 = EntryFactory(published=now - timedelta(hours=26))
    entry1.authors.append(author)
    entry2.authors.append(author)
    entry3.authors.append(author)

    db.session.commit()

    sub = SubscriptionFactory(user=user, author=author, active=True)
    sub2 = SubscriptionFactory(user=user2, author=author, active=True)
    sub.add_period(PERIOD.DAILY)
    sub2.add_period(PERIOD.DAILY)

    user_email = '*****@*****.**'
    user_password = '******'
    add_admin(user_email, user_password)

    db.session.commit()

    # with client as c:
    with mail.record_messages() as outbox:
        h = Headers()
        auth = requests.auth._basic_auth_str(user_email, user_password)
        h.add('Authorization', auth)
        url = url_for('subscriptions.send_update') + '?period=Daily' + '&user_id=' + str(user.id)

        response = client.post(url, headers=h)

        assert response.status_code == 200

        assert len(outbox) == 1
        assert outbox[0].recipients[0] == user.email

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

        email = Email.query.filter_by(
            user_id=user.id,
            period_id=Period.get_period_id(PERIOD.DAILY)).first()

        assert email.address == user.email
        assert len(email.authors) == 1
        assert len(email.entries) == 2
        TestCase().assertCountEqual(email.entries, [entry1, entry2])
        assert email.period == PERIOD.DAILY

        TestCase().assertAlmostEqual(sub.last_email_sent,
                                     datetime.utcnow(),
                                     delta=timedelta(seconds=1))
Ejemplo n.º 5
0
def test_update(subhandler, session):
    user = UserFactory(active=True)
    entry = EntryFactory()
    author = AuthorFactory()
    subscription = SubscriptionFactory(user_id=user.id,
                                       author_id=author.id,
                                       active=True,
                                       user=user,
                                       author=author)
    subscription.add_period(PERIOD.IMMEDIATE)
    entry.authors.append(author)

    with mail.record_messages() as outbox:
        subhandler.update([entry])

        assert len(outbox) == 1
        assert len(outbox[0].recipients) == 1
        assert outbox[0].recipients[0] == user.email

        emails = Email.query.all()
        assert len(emails) == 1
        assert emails[0].address == user.email
        assert emails[0].sent_at is not None
Ejemplo n.º 6
0
def test_get_user_subscriptions_for_period(subhandler, session):
    user = UserFactory()
    sub1 = SubscriptionFactory(user=user,
                               author=AuthorFactory(),
                               active=True)
    sub2 = SubscriptionFactory(user=user,
                               author=AuthorFactory(),
                               active=True)
    sub1.add_period(PERIOD.IMMEDIATE)
    sub1.add_period(PERIOD.WEEKLY)
    sub2.add_period(PERIOD.DAILY)
    db.session.commit()

    subs = subhandler.get_user_subscriptions_for_period(
        user.id, PERIOD.IMMEDIATE)
    assert len(subs) == 1
    assert subs[0] == sub1
Ejemplo n.º 7
0
def test_get_subscriptions_for_period(subhandler, session):
    user = UserFactory()
    sub1 = SubscriptionFactory(user=user, author=AuthorFactory())
    sub1.add_period(PERIOD.IMMEDIATE)

    sub2 = SubscriptionFactory(user=user, author=AuthorFactory())
    sub2.add_period(PERIOD.DAILY)

    sub3 = SubscriptionFactory(user=user, author=AuthorFactory())
    sub3.add_period(PERIOD.WEEKLY)
    subs = [sub1, sub2, sub3]

    immediate = subhandler.get_subscriptions_for_period(
        subs, PERIOD.IMMEDIATE)
    assert len(immediate) == 1
    assert immediate[0] == sub1

    daily = subhandler.get_subscriptions_for_period(subs, PERIOD.DAILY)
    assert len(daily) == 1
    assert daily[0] == sub2

    weekly = subhandler.get_subscriptions_for_period(subs, PERIOD.WEEKLY)
    assert len(weekly) == 1
    assert weekly[0] == sub3