|
35 | 35 | import os |
36 | 36 | import re |
37 | 37 | import html5lib |
| 38 | +import sys |
38 | 39 | import urllib2 |
| 40 | + |
39 | 41 | from unittest.util import strclass |
40 | 42 | from bs4 import BeautifulSoup |
41 | 43 |
|
@@ -137,6 +139,30 @@ def assertNoFormPostErrors(self, response, error_css_selector=".has-error"): |
137 | 139 |
|
138 | 140 | self.assertEqual(response.status_code, 302) |
139 | 141 |
|
| 142 | + def assertMailboxContains(self, mailbox, subject=None, text=None, count=None): |
| 143 | + """ |
| 144 | + Asserts that the given mailbox contains *count* mails with the given |
| 145 | + *subject* and body *text* (if not None). At least one of subject, |
| 146 | + text, and count must be different from None. If count is None, the |
| 147 | + filtered mailbox must be non-empty. |
| 148 | + """ |
| 149 | + if subject is None and text is None and count is None: |
| 150 | + raise self.failureException("No assertion made, both text and count is None") |
| 151 | + mlist = mailbox |
| 152 | + if subject: |
| 153 | + mlist = [ m for m in mlist if subject in m["Subject"] ] |
| 154 | + if text: |
| 155 | + mlist = [ m for m in mlist if text in m.get_payload(decode=True) ] |
| 156 | + if count and len(mlist) != count: |
| 157 | + sys.stderr.write("Wrong count in assertMailboxContains(). The complete mailbox contains %s emails:\n\n" % len(mailbox)) |
| 158 | + for m in mailbox: |
| 159 | + sys.stderr.write(m.as_string()) |
| 160 | + sys.stderr.write('\n\n') |
| 161 | + if count: |
| 162 | + self.assertEqual(len(mlist), count) |
| 163 | + else: |
| 164 | + self.assertGreater(len(mlist), 0) |
| 165 | + |
140 | 166 | def __str__(self): |
141 | 167 | return "%s (%s.%s)" % (self._testMethodName, strclass(self.__class__),self._testMethodName) |
142 | 168 |
|
|
0 commit comments