source: products/qPloneComments/branches/plone-2.1/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.5 KB
Line 
1#########################################################
2##      Helper methods for testing Mail sending        ##
3#########################################################
4
5import sys
6import os, os.path
7from Products.MailHost.MailHost import MailBase
8from Products.SecureMailHost.SecureMailHost import SecureMailBase
9
10
11PREFIX = os.path.abspath(os.path.dirname(__file__))
12
13def sample_file_path(file):
14    return os.path.join(PREFIX, 'sample', file)
15
16def output_file_path(file):
17    return os.path.join(PREFIX, 'output', file)
18
19def getFileContent(f_path):
20    result_f = open(f_path,"r")
21    result = result_f.read()
22    result_f.close()
23    return result
24
25def writeToFile(f_path, text):
26    result_f = open(f_path,"a")
27    result_f.write(text+'\n')
28    result_f.close()
29
30def clearFile(f_path):
31    result_f = open(f_path,"w")
32    result_f.write('')
33    result_f.close()
34
35def 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
46def 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
55def testNotMail(self):
56    result = getFileContent(output_file_path('mail.res'))
57    self.assert_(not result, "Mail was sended")
58
59def _send(self, mfrom, mto, messageText, debug=False):
60    writeToFile(output_file_path('mail.res'), messageText)
61
62def send(self, message, mto=None, mfrom=None, subject=None, encode=None):
63    writeToFile(output_file_path('mail.res'), message)
64
65def 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#########################################################
Note: See TracBrowser for help on using the repository browser.