Exemple #1
0
def send_email_through(config: CFG,
                       sendmail_callable: typing.Callable[[Message], None],
                       message: Message) -> None:
    """
    Send mail encapsulation to send it in async or sync mode.

    TODO BS 20170126: A global mail/sender management should be a good
                      thing. Actually, this method is an fast solution.
    :param config: system configuration
    :param sendmail_callable: A callable who get message on first parameter
    :param message: The message who have to be sent
    """
    if config.EMAIL__PROCESSING_MODE == config.CST.SYNC:
        logger.info(send_email_through,
                    "send email to {} synchronously".format(message["To"]))
        sendmail_callable(message)
    elif config.EMAIL__PROCESSING_MODE == config.CST.ASYNC:
        logger.info(
            send_email_through,
            "send email to {} asynchronously:"
            "mail stored in queue in wait for a"
            "mail_notifier daemon".format(message["To"]),
        )
        redis_connection = get_redis_connection(config)
        queue = get_rq_queue(redis_connection, "mail_sender")
        queue.enqueue(sendmail_callable, message)
    else:
        raise NotImplementedError(
            "Mail sender processing mode {} is not implemented".format(
                config.EMAIL__PROCESSING_MODE))
Exemple #2
0
def send_email_through(
    config: CFG,
    sendmail_callable: typing.Callable[[Message], None],
    message: Message,
) -> None:
    """
    Send mail encapsulation to send it in async or sync mode.

    TODO BS 20170126: A global mail/sender management should be a good
                      thing. Actually, this method is an fast solution.
    :param config: system configuration
    :param sendmail_callable: A callable who get message on first parameter
    :param message: The message who have to be sent
    """

    if config.EMAIL_PROCESSING_MODE == config.CST.SYNC:
        sendmail_callable(message)
    elif config.EMAIL_PROCESSING_MODE == config.CST.ASYNC:
        redis_connection = get_redis_connection(config)
        queue = get_rq_queue(redis_connection, 'mail_sender')
        queue.enqueue(sendmail_callable, message)
    else:
        raise NotImplementedError(
            'Mail sender processing mode {} is not implemented'.format(
                config.EMAIL_PROCESSING_MODE, ))
Exemple #3
0
    def test_func__create_user_with_mail_notification__ok__nominal_case(
            self, mailhog, user_api_factory, app_config):
        api = user_api_factory.get(current_user=None)
        u = api.create_user(
            email="bob@bob",
            password="******",
            name="bob",
            timezone="+2",
            do_save=True,
            do_notify=True,
        )
        assert u is not None
        assert u.email == "bob@bob"
        assert u.validate_password("password")
        assert u.display_name == "bob"
        assert u.timezone == "+2"

        # Send mail async from redis queue
        redis = get_redis_connection(app_config)
        queue = get_rq_queue(redis, "mail_sender")
        worker = SimpleWorker([queue], connection=queue.connection)
        worker.work(burst=True)
        # check mail received
        response = mailhog.get_mailhog_mails()
        headers = response[0]["Content"]["Headers"]
        assert headers["From"][
            0] == "Global manager via Tracim <test_user_from+1@localhost>"
        assert headers["To"][0] == "bob <bob@bob>"
        assert headers["Subject"][0] == "[Tracim] Created account"
    def test_func__create_user_with_mail_notification__ok__nominal_case(self):
        api = UserApi(
            current_user=None,
            session=self.session,
            config=self.app_config,
        )
        u = api.create_user(
            email='bob@bob',
            password='******',
            name='bob',
            timezone='+2',
            do_save=True,
            do_notify=True,
        )
        assert u is not None
        assert u.email == "bob@bob"
        assert u.validate_password('password')
        assert u.display_name == 'bob'
        assert u.timezone == '+2'

        # Send mail async from redis queue
        redis = get_redis_connection(self.app_config)
        queue = get_rq_queue(
            redis,
            'mail_sender',
        )
        worker = SimpleWorker([queue], connection=queue.connection)
        worker.work(burst=True)
        # check mail received
        response = self.get_mailhog_mails()
        headers = response[0]['Content']['Headers']
        assert headers['From'][
            0] == 'Tracim Notifications <test_user_from+0@localhost>'  # nopep8
        assert headers['To'][0] == 'bob <bob@bob>'
        assert headers['Subject'][0] == '[TRACIM] Created account'
 def test_func__reset_password__ok__nominal_case(self):
     uapi = UserApi(
         current_user=None,
         session=self.session,
         config=self.app_config,
     )
     current_user = uapi.get_one_by_email('*****@*****.**')
     uapi.reset_password_notification(current_user, do_save=True)
     transaction.commit()
     # Send mail async from redis queue
     redis = get_redis_connection(self.app_config)
     queue = get_rq_queue(
         redis,
         'mail_sender',
     )
     worker = SimpleWorker([queue], connection=queue.connection)
     worker.work(burst=True)
     # check mail received
     response = requests.get('http://127.0.0.1:8025/api/v1/messages')
     response = response.json()
     headers = response[0]['Content']['Headers']
     assert headers['From'][
         0] == 'Tracim Notifications <test_user_from+0@localhost>'  # nopep8
     assert headers['To'][0] == 'Global manager <*****@*****.**>'
     assert headers['Subject'][0] == '[TRACIM] Reset Password Request'
 def test_func__reset_password__ok__nominal_case(self):
     uapi = UserApi(
         current_user=None,
         session=self.session,
         config=self.app_config,
     )
     current_user = uapi.get_one_by_email('*****@*****.**')
     uapi.reset_password_notification(current_user, do_save=True)
     transaction.commit()
     # Send mail async from redis queue
     redis = get_redis_connection(
         self.app_config
     )
     queue = get_rq_queue(
         redis,
         'mail_sender',
     )
     worker = SimpleWorker([queue], connection=queue.connection)
     worker.work(burst=True)
     # check mail received
     response = self.get_mailhog_mails()
     headers = response[0]['Content']['Headers']
     assert headers['From'][0] == 'Tracim Notifications <test_user_from+0@localhost>'  # nopep8
     assert headers['To'][0] == 'Global manager <*****@*****.**>'
     assert headers['Subject'][0] == '[TRACIM] A password reset has been requested'
Exemple #7
0
 def stop(self) -> None:
     # When _stop_requested at False, tracim.lib.daemons.RQWorker
     # will raise StopRequested exception in worker thread after receive a
     # job.
     self.worker._stop_requested = True
     redis_connection = get_redis_connection(self.config)
     queue = get_rq_queue(redis_connection, 'mail_sender')
     queue.enqueue(do_nothing)
    def test_func__create_new_content_with_notification__ok__nominal_case(
            self):
        uapi = UserApi(
            current_user=None,
            session=self.session,
            config=self.app_config,
        )
        current_user = uapi.get_one_by_email('*****@*****.**')
        # Create new user with notification enabled on w1 workspace
        wapi = WorkspaceApi(
            current_user=current_user,
            session=self.session,
            config=self.app_config,
        )
        workspace = wapi.get_one_by_label('Recipes')
        user = uapi.get_one_by_email('*****@*****.**')
        wapi.enable_notifications(user, workspace)

        api = ContentApi(
            current_user=user,
            session=self.session,
            config=self.app_config,
        )
        item = api.create(
            content_type_list.Folder.slug,
            workspace,
            None,
            'parent',
            do_save=True,
            do_notify=False,
        )
        item2 = api.create(
            content_type_list.File.slug,
            workspace,
            item,
            'file1',
            do_save=True,
            do_notify=True,
        )
        # Send mail async from redis queue
        redis = get_redis_connection(self.app_config)
        queue = get_rq_queue(
            redis,
            'mail_sender',
        )
        worker = SimpleWorker([queue], connection=queue.connection)
        worker.work(burst=True)
        # check mail received
        response = requests.get('http://127.0.0.1:8025/api/v1/messages')
        response = response.json()
        headers = response[0]['Content']['Headers']
        assert headers['From'][
            0] == '"Bob i. via Tracim" <test_user_from+3@localhost>'  # nopep8
        assert headers['To'][0] == 'Global manager <*****@*****.**>'
        assert headers['Subject'][0] == '[TRACIM] [Recipes] file1 (Open)'
        assert headers['References'][0] == 'test_user_refs+22@localhost'
        assert headers['Reply-to'][
            0] == '"Bob i. & all members of Recipes" <test_user_reply+22@localhost>'  # nopep8
    def test_func__create_new_content_with_notification__ok__nominal_case(self):
        uapi = UserApi(
            current_user=None,
            session=self.session,
            config=self.app_config,
        )
        current_user = uapi.get_one_by_email('*****@*****.**')
        # Create new user with notification enabled on w1 workspace
        wapi = WorkspaceApi(
            current_user=current_user,
            session=self.session,
            config=self.app_config,
        )
        workspace = wapi.get_one_by_label('Recipes')
        user = uapi.get_one_by_email('*****@*****.**')
        wapi.enable_notifications(user, workspace)

        api = ContentApi(
            current_user=user,
            session=self.session,
            config=self.app_config,
        )
        item = api.create(
            content_type_list.Folder.slug,
            workspace,
            None,
            'parent',
            do_save=True,
            do_notify=False,
        )
        item2 = api.create(
            content_type_list.File.slug,
            workspace,
            item,
            'file1',
            do_save=True,
            do_notify=True,
        )
        # Send mail async from redis queue
        redis = get_redis_connection(
            self.app_config
        )
        queue = get_rq_queue(
            redis,
            'mail_sender',
        )
        worker = SimpleWorker([queue], connection=queue.connection)
        worker.work(burst=True)
        # check mail received
        response = self.get_mailhog_mails()
        headers = response[0]['Content']['Headers']
        assert headers['From'][0] == '"Bob i. via Tracim" <test_user_from+3@localhost>'  # nopep8
        assert headers['To'][0] == 'Global manager <*****@*****.**>'
        assert headers['Subject'][0] == '[TRACIM] [Recipes] file1 (Open)'
        assert headers['References'][0] == 'test_user_refs+22@localhost'
        assert headers['Reply-to'][0] == '"Bob i. & all members of Recipes" <test_user_reply+22@localhost>'  # nopep8
Exemple #10
0
    def test_func__create_new_content_with_notification__ok__nominal_case(
        self,
        user_api_factory,
        workspace_api_factory,
        content_api_factory,
        mailhog,
        app_config,
        content_type_list,
    ):
        uapi = user_api_factory.get(current_user=None)
        current_user = uapi.get_one_by_email("*****@*****.**")
        # Create new user with notification enabled on w1 workspace
        wapi = workspace_api_factory.get(current_user=current_user)
        workspace = wapi.get_one_by_label("Recipes")
        user = uapi.get_one_by_email("*****@*****.**")
        wapi.enable_notifications(user, workspace)

        api = content_api_factory.get(current_user=user)
        item = api.create(content_type_list.Folder.slug,
                          workspace,
                          None,
                          "parent",
                          do_save=True,
                          do_notify=False)
        api.create(content_type_list.File.slug,
                   workspace,
                   item,
                   "file1",
                   do_save=True,
                   do_notify=True)
        # Send mail async from redis queue
        redis = get_redis_connection(app_config)
        queue = get_rq_queue(redis, "mail_sender")
        worker = SimpleWorker([queue], connection=queue.connection)
        worker.work(burst=True)
        # check mail received
        response = mailhog.get_mailhog_mails()
        headers = response[0]["Content"]["Headers"]
        assert headers["From"][
            0] == '"Bob i. via Tracim" <test_user_from+3@localhost>'
        assert headers["To"][0] == "Global manager <*****@*****.**>"
        assert headers["Subject"][0] == "[Tracim] [Recipes] file1 (Opened)"
        assert headers["References"][0] == "<test_user_refs+22@localhost>"
        assert (
            headers["Reply-to"][0] ==
            '"Bob i. & all members of Recipes" <test_user_reply+22@localhost>')
    def test_api__add_upload_permission__ok_200__with_email_notification_async(
        self,
        workspace_api_factory,
        content_api_factory,
        session,
        web_testapp,
        content_type_list,
        upload_permission_lib_factory,
        admin_user,
        mailhog,
        app_config,
    ) -> None:
        workspace_api = workspace_api_factory.get()
        workspace = workspace_api.create_workspace("test workspace", save_now=True)
        upload_permission_lib = upload_permission_lib_factory.get()  # type: UploadPermissionLib
        upload_permission_lib.add_permission_to_workspace(
            workspace, emails=["*****@*****.**", "toto <[email protected]>"], do_notify=True
        )
        transaction.commit()

        mailhog.cleanup_mailhog()
        # Send mail async from redis queue
        redis = get_redis_connection(app_config)
        queue = get_rq_queue(redis, "mail_sender")
        worker = SimpleWorker([queue], connection=queue.connection)
        worker.work(burst=True)

        response = mailhog.get_mailhog_mails()
        assert len(response) == 3
        valid_dests = [
            "Global manager <*****@*****.**>",
            "*****@*****.**",
            "toto <[email protected]>",
        ]
        for email in response:
            assert (
                email["Content"]["Headers"]["From"][0]
                == "Tracim Notifications <test_user_from+0@localhost>"
                or "Global manager via Tracim <test_user_from+1@localhost>"
            )
            headers = email["Content"]["Headers"]
            assert headers["To"][0] in valid_dests
            valid_dests.remove(headers["To"][0])
        assert valid_dests == []
Exemple #12
0
 def test_func__reset_password__ok__nominal_case(self, user_api_factory,
                                                 mailhog, app_config):
     uapi = user_api_factory.get()
     current_user = uapi.get_one_by_email("*****@*****.**")
     uapi.reset_password_notification(current_user, do_save=True)
     transaction.commit()
     # Send mail async from redis queue
     redis = get_redis_connection(app_config)
     queue = get_rq_queue(redis, "mail_sender")
     worker = SimpleWorker([queue], connection=queue.connection)
     worker.work(burst=True)
     # check mail received
     response = mailhog.get_mailhog_mails()
     headers = response[0]["Content"]["Headers"]
     assert headers["From"][
         0] == "Tracim Notifications <test_user_from+0@localhost>"
     assert headers["To"][0] == "Global manager <*****@*****.**>"
     assert headers["Subject"][
         0] == "[Tracim] A password reset has been requested"
Exemple #13
0
def send_email_through(
        config: CFG,
        sendmail_callable: typing.Callable[[Message], None],
        message: Message,
) -> None:
    """
    Send mail encapsulation to send it in async or sync mode.

    TODO BS 20170126: A global mail/sender management should be a good
                      thing. Actually, this method is an fast solution.
    :param config: system configuration
    :param sendmail_callable: A callable who get message on first parameter
    :param message: The message who have to be sent
    """
    if config.EMAIL_PROCESSING_MODE == config.CST.SYNC:
        logger.info(
            send_email_through,
            'send email to {} synchronously'.format(
                message['To']
            )
        )
        sendmail_callable(message)
    elif config.EMAIL_PROCESSING_MODE == config.CST.ASYNC:
        logger.info(
            send_email_through,
            'send email to {} asynchronously:'
            'mail stored in queue in wait for a'
            'mail_notifier daemon'.format(
                message['To']
            )
        )
        redis_connection = get_redis_connection(config)
        queue = get_rq_queue(redis_connection, 'mail_sender')
        queue.enqueue(sendmail_callable, message)
    else:
        raise NotImplementedError(
            'Mail sender processing mode {} is not implemented'.format(
                config.EMAIL_PROCESSING_MODE,
            )
        )
    def test_func__create_user_with_mail_notification__ok__nominal_case(self):
        api = UserApi(
            current_user=None,
            session=self.session,
            config=self.app_config,
        )
        u = api.create_user(
            email='bob@bob',
            password='******',
            name='bob',
            timezone='+2',
            do_save=True,
            do_notify=True,
        )
        assert u is not None
        assert u.email == "bob@bob"
        assert u.validate_password('password')
        assert u.display_name == 'bob'
        assert u.timezone == '+2'

        # Send mail async from redis queue
        redis = get_redis_connection(
            self.app_config
        )
        queue = get_rq_queue(
            redis,
            'mail_sender',
        )
        worker = SimpleWorker([queue], connection=queue.connection)
        worker.work(burst=True)
        # check mail received
        response = self.get_mailhog_mails()
        headers = response[0]['Content']['Headers']
        assert headers['From'][0] == 'Tracim Notifications <test_user_from+0@localhost>'  # nopep8
        assert headers['To'][0] == 'bob <bob@bob>'
        assert headers['Subject'][0] == '[TRACIM] Created account'
Exemple #15
0
    def run(self) -> None:

        with RQConnection(get_redis_connection(self.config)):
            self.worker = RQWorker(['mail_sender'])
            self.worker.work(burst=self.burst)