Exemplo n.º 1
0
def test_cron_update_single_user(client, session, outbox, admin_user):
    user = UserFactory(active=True)
    user_email = user.email
    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)

    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)

    db.session.commit()

    assert User.query.count() == 3

    assert Email.query.count() == 0

    h = Headers()
    # auth = requests.auth._basic_auth_str(admin_user.email, admin_user.password)
    # h.add('Authorization', auth)
    h["X-Cron-Api-Key"] = CRON_API_KEY
    url = "{0}{1}{2}{3}".format(
        url_for("subscriptions.send_update"), "?period=Daily", "&user_id=", str(user.id)
    )

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

    assert response.status_code == 200

    db.session.add(user)
    db.session.add(author)

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

    assert Email.query.count() == 1

    email = Email.query.filter_by(
        address=user_email, period_id=Period.get_period_id(PERIOD.DAILY)
    ).first()
    assert email is not None

    assert email.address == user_email
    assert len(email.authors) == 1
    assert len(email.entries) == 2

    assert email.period == PERIOD.DAILY

    sub = Subscription.query.filter_by(user_id=user.id, author_id=author.id).first()
    assert sub.last_email_sent is not None
    TestCase().assertAlmostEqual(
        sub.last_email_sent, datetime.utcnow(), delta=timedelta(seconds=1)
    )
Exemplo n.º 2
0
    def test_get_from_date_with_period(self):
        now = datetime.utcnow()
        past = now - relativedelta(days=1)

        period = Period(name=PERIOD.DAILY)
        from_date = Period.get_from_date(period, now)
        self.assertEqual(from_date, past)
Exemplo n.º 3
0
def test_get_from_date_with_period(session):
    now = datetime.utcnow()
    past = now - relativedelta(days=1)

    period = Period(name=PERIOD.DAILY)
    from_date = Period.get_from_date(period, now)
    assert from_date == past
Exemplo 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))
Exemplo n.º 5
0
def send_update(period_name=None, user_id=None):
    period_name = request.args.get('period', period_name)
    user_id = request.args.get('user_id', user_id)

    period = Period.query.filter_by(name=period_name).first()
    if not period:
        abort(400)

    if user_id:
        users = []
        user = User.query.filter_by(id=user_id).first()
        users.append(user)
        if not users:
            abort(400)
    else:
        users = User.query.filter_by(active=True).all()

    app.logger.info(u'Running {0} update for {1} users'
                    .format(period, len(users)))

    emails = []
    subs = []
    now = datetime.utcnow()

    entries_from = Period.get_from_date(period, now)

    for user in users:
        email, sent_subs = subhandler.create_period_email(user, period, entries_from)
        if email:
            emails.append(email)
        if sent_subs:
            subs.extend(sent_subs)

    subhandler.send_emails(emails)
    subhandler.update_subscription_last_email_sent(subs, datetime.utcnow())

    db.session.commit()

    app.logger.info(u'Sent {0} {1} emails'.format(len(emails), period))

    return Response(status=200)
Exemplo n.º 6
0
    def send_update_email(cls, user_id: int, period_name: str) -> None:
        """
        Creates and sends a Periodic update email to a User.

        :param user_id: Id of user
        :param period_name: Name of period
        :return: None
        """
        period = Period.query.filter_by(name=period_name).first()
        if not period:
            app.logger.exception("No Period found with name %s", period_name)
            return

        entries_from = Period.get_from_date(period, datetime.utcnow())

        user = User.query.filter_by(id=user_id).first()
        if not user:
            app.logger.exception("No user found with Id %s", user_id)
            return

        mail_result = cls.create_period_email(user, period, entries_from)
        if not mail_result:
            app.logger.info("No %s Email created for User %s", period.name, user)
            return

        email, sent_subs = mail_result

        cls.send_email(email)
        # task_send_email.delay(email.msg)
        # email.sent_at = datetime.utcnow()
        db.session.add(email)

        cls.update_subscriptions_last_email_sent(sent_subs, datetime.utcnow())
        db.session.add_all(sent_subs)
        app.logger.info("Sent %s Email to %s", period, user)
        db.session.commit()
        return
Exemplo n.º 7
0
def test_cron_update(client, session, outbox, admin_user):
    user = UserFactory(active=True, confirmed_at=datetime(2016, 1, 1))
    user2 = UserFactory(active=False, confirmed_at=datetime(2016, 1, 1))
    user3 = UserFactory(active=True, confirmed_at=datetime(2016, 1, 1))
    author1 = AuthorFactory()
    author2 = 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(author1)
    entry1.authors.append(author2)
    entry2.authors.append(author2)
    entry3.authors.append(author1)

    sub1 = SubscriptionFactory(user=user, author=author1, active=True)
    sub2 = SubscriptionFactory(user=user, author=author2, active=True)
    sub3 = SubscriptionFactory(user=user2, author=author1, active=False)
    sub4 = SubscriptionFactory(user=user3, author=author2, active=True)

    sub1.add_period(PERIOD.DAILY)
    sub2.add_period(PERIOD.DAILY)
    sub3.add_period(PERIOD.DAILY)
    sub4.add_period(PERIOD.DAILY)

    db.session.commit()

    assert Email.query.count() == 0

    h = Headers()
    # auth = requests.auth._basic_auth_str(admin_user.email, admin_user.password)
    # h.add('Authorization', auth)
    h["X-Cron-Api-Key"] = CRON_API_KEY
    url = url_for("subscriptions.send_update") + "?period=Daily"

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

    assert response.status_code == 200

    db.session.add(user)
    db.session.add(entry1)
    db.session.add(entry2)
    db.session.add(sub1)
    db.session.add(author1)

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

    assert Email.query.count() == 2

    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) == 2
    assert len(email.entries) == 2
    TestCase().assertCountEqual(email.entries, [entry1, entry2])
    assert email.period == PERIOD.DAILY

    sub = Subscription.query.filter_by(user_id=user.id, author_id=author1.id).first()
    assert sub.last_email_sent is not None
    TestCase().assertAlmostEqual(
        sub.last_email_sent, datetime.utcnow(), delta=timedelta(seconds=1)
    )