summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJefferson Stachelski <jstachel@thoughtworks.com>2015-04-29 17:30:14 -0300
committerJefferson Stachelski <jstachel@thoughtworks.com>2015-04-29 17:30:14 -0300
commit6202f768b15b7311bb3024da2b7f182c25f57043 (patch)
treedc3f997e7a6a479173b8ddbfc29e2476e66b4a80
parentf83ef10a3c764d734166ed924cff4fb715537e93 (diff)
#337 Created a validation to just open file that the file name starts with 'mbox' and ends with a number
-rw-r--r--service/pixelated/maintenance.py9
-rw-r--r--service/test/unit/maintenance/test_commands.py11
2 files changed, 17 insertions, 3 deletions
diff --git a/service/pixelated/maintenance.py b/service/pixelated/maintenance.py
index 909b2237..661f2576 100644
--- a/service/pixelated/maintenance.py
+++ b/service/pixelated/maintenance.py
@@ -20,6 +20,7 @@ import sys
import json
import argparse
import email
+import re
from os.path import join
from mailbox import mboxMessage
@@ -147,17 +148,23 @@ def load_mails(args, mail_paths):
for root, dirs, files in os.walk(path):
mbx = account.getMailbox('INBOX')
for file_name in files:
+ if not is_mail_file_name_valid(file_name):
+ continue
with open(join(root, file_name), 'r') as email_file:
m = email.message_from_file(email_file)
flags = ("\\RECENT",)
- yield mbx.addMessage(m.as_string(), flags=flags, notify_on_disk=False)
print 'Added message %s' % m.get('subject')
print m.as_string()
+ yield mbx.addMessage(m.as_string(), flags=flags, notify_on_disk=False)
defer.returnValue(args)
return
+def is_mail_file_name_valid(file_name):
+ return re.match('mbox[0-9]+$', file_name)
+
+
def dump_soledad(args):
leap_session, soledad = args
diff --git a/service/test/unit/maintenance/test_commands.py b/service/test/unit/maintenance/test_commands.py
index 6c881cb9..2c47da40 100644
--- a/service/test/unit/maintenance/test_commands.py
+++ b/service/test/unit/maintenance/test_commands.py
@@ -16,7 +16,7 @@
import unittest
import email
-from pixelated.maintenance import delete_all_mails, load_mails
+from pixelated.maintenance import delete_all_mails, load_mails, is_mail_file_name_valid
from pixelated.bitmask_libraries.session import LeapSession
from leap.mail.imap.account import SoledadBackedAccount
from leap.soledad.client import Soledad
@@ -80,7 +80,7 @@ class TestCommands(unittest.TestCase):
def test_load_mails_adds_mails(self):
mail_root = join(dirname(__file__), '..', 'fixtures', 'mailset')
- foo = load_mails(self.args, [mail_root])
+ load_mails(self.args, [mail_root])
self.assertTrue(self.mailbox.addMessage.called)
self.mailbox.addMessage.assert_any_call(self._mail_content(join(mail_root, 'mbox00000000')), flags=("\\RECENT",), notify_on_disk=False)
@@ -90,3 +90,10 @@ class TestCommands(unittest.TestCase):
with open(mail_file, 'r') as fp:
m = email.message_from_file(fp)
return m.as_string()
+
+ def test_the_filter_should_validate_right_file_naming(self):
+ file_name = 'mbox00000'
+
+ validation = is_mail_file_name_valid(file_name)
+
+ self.assertTrue(validation)