Esempio n. 1
0
    def test_notify_on_publish(self, community, send_webpush_mock):
        # owner should not receive any notifications from their own posts
        owner = MembershipFactory(
            community=community, role=Membership.Role.MODERATOR
        ).member

        mentioned = MembershipFactory(
            member=UserFactory(username="******"),
            community=community,
            role=Membership.Role.MEMBER,
        ).member

        # ensure we just have one notification for multiple tags

        movies = Tag.objects.create(name="movies")
        reviews = Tag.objects.create(name="reviews")

        post = PostFactory(
            owner=owner,
            community=community,
            description="hello @danjac from @owner #movies #reviews",
        )

        tag_follower = MembershipFactory(
            community=community,
            member=UserFactory(),
        ).member
        tag_follower.following_tags.add(movies, reviews)

        # owner should also follow tags to ensure they aren't notified
        owner.following_tags.add(movies, reviews)

        assert tag_follower.following_tags.count() == 2

        user_follower = MembershipFactory(
            community=community,
            member=UserFactory(),
        ).member
        user_follower.following.add(post.owner)

        notifications = post.notify_on_publish()
        assert len(notifications) == 3

        assert notifications[0].recipient == mentioned
        assert notifications[0].actor == post.owner
        assert notifications[0].verb == "mention"

        assert notifications[1].recipient == tag_follower
        assert notifications[1].actor == post.owner
        assert notifications[1].verb == "followed_tag"

        assert notifications[2].recipient == user_follower
        assert notifications[2].actor == post.owner
        assert notifications[2].verb == "followed_user"
Esempio n. 2
0
    def test_num_reshares(self, post):

        for _ in range(3):
            member = MembershipFactory(community=post.community)
            post.reshare(member.member)

        # non-member

        post.reshare(UserFactory())

        post = (
            Post.objects.with_num_reshares(UserFactory(), post.community)
            .filter(is_reshare=False)
            .first()
        )
        assert post.num_reshares == 3
Esempio n. 3
0
    def test_notify_on_update_moderator_edit(self, community, send_webpush_mock):

        owner = MembershipFactory(community=community).member

        member = MembershipFactory(
            community=community,
            member=UserFactory(username="******"),
        ).member

        moderator = MembershipFactory(
            community=community,
        ).member

        post = PostFactory(
            owner=owner,
            community=community,
            description="hello from @owner #movies #reviews",
        )

        post.description = "hello @danjac"
        post.editor = moderator
        post.save()

        notifications = post.notify_on_update()
        assert len(notifications) == 2

        assert notifications[0].recipient == member
        assert notifications[0].actor == post.owner
        assert notifications[0].verb == "mention"

        assert notifications[1].recipient == post.owner
        assert notifications[1].actor == moderator
        assert notifications[1].verb == "edit"
Esempio n. 4
0
    def test_notify_on_update(self, community, mailoutbox, send_webpush_mock):

        comment_owner = MembershipFactory(
            community=community,
        ).member

        member = MembershipFactory(
            community=community, member=UserFactory(username="******")
        ).member

        post_owner = MembershipFactory(community=community).member
        post = PostFactory(owner=post_owner, community=community)

        comment = CommentFactory(
            owner=comment_owner,
            community=community,
            content_object=post,
            content="hello",
        )

        comment.content = "hello @danjac"
        comment.save()

        notifications = list(comment.notify_on_update())
        assert len(notifications) == 1

        assert notifications[0].recipient == member
        assert notifications[0].actor == comment_owner
        assert notifications[0].verb == "mention"
Esempio n. 5
0
    def test_notify_on_create_if_no_content_object(self, community):

        comment_owner = MembershipFactory(community=community).member
        post_owner = MembershipFactory(
            community=community,
        ).member

        mentioned = UserFactory(username="******")

        MembershipFactory(member=mentioned, community=community)

        post = PostFactory(
            owner=post_owner, community=community, deleted=timezone.now()
        )

        CommentFactory(
            owner=MembershipFactory(community=community).member,
            content_object=post,
        )

        comment = CommentFactory(
            owner=comment_owner,
            community=community,
            content_object=post,
            content="hello @danjac",
        )

        follower = MembershipFactory(community=community).member
        follower.following.add(comment.owner)

        notifications = list(comment.notify_on_create())

        assert len(notifications) == 0
Esempio n. 6
0
 def test_bookmarked_if_user_has_not_bookmarked(self, message, user):
     BookmarkFactory(
         user=user,
         content_object=message,
         community=message.community,
     )
     assert Message.objects.bookmarked(UserFactory()).count() == 0
Esempio n. 7
0
 def test_bookmarked_if_user_has_not_bookmarked(self, comment, user):
     BookmarkFactory(
         user=user,
         content_object=comment,
         community=comment.community,
     )
     assert Comment.objects.bookmarked(UserFactory()).count() == 0
Esempio n. 8
0
    def test_notify_on_publish_reshare(self, community, send_webpush_mock):

        mentioned = MembershipFactory(
            member=UserFactory(username="******"),
            community=community,
            role=Membership.Role.MEMBER,
        ).member

        # ensure we just have one notification for multiple tags

        movies = Tag.objects.create(name="movies")
        reviews = Tag.objects.create(name="reviews")

        tag_follower = MembershipFactory(
            community=community,
            member=UserFactory(),
        ).member
        tag_follower.following_tags.add(movies, reviews)

        assert tag_follower.following_tags.count() == 2

        owner = MembershipFactory(
            community=community,
        ).member

        post = PostFactory(
            owner=owner,
            community=community,
            description="hello @danjac from @owner #movies #reviews",
        )

        # reshare
        reshare = post.reshare(UserFactory())
        notifications = list(reshare.notify_on_publish())
        assert len(notifications) == 3

        assert notifications[0].recipient == post.owner
        assert notifications[0].actor == reshare.owner
        assert notifications[0].verb == "reshare"

        assert notifications[1].recipient == mentioned
        assert notifications[1].actor == reshare.owner
        assert notifications[1].verb == "mention"

        assert notifications[2].recipient == tag_follower
        assert notifications[2].actor == reshare.owner
        assert notifications[2].verb == "followed_tag"
Esempio n. 9
0
 def test_liked_if_user_has_not_liked(self, comment, user):
     LikeFactory(
         user=user,
         content_object=comment,
         community=comment.community,
         recipient=comment.owner,
     )
     assert Comment.objects.liked(UserFactory()).count() == 0
Esempio n. 10
0
 def test_with_has_bookmarked_if_user_has_not_bookmarked(self, comment, user):
     BookmarkFactory(
         user=user,
         content_object=comment,
         community=comment.community,
     )
     comment = Comment.objects.with_has_bookmarked(UserFactory()).get()
     assert not comment.has_bookmarked
Esempio n. 11
0
 def test_with_has_bookmarked_if_user_has_not_bookmarked(self, post, user):
     BookmarkFactory(
         user=user,
         content_object=post,
         community=post.community,
     )
     activity = Post.objects.with_has_bookmarked(UserFactory()).get()
     assert not activity.has_bookmarked
Esempio n. 12
0
 def test_can_delete_membership_if_not_is_admin_and_is_other_membership(
         self, user, community):
     Membership.objects.create(member=user,
                               community=community,
                               role="member")
     membership = Membership.objects.create(member=UserFactory(),
                                            community=community,
                                            role="admin")
     assert not user.has_perm("communities.delete_membership", membership)
Esempio n. 13
0
 def test_with_has_liked_if_user_has_not_liked(self, comment, user):
     LikeFactory(
         user=user,
         content_object=comment,
         community=comment.community,
         recipient=comment.owner,
     )
     comment = Comment.objects.with_has_liked(UserFactory()).get()
     assert not comment.has_liked
Esempio n. 14
0
 def test_with_bookmarked_timestamp_if_user_has_not_bookmarked(self, comment, user):
     BookmarkFactory(
         user=user,
         content_object=comment,
         community=comment.community,
     )
     # test with *another* user
     comment = Comment.objects.with_bookmarked_timestamp(UserFactory()).first()
     assert comment.bookmarked is None
Esempio n. 15
0
 def test_with_has_bookmarked_if_user_has_not_bookmarked(
         self, message, user):
     BookmarkFactory(
         user=user,
         content_object=message,
         community=message.community,
     )
     message = Message.objects.with_has_bookmarked(UserFactory()).get()
     assert not message.has_bookmarked
Esempio n. 16
0
 def test_with_has_liked_if_user_has_not_liked(self, post, user):
     LikeFactory(
         user=user,
         content_object=post,
         community=post.community,
         recipient=post.owner,
     )
     activity = Post.objects.with_has_liked(UserFactory()).get()
     assert not activity.has_liked
Esempio n. 17
0
 def test_with_bookmarked_timestamp_if_user_has_not_bookmarked(
         self, message, user):
     BookmarkFactory(
         user=user,
         content_object=message,
         community=message.community,
     )
     # test with *another* user
     message = Message.objects.with_bookmarked_timestamp(
         UserFactory()).first()
     assert message.bookmarked is None
Esempio n. 18
0
    def test_reshare_a_reshare(self, post, user):

        reshared = post.reshare(user)
        reshared.reshare(UserFactory())

        assert reshared.title == post.title
        assert reshared.description == post.description
        assert reshared.is_reshare
        assert reshared.parent == post
        assert reshared.community == post.community
        assert reshared.owner == user
        assert reshared.published
Esempio n. 19
0
def login_user(client):
    password = "******"
    user = UserFactory()
    user.set_password(password)
    user.save()
    client.login(username=user.username, password=password)
    return user
Esempio n. 20
0
    def test_post(self, client, mailoutbox, login_user, community):

        admin = UserFactory()
        Membership.objects.create(community=community,
                                  member=admin,
                                  role=Membership.Role.ADMIN)
        response = client.post(reverse("join_requests:create"))
        assert response.url == settings.HOME_PAGE_URL
        join_request = JoinRequest.objects.get()
        assert join_request.sender == login_user
        assert join_request.community == community
        mail = mailoutbox[0]
        assert mail.to == [admin.email]
Esempio n. 21
0
 def test_clean_recipient_if_recipient_blocked(self, member):
     user = MembershipFactory(member=UserFactory(username="******"),
                              community=member.community).member
     member.member.blocked.add(user)
     form = MessageRecipientForm(
         {
             "message": "test",
             "recipient": "@danjac"
         },
         community=member.community,
         sender=member.member,
     )
     assert not form.is_valid()
Esempio n. 22
0
    def test_get_if_member(self, client, member):
        EventFactory(community=member.community, owner=member.member)
        PostFactory(community=member.community, owner=member.member)
        PostFactory(community=member.community, owner=member.member)
        poll = PollFactory(community=member.community, owner=member.member)

        for _ in range(3):
            answer = AnswerFactory(poll=poll)
            answer.voters.add(UserFactory())

        response = client.get(settings.HOME_PAGE_URL)
        assert response.status_code == 200
        assert len(response.context["object_list"]) == 4
Esempio n. 23
0
 def test_clean_recipient_if_recipient_ok(self, member):
     user = MembershipFactory(member=UserFactory(username="******"),
                              community=member.community).member
     form = MessageRecipientForm(
         {
             "message": "test",
             "recipient": "@danjac"
         },
         community=member.community,
         sender=member.member,
     )
     assert form.is_valid()
     assert form.cleaned_data["recipient"] == user
Esempio n. 24
0
    def test_between(self):

        user_a = UserFactory()
        user_b = UserFactory()

        first = MessageFactory(sender=user_a, recipient=user_b)
        second = MessageFactory(recipient=user_a, sender=user_b)
        third = MessageFactory()
        fourth = MessageFactory(sender=user_a)
        fifth = MessageFactory(sender=user_b)
        sixth = MessageFactory(recipient=user_a)
        seventh = MessageFactory(recipient=user_b)
        eighth = MessageFactory(recipient=user_a,
                                sender=user_b,
                                recipient_deleted=timezone.now())
        ninth = MessageFactory(sender=user_a,
                               recipient=user_b,
                               sender_deleted=timezone.now())
        tenth = MessageFactory(sender=user_a,
                               recipient=user_b,
                               recipient_deleted=timezone.now())
        eleventh = MessageFactory(recipient=user_a,
                                  sender=user_b,
                                  sender_deleted=timezone.now())

        messages = Message.objects.between(user_a, user_b)

        assert first in messages
        assert second in messages
        assert third not in messages
        assert fourth not in messages
        assert fifth not in messages
        assert sixth not in messages
        assert seventh not in messages
        assert eighth not in messages
        assert ninth not in messages
        assert tenth in messages
        assert eleventh in messages
Esempio n. 25
0
    def test_unreshared(self, post, user, anonymous_user):

        first = PostFactory()
        PostFactory()

        other = UserFactory()

        first.reshare(user)

        assert Post.objects.unreshared(other).count() == 4
        assert Post.objects.unreshared(anonymous_user).count() == 4

        posts = Post.objects.unreshared(user)
        # incl. reshared post
        assert posts.count() == 3
        assert first not in posts
Esempio n. 26
0
    def test_image_blocked_by_user(self):

        user = UserFactory(show_external_images=True)

        post = PostFactory(
            url="http://imgur.com/test",
            opengraph_description="test",
            opengraph_image="http://imgur.com/cat.png",
        )
        assert render_opengraph_content(user, post) == {
            "og_data": {
                "description": "test",
                "image": ""
            },
            "post": post,
        }
Esempio n. 27
0
    def test_notify_on_create(self, community, mailoutbox, send_webpush_mock):

        comment_owner = MembershipFactory(community=community).member
        post_owner = MembershipFactory(
            community=community,
        ).member

        mentioned = UserFactory(username="******")

        MembershipFactory(member=mentioned, community=community)

        post = PostFactory(owner=post_owner, community=community)

        other_comment = CommentFactory(
            owner=MembershipFactory(community=community).member,
            content_object=post,
        )

        comment = CommentFactory(
            owner=comment_owner,
            community=community,
            content_object=post,
            content="hello @danjac",
        )

        follower = MembershipFactory(community=community).member
        follower.following.add(comment.owner)

        notifications = list(comment.notify_on_create())

        assert len(notifications) == 4

        assert notifications[0].recipient == mentioned
        assert notifications[0].actor == comment.owner
        assert notifications[0].verb == "mention"

        assert notifications[1].recipient == post.owner
        assert notifications[1].actor == comment.owner
        assert notifications[1].verb == "new_comment"

        assert notifications[2].recipient == other_comment.owner
        assert notifications[2].actor == comment.owner
        assert notifications[2].verb == "new_sibling"

        assert notifications[3].recipient == follower
        assert notifications[3].actor == comment.owner
        assert notifications[3].verb == "followed_user"
Esempio n. 28
0
    def test_post_with_reshare(self, client, post_for_member,
                               send_webpush_mock):

        reshare = post_for_member.reshare(UserFactory())

        client.post(
            reverse("posts:update", args=[post_for_member.id]),
            {
                "title": "UPDATED",
                "description": post_for_member.description
            },
        )

        post_for_member.refresh_from_db()
        reshare.refresh_from_db()
        assert post_for_member.title == "UPDATED"
        assert reshare.title == "UPDATED"
Esempio n. 29
0
    def test_post(self, client, member, send_webpush_mock):
        owner = UserFactory()
        MembershipFactory(member=owner, community=member.community)
        post = PostFactory(community=member.community, owner=owner)
        response = client.post(
            reverse("posts:comment", args=[post.id]),
            {"content": "test"},
            HTTP_HOST=member.community.domain,
        )
        comment = Comment.objects.get()
        assert response.url == post.get_absolute_url()
        assert comment.owner == member.member
        assert comment.content_object == post

        notification = Notification.objects.get(recipient=post.owner,
                                                comment=comment)
        assert notification.verb == "new_comment"
Esempio n. 30
0
    def test_post(self, client, member, send_webpush_mock):

        post = PostFactory(
            community=member.community,
            owner=MembershipFactory(
                community=member.community,
                member=UserFactory(),
            ).member,
        )
        response = client.post(
            reverse("posts:reshare", args=[post.id]),
            HTTP_X_REQUESTED_WITH="XMLHttpRequest",
        )
        assert response.url == post.reshares.first().get_absolute_url()
        assert post.reshares.filter(owner=member.member).count() == 1
        assert (Notification.objects.filter(actor=member.member,
                                            recipient=post.owner,
                                            verb="reshare").count() == 1)