Commits

David North committed b6a1c0d

All messages which assert string matches now take a regex not a literal

This allows one to avoid e.g. slightly varying spam scores causing failures

  • Participants
  • Parent commits 73640d6

Comments (0)

Files changed (1)

src/eximunit/smtp.py

+import re
 import os
 import time
 import socket
 ACCEPTED = 250
 MAILBOX_UNAVAILABLE = 550
 
+def assertMatches(regex, msg, failMsg=None):
+    if re.compile(regex).match(msg) is None:
+        if failMsg:
+            raise AssertionError(failMsg)
+        else:
+            raise AssertionError('Expected match for %s but got %s' % (regex, msg))
+
 
 class SMTPTestingSession(object):
     """A single SMTP session with methods for performing commands and making
         code, resp = self.smtp.rcpt(rcpt)
         assert MAILBOX_UNAVAILABLE == code, resp
         if expectedMessage:
-            assert expectedMessage == resp, "Response message did not match expectation"
+            assertMatches(expectedMessage, resp, "Response message did not match expectation")
         return self     
 
     def randomData(self):
     def assertSpamRejected(self, expectedMessage=None):
         self.assertDataRejected('gtube.txt', expectedMessage)
 
+    def assertSpamAccepted(self):
+        return self.data(self._getData('gtube.txt'))
+
     def assertVirusRejected(self, expectedMessage=None):
         self.assertDataRejected('eicar.txt', expectedMessage)
 
+    def assertVirusAccepted(self):
+        return self.data(readFile('eicar.txt'))
+
     def assertDataRejected(self, dataFile, expectedMessage = None):
-        dataFile = os.path.join(os.path.abspath(os.path.dirname(__file__)), dataFile)
+        data = self._getData(dataFile)
+        code, resp = self.smtp.data(data)
+        assert MAILBOX_UNAVAILABLE == code, resp
+        if expectedMessage:
+            assertMatches(expectedMessage, resp)
+
+    def _getData(self, fileName):
+        dataFile = os.path.join(os.path.abspath(os.path.dirname(__file__)), fileName)
         with open(dataFile, 'r') as f:
-            code, resp = self.smtp.data(f.read())
-            assert MAILBOX_UNAVAILABLE == code, resp
-            if expectedMessage:
-                assert expectedMessage == resp, "Expected response did not match actual"
+            return f.read()
 
     def data(self, data):
         """Sends an SMTP DATA command with the given data, and asserts that it worked"""