Пример #1
0
def test_notification_is_created_when_synthesis_is_posted_and_participant_had_proactively_materialized_default_notification_subscriptions(test_app, test_session, discussion, participant1_user, synthesis_post_1):
    # The purpose of this test is to check that a participant who has not changed their notification preferences (from discussion's defaults), but who have proactively asked for materialization of their notification subscriptions (for example by visiting their "Notifications" page, which calls `user.get_notification_subscriptions()` which materializes missing user notification subscriptions from default discussion notification settings) does actually receive a notification.

    # `discussion` fixture has FOLLOW_SYNTHESES enabled in its default notification subscription settings. So a participant who has not changed anything to their notification subscriptions also has this subscription enabled. Participant `participant1_user` is in this case.

    test_session.flush()

    # Thanks to `model_tests/test_notification.py::test_notification_follow_synthesis()`, we already know that a participant will receive a synthesis notification if they have first enabled synthesis notification subscription by themselves, for example with this commented code:
    # subscription2 = NotificationSubscriptionFollowSyntheses(
    #     discussion=discussion,
    #     user=participant1_user,
    #     creation_origin=NotificationCreationOrigin.USER_REQUESTED,
    # )
    # test_session.add(subscription2)


    # Before sending a synthesis, we call on purpose an API route that materializes participant notification subscriptions, from discussion defaults
    response = test_app.get(
        '/data/Discussion/%d/all_users/%d/notification_subscriptions' % (
            discussion.id, participant1_user.id))
    assert response.status_code == 200
    user_notification_subscriptions = response.json
    assert len(user_notification_subscriptions)


    initial_notification_count = test_session.query(Notification).count()
    
    # Simulate publication of a synthesis
    dispatcher = ModelEventWatcherNotificationSubscriptionDispatcher()
    dispatcher.processPostCreated(synthesis_post_1.id)

    notification_count = test_session.query(Notification).count()
    assert notification_count == initial_notification_count + 1, "The synthesis post should have matched and created a notification"
Пример #2
0
def test_users_not_subscribed_to_discussion(test_session, discussion,
                                            participant1_user, reply_post_2,
                                            test_app, root_post_1,
                                            synthesis_post_1,
                                            discussion2_root_post_1,
                                            test_webrequest):
    test_session.flush()
    subscription = NotificationSubscriptionFollowAllMessages(
        discussion=discussion,
        user=participant1_user,
        creation_origin=NotificationCreationOrigin.USER_REQUESTED,
    )
    test_session.add(subscription)

    initial_notification_count = test_session.query(Notification).count()
    dispatcher = ModelEventWatcherNotificationSubscriptionDispatcher()
    dispatcher.processPostCreated(reply_post_2.id)
    notification_count = test_session.query(Notification).count()
    assert notification_count == initial_notification_count + 1, "A new post should have been caught"

    # Smoke test that unsubscribing from a discussion
    # does inhibit notifications
    participant1_user.unsubscribe(discussion)
    test_session.commit()
    dispatcher.processPostCreated(reply_post_2.id)
    notification_count = test_session.query(Notification).count()
    assert notification_count == initial_notification_count + 1, "The user should NOT receive notification if not subscribed to the discussion"
    participant1_user.subscribe(discussion)
Пример #3
0
def test_notification_follow_direct_replies(test_session, discussion,
                                            participant1_user,
                                            participant2_user, root_post_1,
                                            reply_post_1, reply_post_2,
                                            test_app):
    # Note:
    # the author of root_post_1 is participant1_user
    # the author of reply_post_1 is participant2_user
    # the author of reply_post_2 is participant1_user
    # the author of reply_post_3 is participant2_user
    test_session.flush()
    subscription = NotificationSubscriptionFollowOwnMessageDirectReplies(
        discussion=discussion,
        user=participant1_user,
        creation_origin=NotificationCreationOrigin.USER_REQUESTED,
    )
    test_session.add(subscription)

    initial_notification_count = test_session.query(Notification).count()
    dispatcher = ModelEventWatcherNotificationSubscriptionDispatcher()

    dispatcher.processPostCreated(reply_post_1.id)
    notification_count = test_session.query(Notification).count()
    assert notification_count == initial_notification_count + 1, "Direct reply should have created a notification"

    dispatcher.processPostCreated(reply_post_2.id)
    notification_count = test_session.query(Notification).count()
    assert notification_count == initial_notification_count + 1, "Indirect reply should not have created a new notification"
Пример #4
0
def test_synthesis_notification_in_user_preferred_language_en(test_session, discussion,
                                                           participant1_user, participant1_user_language_preference_en_cookie,
                                                           test_app, root_post_1,
                                                           synthesis_post_1):
    test_session.flush()
    subscription = NotificationSubscriptionFollowSyntheses(
        discussion=discussion,
        user=participant1_user,
        creation_origin=NotificationCreationOrigin.USER_REQUESTED
    )
    test_session.add(subscription)

    initial_notification_count = test_session.query(Notification).count()
    dispatcher = ModelEventWatcherNotificationSubscriptionDispatcher()

    notification_count = test_session.query(Notification).count()
    
    dispatcher.processPostCreated(synthesis_post_1.id)

    last_notification = participant1_user.notifications[-1]

    html_content = last_notification.render_to_email_html_part()

    assert "subject EN" in html_content
    assert "introduction EN" in html_content
    assert "conclusion EN" in html_content
Пример #5
0
def test_notification_is_created_when_synthesis_is_posted_and_participant_had_proactively_materialized_default_notification_subscriptions(
        test_app, test_session, discussion, participant1_user,
        synthesis_post_1):
    # The purpose of this test is to check that a participant who has not changed their notification preferences (from discussion's defaults), but who have proactively asked for materialization of their notification subscriptions (for example by visiting their "Notifications" page, which calls `user.get_notification_subscriptions()` which materializes missing user notification subscriptions from default discussion notification settings) does actually receive a notification.

    # `discussion` fixture has FOLLOW_SYNTHESES enabled in its default notification subscription settings. So a participant who has not changed anything to their notification subscriptions also has this subscription enabled. Participant `participant1_user` is in this case.

    test_session.flush()

    # Thanks to `model_tests/test_notification.py::test_notification_follow_synthesis()`, we already know that a participant will receive a synthesis notification if they have first enabled synthesis notification subscription by themselves, for example with this commented code:
    # subscription2 = NotificationSubscriptionFollowSyntheses(
    #     discussion=discussion,
    #     user=participant1_user,
    #     creation_origin=NotificationCreationOrigin.USER_REQUESTED,
    # )
    # test_session.add(subscription2)

    # Before sending a synthesis, we call on purpose an API route that materializes participant notification subscriptions, from discussion defaults
    response = test_app.get(
        '/data/Discussion/%d/all_users/%d/notification_subscriptions' %
        (discussion.id, participant1_user.id))
    assert response.status_code == 200
    user_notification_subscriptions = response.json
    assert len(user_notification_subscriptions)

    initial_notification_count = test_session.query(Notification).count()

    # Simulate publication of a synthesis
    dispatcher = ModelEventWatcherNotificationSubscriptionDispatcher()
    dispatcher.processPostCreated(synthesis_post_1.id)

    notification_count = test_session.query(Notification).count()
    assert notification_count == initial_notification_count + 1, "The synthesis post should have matched and created a notification"
Пример #6
0
def test_synthesis_notification_in_user_preferred_language_fr(
        test_session, discussion, participant1_user,
        participant1_user_language_preference_fr_cookie, test_app, root_post_1,
        synthesis_post_1):
    test_session.flush()
    subscription = NotificationSubscriptionFollowSyntheses(
        discussion=discussion,
        user=participant1_user,
        creation_origin=NotificationCreationOrigin.USER_REQUESTED)
    test_session.add(subscription)

    initial_notification_count = test_session.query(Notification).count()
    dispatcher = ModelEventWatcherNotificationSubscriptionDispatcher()

    notification_count = test_session.query(Notification).count()

    dispatcher.processPostCreated(synthesis_post_1.id)

    last_notification = participant1_user.notifications[-1]

    html_content = last_notification.render_to_email_html_part()

    assert "subject FR" in html_content
    assert "introduction FR" in html_content
    assert "conclusion FR" in html_content
Пример #7
0
def test_notification_follow_all_messages(test_session, discussion,
                                          participant1_user, reply_post_2,
                                          test_app, root_post_1,
                                          synthesis_post_1,
                                          discussion2_root_post_1):
    test_session.flush()
    subscription = NotificationSubscriptionFollowAllMessages(
        discussion=discussion,
        user=participant1_user,
        creation_origin=NotificationCreationOrigin.USER_REQUESTED,
    )
    test_session.add(subscription)

    initial_notification_count = test_session.query(Notification).count()
    dispatcher = ModelEventWatcherNotificationSubscriptionDispatcher()
    dispatcher.processPostCreated(reply_post_2.id)
    notification_count = test_session.query(Notification).count()
    assert notification_count == initial_notification_count + 1, "A new post should have been caught"

    # Check that subclasses are still caught
    dispatcher.processPostCreated(synthesis_post_1.id)
    notification_count = test_session.query(Notification).count()
    assert notification_count == initial_notification_count + 2, "A post with a subclass should have been caught"

    # Smoke test that other discussion's post do not leak
    dispatcher.processPostCreated(discussion2_root_post_1.id)
    notification_count = test_session.query(Notification).count()
    assert notification_count == initial_notification_count + 2, "A post from another discussion should NOT have been caught"
Пример #8
0
def test_notification_follow_synthesis(test_session, discussion,
                                       participant1_user, reply_post_2,
                                       test_app, root_post_1,
                                       synthesis_post_1):
    test_session.flush()
    subscription = NotificationSubscriptionFollowSyntheses(
        discussion=discussion,
        user=participant1_user,
        creation_origin=NotificationCreationOrigin.USER_REQUESTED)
    test_session.add(subscription)

    initial_notification_count = test_session.query(Notification).count()
    dispatcher = ModelEventWatcherNotificationSubscriptionDispatcher()
    dispatcher.processPostCreated(reply_post_2.id)
    notification_count = test_session.query(Notification).count()
    assert notification_count == initial_notification_count, "The post wasn't a synthesis and shouldn't have been caught"
    initial_last_status_change_date = subscription.last_status_change_date
    subscription.status = NotificationSubscriptionStatus.UNSUBSCRIBED
    assert subscription.last_status_change_date > initial_last_status_change_date, "The last status change date should have auto-updated"
    dispatcher.processPostCreated(synthesis_post_1.id)
    notification_count = test_session.query(Notification).count()
    assert notification_count == initial_notification_count, "The synthesis shouldn't have created a notification, because the subscription is unsubscribed"
    subscription.status = NotificationSubscriptionStatus.ACTIVE
    dispatcher.processPostCreated(synthesis_post_1.id)
    notification_count = test_session.query(Notification).count()
    assert notification_count == initial_notification_count + 1, "The synthesis post should have matched and created a notification"
Пример #9
0
def test_notification_is_created_when_synthesis_is_posted_with_default_notification_subscriptions_without_proactive_materialization(test_app, test_session, discussion, participant1_user, synthesis_post_1):
    # The purpose of this test is to check that a participant who has not changed their notification preferences from discussion's default and who has not accessed the debate since any discussion notification settings changes (which means they have not asked proactively for notification materialization, for example by visiting their "Notifications" page, which calls `user.get_notification_subscriptions()` which materializes missing user notification subscriptions from default discussion notification settings) does actually receive a notification.

    # `discussion` fixture has FOLLOW_SYNTHESES enabled in its default notification subscription settings. So a participant who has not changed anything to their notification subscriptions also has this subscription enabled. Participant `participant1_user` is in this case.

    test_session.flush()

    initial_notification_count = test_session.query(Notification).count()

    # Simulate publication of a synthesis
    dispatcher = ModelEventWatcherNotificationSubscriptionDispatcher()
    dispatcher.processPostCreated(synthesis_post_1.id)

    notification_count = test_session.query(Notification).count()
    assert notification_count == initial_notification_count + 1, "The synthesis post should have matched and created a notification"
Пример #10
0
def test_notification_is_created_when_synthesis_is_posted_with_default_notification_subscriptions_without_proactive_materialization(
        test_app, test_session, discussion, participant1_user,
        synthesis_post_1):
    # The purpose of this test is to check that a participant who has not changed their notification preferences from discussion's default and who has not accessed the debate since any discussion notification settings changes (which means they have not asked proactively for notification materialization, for example by visiting their "Notifications" page, which calls `user.get_notification_subscriptions()` which materializes missing user notification subscriptions from default discussion notification settings) does actually receive a notification.

    # `discussion` fixture has FOLLOW_SYNTHESES enabled in its default notification subscription settings. So a participant who has not changed anything to their notification subscriptions also has this subscription enabled. Participant `participant1_user` is in this case.

    test_session.flush()

    initial_notification_count = test_session.query(Notification).count()

    # Simulate publication of a synthesis
    dispatcher = ModelEventWatcherNotificationSubscriptionDispatcher()
    dispatcher.processPostCreated(synthesis_post_1.id)

    notification_count = test_session.query(Notification).count()
    assert notification_count == initial_notification_count + 1, "The synthesis post should have matched and created a notification"
Пример #11
0
def test_notification_follow_synthesis(test_session, discussion,
                                       participant1_user, reply_post_2,
                                       test_app, root_post_1,
                                       synthesis_post_1):
    test_session.flush()
    subscription = NotificationSubscriptionFollowSyntheses(
        discussion=discussion,
        user=participant1_user,
        creation_origin=NotificationCreationOrigin.USER_REQUESTED
    )
    test_session.add(subscription)

    initial_notification_count = test_session.query(Notification).count()
    dispatcher = ModelEventWatcherNotificationSubscriptionDispatcher()
    dispatcher.processPostCreated(reply_post_2.id)
    notification_count = test_session.query(Notification).count()
    assert notification_count == initial_notification_count, "The post wasn't a synthesis and shouldn't have been caught"
    initial_last_status_change_date = subscription.last_status_change_date
    subscription.status = NotificationSubscriptionStatus.UNSUBSCRIBED
    assert subscription.last_status_change_date > initial_last_status_change_date, "The last status change date should have auto-updated"
    dispatcher.processPostCreated(synthesis_post_1.id)
    notification_count = test_session.query(Notification).count()
    assert notification_count == initial_notification_count, "The synthesis shouldn't have created a notification, because the subscription is unsubscribed"
    subscription.status = NotificationSubscriptionStatus.ACTIVE
    dispatcher.processPostCreated(synthesis_post_1.id)
    notification_count = test_session.query(Notification).count()
    assert notification_count == initial_notification_count + 1, "The synthesis post should have matched and created a notification"
Пример #12
0
def test_notification_follow_all_messages(test_session, discussion,
                                          participant1_user, reply_post_2,
                                          test_app, root_post_1,
                                          synthesis_post_1,
                                          discussion2_root_post_1):
    test_session.flush()
    subscription = NotificationSubscriptionFollowAllMessages(
        discussion=discussion,
        user=participant1_user,
        creation_origin=NotificationCreationOrigin.USER_REQUESTED,
    )
    test_session.add(subscription)

    initial_notification_count = test_session.query(Notification).count()
    dispatcher = ModelEventWatcherNotificationSubscriptionDispatcher()
    dispatcher.processPostCreated(reply_post_2.id)
    notification_count = test_session.query(Notification).count()
    assert notification_count == initial_notification_count + 1, "A new post should have been caught"

    # Check that subclasses are still caught
    dispatcher.processPostCreated(synthesis_post_1.id)
    notification_count = test_session.query(Notification).count()
    assert notification_count == initial_notification_count + 2, "A post with a subclass should have been caught"

    # Smoke test that other discussion's post do not leak
    dispatcher.processPostCreated(discussion2_root_post_1.id)
    notification_count = test_session.query(Notification).count()
    assert notification_count == initial_notification_count + 2, "A post from another discussion should NOT have been caught"
Пример #13
0
def test_notification_follow_direct_replies(test_session, discussion,
                                            participant1_user,
                                            participant2_user, root_post_1,
                                            reply_post_1, reply_post_2,
                                            test_app):
    # Note:
    # the author of root_post_1 is participant1_user
    # the author of reply_post_1 is participant2_user
    # the author of reply_post_2 is participant1_user
    # the author of reply_post_3 is participant2_user
    test_session.flush()
    subscription = NotificationSubscriptionFollowOwnMessageDirectReplies(
        discussion=discussion,
        user=participant1_user,
        creation_origin=NotificationCreationOrigin.USER_REQUESTED,
    )
    test_session.add(subscription)

    initial_notification_count = test_session.query(Notification).count()
    dispatcher = ModelEventWatcherNotificationSubscriptionDispatcher()

    dispatcher.processPostCreated(reply_post_1.id)
    notification_count = test_session.query(Notification).count()
    assert notification_count == initial_notification_count + 1, "Direct reply should have created a notification"

    dispatcher.processPostCreated(reply_post_2.id)
    notification_count = test_session.query(Notification).count()
    assert notification_count == initial_notification_count + 1, "Indirect reply should not have created a new notification"
Пример #14
0
def test_users_not_subscribed_to_discussion(test_session, discussion,
                                            participant1_user, reply_post_2,
                                            test_app, root_post_1,
                                            synthesis_post_1,
                                            discussion2_root_post_1,
                                            test_webrequest):
    test_session.flush()
    subscription = NotificationSubscriptionFollowAllMessages(
        discussion=discussion,
        user=participant1_user,
        creation_origin=NotificationCreationOrigin.USER_REQUESTED,
    )
    test_session.add(subscription)

    initial_notification_count = test_session.query(Notification).count()
    dispatcher = ModelEventWatcherNotificationSubscriptionDispatcher()
    dispatcher.processPostCreated(reply_post_2.id)
    notification_count = test_session.query(Notification).count()
    assert notification_count == initial_notification_count + 1, "A new post should have been caught"

    # Smoke test that unsubscribing from a discussion
    # does inhibit notifications
    participant1_user.unsubscribe(discussion)
    test_session.commit()
    dispatcher.processPostCreated(reply_post_2.id)
    notification_count = test_session.query(Notification).count()
    assert notification_count == initial_notification_count + 1, "The user should NOT receive notification if not subscribed to the discussion"
    participant1_user.subscribe(discussion)
Пример #15
0
def test_notification_multiple_subscriptions_create_single_notification(
        test_session, discussion, participant1_user, reply_post_2, test_app,
        root_post_1, synthesis_post_1):
    test_session.flush()
    subscription = NotificationSubscriptionFollowAllMessages(
        discussion=discussion,
        user=participant1_user,
        creation_origin=NotificationCreationOrigin.USER_REQUESTED,
    )
    test_session.add(subscription)
    subscription2 = NotificationSubscriptionFollowSyntheses(
        discussion=discussion,
        user=participant1_user,
        creation_origin=NotificationCreationOrigin.USER_REQUESTED,
    )
    test_session.add(subscription2)

    initial_notification_count = test_session.query(Notification).count()
    dispatcher = ModelEventWatcherNotificationSubscriptionDispatcher()
    dispatcher.processPostCreated(synthesis_post_1.id)
    notification_count = test_session.query(Notification).count()
    assert notification_count == initial_notification_count + 1
Пример #16
0
def test_notification_multiple_subscriptions_create_single_notification(
    test_session, discussion, participant1_user, reply_post_2, test_app,
        root_post_1, synthesis_post_1):
    test_session.flush()
    subscription = NotificationSubscriptionFollowAllMessages(
        discussion=discussion,
        user=participant1_user,
        creation_origin=NotificationCreationOrigin.USER_REQUESTED,
    )
    test_session.add(subscription)
    subscription2 = NotificationSubscriptionFollowSyntheses(
        discussion=discussion,
        user=participant1_user,
        creation_origin=NotificationCreationOrigin.USER_REQUESTED,
    )
    test_session.add(subscription2)

    initial_notification_count = test_session.query(Notification).count()
    dispatcher = ModelEventWatcherNotificationSubscriptionDispatcher()
    dispatcher.processPostCreated(synthesis_post_1.id)
    notification_count = test_session.query(Notification).count()
    assert notification_count == initial_notification_count + 1
Пример #17
0
def create_dispatcher():
    from assembl.models.notification import (
        ModelEventWatcherNotificationSubscriptionDispatcher)
    global _dispatcher
    _dispatcher = ModelEventWatcherNotificationSubscriptionDispatcher()
Пример #18
0
def processPostCreatedTask(id):
    from assembl.models.notification import (
        ModelEventWatcherNotificationSubscriptionDispatcher)
    ModelEventWatcherNotificationSubscriptionDispatcher.createNotifications(id)