Ϫfi2dZddlZddlZddlmZddlmZddlmZddl m Z ddl m Z ddl mZdd lmZdd lmZdd lmZdd lmZdd lmZGddeZy)zi Tests for L{twisted.mail.scripts.mailmail}, the implementation of the command line program I{mailmail}. N)StringIO)skipIf)version)Deferred) MemoryReactor)smtp)mailmail) parseOptions)Failure) platformType)TestCaseceZdZdZeZdZdZdZdZ dZ dZ dZ d Z d Zd Zd Zd Zeedk(ddZeedk(ddZdZdZdZy) OptionsTestsz Tests for L{parseOptions} which parses command line arguments and reads message text from stdin to produce an L{Options} instance which can be used to send a message. ct_ddlm}m}|j}j t d||d_d_d_ fd}j t d|y)zz Override some things in mailmail, so that we capture C{stdout}, and do not call L{reactor.stop}. r)LoggertextFileLogObserver_log)observerNc|_|_|_tj||j |j |jjS)N)reactor) hostoptionsidentrsendmailsendertobody memoryReactor)rrrselfs A/usr/lib/python3/dist-packages/twisted/mail/test/test_mailmail.pyrz$OptionsTests.setUp..sendmail3sJDI"DLDJ==  **  r) routtwisted.loggerrrpatchr rrr)rrr logObserverrs` r setUpzOptionsTests.setUp#s` :>)$((3  8VV[%AB     8Z2r!c|jtdtd|jtt g}|j |jdy)z If no recipients are given in the argument list and there is no recipient header in the message text, L{parseOptions} raises L{SystemExit} with a string describing the problem. stdinzSubject: foo Hello, goodbye. )zNo recipients specified.N)r$sysr assertRaises SystemExitr assertEqualargsrexcs r test_unspecifiedRecipientsz'OptionsTests.test_unspecifiedRecipientsAsC 3*S!TU L"= #@Ar!ct|jttdg}|j|jdy)z The I{-bp} option for listing queue information is unsupported and if it is passed to L{parseOptions}, L{SystemExit} is raised. z-bpzUnsupported option.Nr*r+r r,r-r.s r test_listQueueInformationz&OptionsTests.test_listQueueInformationKs/  L5'B #;r(z-odbN)rr$r)r assertTrue backgroundrr(os r test_backgroundDeliveryz$OptionsTests.test_backgroundDelivery}s7 3'    %r!ctd}|jtd|td}|j |j y)zB The I{-odf} flags specifies foreground delivery. r>r(z-odfN)rr$r)r assertFalserFrGs r test_foregroundDeliveryz$OptionsTests.test_foregroundDeliverys9 3'   &r!ctd}|jtd|td}|j t |j dy)zd The I{-t} flags specifies that recipients should be obtained from headers. zTo: Curly Cc: Larry Bcc: Moe Oh, a wise guy? r(-tN)rr$r)r r,lenrrGs r test_recipientsFromHeadersz'OptionsTests.test_recipientsFromHeaderssG    3'   QTTA&r!ctd}|jtd|tgd}|j |j dy)zw When a message has no I{From:} header, a I{From:} value can be specified with the I{-F} flag. z3To: invaliduser2@example.com Subject: A wise guy? r()-F Larry rNrTNrr$r)r r,rrGs r test_setFromzOptionsTests.test_setFroms? TU 3' I J #EFr!ctd}|jtd|tgd}|j |j dy)z The I{-F} flag specifies the From: value. However, I{-F} flag is overriden by the value of From: in the e-mail header. zLTo: Curly From: Shemp r()rSz"Groucho rNzinvaliduser4@example.comNrUrGs r !test_overrideFromFlagByFromHeaderz.OptionsTests.test_overrideFromFlagByFromHeadersE  7  3' K L #=>r!win32zImailmail.run() does not work on win32 due to lack of support for getuid()cd}td}|jtd||jtd|tj|j |j jtj|j |j jdg|j|j j|jtjy)z Call L{mailmail.run}, and specify I{-oep} to print errors to stderr. The sender, to, and printErrors options should be set and there should be no failure. ztest_mailmail.pyinvaliduser2@example.comz-oepr>argvr(r\N)rr$r)r runr,rrgetloginrrE printErrors assertIsNonefailed)rr]r(s r test_runErrorsToStderrz#OptionsTests.test_runErrorsToStderrsH 3% 3'  ,,h.?.?.AB +E*FG  001 (//*r!ctd}|jtd||j}t j }t j }t|d5}|jdj||ddd|jtd|d}|jtd|tj|j|jjd |j|jjd |j|jjd y#1swYxYw) z Error messages for illegal UID value, illegal GID value, and illegal identity entry will be sent to stderr. r>r(wa[useraccess] allow=invaliduser2,invaliduser1 deny=invaliduser3,invaliduser4,{} order=allow,deny [groupaccess] allow=invalidgid1,invalidgid2 deny=invalidgid1,invalidgid2,{} order=deny,allow [identity] localhost=funny [addresses] smarthost=localhost default_domain=example.com N LOCAL_CFGr[r]z3Illegal UID in \[useraccess\] section: invaliduser1z3Illegal GID in \[groupaccess\] section: invalidgid1z,Illegal entry in \[identity\] section: funny)rr$r)mktemposgetuidgetgidopenwriteformatr r^ assertRegexr"r@)rr(filenamemyUidmyGidfr]s r test_readInvalidConfigz#OptionsTests.test_readInvalidConfigs 3';;=   (C  A GG /06veU/C  2 8[(3G 3%   HH    F   HH    F   HH   !Q M  s '"EEcddlm}|j}t|d5}|j |ddd||S#1swYxYw)a Read a mailmail configuration file. The mailmail script checks the twisted.mail.scripts.mailmail.GLOBAL_CFG variable and then the twisted.mail.scripts.mailmail.LOCAL_CFG variable for the path to its config file. @param config: path to config file @type config: L{str} @return: A parsed config. @rtype: L{twisted.mail.scripts.mailmail.Configuration} r) loadConfigreN)twisted.mail.scripts.mailmailrurgrkrl)rconfigrurorrs r getConfigFromFilezOptionsTests.getConfigFromFilesK =;;= (C  A GGFO (##  s AAc|jd}|j|jd|jd}|j|jd|jd}|j|jd|j|jd|jd}|j d|j |j|j dd d g|jd }|j|j d g|jd }|j|jd dg|jd}|j|j d dg|j|jddg|jd}|j|jdg|jd}|j|jdg|jd}|j|jddg|j|jddgy)zj L{twisted.mail.scripts.mailmail.loadConfig} parses the config file for mailmail. z [addresses] smarthost=localhost localhostz' [addresses] default_domain=example.comz example.comz; [addresses] smarthost=localhost default_domain=example.comz1 [identity] host1=invalid host2=username:passwordhost1host2usernamepasswordz+ [useraccess] allow=invalid1,35 order=allow#z# [useraccess] deny=35,36 order=deny$z/ [useraccess] allow=35,36 deny=37,38 order=deny%&z( [groupaccess] allow=gid1,41 order=allow)z! [groupaccess] deny=41 order=denyz6 [groupaccess] allow=41,42 deny=43,44 order=allow,deny*+,N) rxr, smarthostdomain assertNotIn identities allowUIDsdenyUIDs allowGIDsdenyGIDs)rrws r test_loadConfigzOptionsTests.test_loadConfigs6 ''   ));7''    6''   ));7  6''   &"3"34 **73j*5MN''   ))B40''   2r(3''   ))B84 2r(3''   ))B40''   2$/''   ))B84 2r(3r!cfd}jtd|tj}d|_t t }tj||y)z L{twisted.mail.scripts.mailmail.senderror} sends mail back to the sender if an error occurs while sending mail to the recipient. cj|dj|dgj|jdtS)Nz postmaster@ testsender ValueError)rnr,r@r)rr recipientrrs r rz-OptionsTests.test_senderror..sendmail}sC   V] 3   Y 7   T]]_l ;: r!rrN)r$rr Optionsrr r senderror)rroptsfails` r test_senderrorzOptionsTests.test_senderrorwsK   4X.!" z|$4&r!N)__name__ __module__ __qualname____doc__rrr&r0r4r6r9r;rCrIrLrQrVrXrr rcrsrxrrr!r rrs "OM3<B== == A&'' G ? V+ +$ V4 4 l$0W4r'r!r)rrhr)iorunittestrtwisted.copyrightrtwisted.internet.deferrtwisted.internet.testingr twisted.mailrtwisted.mail.scriptsr rvr twisted.python.failurer twisted.python.runtimer twisted.trial.unittestr rrr!r rsA   %+2)6*/+m'8m'r!