コード例 #1
0
def test_notification_sent_on_new_post(kind):
    u1 = UserFactory()
    u2 = UserFactory()
    f = ForumFactory(type=Forum.FORUM_POST)
    follow(user=u2, obj=f)
    t = TopicFactory(forum=f, poster=u1, type=kind)
    PostFactory(topic=t, poster=u2)

    notifications = Notification.objects.all()
    topic_string = format_html('<a href="{}">{}</a>', t.get_absolute_url(), t)
    forum_string = format_html('<a href="{}">{}</a>', f.get_absolute_url(), f)
    assert len(notifications) == 2
    assert (
        notifications[1]
        .print_notification(user=u1)
        .startswith(f"{user_profile_link(u2)} replied to {topic_string}")
    )

    if kind == Topic.TOPIC_ANNOUNCE:
        assert (
            notifications[0]
            .print_notification(user=u2)
            .startswith(
                f"{user_profile_link(t.poster)} announced {topic_string} in {forum_string}"
            )
        )
    else:
        assert (
            notifications[0]
            .print_notification(user=u2)
            .startswith(
                f"{user_profile_link(t.poster)} posted {topic_string} in {forum_string}"
            )
        )
コード例 #2
0
def test_follow_clean_up_after_forum_removal():
    u = UserFactory()
    f1 = ForumFactory(type=Forum.FORUM_POST)
    f2 = ForumFactory(type=Forum.FORUM_POST)
    follow(u, f1, send_action=False)
    follow(u, f2, send_action=False)

    f1.delete()

    assert not is_following(u, f1)
コード例 #3
0
def test_follow_if_post_in_topic():
    u = UserFactory()
    f = ForumFactory(type=Forum.FORUM_POST)
    t = TopicFactory(forum=f, type=Topic.TOPIC_POST)
    PostFactory(topic=t, poster=u)

    assert Follow.objects.is_following(user=u, instance=t)
コード例 #4
0
def test_follow_view_permissions(client):
    user1 = UserFactory()
    user2 = UserFactory()
    f = ForumFactory(type=Forum.FORUM_POST)
    follow(user1, f)

    response = get_view_for_user(
        viewname="notifications:follow-list",
        client=client,
        method=client.get,
        user=user2,
    )
    assert response.status_code == 200
    assert (str(Follow.objects.get().follow_object)
            not in response.rendered_content)

    # user1 cannot see user2 notifications
    response = get_view_for_user(
        viewname="notifications:follow-list",
        client=client,
        method=client.get,
        user=user1,
    )
    assert response.status_code == 200
    assert str(Follow.objects.get().follow_object) in response.rendered_content
コード例 #5
0
def test_follow_deletion_through_api(client):
    user1 = UserFactory()
    user2 = UserFactory()
    f = ForumFactory(type=Forum.FORUM_POST)
    follow(user1, f)

    assert len(Follow.objects.all()) == 1

    # users can only delete their own follows
    response = get_view_for_user(
        viewname="api:follow-detail",
        client=client,
        method=client.delete,
        reverse_kwargs={"pk": Follow.objects.get().pk},
        content_type="application/json",
        user=user2,
    )
    assert response.status_code == 404

    response = get_view_for_user(
        viewname="api:follow-detail",
        client=client,
        method=client.delete,
        reverse_kwargs={"pk": Follow.objects.get().pk},
        content_type="application/json",
        user=user1,
    )
    assert response.status_code == 204
    assert len(Follow.objects.all()) == 0
コード例 #6
0
def test_user_follow_clean_up(client):
    user = UserFactory()
    # create a forum that the user follows
    f = ForumFactory(type=Forum.FORUM_POST)
    follow(user, f)
    assert Follow.objects.count() == 1

    # delete user and check that follow is deleted as well
    user.delete()
    assert Follow.objects.count() == 0
コード例 #7
0
def test_follow_clean_up_after_topic_removal():
    u = UserFactory()
    f = ForumFactory(type=Forum.FORUM_POST)
    t1 = TopicFactory(forum=f, type=Topic.TOPIC_POST)
    t2 = TopicFactory(forum=f, type=Topic.TOPIC_POST)
    follow(u, t1, send_action=False)
    follow(u, t2, send_action=False)

    t1.delete()

    assert not is_following(u, t1)
コード例 #8
0
def test_follow_clean_up_after_post_removal():
    u = UserFactory()
    f = ForumFactory(type=Forum.FORUM_POST)
    t = TopicFactory(forum=f, type=Topic.TOPIC_POST)
    p1 = PostFactory(topic=t, poster=u)
    p2 = PostFactory(topic=t, poster=u)

    follow(u, p1)
    follow(u, p2)

    p1.delete()

    assert not is_following(u, p1)
コード例 #9
0
def test_action_created_on_new_topic(kind):
    p = UserFactory()
    f = ForumFactory(type=Forum.FORUM_POST)
    t = TopicFactory(forum=f, poster=p, type=kind)

    assert Follow.objects.is_following(user=p, instance=t)

    action = Action.objects.get()

    if kind == Topic.TOPIC_ANNOUNCE:
        assert str(action).startswith(f"{p} announced {t} on {f}")
    else:
        assert str(action).startswith(f"{p} posted {t} on {f}")
コード例 #10
0
def test_notification_created_for_target_followers_on_action_creation():
    user1 = UserFactory()
    user2 = UserFactory()
    f = ForumFactory(type=Forum.FORUM_POST)
    follow(user1, f, send_action=False)
    follow(user2, f, send_action=False)

    # creating a post creates an action automatically
    _ = TopicFactory(forum=f, poster=user1, type=Topic.TOPIC_POST)
    assert len(Notification.objects.all()) == 1

    notification = Notification.objects.get()
    # check that the poster did not receive a notification
    assert notification.user == user2
    assert notification.user != user1
コード例 #11
0
def test_notification_sent_on_new_topic(kind):
    p = UserFactory()
    u = UserFactory()
    f = ForumFactory(type=Forum.FORUM_POST)
    follow(user=u, obj=f)
    t = TopicFactory(forum=f, poster=p, type=kind)

    notification = Notification.objects.get()
    topic_string = format_html('<a href="{}">{}</a>', t.get_absolute_url(), t)
    if kind == Topic.TOPIC_ANNOUNCE:
        assert notification.print_notification(user=u).startswith(
            f"{user_profile_link(p)} announced {topic_string}")
    else:
        assert notification.print_notification(
            user=u).startswith(f"{user_profile_link(p)} posted {topic_string}")
コード例 #12
0
def test_action_created_on_new_post(kind):
    u = UserFactory()
    f = ForumFactory(type=Forum.FORUM_POST)
    t = TopicFactory(forum=f, type=kind)
    PostFactory(topic=t, poster=u)

    actions = Action.objects.all()

    assert len(actions) == 2
    assert str(actions[0]).startswith(f"{u} replied to {t}")

    if kind == Topic.TOPIC_ANNOUNCE:
        assert str(actions[1]).startswith(f"{t.poster} announced {t} on {f}")
    else:
        assert str(actions[1]).startswith(f"{t.poster} posted {t} on {f}")
コード例 #13
0
def test_topic_subscribe_and_unsubscribe(client):
    user1 = UserFactory()
    user2 = UserFactory()
    f = ForumFactory(type=Forum.FORUM_POST)
    UserForumPermission.objects.create(
        permission=ForumPermission.objects.filter(
            codename="can_read_forum").get(),
        user=user1,
        forum=f,
        has_perm=True,
    )
    t = TopicFactory(forum=f, poster=user2, type=Topic.TOPIC_POST)

    assert not is_following(user1, t)

    response = get_view_for_user(
        viewname="notifications:follow-create",
        client=client,
        method=client.post,
        data={
            "user":
            user1.id,
            "content_type":
            ContentType.objects.get(
                app_label=t._meta.app_label,
                model=t._meta.model_name,
            ).id,
            "object_id":
            t.id,
            "actor_only":
            False,
        },
        user=user1,
    )
    assert response.status_code == 302
    assert is_following(user1, t)

    response = get_view_for_user(
        viewname="notifications:follow-delete",
        client=client,
        method=client.post,
        reverse_kwargs={"pk": Follow.objects.filter(user=user1).first().id},
        user=user1,
    )
    assert response.status_code == 302
    assert not is_following(user1, t)
コード例 #14
0
def test_follow_list_view_num_queries():
    user1 = UserFactory()
    f = ForumFactory(type=Forum.FORUM_POST)
    follow(user=user1, obj=f)

    follows = Follow.objects.select_related("user", "content_type").all()
    follows_with_prefetched_gfks = prefetch_generic_foreign_key_objects(
        Follow.objects.select_related("user", "content_type").all())

    try:
        settings.DEBUG = True
        follows[0].follow_object
        assert len(connection.queries) == 2
        reset_queries()
        follows_with_prefetched_gfks[0].follow_object
        assert len(connection.queries) == 0
    finally:
        settings.DEBUG = False
        reset_queries()
コード例 #15
0
def test_follow_delete_permission(client):
    user1 = UserFactory()
    user2 = UserFactory()
    f = ForumFactory(type=Forum.FORUM_POST)
    follow(user1, f)

    response = get_view_for_user(
        viewname="notifications:follow-delete",
        client=client,
        method=client.post,
        reverse_kwargs={"pk": Follow.objects.get().id},
        user=user2,
    )
    assert response.status_code == 403

    response = get_view_for_user(
        viewname="notifications:follow-delete",
        client=client,
        method=client.post,
        reverse_kwargs={"pk": Follow.objects.get().id},
        user=user1,
    )
    assert response.status_code == 302
コード例 #16
0
def test_follow_create_permission(client):
    user1 = UserFactory()
    user2 = UserFactory()
    f = ForumFactory(type=Forum.FORUM_POST)

    # wrong user
    _ = get_view_for_user(
        viewname="notifications:follow-create",
        client=client,
        method=client.post,
        data={
            "user":
            user1.id,
            "content_type":
            ContentType.objects.get(app_label=f._meta.app_label,
                                    model=f._meta.model_name).id,
            "object_id":
            f.id,
            "actor_only":
            False,
        },
        user=user2,
    )
    assert len(Follow.objects.all()) == 0

    # correct user, but does not have permission to subscribe
    _ = get_view_for_user(
        viewname="notifications:follow-create",
        client=client,
        method=client.post,
        data={
            "user":
            user1.id,
            "content_type":
            ContentType.objects.get(app_label=f._meta.app_label,
                                    model=f._meta.model_name).id,
            "object_id":
            f.id,
            "actor_only":
            False,
        },
        user=user1,
    )
    assert len(Follow.objects.all()) == 0

    UserForumPermission.objects.create(
        permission=ForumPermission.objects.filter(
            codename="can_read_forum").get(),
        user=user1,
        forum=f,
        has_perm=True,
    )

    response = get_view_for_user(
        viewname="notifications:follow-create",
        client=client,
        method=client.post,
        data={
            "user":
            user1.id,
            "content_type":
            ContentType.objects.get(app_label=f._meta.app_label,
                                    model=f._meta.model_name).id,
            "object_id":
            f.id,
            "actor_only":
            False,
        },
        user=user1,
    )
    assert response.status_code == 302
    assert is_following(user1, f)