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