def test_send_comment_notifications(post_id, comment_id): """Tests that send_comment_notifications works correctly""" comment_users = UserFactory.create_batch(5) for user in comment_users: NotificationSettingsFactory.create(user=user, comments_type=True) # create both so we cover the notifiication deduplication SubscriptionFactory.create(user=user, post_id=post_id, comment_id=comment_id) SubscriptionFactory.create(user=user, post_id=post_id, comment_id=None) post_users = UserFactory.create_batch(5) for user in post_users: NotificationSettingsFactory.create(user=user, comments_type=True) SubscriptionFactory.create(user=user, post_id=post_id, comment_id=None) # create just a subscription for a user so we can test no settings scenario SubscriptionFactory.create(post_id=post_id, comment_id=comment_id) # create a bunch on other subscriptions SubscriptionFactory.create_batch(10) api.send_comment_notifications(post_id, comment_id, "abc") users = post_users + comment_users assert CommentEvent.objects.count() == len(users) for event in CommentEvent.objects.all(): assert event.post_id == post_id assert event.comment_id == "abc" assert event.user in users
def channels_and_users(): """Channels and users for testing""" return ( [ ChannelFactory.create(name=channel_name) for channel_name in ["a", "b", "c"] ], UserFactory.create_batch(4), )
def test_prefetched_iterator(chunk_size): """ prefetched_iterator should yield all items in the record set across chunk boundaries """ users = UserFactory.create_batch(10) fetched_users = list( prefetched_iterator(User.objects.all(), chunk_size=chunk_size)) assert len(users) == len(fetched_users) for user in users: assert user in fetched_users
def test_update_moira_list_users(mock_moira_client): """Test that update_moira_list_users updates the moira lists' users""" moira_users = UserFactory.create_batch(3) moira_list = MoiraListFactory.create(users=[moira_users[2]]) assert list(moira_list.users.all()) == [moira_users[2]] mock_moira_client.return_value.list_members.return_value = [ user.email for user in moira_users[:2] ] update_moira_list_users(moira_list) assert list(moira_list.users.order_by("id")) == sorted( moira_users[:2], key=lambda user: user.id)
def test_serialize_bulk_profiles(mocker): """ Test that serialize_bulk_profiles calls serialize_profile_for_bulk for every existing profile """ mock_serialize_profile = mocker.patch( "search.serializers.serialize_profile_for_bulk" ) users = UserFactory.create_batch(5) list(serialize_bulk_profiles([profile.id for profile in Profile.objects.all()])) for user in users: mock_serialize_profile.assert_any_call(user.profile)
def test_list_subscribers(staff_client, staff_api, public_channel): """ The correct list of subscriber usernames is returned. """ users = UserFactory.create_batch(2) for user in users: staff_api.add_subscriber(user.username, public_channel.name) url = reverse("subscriber-list", kwargs={"channel_name": public_channel.name}) resp = staff_client.get(url) assert resp.status_code == status.HTTP_200_OK for user in users: assert {"subscriber_name": user.username} in resp.json()
def test_subscribe_user_range_to_channels(mocker): """Test that the main task batches out smaller tasks correctly""" mock_add_subscriber = mocker.patch("channels.api.Api.add_subscriber") mocker.patch("channels.api._get_client", autospec=True) usernames = [user.username for user in UserFactory.create_batch(5)] channel_names = ["nochannel_1", "nochannel_2"] tasks.subscribe_user_range_to_channels.delay(usernames=usernames, channel_names=channel_names) assert mock_add_subscriber.call_count == (len(usernames) * len(channel_names)) for username in usernames: for channel_name in channel_names: mock_add_subscriber.assert_any_call(username, channel_name)
def test_send_message(mailoutbox): """Tests that send_messages works as expected""" users = UserFactory.create_batch(5) messages = list( messages_for_recipients( [( recipient, context_for_user(user=user, extra_context={"url": "https://example.com"}), ) for recipient, user in safe_format_recipients(users)], "sample", )) send_messages(messages) for message in mailoutbox: assert message in messages
def test_messages_for_recipients(): """Tests that messages_for_recipients works as expected""" users = UserFactory.create_batch(5) messages = list( messages_for_recipients( [( recipient, context_for_user(user=user, extra_context={"url": "https://example.com"}), ) for recipient, user in safe_format_recipients(users)], "sample", )) assert len(messages) == len(users) for user, msg in zip(users, messages): assert user.email in str(msg.to[0]) assert msg.subject == "Welcome {}".format(user.profile.name)
def test_send_message_failure(mocker): """Tests that send_messages logs all exceptions""" sendmail = mocker.patch("mail.api.AnymailMessage.send", side_effect=ConnectionError) patched_logger = mocker.patch("mail.api.log") users = UserFactory.create_batch(2) messages = list( messages_for_recipients( [( recipient, context_for_user(user=user, extra_context={"url": "https://example.com"}), ) for recipient, user in safe_format_recipients(users)], "sample", )) send_messages(messages) assert sendmail.call_count == len(users) assert patched_logger.exception.call_count == len(users)
def test_subscribe_all_users_to_channels(settings, mocker, mocked_celery): """Test that the main task batches out smaller tasks correctly""" settings.OPEN_DISCUSSIONS_DEFAULT_CHANNEL_BACKPOPULATE_BATCH_SIZE = 2 users = sorted(UserFactory.create_batch(4), key=lambda user: user.username) channel_names = ["nochannel_1", "nochannel_2"] mock_subscribe_user_range_to_channels = mocker.patch( "channels.tasks.subscribe_user_range_to_channels") with pytest.raises(mocked_celery.replace_exception_class): tasks.subscribe_all_users_to_channels.delay( channel_names=channel_names) assert mocked_celery.group.call_count == 1 list(mocked_celery.group.call_args[0][0]) mock_subscribe_user_range_to_channels.si.assert_any_call( usernames=[users[0].username, users[1].username], channel_names=channel_names) mock_subscribe_user_range_to_channels.si.assert_any_call( usernames=[users[2].username, users[3].username], channel_names=channel_names)