Skip to content

Commit 98d9e99

Browse files
author
Johannes Gijsbers
committed
Message comparison is no longer a straight text comparison...
...but a semantic comparison. The header order changed while refactoring email sending, but this way we won't have to worry about that affecting the tests anymore.
1 parent 1be6854 commit 98d9e99

File tree

1 file changed

+63
-49
lines changed

1 file changed

+63
-49
lines changed

test/test_mailgw.py

Lines changed: 63 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,39 @@
88
# but WITHOUT ANY WARRANTY; without even the implied warranty of
99
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1010
#
11-
# $Id: test_mailgw.py,v 1.48 2003-09-06 10:37:10 jlgijsbers Exp $
11+
# $Id: test_mailgw.py,v 1.49 2003-09-07 13:08:08 jlgijsbers Exp $
1212

13-
import unittest, cStringIO, tempfile, os, shutil, errno, imp, sys, difflib
14-
import rfc822
13+
import unittest, tempfile, os, shutil, errno, imp, sys, difflib, rfc822
1514

16-
# Note: Should parse emails according to RFC2822 instead of performing a
17-
# literal string comparision. Parsing the messages allows the tests to work for
18-
# any legal serialization of an email.
19-
#try :
20-
# import email
21-
#except ImportError :
22-
# import rfc822 as email
15+
from cStringIO import StringIO
2316

2417
from roundup.mailgw import MailGW, Unauthorized, uidFromAddress
2518
from roundup import init, instance, rfc2822
2619

27-
# TODO: make this output only enough equal lines for context, not all of
28-
# them
20+
class Message(rfc822.Message):
21+
"""String-based Message class with equivalence test."""
22+
def __init__(self, s):
23+
rfc822.Message.__init__(self, StringIO(s.strip()))
24+
25+
def __eq__(self, other):
26+
del self['date'], other['date']
27+
28+
self.headers.sort()
29+
other.headers.sort()
30+
31+
self.rewindbody()
32+
other.rewindbody()
33+
34+
return (self.headers == other.headers and
35+
self.fp.read() == other.fp.read())
36+
37+
# TODO: Do a semantic diff instead of a straight text diff when a test fails.
2938
class DiffHelper:
39+
def compareMessages(self, s2, s1):
40+
"""Compare messages for semantic equivalence."""
41+
if not Message(s2) == Message(s1):
42+
self.compareStrings(s2, s1)
43+
3044
def compareStrings(self, s2, s1):
3145
'''Note the reversal of s2 and s1 - difflib.SequenceMatcher wants
3246
the first to be the "original" but in the calls in this file,
@@ -96,7 +110,7 @@ def tearDown(self):
96110
if error.errno not in (errno.ENOENT, errno.ESRCH): raise
97111

98112
def testEmptyMessage(self):
99-
message = cStringIO.StringIO('''Content-Type: text/plain;
113+
message = StringIO('''Content-Type: text/plain;
100114
charset="iso-8859-1"
101115
From: Chef <[email protected]>
102116
@@ -114,7 +128,7 @@ def testEmptyMessage(self):
114128
self.assertEqual(self.db.issue.get(nodeid, 'title'), 'Testing...')
115129

116130
def doNewIssue(self):
117-
message = cStringIO.StringIO('''Content-Type: text/plain;
131+
message = StringIO('''Content-Type: text/plain;
118132
charset="iso-8859-1"
119133
From: Chef <[email protected]>
120134
@@ -140,7 +154,7 @@ def testNewIssue(self):
140154

141155
def testNewIssueNosy(self):
142156
self.instance.config.ADD_AUTHOR_TO_NOSY = 'yes'
143-
message = cStringIO.StringIO('''Content-Type: text/plain;
157+
message = StringIO('''Content-Type: text/plain;
144158
charset="iso-8859-1"
145159
From: Chef <[email protected]>
146160
@@ -161,7 +175,7 @@ def testNewIssueNosy(self):
161175
self.assertEqual(l, ['3', '4'])
162176

163177
def testAlternateAddress(self):
164-
message = cStringIO.StringIO('''Content-Type: text/plain;
178+
message = StringIO('''Content-Type: text/plain;
165179
charset="iso-8859-1"
166180
From: John Doe <john.doe@test>
167181
@@ -181,7 +195,7 @@ def testAlternateAddress(self):
181195
"user created when it shouldn't have been")
182196

183197
def testNewIssueNoClass(self):
184-
message = cStringIO.StringIO('''Content-Type: text/plain;
198+
message = StringIO('''Content-Type: text/plain;
185199
charset="iso-8859-1"
186200
From: Chef <[email protected]>
187201
@@ -199,7 +213,7 @@ def testNewIssueNoClass(self):
199213
self.assertEqual('no error', error)
200214

201215
def testNewIssueAuthMsg(self):
202-
message = cStringIO.StringIO('''Content-Type: text/plain;
216+
message = StringIO('''Content-Type: text/plain;
203217
charset="iso-8859-1"
204218
From: Chef <[email protected]>
205219
@@ -214,7 +228,7 @@ def testNewIssueAuthMsg(self):
214228
self.db.config.MESSAGES_TO_AUTHOR = 'yes'
215229
handler.main(message)
216230

217-
self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
231+
self.compareMessages(open(os.environ['SENDMAILDEBUG']).read(),
218232
219233
TO: [email protected], mary@test, richard@test
220234
Content-Type: text/plain; charset=utf-8
@@ -256,7 +270,7 @@ def testNewIssueAuthMsg(self):
256270

257271
def testSimpleFollowup(self):
258272
self.doNewIssue()
259-
message = cStringIO.StringIO('''Content-Type: text/plain;
273+
message = StringIO('''Content-Type: text/plain;
260274
charset="iso-8859-1"
261275
From: mary <mary@test>
262276
@@ -269,7 +283,7 @@ def testSimpleFollowup(self):
269283
handler = self.instance.MailGW(self.instance, self.db)
270284
handler.trapExceptions = 0
271285
handler.main(message)
272-
self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
286+
self.compareMessages(open(os.environ['SENDMAILDEBUG']).read(),
273287
274288
TO: [email protected], richard@test
275289
Content-Type: text/plain; charset=utf-8
@@ -300,7 +314,7 @@ def testSimpleFollowup(self):
300314
def testFollowup(self):
301315
self.doNewIssue()
302316

303-
message = cStringIO.StringIO('''Content-Type: text/plain;
317+
message = StringIO('''Content-Type: text/plain;
304318
charset="iso-8859-1"
305319
From: richard <richard@test>
306320
@@ -317,7 +331,7 @@ def testFollowup(self):
317331
l.sort()
318332
self.assertEqual(l, ['3', '4', '5', '6'])
319333

320-
self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
334+
self.compareMessages(open(os.environ['SENDMAILDEBUG']).read(),
321335
322336
TO: [email protected], john@test, mary@test
323337
Content-Type: text/plain; charset=utf-8
@@ -349,7 +363,7 @@ def testFollowup(self):
349363

350364
def testFollowupTitleMatch(self):
351365
self.doNewIssue()
352-
message = cStringIO.StringIO('''Content-Type: text/plain;
366+
message = StringIO('''Content-Type: text/plain;
353367
charset="iso-8859-1"
354368
From: richard <richard@test>
355369
@@ -363,7 +377,7 @@ def testFollowupTitleMatch(self):
363377
handler.trapExceptions = 0
364378
handler.main(message)
365379

366-
self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
380+
self.compareMessages(open(os.environ['SENDMAILDEBUG']).read(),
367381
368382
TO: [email protected], john@test, mary@test
369383
Content-Type: text/plain; charset=utf-8
@@ -396,7 +410,7 @@ def testFollowupTitleMatch(self):
396410
def testFollowupNosyAuthor(self):
397411
self.doNewIssue()
398412
self.db.config.ADD_AUTHOR_TO_NOSY = 'yes'
399-
message = cStringIO.StringIO('''Content-Type: text/plain;
413+
message = StringIO('''Content-Type: text/plain;
400414
charset="iso-8859-1"
401415
From: john@test
402416
@@ -410,7 +424,7 @@ def testFollowupNosyAuthor(self):
410424
handler.trapExceptions = 0
411425
handler.main(message)
412426

413-
self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
427+
self.compareMessages(open(os.environ['SENDMAILDEBUG']).read(),
414428
415429
TO: [email protected], richard@test
416430
Content-Type: text/plain; charset=utf-8
@@ -443,7 +457,7 @@ def testFollowupNosyAuthor(self):
443457
def testFollowupNosyRecipients(self):
444458
self.doNewIssue()
445459
self.db.config.ADD_RECIPIENTS_TO_NOSY = 'yes'
446-
message = cStringIO.StringIO('''Content-Type: text/plain;
460+
message = StringIO('''Content-Type: text/plain;
447461
charset="iso-8859-1"
448462
From: richard@test
449463
@@ -458,7 +472,7 @@ def testFollowupNosyRecipients(self):
458472
handler.trapExceptions = 0
459473
handler.main(message)
460474

461-
self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
475+
self.compareMessages(open(os.environ['SENDMAILDEBUG']).read(),
462476
463477
464478
Content-Type: text/plain; charset=utf-8
@@ -492,7 +506,7 @@ def testFollowupNosyAuthorAndCopy(self):
492506
self.doNewIssue()
493507
self.db.config.ADD_AUTHOR_TO_NOSY = 'yes'
494508
self.db.config.MESSAGES_TO_AUTHOR = 'yes'
495-
message = cStringIO.StringIO('''Content-Type: text/plain;
509+
message = StringIO('''Content-Type: text/plain;
496510
charset="iso-8859-1"
497511
From: john@test
498512
@@ -506,7 +520,7 @@ def testFollowupNosyAuthorAndCopy(self):
506520
handler.trapExceptions = 0
507521
handler.main(message)
508522

509-
self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
523+
self.compareMessages(open(os.environ['SENDMAILDEBUG']).read(),
510524
511525
TO: [email protected], john@test, richard@test
512526
Content-Type: text/plain; charset=utf-8
@@ -539,7 +553,7 @@ def testFollowupNosyAuthorAndCopy(self):
539553
def testFollowupNoNosyAuthor(self):
540554
self.doNewIssue()
541555
self.instance.config.ADD_AUTHOR_TO_NOSY = 'no'
542-
message = cStringIO.StringIO('''Content-Type: text/plain;
556+
message = StringIO('''Content-Type: text/plain;
543557
charset="iso-8859-1"
544558
From: john@test
545559
@@ -553,7 +567,7 @@ def testFollowupNoNosyAuthor(self):
553567
handler.trapExceptions = 0
554568
handler.main(message)
555569

556-
self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
570+
self.compareMessages(open(os.environ['SENDMAILDEBUG']).read(),
557571
558572
TO: [email protected], richard@test
559573
Content-Type: text/plain; charset=utf-8
@@ -585,7 +599,7 @@ def testFollowupNoNosyAuthor(self):
585599
def testFollowupNoNosyRecipients(self):
586600
self.doNewIssue()
587601
self.instance.config.ADD_RECIPIENTS_TO_NOSY = 'no'
588-
message = cStringIO.StringIO('''Content-Type: text/plain;
602+
message = StringIO('''Content-Type: text/plain;
589603
charset="iso-8859-1"
590604
From: richard@test
591605
@@ -600,7 +614,7 @@ def testFollowupNoNosyRecipients(self):
600614
handler.trapExceptions = 0
601615
handler.main(message)
602616

603-
self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
617+
self.compareMessages(open(os.environ['SENDMAILDEBUG']).read(),
604618
605619
606620
Content-Type: text/plain; charset=utf-8
@@ -632,7 +646,7 @@ def testFollowupNoNosyRecipients(self):
632646
def testFollowupEmptyMessage(self):
633647
self.doNewIssue()
634648

635-
message = cStringIO.StringIO('''Content-Type: text/plain;
649+
message = StringIO('''Content-Type: text/plain;
636650
charset="iso-8859-1"
637651
From: richard <richard@test>
638652
@@ -654,7 +668,7 @@ def testFollowupEmptyMessage(self):
654668
def testNosyRemove(self):
655669
self.doNewIssue()
656670

657-
message = cStringIO.StringIO('''Content-Type: text/plain;
671+
message = StringIO('''Content-Type: text/plain;
658672
charset="iso-8859-1"
659673
From: richard <richard@test>
660674
@@ -692,7 +706,7 @@ def testNewUserAuthor(self):
692706
693707
This is a test submission of a new issue.
694708
'''
695-
message = cStringIO.StringIO(s)
709+
message = StringIO(s)
696710
handler = self.instance.MailGW(self.instance, self.db)
697711
handler.trapExceptions = 0
698712
self.assertRaises(Unauthorized, handler.main, message)
@@ -705,15 +719,15 @@ def testNewUserAuthor(self):
705719
self.db.security.role['anonymous'].permissions=[p]
706720
handler = self.instance.MailGW(self.instance, self.db)
707721
handler.trapExceptions = 0
708-
message = cStringIO.StringIO(s)
722+
message = StringIO(s)
709723
handler.main(message)
710724
m = self.db.user.list()
711725
m.sort()
712726
self.assertNotEqual(l, m)
713727

714728
def testEnc01(self):
715729
self.doNewIssue()
716-
message = cStringIO.StringIO('''Content-Type: text/plain;
730+
message = StringIO('''Content-Type: text/plain;
717731
charset="iso-8859-1"
718732
From: mary <mary@test>
719733
@@ -730,7 +744,7 @@ def testEnc01(self):
730744
handler = self.instance.MailGW(self.instance, self.db)
731745
handler.trapExceptions = 0
732746
handler.main(message)
733-
self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
747+
self.compareMessages(open(os.environ['SENDMAILDEBUG']).read(),
734748
735749
TO: [email protected], richard@test
736750
Content-Type: text/plain; charset=utf-8
@@ -761,7 +775,7 @@ def testEnc01(self):
761775

762776
def testMultipartEnc01(self):
763777
self.doNewIssue()
764-
message = cStringIO.StringIO('''Content-Type: text/plain;
778+
message = StringIO('''Content-Type: text/plain;
765779
charset="iso-8859-1"
766780
From: mary <mary@test>
767781
@@ -785,7 +799,7 @@ def testMultipartEnc01(self):
785799
handler = self.instance.MailGW(self.instance, self.db)
786800
handler.trapExceptions = 0
787801
handler.main(message)
788-
self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
802+
self.compareMessages(open(os.environ['SENDMAILDEBUG']).read(),
789803
790804
TO: [email protected], richard@test
791805
Content-Type: text/plain; charset=utf-8
@@ -815,7 +829,7 @@ def testMultipartEnc01(self):
815829

816830
def testContentDisposition(self):
817831
self.doNewIssue()
818-
message = cStringIO.StringIO('''Content-Type: text/plain;
832+
message = StringIO('''Content-Type: text/plain;
819833
charset="iso-8859-1"
820834
From: mary <mary@test>
821835
@@ -851,7 +865,7 @@ def testContentDisposition(self):
851865
def testFollowupStupidQuoting(self):
852866
self.doNewIssue()
853867

854-
message = cStringIO.StringIO('''Content-Type: text/plain;
868+
message = StringIO('''Content-Type: text/plain;
855869
charset="iso-8859-1"
856870
From: richard <richard@test>
857871
@@ -865,7 +879,7 @@ def testFollowupStupidQuoting(self):
865879
handler.trapExceptions = 0
866880
handler.main(message)
867881

868-
self.compareStrings(open(os.environ['SENDMAILDEBUG']).read(),
882+
self.compareMessages(open(os.environ['SENDMAILDEBUG']).read(),
869883
870884
871885
Content-Type: text/plain; charset=utf-8
@@ -913,7 +927,7 @@ def innerTestQuoting(self, expect):
913927

914928
messages = self.db.issue.get(nodeid, 'messages')
915929

916-
message = cStringIO.StringIO('''Content-Type: text/plain;
930+
message = StringIO('''Content-Type: text/plain;
917931
charset="iso-8859-1"
918932
From: richard <richard@test>
919933
@@ -938,7 +952,7 @@ def innerTestQuoting(self, expect):
938952
newmessages.remove(msg)
939953
messageid = newmessages[0]
940954

941-
self.compareStrings(self.db.msg.get(messageid, 'content'), expect)
955+
self.compareMessages(self.db.msg.get(messageid, 'content'), expect)
942956

943957
def testUserLookup(self):
944958
i = self.db.user.create(username='user1', address='[email protected]')
@@ -962,7 +976,7 @@ def testRFC2822(self):
962976
def testRegistrationConfirmation(self):
963977
otk = "Aj4euk4LZSAdwePohj90SME5SpopLETL"
964978
self.db.otks.set(otk, username='johannes', __time='')
965-
message = cStringIO.StringIO('''Content-Type: text/plain;
979+
message = StringIO('''Content-Type: text/plain;
966980
charset="iso-8859-1"
967981
From: Chef <[email protected]>
968982

0 commit comments

Comments
 (0)