Changeset - fdde16d7cea0
[Not reviewed]
default
0 6 0
Mads Kiilerich - 6 years ago 2020-02-29 16:06:30
mads@kiilerich.com
Grafted from: bc10216c34c7
celery: fix send_email to work with JSON encoding (Issue #363)

Long time ago, c935bcaf7086 introduced an optional User object parameter to the
send_email task and used the computed full_name_or_username property. Due to the
magic of pickle, that also worked when using Celery to run the task async.

Now, Celery 4 changed the default encoding from Pickle to JSON, which we
anticipated in e539db6cc0da. That broke send_email in some cases, for example
when a user comments on another user's changeset.

Fixed by passing the "From" name as string instead of passing the whole User
object.

Thanks to vyom for reporting.
6 files changed with 34 insertions and 32 deletions:
0 comments (0 inline, 0 general)
kallithea/lib/celerylib/tasks.py
Show inline comments
 
@@ -212,97 +212,98 @@ def get_commits_stats(repo_name, ts_min_
 
            DBS.commit()
 
        except:
 
            log.error(traceback.format_exc())
 
            DBS.rollback()
 
            lock.release()
 
            return False
 

	
 
        # final release
 
        lock.release()
 

	
 
        # execute another task if celery is enabled
 
        if len(repo.revisions) > 1 and kallithea.CELERY_APP and recurse_limit > 0:
 
            get_commits_stats(repo_name, ts_min_y, ts_max_y, recurse_limit - 1)
 
        elif recurse_limit <= 0:
 
            log.debug('Not recursing - limit has been reached')
 
        else:
 
            log.debug('Not recursing')
 
    except celerylib.LockHeld:
 
        log.info('Task with key %s already running', lockkey)
 
        return 'Task with key %s already running' % lockkey
 

	
 

	
 
@celerylib.task
 
@celerylib.dbsession
 
def send_email(recipients, subject, body='', html_body='', headers=None, author=None):
 
def send_email(recipients, subject, body='', html_body='', headers=None, from_name=None):
 
    """
 
    Sends an email with defined parameters from the .ini files.
 

	
 
    :param recipients: list of recipients, if this is None, the defined email
 
        address from field 'email_to' and all admins is used instead
 
    :param subject: subject of the mail
 
    :param body: body of the mail
 
    :param html_body: html version of body
 
    :param headers: dictionary of prepopulated e-mail headers
 
    :param author: User object of the author of this mail, if known and relevant
 
    :param from_name: full name to be used as sender of this mail - often a
 
    .full_name_or_username value
 
    """
 
    assert isinstance(recipients, list), recipients
 
    if headers is None:
 
        headers = {}
 
    else:
 
        # do not modify the original headers object passed by the caller
 
        headers = headers.copy()
 

	
 
    email_config = config
 
    email_prefix = email_config.get('email_prefix', '')
 
    if email_prefix:
 
        subject = "%s %s" % (email_prefix, subject)
 

	
 
    if not recipients:
 
        # if recipients are not defined we send to email_config + all admins
 
        recipients = [u.email for u in User.query()
 
                      .filter(User.admin == True).all()]
 
        if email_config.get('email_to') is not None:
 
            recipients += email_config.get('email_to').split(',')
 

	
 
        # If there are still no recipients, there are no admins and no address
 
        # configured in email_to, so return.
 
        if not recipients:
 
            log.error("No recipients specified and no fallback available.")
 
            return False
 

	
 
        log.warning("No recipients specified for '%s' - sending to admins %s", subject, ' '.join(recipients))
 

	
 
    # SMTP sender
 
    envelope_from = email_config.get('app_email_from', 'Kallithea')
 
    # 'From' header
 
    if author is not None:
 
        # set From header based on author but with a generic e-mail address
 
    if from_name is not None:
 
        # set From header based on from_name but with a generic e-mail address
 
        # In case app_email_from is in "Some Name <e-mail>" format, we first
 
        # extract the e-mail address.
 
        envelope_addr = author_email(envelope_from)
 
        headers['From'] = '"%s" <%s>' % (
 
            email.utils.quote('%s (no-reply)' % author.full_name_or_username),
 
            email.utils.quote('%s (no-reply)' % from_name),
 
            envelope_addr)
 

	
 
    user = email_config.get('smtp_username')
 
    passwd = email_config.get('smtp_password')
 
    mail_server = email_config.get('smtp_server')
 
    mail_port = email_config.get('smtp_port')
 
    tls = str2bool(email_config.get('smtp_use_tls'))
 
    ssl = str2bool(email_config.get('smtp_use_ssl'))
 
    debug = str2bool(email_config.get('debug'))
 
    smtp_auth = email_config.get('smtp_auth')
 

	
 
    logmsg = ("Mail details:\n"
 
              "recipients: %s\n"
 
              "headers: %s\n"
 
              "subject: %s\n"
 
              "body:\n%s\n"
 
              "html:\n%s\n"
 
              % (' '.join(recipients), headers, subject, body, html_body))
 

	
 
    if mail_server:
 
        log.debug("Sending e-mail. " + logmsg)
 
    else:
 
        log.error("SMTP mail server not configured - cannot send e-mail.")
 
        log.warning(logmsg)
kallithea/model/notification.py
Show inline comments
 
@@ -111,49 +111,50 @@ class NotificationModel(object):
 
                  }
 

	
 
        txt_kwargs = {
 
                  'subject': subject,
 
                  'body': body,
 
                  'when': created_on,
 
                  'user': created_by_obj.username,
 
                  }
 

	
 
        html_kwargs.update(email_kwargs)
 
        txt_kwargs.update(email_kwargs)
 
        email_subject = EmailNotificationModel() \
 
                            .get_email_description(type_, **txt_kwargs)
 
        email_txt_body = EmailNotificationModel() \
 
                            .get_email_tmpl(type_, 'txt', **txt_kwargs)
 
        email_html_body = EmailNotificationModel() \
 
                            .get_email_tmpl(type_, 'html', **html_kwargs)
 

	
 
        # don't send email to person who created this comment
 
        rec_objs = set(recipients_objs).difference(set([created_by_obj]))
 

	
 
        # send email with notification to all other participants
 
        for rec in rec_objs:
 
            tasks.send_email([rec.email], email_subject, email_txt_body,
 
                     email_html_body, headers, author=created_by_obj)
 
                     email_html_body, headers,
 
                     from_name=created_by_obj.full_name_or_username)
 

	
 

	
 
class EmailNotificationModel(object):
 

	
 
    TYPE_CHANGESET_COMMENT = NotificationModel.TYPE_CHANGESET_COMMENT
 
    TYPE_MESSAGE = NotificationModel.TYPE_MESSAGE # only used for testing
 
    # NotificationModel.TYPE_MENTION is not used
 
    TYPE_PASSWORD_RESET = 'password_link'
 
    TYPE_REGISTRATION = NotificationModel.TYPE_REGISTRATION
 
    TYPE_PULL_REQUEST = NotificationModel.TYPE_PULL_REQUEST
 
    TYPE_PULL_REQUEST_COMMENT = NotificationModel.TYPE_PULL_REQUEST_COMMENT
 
    TYPE_DEFAULT = 'default'
 

	
 
    def __init__(self):
 
        super(EmailNotificationModel, self).__init__()
 
        self._tmpl_lookup = app_globals.mako_lookup
 
        self.email_types = {
 
            self.TYPE_CHANGESET_COMMENT: 'changeset_comment',
 
            self.TYPE_PASSWORD_RESET: 'password_reset',
 
            self.TYPE_REGISTRATION: 'registration',
 
            self.TYPE_DEFAULT: 'default',
 
            self.TYPE_PULL_REQUEST: 'pull_request',
 
            self.TYPE_PULL_REQUEST_COMMENT: 'pull_request_comment',
 
        }
kallithea/tests/functional/test_login.py
Show inline comments
 
@@ -389,49 +389,49 @@ class TestLoginController(base.TestContr
 
                                    action='password_reset'))
 
        assert response.status == '200 OK'
 

	
 
        username = 'test_password_reset_1'
 
        password = 'qweqwe'
 
        email = 'username@example.com'
 
        name = 'passwd'
 
        lastname = 'reset'
 
        timestamp = int(time.time())
 

	
 
        new = User()
 
        new.username = username
 
        new.password = password
 
        new.email = email
 
        new.name = name
 
        new.lastname = lastname
 
        new.api_key = generate_api_key()
 
        Session().add(new)
 
        Session().commit()
 

	
 
        token = UserModel().get_reset_password_token(
 
            User.get_by_username(username), timestamp, self.session_csrf_secret_token())
 

	
 
        collected = []
 
        def mock_send_email(recipients, subject, body='', html_body='', headers=None, author=None):
 
        def mock_send_email(recipients, subject, body='', html_body='', headers=None, from_name=None):
 
            collected.append((recipients, subject, body, html_body))
 

	
 
        with mock.patch.object(kallithea.lib.celerylib.tasks, 'send_email', mock_send_email), \
 
                mock.patch.object(time, 'time', lambda: timestamp):
 
            response = self.app.post(base.url(controller='login',
 
                                         action='password_reset'),
 
                                     {'email': email,
 
                                      '_session_csrf_secret_token': self.session_csrf_secret_token()})
 

	
 
        self.checkSessionFlash(response, 'A password reset confirmation code has been sent')
 

	
 
        ((recipients, subject, body, html_body),) = collected
 
        assert recipients == ['username@example.com']
 
        assert subject == 'Password reset link'
 
        assert '\n%s\n' % token in body
 
        (confirmation_url,) = (line for line in body.splitlines() if line.startswith('http://'))
 
        assert ' href="%s"' % confirmation_url.replace('&', '&amp;').replace('@', '%40') in html_body
 

	
 
        d = urllib.parse.parse_qs(urllib.parse.urlparse(confirmation_url).query)
 
        assert d['token'] == [token]
 
        assert d['timestamp'] == [str(timestamp)]
 
        assert d['email'] == [email]
 

	
 
        response = response.follow()
kallithea/tests/models/test_dump_html_mails.ref.html
Show inline comments
 
<!doctype html>
 
<html lang="en">
 
<head><title>Notifications</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>
 
<body>
 
<hr/>
 
<h1>cs_comment, is_mention=False, status_change=None</h1>
 
<pre>
 
From: u1
 
From: u1 u1 <name@example.com>
 
To: u2@example.com
 
Subject: [Comment] repo/name changeset cafe1234 "This changeset did something cl..." on brunch
 
</pre>
 
<hr/>
 
<pre>http://comment.org
 

	
 
Comment on Changeset "This changeset did something clever which is hard to explain"
 

	
 

	
 
Opinionated User (jsmith):
 

	
 
This is the new 'comment'.
 

	
 
 - and here it ends indented.
 

	
 

	
 
Changeset on http://example.com/repo_target branch brunch:
 
"This changeset did something clever which is hard to explain" by u2 u3 (u2).
 

	
 

	
 
View Comment: http://comment.org
 
</pre>
 
<hr/>
 
<!--!doctype html-->
 
@@ -143,49 +143,49 @@ View Comment: http://comment.org
 
            </td>
 
        </tr>
 
    </table>
 
</center>
 
        </td>
 
    </tr>
 
</table>
 
                    </td>
 
                    <td></td>
 
                </tr>
 
                <tr>
 
                    <td height="30px" style="height:30px" colspan="3"></td>
 
                </tr>
 
            </table>
 
        </td>
 
        <td width="30px" style="width:30px"></td>
 
    </tr>
 
</table>
 
<!--/body-->
 
<!--/html-->
 
<hr/>
 
<hr/>
 
<h1>cs_comment, is_mention=True, status_change=None</h1>
 
<pre>
 
From: u1
 
From: u1 u1 <name@example.com>
 
To: u2@example.com
 
Subject: [Comment] repo/name changeset cafe1234 "This changeset did something cl..." on brunch
 
</pre>
 
<hr/>
 
<pre>http://comment.org
 

	
 
Mention in Comment on Changeset "This changeset did something clever which is hard to explain"
 

	
 

	
 
Opinionated User (jsmith):
 

	
 
This is the new 'comment'.
 

	
 
 - and here it ends indented.
 

	
 

	
 
Changeset on http://example.com/repo_target branch brunch:
 
"This changeset did something clever which is hard to explain" by u2 u3 (u2).
 

	
 

	
 
View Comment: http://comment.org
 
</pre>
 
<hr/>
 
<!--!doctype html-->
 
@@ -302,49 +302,49 @@ View Comment: http://comment.org
 
            </td>
 
        </tr>
 
    </table>
 
</center>
 
        </td>
 
    </tr>
 
</table>
 
                    </td>
 
                    <td></td>
 
                </tr>
 
                <tr>
 
                    <td height="30px" style="height:30px" colspan="3"></td>
 
                </tr>
 
            </table>
 
        </td>
 
        <td width="30px" style="width:30px"></td>
 
    </tr>
 
</table>
 
<!--/body-->
 
<!--/html-->
 
<hr/>
 
<hr/>
 
<h1>cs_comment, is_mention=False, status_change='Approved'</h1>
 
<pre>
 
From: u1
 
From: u1 u1 <name@example.com>
 
To: u2@example.com
 
Subject: [Approved: Comment] repo/name changeset cafe1234 "This changeset did something cl..." on brunch
 
</pre>
 
<hr/>
 
<pre>http://comment.org
 

	
 
Comment on Changeset "This changeset did something clever which is hard to explain"
 

	
 

	
 
Opinionated User (jsmith):
 

	
 
Status change: Approved
 

	
 
This is the new 'comment'.
 

	
 
 - and here it ends indented.
 

	
 

	
 
Changeset on http://example.com/repo_target branch brunch:
 
"This changeset did something clever which is hard to explain" by u2 u3 (u2).
 

	
 

	
 
View Comment: http://comment.org
 
</pre>
 
@@ -479,49 +479,49 @@ View Comment: http://comment.org
 
            </td>
 
        </tr>
 
    </table>
 
</center>
 
        </td>
 
    </tr>
 
</table>
 
                    </td>
 
                    <td></td>
 
                </tr>
 
                <tr>
 
                    <td height="30px" style="height:30px" colspan="3"></td>
 
                </tr>
 
            </table>
 
        </td>
 
        <td width="30px" style="width:30px"></td>
 
    </tr>
 
</table>
 
<!--/body-->
 
<!--/html-->
 
<hr/>
 
<hr/>
 
<h1>cs_comment, is_mention=True, status_change='Approved'</h1>
 
<pre>
 
From: u1
 
From: u1 u1 <name@example.com>
 
To: u2@example.com
 
Subject: [Approved: Comment] repo/name changeset cafe1234 "This changeset did something cl..." on brunch
 
</pre>
 
<hr/>
 
<pre>http://comment.org
 

	
 
Mention in Comment on Changeset "This changeset did something clever which is hard to explain"
 

	
 

	
 
Opinionated User (jsmith):
 

	
 
Status change: Approved
 

	
 
This is the new 'comment'.
 

	
 
 - and here it ends indented.
 

	
 

	
 
Changeset on http://example.com/repo_target branch brunch:
 
"This changeset did something clever which is hard to explain" by u2 u3 (u2).
 

	
 

	
 
View Comment: http://comment.org
 
</pre>
 
@@ -656,49 +656,49 @@ View Comment: http://comment.org
 
            </td>
 
        </tr>
 
    </table>
 
</center>
 
        </td>
 
    </tr>
 
</table>
 
                    </td>
 
                    <td></td>
 
                </tr>
 
                <tr>
 
                    <td height="30px" style="height:30px" colspan="3"></td>
 
                </tr>
 
            </table>
 
        </td>
 
        <td width="30px" style="width:30px"></td>
 
    </tr>
 
</table>
 
<!--/body-->
 
<!--/html-->
 
<hr/>
 
<hr/>
 
<h1>message</h1>
 
<pre>
 
From: u1
 
From: u1 u1 <name@example.com>
 
To: u2@example.com
 
Subject: Test Message
 
</pre>
 
<hr/>
 
<pre>This is the 'body' of the "test" message
 
 - nothing interesting here except indentation.</pre>
 
<hr/>
 
<!--!doctype html-->
 
<!--html lang="en"-->
 
<!--head-->
 
    <!--title--><!--/title-->
 
    <!--meta name="viewport" content="width=device-width"-->
 
    <!--meta http-equiv="Content-Type" content="text/html; charset=UTF-8"-->
 
<!--/head-->
 
<!--body-->
 
<table align="center" cellpadding="0" cellspacing="0" border="0" style="min-width:348px;max-width:800px;font-family:Helvetica,Arial,sans-serif;font-weight:200;font-size:14px;line-height:17px;color:#202020">
 
    <tr>
 
        <td width="30px" style="width:30px"></td>
 
        <td>
 
            <table width="100%" cellpadding="0" cellspacing="0" border="0"
 
                   style="table-layout:fixed;font-family:Helvetica,Arial,sans-serif;border:1px solid #ddd">
 
                <tr><td width="30px" style="width:30px"></td><td></td><td width="30px" style="width:30px"></td></tr>
 
                <tr>
 
                    <td colspan="3">
 
@@ -727,49 +727,49 @@ Subject: Test Message
 
                    <td></td>
 
                    <td>
 
<table cellpadding="0" cellspacing="0" border="0" width="100%">
 
    <tr>
 
        <td style="font-family:Lucida Console,Consolas,Monaco,Inconsolata,Liberation Mono,monospace;white-space:pre-wrap"><div class="formatted-fixed">This is the &#39;body&#39; of the &quot;test&quot; message<br/> - nothing interesting here except indentation.</div></td>
 
    </tr>
 
</table>
 
                    </td>
 
                    <td></td>
 
                </tr>
 
                <tr>
 
                    <td height="30px" style="height:30px" colspan="3"></td>
 
                </tr>
 
            </table>
 
        </td>
 
        <td width="30px" style="width:30px"></td>
 
    </tr>
 
</table>
 
<!--/body-->
 
<!--/html-->
 
<hr/>
 
<hr/>
 
<h1>registration</h1>
 
<pre>
 
From: u1
 
From: u1 u1 <name@example.com>
 
To: u2@example.com
 
Subject: New user newbie registered
 
</pre>
 
<hr/>
 
<pre>http://newbie.org
 

	
 
New User Registration
 

	
 

	
 
Username: newbie
 

	
 
Full Name: New Full Name
 

	
 
Email: new@email.com
 

	
 

	
 
View User Profile: http://newbie.org
 
</pre>
 
<hr/>
 
<!--!doctype html-->
 
<!--html lang="en"-->
 
<!--head-->
 
    <!--title--><!--/title-->
 
    <!--meta name="viewport" content="width=device-width"-->
 
@@ -860,49 +860,49 @@ View User Profile: http://newbie.org
 
            </td>
 
        </tr>
 
    </table>
 
</center>
 
        </td>
 
    </tr>
 
</table>
 
                    </td>
 
                    <td></td>
 
                </tr>
 
                <tr>
 
                    <td height="30px" style="height:30px" colspan="3"></td>
 
                </tr>
 
            </table>
 
        </td>
 
        <td width="30px" style="width:30px"></td>
 
    </tr>
 
</table>
 
<!--/body-->
 
<!--/html-->
 
<hr/>
 
<hr/>
 
<h1>pull_request, is_mention=False</h1>
 
<pre>
 
From: u1
 
From: u1 u1 <name@example.com>
 
To: u2@example.com
 
Subject: [Review] repo/name PR #7 "The Title" from devbranch by u2
 
</pre>
 
<hr/>
 
<pre>http://pr.org/7
 

	
 
Added as Reviewer of Pull Request #7 "The Title" by Requesting User (root)
 

	
 

	
 
Pull request #7 "The Title" by u2 u3 (u2)
 
from https://dev.org/repo branch devbranch
 
to http://mainline.com/repo branch trunk
 

	
 

	
 
Description:
 

	
 
This PR is 'awesome' because it does <stuff>
 
 - please approve indented!
 

	
 

	
 
Changesets:
 

	
 
Introduce one and two
 
Make one plus two equal tree
 
@@ -1051,49 +1051,49 @@ View Pull Request: http://pr.org/7
 
            </td>
 
        </tr>
 
    </table>
 
</center>
 
        </td>
 
    </tr>
 
</table>
 
                    </td>
 
                    <td></td>
 
                </tr>
 
                <tr>
 
                    <td height="30px" style="height:30px" colspan="3"></td>
 
                </tr>
 
            </table>
 
        </td>
 
        <td width="30px" style="width:30px"></td>
 
    </tr>
 
</table>
 
<!--/body-->
 
<!--/html-->
 
<hr/>
 
<hr/>
 
<h1>pull_request, is_mention=True</h1>
 
<pre>
 
From: u1
 
From: u1 u1 <name@example.com>
 
To: u2@example.com
 
Subject: [Review] repo/name PR #7 "The Title" from devbranch by u2
 
</pre>
 
<hr/>
 
<pre>http://pr.org/7
 

	
 
Mention on Pull Request #7 "The Title" by Requesting User (root)
 

	
 

	
 
Pull request #7 "The Title" by u2 u3 (u2)
 
from https://dev.org/repo branch devbranch
 
to http://mainline.com/repo branch trunk
 

	
 

	
 
Description:
 

	
 
This PR is 'awesome' because it does <stuff>
 
 - please approve indented!
 

	
 

	
 
Changesets:
 

	
 
Introduce one and two
 
Make one plus two equal tree
 
@@ -1242,49 +1242,49 @@ View Pull Request: http://pr.org/7
 
            </td>
 
        </tr>
 
    </table>
 
</center>
 
        </td>
 
    </tr>
 
</table>
 
                    </td>
 
                    <td></td>
 
                </tr>
 
                <tr>
 
                    <td height="30px" style="height:30px" colspan="3"></td>
 
                </tr>
 
            </table>
 
        </td>
 
        <td width="30px" style="width:30px"></td>
 
    </tr>
 
</table>
 
<!--/body-->
 
<!--/html-->
 
<hr/>
 
<hr/>
 
<h1>pull_request_comment, is_mention=False, status_change=None, closing_pr=False</h1>
 
<pre>
 
From: u1
 
From: u1 u1 <name@example.com>
 
To: u2@example.com
 
Subject: [Comment] repo/name PR #7 "The Title" from devbranch by u2
 
</pre>
 
<hr/>
 
<pre>http://pr.org/comment
 

	
 
Comment on Pull Request #7 "The Title"
 

	
 

	
 
Opinionated User (jsmith):
 

	
 
Me too!
 

	
 
 - and indented on second line
 

	
 

	
 
Pull request #7 "The Title" by u2 u3 (u2)
 
from https://dev.org/repo branch devbranch
 
to http://mainline.com/repo branch trunk
 

	
 

	
 
View Comment: http://pr.org/comment
 
</pre>
 
<hr/>
 
@@ -1409,49 +1409,49 @@ View Comment: http://pr.org/comment
 
            </td>
 
        </tr>
 
    </table>
 
</center>
 
        </td>
 
    </tr>
 
</table>
 
                    </td>
 
                    <td></td>
 
                </tr>
 
                <tr>
 
                    <td height="30px" style="height:30px" colspan="3"></td>
 
                </tr>
 
            </table>
 
        </td>
 
        <td width="30px" style="width:30px"></td>
 
    </tr>
 
</table>
 
<!--/body-->
 
<!--/html-->
 
<hr/>
 
<hr/>
 
<h1>pull_request_comment, is_mention=True, status_change=None, closing_pr=False</h1>
 
<pre>
 
From: u1
 
From: u1 u1 <name@example.com>
 
To: u2@example.com
 
Subject: [Comment] repo/name PR #7 "The Title" from devbranch by u2
 
</pre>
 
<hr/>
 
<pre>http://pr.org/comment
 

	
 
Mention in Comment on Pull Request #7 "The Title"
 

	
 

	
 
Opinionated User (jsmith):
 

	
 
Me too!
 

	
 
 - and indented on second line
 

	
 

	
 
Pull request #7 "The Title" by u2 u3 (u2)
 
from https://dev.org/repo branch devbranch
 
to http://mainline.com/repo branch trunk
 

	
 

	
 
View Comment: http://pr.org/comment
 
</pre>
 
<hr/>
 
@@ -1576,49 +1576,49 @@ View Comment: http://pr.org/comment
 
            </td>
 
        </tr>
 
    </table>
 
</center>
 
        </td>
 
    </tr>
 
</table>
 
                    </td>
 
                    <td></td>
 
                </tr>
 
                <tr>
 
                    <td height="30px" style="height:30px" colspan="3"></td>
 
                </tr>
 
            </table>
 
        </td>
 
        <td width="30px" style="width:30px"></td>
 
    </tr>
 
</table>
 
<!--/body-->
 
<!--/html-->
 
<hr/>
 
<hr/>
 
<h1>pull_request_comment, is_mention=False, status_change='Under Review', closing_pr=False</h1>
 
<pre>
 
From: u1
 
From: u1 u1 <name@example.com>
 
To: u2@example.com
 
Subject: [Under Review: Comment] repo/name PR #7 "The Title" from devbranch by u2
 
</pre>
 
<hr/>
 
<pre>http://pr.org/comment
 

	
 
Comment on Pull Request #7 "The Title"
 

	
 

	
 
Opinionated User (jsmith):
 

	
 
Status change: Under Review
 

	
 
Me too!
 

	
 
 - and indented on second line
 

	
 

	
 
Pull request #7 "The Title" by u2 u3 (u2)
 
from https://dev.org/repo branch devbranch
 
to http://mainline.com/repo branch trunk
 

	
 

	
 
View Comment: http://pr.org/comment
 
@@ -1761,49 +1761,49 @@ View Comment: http://pr.org/comment
 
            </td>
 
        </tr>
 
    </table>
 
</center>
 
        </td>
 
    </tr>
 
</table>
 
                    </td>
 
                    <td></td>
 
                </tr>
 
                <tr>
 
                    <td height="30px" style="height:30px" colspan="3"></td>
 
                </tr>
 
            </table>
 
        </td>
 
        <td width="30px" style="width:30px"></td>
 
    </tr>
 
</table>
 
<!--/body-->
 
<!--/html-->
 
<hr/>
 
<hr/>
 
<h1>pull_request_comment, is_mention=True, status_change='Under Review', closing_pr=False</h1>
 
<pre>
 
From: u1
 
From: u1 u1 <name@example.com>
 
To: u2@example.com
 
Subject: [Under Review: Comment] repo/name PR #7 "The Title" from devbranch by u2
 
</pre>
 
<hr/>
 
<pre>http://pr.org/comment
 

	
 
Mention in Comment on Pull Request #7 "The Title"
 

	
 

	
 
Opinionated User (jsmith):
 

	
 
Status change: Under Review
 

	
 
Me too!
 

	
 
 - and indented on second line
 

	
 

	
 
Pull request #7 "The Title" by u2 u3 (u2)
 
from https://dev.org/repo branch devbranch
 
to http://mainline.com/repo branch trunk
 

	
 

	
 
View Comment: http://pr.org/comment
 
@@ -1946,49 +1946,49 @@ View Comment: http://pr.org/comment
 
            </td>
 
        </tr>
 
    </table>
 
</center>
 
        </td>
 
    </tr>
 
</table>
 
                    </td>
 
                    <td></td>
 
                </tr>
 
                <tr>
 
                    <td height="30px" style="height:30px" colspan="3"></td>
 
                </tr>
 
            </table>
 
        </td>
 
        <td width="30px" style="width:30px"></td>
 
    </tr>
 
</table>
 
<!--/body-->
 
<!--/html-->
 
<hr/>
 
<hr/>
 
<h1>pull_request_comment, is_mention=False, status_change=None, closing_pr=True</h1>
 
<pre>
 
From: u1
 
From: u1 u1 <name@example.com>
 
To: u2@example.com
 
Subject: [Closing: Comment] repo/name PR #7 "The Title" from devbranch by u2
 
</pre>
 
<hr/>
 
<pre>http://pr.org/comment
 

	
 
Pull Request #7 "The Title" Closed
 

	
 

	
 
Opinionated User (jsmith):
 

	
 
The pull request has been closed.
 

	
 
Me too!
 

	
 
 - and indented on second line
 

	
 

	
 
Pull request #7 "The Title" by u2 u3 (u2)
 
from https://dev.org/repo branch devbranch
 
to http://mainline.com/repo branch trunk
 

	
 

	
 
View Comment: http://pr.org/comment
 
@@ -2130,49 +2130,49 @@ View Comment: http://pr.org/comment
 
            </td>
 
        </tr>
 
    </table>
 
</center>
 
        </td>
 
    </tr>
 
</table>
 
                    </td>
 
                    <td></td>
 
                </tr>
 
                <tr>
 
                    <td height="30px" style="height:30px" colspan="3"></td>
 
                </tr>
 
            </table>
 
        </td>
 
        <td width="30px" style="width:30px"></td>
 
    </tr>
 
</table>
 
<!--/body-->
 
<!--/html-->
 
<hr/>
 
<hr/>
 
<h1>pull_request_comment, is_mention=True, status_change=None, closing_pr=True</h1>
 
<pre>
 
From: u1
 
From: u1 u1 <name@example.com>
 
To: u2@example.com
 
Subject: [Closing: Comment] repo/name PR #7 "The Title" from devbranch by u2
 
</pre>
 
<hr/>
 
<pre>http://pr.org/comment
 

	
 
Mention in Comment on Pull Request #7 "The Title"
 

	
 

	
 
Opinionated User (jsmith):
 

	
 
The pull request has been closed.
 

	
 
Me too!
 

	
 
 - and indented on second line
 

	
 

	
 
Pull request #7 "The Title" by u2 u3 (u2)
 
from https://dev.org/repo branch devbranch
 
to http://mainline.com/repo branch trunk
 

	
 

	
 
View Comment: http://pr.org/comment
 
@@ -2314,49 +2314,49 @@ View Comment: http://pr.org/comment
 
            </td>
 
        </tr>
 
    </table>
 
</center>
 
        </td>
 
    </tr>
 
</table>
 
                    </td>
 
                    <td></td>
 
                </tr>
 
                <tr>
 
                    <td height="30px" style="height:30px" colspan="3"></td>
 
                </tr>
 
            </table>
 
        </td>
 
        <td width="30px" style="width:30px"></td>
 
    </tr>
 
</table>
 
<!--/body-->
 
<!--/html-->
 
<hr/>
 
<hr/>
 
<h1>pull_request_comment, is_mention=False, status_change='Under Review', closing_pr=True</h1>
 
<pre>
 
From: u1
 
From: u1 u1 <name@example.com>
 
To: u2@example.com
 
Subject: [Under Review, Closing: Comment] repo/name PR #7 "The Title" from devbranch by u2
 
</pre>
 
<hr/>
 
<pre>http://pr.org/comment
 

	
 
Pull Request #7 "The Title" Closed
 

	
 

	
 
Opinionated User (jsmith):
 

	
 
Status change: Under Review
 

	
 
The pull request has been closed.
 

	
 
Me too!
 

	
 
 - and indented on second line
 

	
 

	
 
Pull request #7 "The Title" by u2 u3 (u2)
 
from https://dev.org/repo branch devbranch
 
to http://mainline.com/repo branch trunk
 

	
 
@@ -2504,49 +2504,49 @@ View Comment: http://pr.org/comment
 
            </td>
 
        </tr>
 
    </table>
 
</center>
 
        </td>
 
    </tr>
 
</table>
 
                    </td>
 
                    <td></td>
 
                </tr>
 
                <tr>
 
                    <td height="30px" style="height:30px" colspan="3"></td>
 
                </tr>
 
            </table>
 
        </td>
 
        <td width="30px" style="width:30px"></td>
 
    </tr>
 
</table>
 
<!--/body-->
 
<!--/html-->
 
<hr/>
 
<hr/>
 
<h1>pull_request_comment, is_mention=True, status_change='Under Review', closing_pr=True</h1>
 
<pre>
 
From: u1
 
From: u1 u1 <name@example.com>
 
To: u2@example.com
 
Subject: [Under Review, Closing: Comment] repo/name PR #7 "The Title" from devbranch by u2
 
</pre>
 
<hr/>
 
<pre>http://pr.org/comment
 

	
 
Mention in Comment on Pull Request #7 "The Title"
 

	
 

	
 
Opinionated User (jsmith):
 

	
 
Status change: Under Review
 

	
 
The pull request has been closed.
 

	
 
Me too!
 

	
 
 - and indented on second line
 

	
 

	
 
Pull request #7 "The Title" by u2 u3 (u2)
 
from https://dev.org/repo branch devbranch
 
to http://mainline.com/repo branch trunk
 

	
 
@@ -2694,49 +2694,49 @@ View Comment: http://pr.org/comment
 
            </td>
 
        </tr>
 
    </table>
 
</center>
 
        </td>
 
    </tr>
 
</table>
 
                    </td>
 
                    <td></td>
 
                </tr>
 
                <tr>
 
                    <td height="30px" style="height:30px" colspan="3"></td>
 
                </tr>
 
            </table>
 
        </td>
 
        <td width="30px" style="width:30px"></td>
 
    </tr>
 
</table>
 
<!--/body-->
 
<!--/html-->
 
<hr/>
 
<hr/>
 
<h1>TYPE_PASSWORD_RESET</h1>
 
<pre>
 
From: u1
 
From: u1 u1 <name@example.com>
 
To: john@doe.com
 
Subject: Password reset link
 
</pre>
 
<hr/>
 
<pre>Password Reset Request
 

	
 
Hello John Doe,
 

	
 
We have received a request to reset the password for your account.
 

	
 
To set a new password, click the following link:
 

	
 
http://reset.com/decbf64715098db5b0bd23eab44bd792670ab746
 

	
 
Should you not be able to use the link above, please type the following code into the password reset form:
 
decbf64715098db5b0bd23eab44bd792670ab746
 

	
 
If it weren't you who requested the password reset, just disregard this message.
 
</pre>
 
<hr/>
 
<!--!doctype html-->
 
<!--html lang="en"-->
 
<!--head-->
 
    <!--title--><!--/title-->
kallithea/tests/models/test_notifications.py
Show inline comments
 
@@ -22,69 +22,69 @@ class TestNotifications(base.TestControl
 
                                        password='qweqwe',
 
                                        email='u1@example.com',
 
                                        firstname='u1', lastname='u1')
 
        Session().commit()
 
        self.u1 = u1.user_id
 

	
 
        u2 = UserModel().create_or_update(username='u2',
 
                                        password='qweqwe',
 
                                        email='u2@example.com',
 
                                        firstname='u2', lastname='u3')
 
        Session().commit()
 
        self.u2 = u2.user_id
 

	
 
        u3 = UserModel().create_or_update(username='u3',
 
                                        password='qweqwe',
 
                                        email='u3@example.com',
 
                                        firstname='u3', lastname='u3')
 
        Session().commit()
 
        self.u3 = u3.user_id
 

	
 
    def test_create_notification(self):
 
        with test_context(self.app):
 
            usrs = [self.u1, self.u2]
 

	
 
            def send_email(recipients, subject, body='', html_body='', headers=None, author=None):
 
            def send_email(recipients, subject, body='', html_body='', headers=None, from_name=None):
 
                assert recipients == ['u2@example.com']
 
                assert subject == 'Test Message'
 
                assert body == "hi there"
 
                assert '>hi there<' in html_body
 
                assert author.username == 'u1'
 
                assert from_name == 'u1 u1'
 
            with mock.patch.object(kallithea.lib.celerylib.tasks, 'send_email', send_email):
 
                NotificationModel().create(created_by=self.u1,
 
                                                   subject='subj', body='hi there',
 
                                                   recipients=usrs)
 

	
 
    @mock.patch.object(h, 'canonical_url', (lambda arg, **kwargs: 'http://%s/?%s' % (arg, '&'.join('%s=%s' % (k, v) for (k, v) in sorted(kwargs.items())))))
 
    def test_dump_html_mails(self):
 
        # Exercise all notification types and dump them to one big html file
 
        l = []
 

	
 
        def send_email(recipients, subject, body='', html_body='', headers=None, author=None):
 
        def send_email(recipients, subject, body='', html_body='', headers=None, from_name=None):
 
            l.append('<hr/>\n')
 
            l.append('<h1>%s</h1>\n' % desc) # desc is from outer scope
 
            l.append('<pre>\n')
 
            l.append('From: %s\n' % author.username)
 
            l.append('From: %s <name@example.com>\n' % from_name)
 
            l.append('To: %s\n' % ' '.join(recipients))
 
            l.append('Subject: %s\n' % subject)
 
            l.append('</pre>\n')
 
            l.append('<hr/>\n')
 
            l.append('<pre>%s</pre>\n' % body)
 
            l.append('<hr/>\n')
 
            l.append(html_body)
 
            l.append('<hr/>\n')
 

	
 
        with test_context(self.app):
 
            with mock.patch.object(kallithea.lib.celerylib.tasks, 'send_email', send_email):
 
                pr_kwargs = dict(
 
                    pr_nice_id='#7',
 
                    pr_title='The Title',
 
                    pr_title_short='The Title',
 
                    pr_url='http://pr.org/7',
 
                    pr_target_repo='http://mainline.com/repo',
 
                    pr_target_branch='trunk',
 
                    pr_source_repo='https://dev.org/repo',
 
                    pr_source_branch='devbranch',
 
                    pr_owner=User.get(self.u2),
 
                    pr_owner_username='u2'
 
                    )
 

	
 
@@ -138,37 +138,37 @@ class TestNotifications(base.TestControl
 
                    for param_name in ['is_mention', 'status_change', 'closing_pr']: # TODO: inline/general
 
                        if not isinstance(kwargs.get(param_name), list):
 
                            continue
 
                        new_params = []
 
                        for v in kwargs[param_name]:
 
                            for desc, type_, body, kwargs in params:
 
                                kwargs = dict(kwargs)
 
                                kwargs[param_name] = v
 
                                new_params.append(('%s, %s=%r' % (desc, param_name, v), type_, body, kwargs))
 
                        params = new_params
 

	
 
                    for desc, type_, body, kwargs in params:
 
                        # desc is used as "global" variable
 
                        NotificationModel().create(created_by=self.u1,
 
                                                           subject='unused', body=body, email_kwargs=kwargs,
 
                                                           recipients=[self.u2], type_=type_)
 

	
 
                # Email type TYPE_PASSWORD_RESET has no corresponding notification type - test it directly:
 
                desc = 'TYPE_PASSWORD_RESET'
 
                kwargs = dict(user='John Doe', reset_token='decbf64715098db5b0bd23eab44bd792670ab746', reset_url='http://reset.com/decbf64715098db5b0bd23eab44bd792670ab746')
 
                kallithea.lib.celerylib.tasks.send_email(['john@doe.com'],
 
                    "Password reset link",
 
                    EmailNotificationModel().get_email_tmpl(EmailNotificationModel.TYPE_PASSWORD_RESET, 'txt', **kwargs),
 
                    EmailNotificationModel().get_email_tmpl(EmailNotificationModel.TYPE_PASSWORD_RESET, 'html', **kwargs),
 
                    author=User.get(self.u1))
 
                    from_name=User.get(self.u1).full_name_or_username)
 

	
 
        out = '<!doctype html>\n<html lang="en">\n<head><title>Notifications</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>\n<body>\n%s\n</body>\n</html>\n' % \
 
            re.sub(r'<(/?(?:!doctype|html|head|title|meta|body)\b[^>]*)>', r'<!--\1-->', ''.join(l))
 

	
 
        outfn = os.path.join(os.path.dirname(__file__), 'test_dump_html_mails.out.html')
 
        reffn = os.path.join(os.path.dirname(__file__), 'test_dump_html_mails.ref.html')
 
        with open(outfn, 'w') as f:
 
            f.write(out)
 
        with open(reffn) as f:
 
            ref = f.read()
 
        assert ref == out # copy test_dump_html_mails.out.html to test_dump_html_mails.ref.html to update expectations
 
        os.unlink(outfn)
kallithea/tests/other/test_mail.py
Show inline comments
 
@@ -114,84 +114,84 @@ class TestMail(base.TestController):
 
        with mock.patch('kallithea.lib.celerylib.tasks.config', config_mock):
 
            kallithea.lib.celerylib.tasks.send_email(recipients, subject, body, html_body)
 

	
 
        assert smtplib_mock.lastdest == set([base.TEST_USER_ADMIN_EMAIL])
 
        assert smtplib_mock.lastsender == envelope_from
 
        assert 'From: %s' % envelope_from in smtplib_mock.lastmsg
 
        assert 'Subject: %s' % subject in smtplib_mock.lastmsg
 
        assert body in smtplib_mock.lastmsg
 
        assert html_body in smtplib_mock.lastmsg
 

	
 
    def test_send_mail_with_author(self):
 
        mailserver = 'smtp.mailserver.org'
 
        recipients = ['rcpt1', 'rcpt2']
 
        envelope_from = 'noreply@mailserver.org'
 
        subject = 'subject'
 
        body = 'body'
 
        html_body = 'html_body'
 
        author = User.get_by_username(base.TEST_USER_REGULAR_LOGIN)
 

	
 
        config_mock = {
 
            'smtp_server': mailserver,
 
            'app_email_from': envelope_from,
 
        }
 
        with mock.patch('kallithea.lib.celerylib.tasks.config', config_mock):
 
            kallithea.lib.celerylib.tasks.send_email(recipients, subject, body, html_body, author=author)
 
            kallithea.lib.celerylib.tasks.send_email(recipients, subject, body, html_body, from_name=author.full_name_or_username)
 

	
 
        assert smtplib_mock.lastdest == set(recipients)
 
        assert smtplib_mock.lastsender == envelope_from
 
        assert 'From: "Kallithea Admin (no-reply)" <%s>' % envelope_from in smtplib_mock.lastmsg
 
        assert 'Subject: %s' % subject in smtplib_mock.lastmsg
 
        assert body in smtplib_mock.lastmsg
 
        assert html_body in smtplib_mock.lastmsg
 

	
 
    def test_send_mail_with_author_full_mail_from(self):
 
        mailserver = 'smtp.mailserver.org'
 
        recipients = ['ræcpt1', 'receptor2 <rcpt2@example.com>', 'tæst@example.com', 'Tæst <test@example.com>']
 
        envelope_addr = 'noreply@mailserver.org'
 
        envelope_from = 'Söme Næme <%s>' % envelope_addr
 
        subject = 'subject'
 
        body = 'body'
 
        html_body = 'html_body'
 
        author = User.get_by_username(base.TEST_USER_REGULAR_LOGIN)
 

	
 
        config_mock = {
 
            'smtp_server': mailserver,
 
            'app_email_from': envelope_from,
 
        }
 
        with mock.patch('kallithea.lib.celerylib.tasks.config', config_mock):
 
            kallithea.lib.celerylib.tasks.send_email(recipients, subject, body, html_body, author=author)
 
            kallithea.lib.celerylib.tasks.send_email(recipients, subject, body, html_body, from_name=author.full_name_or_username)
 

	
 
        assert smtplib_mock.lastdest == set(recipients)
 
        assert smtplib_mock.lastsender == envelope_from
 
        assert 'From: "Kallithea Admin (no-reply)" <%s>' % envelope_addr in smtplib_mock.lastmsg
 
        assert 'Subject: %s' % subject in smtplib_mock.lastmsg
 
        assert body in smtplib_mock.lastmsg
 
        assert html_body in smtplib_mock.lastmsg
 

	
 
    def test_send_mail_extra_headers(self):
 
        mailserver = 'smtp.mailserver.org'
 
        recipients = ['rcpt1', 'rcpt2']
 
        envelope_from = 'noreply@mailserver.org'
 
        subject = 'subject'
 
        body = 'body'
 
        html_body = 'html_body'
 
        author = User(name='foo', lastname='(fubar) "baz"')
 
        headers = {'extra': 'yes'}
 

	
 
        config_mock = {
 
            'smtp_server': mailserver,
 
            'app_email_from': envelope_from,
 
        }
 
        with mock.patch('kallithea.lib.celerylib.tasks.config', config_mock):
 
            kallithea.lib.celerylib.tasks.send_email(recipients, subject, body, html_body,
 
                                                     author=author, headers=headers)
 
                                                     from_name=author.full_name_or_username, headers=headers)
 

	
 
        assert smtplib_mock.lastdest == set(recipients)
 
        assert smtplib_mock.lastsender == envelope_from
 
        assert r'From: "foo (fubar) \"baz\" (no-reply)" <%s>' % envelope_from in smtplib_mock.lastmsg
 
        assert 'Subject: %s' % subject in smtplib_mock.lastmsg
 
        assert body in smtplib_mock.lastmsg
 
        assert html_body in smtplib_mock.lastmsg
 
        assert 'Extra: yes' in smtplib_mock.lastmsg
 
        # verify that headers dict hasn't mutated by send_email
 
        assert headers == {'extra': 'yes'}
0 comments (0 inline, 0 general)