Exemplo n.º 1
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"
Exemplo n.º 2
0
def send_email_through(
    config: CFG, sendmail_callable: typing.Callable[[MIMEMultipart], None], message: MIMEMultipart
) -> 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.JOBS__PROCESSING_MODE == config.CST.SYNC:
        logger.info(send_email_through, "send email to {} synchronously".format(message["To"]))
        sendmail_callable(message)
    elif config.JOBS__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.JOBS__PROCESSING_MODE)
        )
Exemplo n.º 3
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)
Exemplo n.º 4
0
 def publish_messages_for_event(self, event_id: int) -> None:
     redis_connection = get_redis_connection(self._config)
     queue = get_rq_queue(redis_connection, RQ_QUEUE_NAME)
     logger.debug(
         self,
         "publish event(id={}) asynchronously to RQ queue {}".format(
             event_id, RQ_QUEUE_NAME),
     )
     queue.enqueue(self._publish_messages_for_event, event_id)
Exemplo n.º 5
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"
Exemplo n.º 6
0
 def test_unit__submit_job__OK_nominal_case(self, app_config, session,
                                            rq_database_worker) -> None:
     redis = get_redis_connection(app_config)
     queue = get_rq_queue(redis, queue_name="event")
     job = queue.enqueue(
         # need to enqueue by name as enqueuing with get_public_name fails in pytest
         "tracim_backend.tests.library.test_rq.get_public_name",
         1,
         timeout=self.JOB_EXECUTION_TIMEOUT,
     )
     while not job.result:
         if job.exc_info:
             raise job.exc_info
         time.sleep(0.1)
     user_api = UserApi(current_user=None,
                        session=session,
                        config=app_config)
     user = user_api.get_one(1)
     job_public_name = job.result
     assert user.public_name == job_public_name
Exemplo n.º 7
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_filemanager_filename("Recipes.space")
        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>'
        )
Exemplo n.º 8
0
 def empty_event_queue():
     redis_connection = get_redis_connection(app_config)
     queue = get_rq_queue(redis_connection, RQ_QUEUE_NAME)
     queue.delete()
Exemplo n.º 9
0
    def run(self) -> None:

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