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}" ) )
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)
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}")
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)
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)
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}")
def test_non_posters_notified(group): p = UserFactory() u = UserFactory() c = ChallengeFactory() c.add_admin(user=p) add_method = getattr(c, f"add_{group}") add_method(user=u) TopicFactory(forum=c.forum, poster=p, type=Topic.TOPIC_ANNOUNCE) assert u.user_profile.has_unread_notifications is True assert p.user_profile.has_unread_notifications is False
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
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}")
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)