Mercurial > p > roundup > code
comparison test/test_mailgw.py @ 4342:94c992852f12
add in-memory hyperdb implementation to speed up testing
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Tue, 02 Feb 2010 04:44:18 +0000 |
| parents | ac3f80e39d7a |
| children | 7a0fe3cff4de |
comparison
equal
deleted
inserted
replaced
| 4341:7f67092fe03d | 4342:94c992852f12 |
|---|---|
| 24 from roundup.mailgw import MailGW, Unauthorized, uidFromAddress, \ | 24 from roundup.mailgw import MailGW, Unauthorized, uidFromAddress, \ |
| 25 parseContent, IgnoreLoop, IgnoreBulk, MailUsageError, MailUsageHelp | 25 parseContent, IgnoreLoop, IgnoreBulk, MailUsageError, MailUsageHelp |
| 26 from roundup import init, instance, password, rfc2822, __version__ | 26 from roundup import init, instance, password, rfc2822, __version__ |
| 27 from roundup.anypy.sets_ import set | 27 from roundup.anypy.sets_ import set |
| 28 | 28 |
| 29 import db_test_base | 29 #import db_test_base |
| 30 import memorydb | |
| 30 | 31 |
| 31 class Message(rfc822.Message): | 32 class Message(rfc822.Message): |
| 32 """String-based Message class with equivalence test.""" | 33 """String-based Message class with equivalence test.""" |
| 33 def __init__(self, s): | 34 def __init__(self, s): |
| 34 rfc822.Message.__init__(self, StringIO(s.strip())) | 35 rfc822.Message.__init__(self, StringIO(s.strip())) |
| 35 | 36 |
| 36 def __eq__(self, other): | 37 def __eq__(self, other): |
| 37 return (self.dict == other.dict and | 38 return (self.dict == other.dict and |
| 38 self.fp.read() == other.fp.read()) | 39 self.fp.read() == other.fp.read()) |
| 40 | |
| 41 class Tracker(object): | |
| 42 def open(self, journaltag): | |
| 43 return self.db | |
| 39 | 44 |
| 40 class DiffHelper: | 45 class DiffHelper: |
| 41 def compareMessages(self, new, old): | 46 def compareMessages(self, new, old): |
| 42 """Compare messages for semantic equivalence.""" | 47 """Compare messages for semantic equivalence.""" |
| 43 new, old = Message(new), Message(old) | 48 new, old = Message(new), Message(old) |
| 113 class MailgwTestCase(unittest.TestCase, DiffHelper): | 118 class MailgwTestCase(unittest.TestCase, DiffHelper): |
| 114 count = 0 | 119 count = 0 |
| 115 schema = 'classic' | 120 schema = 'classic' |
| 116 def setUp(self): | 121 def setUp(self): |
| 117 MailgwTestCase.count = MailgwTestCase.count + 1 | 122 MailgwTestCase.count = MailgwTestCase.count + 1 |
| 118 self.dirname = '_test_mailgw_%s'%self.count | 123 |
| 119 # set up and open a tracker | 124 # and open the database / "instance" |
| 120 self.instance = db_test_base.setupTracker(self.dirname) | 125 self.db = memorydb.create('admin') |
| 121 | 126 self.instance = Tracker() |
| 122 # and open the database | 127 self.instance.db = self.db |
| 123 self.db = self.instance.open('admin') | 128 self.instance.config = self.db.config |
| 129 self.instance.MailGW = MailGW | |
| 130 | |
| 124 self.chef_id = self.db.user.create(username='Chef', | 131 self.chef_id = self.db.user.create(username='Chef', |
| 125 address='chef@bork.bork.bork', realname='Bork, Chef', roles='User') | 132 address='chef@bork.bork.bork', realname='Bork, Chef', roles='User') |
| 126 self.richard_id = self.db.user.create(username='richard', | 133 self.richard_id = self.db.user.create(username='richard', |
| 127 address='richard@test.test', roles='User') | 134 address='richard@test.test', roles='User') |
| 128 self.mary_id = self.db.user.create(username='mary', | 135 self.mary_id = self.db.user.create(username='mary', |
| 133 | 140 |
| 134 def tearDown(self): | 141 def tearDown(self): |
| 135 if os.path.exists(SENDMAILDEBUG): | 142 if os.path.exists(SENDMAILDEBUG): |
| 136 os.remove(SENDMAILDEBUG) | 143 os.remove(SENDMAILDEBUG) |
| 137 self.db.close() | 144 self.db.close() |
| 138 try: | |
| 139 shutil.rmtree(self.dirname) | |
| 140 except OSError, error: | |
| 141 if error.errno not in (errno.ENOENT, errno.ESRCH): raise | |
| 142 | 145 |
| 143 def _handle_mail(self, message): | 146 def _handle_mail(self, message): |
| 144 # handler will open a new db handle. On single-threaded | |
| 145 # databases we'll have to close our current connection | |
| 146 self.db.commit() | |
| 147 self.db.close() | |
| 148 handler = self.instance.MailGW(self.instance) | 147 handler = self.instance.MailGW(self.instance) |
| 149 handler.trapExceptions = 0 | 148 handler.trapExceptions = 0 |
| 150 ret = handler.main(StringIO(message)) | 149 return handler.main(StringIO(message)) |
| 151 # handler had its own database, open new connection | |
| 152 self.db = self.instance.open('admin') | |
| 153 return ret | |
| 154 | 150 |
| 155 def _get_mail(self): | 151 def _get_mail(self): |
| 156 f = open(SENDMAILDEBUG) | 152 f = open(SENDMAILDEBUG) |
| 157 try: | 153 try: |
| 158 return f.read() | 154 return f.read() |
| 170 Subject: [issue] Testing... | 166 Subject: [issue] Testing... |
| 171 | 167 |
| 172 ''') | 168 ''') |
| 173 assert not os.path.exists(SENDMAILDEBUG) | 169 assert not os.path.exists(SENDMAILDEBUG) |
| 174 self.assertEqual(self.db.issue.get(nodeid, 'title'), 'Testing...') | 170 self.assertEqual(self.db.issue.get(nodeid, 'title'), 'Testing...') |
| 171 | |
| 172 def testMessageWithFromInIt(self): | |
| 173 nodeid = self._handle_mail('''Content-Type: text/plain; | |
| 174 charset="iso-8859-1" | |
| 175 From: Chef <chef@bork.bork.bork> | |
| 176 To: issue_tracker@your.tracker.email.domain.example | |
| 177 Cc: richard@test.test | |
| 178 Reply-To: chef@bork.bork.bork | |
| 179 Message-Id: <dummy_test_message_id> | |
| 180 Subject: [issue] Testing... | |
| 181 | |
| 182 From here to there! | |
| 183 ''') | |
| 184 assert not os.path.exists(SENDMAILDEBUG) | |
| 185 msgid = self.db.issue.get(nodeid, 'msg')[0] | |
| 186 self.assertEqual(self.db.issue.get(msgid, 'content'), 'From here to there!') | |
| 175 | 187 |
| 176 def doNewIssue(self): | 188 def doNewIssue(self): |
| 177 nodeid = self._handle_mail('''Content-Type: text/plain; | 189 nodeid = self._handle_mail('''Content-Type: text/plain; |
| 178 charset="iso-8859-1" | 190 charset="iso-8859-1" |
| 179 From: Chef <chef@bork.bork.bork> | 191 From: Chef <chef@bork.bork.bork> |
| 1018 | 1030 |
| 1019 # NO NOSY MESSAGE SHOULD BE SENT! | 1031 # NO NOSY MESSAGE SHOULD BE SENT! |
| 1020 assert not os.path.exists(SENDMAILDEBUG) | 1032 assert not os.path.exists(SENDMAILDEBUG) |
| 1021 | 1033 |
| 1022 def testNewUserAuthor(self): | 1034 def testNewUserAuthor(self): |
| 1023 | |
| 1024 l = self.db.user.list() | 1035 l = self.db.user.list() |
| 1025 l.sort() | 1036 l.sort() |
| 1026 message = '''Content-Type: text/plain; | 1037 message = '''Content-Type: text/plain; |
| 1027 charset="iso-8859-1" | 1038 charset="iso-8859-1" |
| 1028 From: fubar <fubar@bork.bork.bork> | 1039 From: fubar <fubar@bork.bork.bork> |
| 1030 Message-Id: <dummy_test_message_id> | 1041 Message-Id: <dummy_test_message_id> |
| 1031 Subject: [issue] Testing... | 1042 Subject: [issue] Testing... |
| 1032 | 1043 |
| 1033 This is a test submission of a new issue. | 1044 This is a test submission of a new issue. |
| 1034 ''' | 1045 ''' |
| 1035 def hook (db, **kw): | 1046 self.db.security.role['anonymous'].permissions=[] |
| 1036 ''' set up callback for db open ''' | 1047 anonid = self.db.user.lookup('anonymous') |
| 1037 db.security.role['anonymous'].permissions=[] | 1048 self.db.user.set(anonid, roles='Anonymous') |
| 1038 anonid = db.user.lookup('anonymous') | |
| 1039 db.user.set(anonid, roles='Anonymous') | |
| 1040 self.instance.schema_hook = hook | |
| 1041 try: | 1049 try: |
| 1042 self._handle_mail(message) | 1050 self._handle_mail(message) |
| 1043 except Unauthorized, value: | 1051 except Unauthorized, value: |
| 1044 body_diff = self.compareMessages(str(value), """ | 1052 body_diff = self.compareMessages(str(value), """ |
| 1045 You are not a registered user. | 1053 You are not a registered user. |
| 1046 | 1054 |
| 1047 Unknown address: fubar@bork.bork.bork | 1055 Unknown address: fubar@bork.bork.bork |
| 1048 """) | 1056 """) |
| 1049 | |
| 1050 assert not body_diff, body_diff | 1057 assert not body_diff, body_diff |
| 1051 | |
| 1052 else: | 1058 else: |
| 1053 raise AssertionError, "Unathorized not raised when handling mail" | 1059 raise AssertionError, "Unathorized not raised when handling mail" |
| 1054 | 1060 |
| 1055 | 1061 # Add Web Access role to anonymous, and try again to make sure |
| 1056 def hook (db, **kw): | 1062 # we get a "please register at:" message this time. |
| 1057 ''' set up callback for db open ''' | 1063 p = [ |
| 1058 # Add Web Access role to anonymous, and try again to make sure | 1064 self.db.security.getPermission('Register', 'user'), |
| 1059 # we get a "please register at:" message this time. | 1065 self.db.security.getPermission('Web Access', None), |
| 1060 p = [ | 1066 ] |
| 1061 db.security.getPermission('Register', 'user'), | 1067 self.db.security.role['anonymous'].permissions=p |
| 1062 db.security.getPermission('Web Access', None), | |
| 1063 ] | |
| 1064 db.security.role['anonymous'].permissions=p | |
| 1065 self.instance.schema_hook = hook | |
| 1066 try: | 1068 try: |
| 1067 self._handle_mail(message) | 1069 self._handle_mail(message) |
| 1068 except Unauthorized, value: | 1070 except Unauthorized, value: |
| 1069 body_diff = self.compareMessages(str(value), """ | 1071 body_diff = self.compareMessages(str(value), """ |
| 1070 You are not a registered user. Please register at: | 1072 You are not a registered user. Please register at: |
| 1073 | 1075 |
| 1074 ...before sending mail to the tracker. | 1076 ...before sending mail to the tracker. |
| 1075 | 1077 |
| 1076 Unknown address: fubar@bork.bork.bork | 1078 Unknown address: fubar@bork.bork.bork |
| 1077 """) | 1079 """) |
| 1078 | |
| 1079 assert not body_diff, body_diff | 1080 assert not body_diff, body_diff |
| 1080 | |
| 1081 else: | 1081 else: |
| 1082 raise AssertionError, "Unathorized not raised when handling mail" | 1082 raise AssertionError, "Unathorized not raised when handling mail" |
| 1083 | 1083 |
| 1084 # Make sure list of users is the same as before. | 1084 # Make sure list of users is the same as before. |
| 1085 m = self.db.user.list() | 1085 m = self.db.user.list() |
| 1086 m.sort() | 1086 m.sort() |
| 1087 self.assertEqual(l, m) | 1087 self.assertEqual(l, m) |
| 1088 | 1088 |
| 1089 def hook (db, **kw): | 1089 # now with the permission |
| 1090 ''' set up callback for db open ''' | 1090 p = [ |
| 1091 # now with the permission | 1091 self.db.security.getPermission('Register', 'user'), |
| 1092 p = [ | 1092 self.db.security.getPermission('Email Access', None), |
| 1093 db.security.getPermission('Register', 'user'), | 1093 ] |
| 1094 db.security.getPermission('Email Access', None), | 1094 self.db.security.role['anonymous'].permissions=p |
| 1095 ] | |
| 1096 db.security.role['anonymous'].permissions=p | |
| 1097 self.instance.schema_hook = hook | |
| 1098 self._handle_mail(message) | 1095 self._handle_mail(message) |
| 1099 m = self.db.user.list() | 1096 m = self.db.user.list() |
| 1100 m.sort() | 1097 m.sort() |
| 1101 self.assertNotEqual(l, m) | 1098 self.assertNotEqual(l, m) |
| 1102 | 1099 |
| 1110 Message-Id: <dummy_test_message_id> | 1107 Message-Id: <dummy_test_message_id> |
| 1111 Subject: [issue] Testing... | 1108 Subject: [issue] Testing... |
| 1112 | 1109 |
| 1113 This is a test submission of a new issue. | 1110 This is a test submission of a new issue. |
| 1114 ''' | 1111 ''' |
| 1115 def hook (db, **kw): | 1112 p = [ |
| 1116 ''' set up callback for db open ''' | 1113 self.db.security.getPermission('Register', 'user'), |
| 1117 p = [ | 1114 self.db.security.getPermission('Email Access', None), |
| 1118 db.security.getPermission('Register', 'user'), | 1115 self.db.security.getPermission('Create', 'issue'), |
| 1119 db.security.getPermission('Email Access', None), | 1116 self.db.security.getPermission('Create', 'msg'), |
| 1120 db.security.getPermission('Create', 'issue'), | 1117 ] |
| 1121 db.security.getPermission('Create', 'msg'), | 1118 self.db.security.role['anonymous'].permissions = p |
| 1122 ] | |
| 1123 db.security.role['anonymous'].permissions = p | |
| 1124 self.instance.schema_hook = hook | |
| 1125 self._handle_mail(message) | 1119 self._handle_mail(message) |
| 1126 m = set(self.db.user.list()) | 1120 m = set(self.db.user.list()) |
| 1127 new = list(m - l)[0] | 1121 new = list(m - l)[0] |
| 1128 name = self.db.user.get(new, 'realname') | 1122 name = self.db.user.get(new, 'realname') |
| 1129 self.assertEquals(name, 'H€llo') | 1123 self.assertEquals(name, 'H€llo') |
