| 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 | ALL_PROPS = ['enable_approve_user_notification', 'enable_reply_user_notification', |
|---|
| 18 | 'enable_rejected_user_notification','enable_moderation', |
|---|
| 19 | 'require_email', 'enable_anonymous_commenting', |
|---|
| 20 | 'enable_published_notification', 'enable_approve_notification'] |
|---|
| 21 | |
|---|
| 22 | def sample_file_path(file): |
|---|
| 23 | return os.path.join(PREFIX, 'sample', file) |
|---|
| 24 | |
|---|
| 25 | def output_file_path(file): |
|---|
| 26 | return os.path.join(PREFIX, 'output', file) |
|---|
| 27 | |
|---|
| 28 | def getFileContent(f_path): |
|---|
| 29 | result_f = open(f_path,"r") |
|---|
| 30 | result = result_f.read() |
|---|
| 31 | result_f.close() |
|---|
| 32 | return result |
|---|
| 33 | |
|---|
| 34 | def writeToFile(f_path, text): |
|---|
| 35 | result_f = open(f_path,'w') |
|---|
| 36 | result_f.write(text+'\n') |
|---|
| 37 | result_f.close() |
|---|
| 38 | |
|---|
| 39 | def clearFile(f_path): |
|---|
| 40 | result_f = open(f_path,"w") |
|---|
| 41 | result_f.write('') |
|---|
| 42 | result_f.close() |
|---|
| 43 | |
|---|
| 44 | def _send_MH( self, mfrom, mto, messageText ): |
|---|
| 45 | files = [f for f in os.listdir(output_file_path("")) if f.startswith('mail')] |
|---|
| 46 | files.sort() |
|---|
| 47 | fn = files and (files[-1]+ '1') or 'mail' |
|---|
| 48 | writeToFile(output_file_path(fn), messageText) |
|---|
| 49 | |
|---|
| 50 | def _send_SMH(self, mfrom, mto, messageText, debug=False): |
|---|
| 51 | files = [f for f in os.listdir(output_file_path("")) if f.startswith('mail')] |
|---|
| 52 | files.sort() |
|---|
| 53 | fn = files and (files[-1]+ '1') or 'mail' |
|---|
| 54 | writeToFile(output_file_path(fn), messageText) |
|---|
| 55 | |
|---|
| 56 | def send_SMH(self, message, mto=None, mfrom=None, subject=None, encode=None): |
|---|
| 57 | files = [f for f in os.listdir(output_file_path("")) if f.startswith('mail')] |
|---|
| 58 | files.sort() |
|---|
| 59 | fn = files and (files[-1]+ '1') or 'mail' |
|---|
| 60 | writeToFile(output_file_path(fn), message) |
|---|
| 61 | |
|---|
| 62 | def prepareMailSendTest(): |
|---|
| 63 | # patch MailHost |
|---|
| 64 | MailBase._send = _send_MH |
|---|
| 65 | if ver == "2.1": |
|---|
| 66 | # patch SecureMailHost |
|---|
| 67 | SecureMailBase.send = send_SMH |
|---|
| 68 | SecureMailBase._send = _send_SMH |
|---|
| 69 | |
|---|
| 70 | def setProperties(prop_sheet, *props): |
|---|
| 71 | for p in ALL_PROPS: |
|---|
| 72 | prop_sheet._updateProperty(p, p in props) |
|---|
| 73 | |
|---|
| 74 | def testMailExistance(): |
|---|
| 75 | for f in os.listdir(output_file_path("")): |
|---|
| 76 | if f.startswith('mail'): |
|---|
| 77 | return True |
|---|
| 78 | return False |
|---|
| 79 | |
|---|
| 80 | def getMails(): |
|---|
| 81 | return [file(output_file_path(f),'r').read() for f in os.listdir(output_file_path("")) if f.startswith('mail')] |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | def cleanOutputDir(): |
|---|
| 85 | for f in os.listdir(output_file_path("")): |
|---|
| 86 | if f.startswith('mail'): os.remove(output_file_path(f)) |
|---|