source: products/qPloneComments/branches/plone205-21merge/tests/helperNotify.py @ 1

Last change on this file since 1 was 1, checked in by myroslav, 19 years ago

Building directory structure

File size: 2.8 KB
Line 
1#########################################################
2##      Helper methods for testing Mail sending        ##
3#########################################################
4
5import sys
6import os, os.path
7from Products.MailHost.MailHost import MailBase
8ver = "2.0.5"
9try:
10    from Products.SecureMailHost.SecureMailHost import SecureMailBase
11    ver = "2.1"
12except ImportError:
13    pass
14
15PREFIX = os.path.abspath(os.path.dirname(__file__))
16
17def sample_file_path(file):
18    return os.path.join(PREFIX, 'sample', file)
19
20def output_file_path(file):
21    return os.path.join(PREFIX, 'output', file)
22
23def getFileContent(f_path):
24    result_f = open(f_path,"r")
25    result = result_f.read()
26    result_f.close()
27    return result
28
29def writeToFile(f_path, text):
30    result_f = open(f_path,"a")
31    result_f.write(text+'\n')
32    result_f.close()
33
34def clearFile(f_path):
35    result_f = open(f_path,"w")
36    result_f.write('')
37    result_f.close()
38
39def 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
50def 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
59def testNotMail(self):
60    result = getFileContent(output_file_path('mail.res'))
61    self.assert_(not result, "Mail was sended")
62
63def _send_MH( self, mfrom, mto, messageText ):
64    writeToFile(output_file_path('mail.res'), messageText)
65
66def _send_SMH(self, mfrom, mto, messageText, debug=False):
67    writeToFile(output_file_path('mail.res'), messageText)
68
69def send_SMH(self, message, mto=None, mfrom=None, subject=None, encode=None):
70    writeToFile(output_file_path('mail.res'), message)
71
72def 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#########################################################
Note: See TracBrowser for help on using the repository browser.