1 | ######################################################### |
---|
2 | ## Helper methods for testing Mail sending ## |
---|
3 | ######################################################### |
---|
4 | |
---|
5 | import sys |
---|
6 | import os, os.path |
---|
7 | from Products.MailHost.MailHost import MailBase |
---|
8 | ver = "2.0.5" |
---|
9 | try: |
---|
10 | from Products.SecureMailHost.SecureMailHost import SecureMailBase |
---|
11 | ver = "2.1" |
---|
12 | except ImportError: |
---|
13 | pass |
---|
14 | |
---|
15 | PREFIX = os.path.abspath(os.path.dirname(__file__)) |
---|
16 | |
---|
17 | def verifyMail(text, mail_type): |
---|
18 | pass |
---|
19 | |
---|
20 | def sample_file_path(file): |
---|
21 | return os.path.join(PREFIX, 'sample', file) |
---|
22 | |
---|
23 | def output_file_path(file): |
---|
24 | return os.path.join(PREFIX, 'output', file) |
---|
25 | |
---|
26 | def getFileContent(f_path): |
---|
27 | result_f = open(f_path,"r") |
---|
28 | result = result_f.read() |
---|
29 | result_f.close() |
---|
30 | return result |
---|
31 | |
---|
32 | def writeToFile(f_path, text): |
---|
33 | result_f = open(f_path,"a") |
---|
34 | result_f.write(text+'\n') |
---|
35 | result_f.close() |
---|
36 | |
---|
37 | def clearFile(f_path): |
---|
38 | result_f = open(f_path,"w") |
---|
39 | result_f.write('') |
---|
40 | result_f.close() |
---|
41 | |
---|
42 | def testMailSend(self, state='approve'): |
---|
43 | result = getFileContent(output_file_path('mail.res')) |
---|
44 | sample = getFileContent(sample_file_path(state+'.mail')) |
---|
45 | # Check headers |
---|
46 | sample_headers = sample.split('\n')[:2] |
---|
47 | for header in sample_headers: |
---|
48 | self.assert_(header in result, "State:'%s'. Header '%s' not present in sended mail" % (state, header) ) |
---|
49 | # Check footer |
---|
50 | sample_footer = sample.split('\n')[-2:-1] |
---|
51 | self.assert_(sample_footer[0] in result, "State:'%s'. Footer '%s' not present in sended mail" % (state, sample_footer[0]) ) |
---|
52 | |
---|
53 | def testNotMailSend(self, state='approve'): |
---|
54 | result = getFileContent(output_file_path('mail.res')) |
---|
55 | sample = getFileContent(sample_file_path(state+'.mail')) |
---|
56 | # Check headers |
---|
57 | sample_headers = sample.split('\n')[:2] |
---|
58 | del sample_headers[1] |
---|
59 | for header in sample_headers: |
---|
60 | self.assert_(not header in result, "State:'%s'. Header '%s' present in sended mail" % (state, header) ) |
---|
61 | |
---|
62 | def testNotMail(self): |
---|
63 | result = getFileContent(output_file_path('mail.res')) |
---|
64 | self.assert_(not result, "Mail was sended") |
---|
65 | |
---|
66 | def _send_MH( self, mfrom, mto, messageText ): |
---|
67 | writeToFile(output_file_path('mail.res'), messageText) |
---|
68 | |
---|
69 | def _send_SMH(self, mfrom, mto, messageText, debug=False): |
---|
70 | writeToFile(output_file_path('mail.res'), messageText) |
---|
71 | |
---|
72 | def send_SMH(self, message, mto=None, mfrom=None, subject=None, encode=None): |
---|
73 | writeToFile(output_file_path('mail.res'), message) |
---|
74 | |
---|
75 | def prepareMailSendTest(): |
---|
76 | # patch MailHost |
---|
77 | MailBase._send = _send_MH |
---|
78 | if ver == "2.1": |
---|
79 | # patch SecureMailHost |
---|
80 | SecureMailBase.send = send_SMH |
---|
81 | SecureMailBase._send = _send_SMH |
---|
82 | # clear 'mail.res' file |
---|
83 | clearFile(output_file_path('mail.res')) |
---|