Merge lp:~thumper/launchpad/strip-email-attachment-path into lp:launchpad

Proposed by Tim Penhey
Status: Merged
Approved by: Robert Collins
Approved revision: no longer in the source branch.
Merged at revision: 12563
Proposed branch: lp:~thumper/launchpad/strip-email-attachment-path
Merge into: lp:launchpad
Diff against target: 66 lines (+30/-1)
2 files modified
lib/canonical/launchpad/database/message.py (+3/-1)
lib/canonical/launchpad/database/tests/test_message.py (+27/-0)
To merge this branch: bzr merge lp:~thumper/launchpad/strip-email-attachment-path
Reviewer Review Type Date Requested Status
Robert Collins (community) Approve
Ian Booth (community) *code Approve
Review via email: mp+52159@code.launchpad.net

Commit message

[r=lifeless,wallyworld][bug=221938] Strip any path information off email attachments when storing in the librarian.

Description of the change

Another relatively trivial email OOPS.

The attachment disposition may specify a path in the filename, but the librarian database table really doesn't like that, so we strip it off.

To post a comment you must log in.
Revision history for this message
Ian Booth (wallyworld) wrote :

Looks good to me. Nice test.

review: Approve (*code)
Revision history for this message
Robert Collins (lifeless) wrote :

If you use the fake librarian you can do without the commit() and the test will be faster. This is optional but *highly* recommended.

review: Approve
Revision history for this message
Tim Penhey (thumper) wrote :

I got weird failures when attempting to use the fake librarian. Didn't dive too deep as trying to clear current work in progress. Still using the real librarian.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'lib/canonical/launchpad/database/message.py'
--- lib/canonical/launchpad/database/message.py 2011-02-17 17:02:54 +0000
+++ lib/canonical/launchpad/database/message.py 2011-03-08 21:52:53 +0000
@@ -14,7 +14,6 @@
14 'UserToUserEmail',14 'UserToUserEmail',
15 ]15 ]
1616
17
18from cStringIO import StringIO as cStringIO17from cStringIO import StringIO as cStringIO
19from datetime import datetime18from datetime import datetime
20import email19import email
@@ -29,6 +28,7 @@
29 parsedate_tz,28 parsedate_tz,
30 )29 )
31from operator import attrgetter30from operator import attrgetter
31import os.path
3232
33from lazr.config import as_timedelta33from lazr.config import as_timedelta
34from lazr.enum import (34from lazr.enum import (
@@ -476,6 +476,8 @@
476 sequence += 1476 sequence += 1
477 else:477 else:
478 filename = part.get_filename() or 'unnamed'478 filename = part.get_filename() or 'unnamed'
479 # Strip off any path information.
480 filename = os.path.basename(filename)
479 # Note we use the Content-Type header instead of481 # Note we use the Content-Type header instead of
480 # part.get_content_type() here to ensure we keep482 # part.get_content_type() here to ensure we keep
481 # parameters as sent. If Content-Type is None we default483 # parameters as sent. If Content-Type is None we default
482484
=== modified file 'lib/canonical/launchpad/database/tests/test_message.py'
--- lib/canonical/launchpad/database/tests/test_message.py 2010-11-06 12:50:22 +0000
+++ lib/canonical/launchpad/database/tests/test_message.py 2011-03-08 21:52:53 +0000
@@ -114,6 +114,33 @@
114 transaction.commit()114 transaction.commit()
115 self.assertEqual('This is the diff, honest.', diff.blob.read())115 self.assertEqual('This is the diff, honest.', diff.blob.read())
116116
117 def test_fromEmail_strips_attachment_paths(self):
118 # Build a simple multipart message with a plain text first part
119 # and an text/x-diff attachment.
120 sender = self.factory.makePerson()
121 msg = MIMEMultipart()
122 msg['Message-Id'] = make_msgid('launchpad')
123 msg['Date'] = formatdate()
124 msg['To'] = 'to@example.com'
125 msg['From'] = sender.preferredemail.email
126 msg['Subject'] = 'Sample'
127 msg.attach(MIMEText('This is the body of the email.'))
128 attachment = Message()
129 attachment.set_payload('This is the diff, honest.')
130 attachment['Content-Type'] = 'text/x-diff'
131 attachment['Content-Disposition'] = (
132 'attachment; filename="/tmp/foo/review.diff"')
133 msg.attach(attachment)
134 # Now create the message from the MessageSet.
135 message = MessageSet().fromEmail(msg.as_string())
136 text, diff = message.chunks
137 self.assertEqual('This is the body of the email.', text.content)
138 self.assertEqual('review.diff', diff.blob.filename)
139 self.assertEqual('text/x-diff', diff.blob.mimetype)
140 # Need to commit in order to read back out of the librarian.
141 transaction.commit()
142 self.assertEqual('This is the diff, honest.', diff.blob.read())
143
117144
118class TestMessageJob(TestCaseWithFactory):145class TestMessageJob(TestCaseWithFactory):
119 """Tests for MessageJob."""146 """Tests for MessageJob."""