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