Esempio n. 1
0
 def test_exception_raised_when_email_task_params_is_not_serializable(self):
     params = {'param1': set()}
     serialization_exception = self.assertRaisesRegex(
         ValueError,
         'The params added to the email task call cannot be json serialized'
     )
     with serialization_exception:
         taskqueue_services.enqueue_task(
             feconf.TASK_URL_FEEDBACK_MESSAGE_EMAILS, params, 0)
Esempio n. 2
0
def enqueue_feedback_message_batch_email_task(user_id):
    """Adds a 'send feedback email' (batch) task into the task queue.

    Args:
        user_id: str. The user to be notified.
    """
    taskqueue_services.enqueue_task(
        feconf.TASK_URL_FEEDBACK_MESSAGE_EMAILS, {'user_id': user_id},
        feconf.DEFAULT_FEEDBACK_MESSAGE_EMAIL_COUNTDOWN_SECS)
Esempio n. 3
0
 def test_exception_raised_when_email_task_params_is_not_serializable(
         self) -> None:
     params: Dict[str, Set[str]] = {'param1': set()}
     serialization_exception = self.assertRaisesRegex(  # type: ignore[no-untyped-call]
         ValueError,
         'The params added to the email task call cannot be json serialized'
     )
     with serialization_exception:
         taskqueue_services.enqueue_task(
             feconf.TASK_URL_FEEDBACK_MESSAGE_EMAILS, params, 0)
Esempio n. 4
0
def enqueue_feedback_message_instant_email_task(user_id, reference):
    """Adds a 'send feedback email' (instant) task into the task queue.

    Args:
        user_id: str. The user to be notified.
        reference: FeedbackMessageReference. A reference that contains the data
            needed to identify the feedback message.
    """
    payload = {'user_id': user_id, 'reference_dict': reference.to_dict()}
    taskqueue_services.enqueue_task(feconf.TASK_URL_INSTANT_FEEDBACK_EMAILS,
                                    payload, 0)
Esempio n. 5
0
def enqueue_flag_exploration_email_task(exploration_id, report_text,
                                        reporter_id):
    """Adds a 'send flagged exploration email' task into taskqueue."""
    payload = {
        'exploration_id': exploration_id,
        'report_text': report_text,
        'reporter_id': reporter_id,
    }

    # Emails about flagged explorations are sent immediately to moderators.
    taskqueue_services.enqueue_task(feconf.TASK_URL_FLAG_EXPLORATION_EMAILS,
                                    payload, 0)
Esempio n. 6
0
    def test_email_sent_to_moderator_after_flag(self):
        """Tests Flagged Exploration Email Handler."""

        def fake_get_user_ids_by_role(_):
            """Replaces get_user_ids_by_role for testing purposes."""
            return [self.moderator_id]
        get_moderator_id_as_list = self.swap(
            user_services, 'get_user_ids_by_role',
            fake_get_user_ids_by_role)

        with self.can_send_feedback_email_ctx, self.can_send_emails_ctx:
            with get_moderator_id_as_list:

                # Create thread.
                feedback_services.create_thread(
                    feconf.ENTITY_TYPE_EXPLORATION, self.exploration.id,
                    self.user_id_a, 'bad subject', 'bad text')

                # User B reports thread, sends email.
                payload = {
                    'exploration_id': self.exploration.id,
                    'report_text': 'He said a bad word :-( ',
                    'reporter_id': self.user_id_b}
                taskqueue_services.enqueue_task(
                    feconf.TASK_URL_FLAG_EXPLORATION_EMAILS,
                    payload, 0)
                # Ensure moderator has no messages sent to him yet.
                messages = self._get_sent_email_messages(
                    self.MODERATOR_EMAIL)
                self.assertEqual(len(messages), 0)

                # Invoke Flag Exploration Email Handler.
                self.process_and_flush_pending_tasks()

                # Ensure moderator has 1 email now.
                messages = self._get_sent_email_messages(
                    self.MODERATOR_EMAIL)
                self.assertEqual(len(messages), 1)

                # Ensure moderator has received correct email.
                expected_message = (
                    'Hello Moderator,\nuserB has flagged exploration "Title"'
                    ' on the following grounds: \nHe said a bad word :-( '
                    ' .\nYou can modify the exploration by clicking here'
                    '.\n\nThanks!\n- The Oppia Team\n\nYou can change your'
                    ' email preferences via the Preferences page.')
                self.assertEqual(messages[0].body, expected_message)
Esempio n. 7
0
def _enqueue_feedback_thread_status_change_email_task_transactional(
        user_id, reference, old_status, new_status):
    """Adds a task for sending email when a feedback thread status is changed.

    Args:
        user_id: str. The user to be notified.
        reference: FeedbackMessageReference. The feedback message reference
            object to be converted to dict.
        old_status: str. One of STATUS_CHOICES.
        new_status: str. One of STATUS_CHOICES.
    """
    payload = {
        'user_id': user_id,
        'reference_dict': reference.to_dict(),
        'old_status': old_status,
        'new_status': new_status
    }
    taskqueue_services.enqueue_task(feconf.TASK_URL_FEEDBACK_STATUS_EMAILS,
                                    payload, 0)
Esempio n. 8
0
    def test_enqueue_task_makes_the_correct_request(self):
        correct_payload = {'user_id': '1'}
        correct_url = feconf.TASK_URL_FEEDBACK_MESSAGE_EMAILS
        correct_queue_name = taskqueue_services.QUEUE_NAME_EMAILS

        def mock_create_http_task(queue_name,
                                  url,
                                  payload=None,
                                  scheduled_for=None,
                                  task_name=None):
            self.assertEqual(queue_name, correct_queue_name)
            self.assertEqual(url, correct_url)
            self.assertEqual(payload, correct_payload)
            self.assertIsNotNone(scheduled_for)
            self.assertIsNone(task_name)

        swap_create_http_task = self.swap(
            taskqueue_services.platform_taskqueue_services, 'create_http_task',
            mock_create_http_task)

        with swap_create_http_task:
            taskqueue_services.enqueue_task(correct_url, correct_payload, 0)
Esempio n. 9
0
    def test_enqueue_task_makes_the_correct_request(self) -> None:
        correct_payload = {'user_id': '1'}
        correct_url = feconf.TASK_URL_FEEDBACK_MESSAGE_EMAILS
        correct_queue_name = taskqueue_services.QUEUE_NAME_EMAILS

        def mock_create_http_task(queue_name: str,
                                  url: str,
                                  payload: Optional[Dict[str, Any]] = None,
                                  scheduled_for: Optional[
                                      datetime.datetime] = None,
                                  task_name: Optional[str] = None) -> None:
            self.assertEqual(queue_name, correct_queue_name)
            self.assertEqual(url, correct_url)
            self.assertEqual(payload, correct_payload)
            self.assertIsNotNone(scheduled_for)
            self.assertIsNone(task_name)

        swap_create_http_task = self.swap(platform_taskqueue_services,
                                          'create_http_task',
                                          mock_create_http_task)

        with swap_create_http_task:
            taskqueue_services.enqueue_task(correct_url, correct_payload, 0)
Esempio n. 10
0
    def test_email_is_sent_when_suggestion_created(self):
        """Tests SuggestionEmailHandler functionality."""

        user_id_b = self.user_id_b

        class MockActivityRights:
            def __init__(self,
                         exploration_id,
                         owner_ids,
                         editor_ids,
                         voice_artist_ids,
                         viewer_ids,
                         community_owned=False,
                         cloned_from=None,
                         status=True,
                         viewable_if_private=False,
                         first_published_msec=None):
                # User B ID hardcoded into owner_ids to get email_manager
                # to send email to user B to test functionality.
                self.id = exploration_id
                self.getLintToShutUp = owner_ids
                self.editor_ids = editor_ids
                self.voice_artist_ids = voice_artist_ids
                self.viewer_ids = viewer_ids
                self.community_owned = community_owned
                self.cloned_from = cloned_from
                self.status = status
                self.viewable_if_private = viewable_if_private
                self.first_published_msec = first_published_msec
                self.owner_ids = [user_id_b]

        email_user_b = self.swap(rights_domain, 'ActivityRights',
                                 MockActivityRights)
        with email_user_b, self.can_send_feedback_email_ctx:
            with self.can_send_emails_ctx:
                change = {
                    'cmd': exp_domain.CMD_EDIT_STATE_PROPERTY,
                    'property_name': exp_domain.STATE_PROPERTY_CONTENT,
                    'state_name': 'state_1',
                    'new_value': 'new suggestion content'
                }

                # Create suggestion from user A to user B.
                suggestion_services.create_suggestion(
                    feconf.SUGGESTION_TYPE_EDIT_STATE_CONTENT,
                    feconf.ENTITY_TYPE_EXPLORATION, self.exploration.id, 1,
                    self.user_id_a, change, 'test description')
                threadlist = feedback_services.get_all_threads(
                    feconf.ENTITY_TYPE_EXPLORATION, self.exploration.id, True)
                thread_id = threadlist[0].id

                # Enqueue and send suggestion email task.
                payload = {
                    'exploration_id': self.exploration.id,
                    'thread_id': thread_id
                }
                messages = self._get_all_sent_email_messages()
                self.assertEqual(len(messages), 0)
                taskqueue_services.enqueue_task(
                    feconf.TASK_URL_SUGGESTION_EMAILS, payload, 0)
                self.process_and_flush_pending_tasks()

                # Check that user B received message.
                messages = self._get_sent_email_messages(self.USER_B_EMAIL)
                self.assertEqual(len(messages), 1)

                # Check that user B received correct message.
                expected_message = (
                    'Hi userB,\nuserA has submitted a new suggestion'
                    ' for your Oppia exploration, "Title".\nYou can'
                    ' accept or reject this suggestion by visiting'
                    ' the feedback page for your exploration.\n\nTha'
                    'nks!\n- The Oppia Team\n\nYou can change your'
                    ' email preferences via the Preferences page.')
                self.assertEqual(messages[0].body, expected_message)