
    Vhi2                         d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ  G d de      Zy)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)TestCasec                       e Zd ZdZ e       Zd Zd Zd Zd Z	d Z
d Zd Zd	 Zd
 Zd Zd Zd Z eedk(  d      d        Z eedk(  d      d        Zd Zd Zd Zy)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.
    c                      t                _        ddlm}m}  | j                        } j                  t        d ||             d _        d _        d _	         fd} j                  t        d|       y)zz
        Override some things in mailmail, so that we capture C{stdout},
        and do not call L{reactor.stop}.
        r   )LoggertextFileLogObserver_log)observerNc                     | _         |_        |_        t        j                  | |j
                  |j                  |j                  j                        S )N)reactor)	hostoptionsidentr   sendmailsendertobodymemoryReactor)r   r   r   selfs      O/home/dcms/DCMS/lib/python3.12/site-packages/twisted/mail/test/test_mailmail.pyr   z$OptionsTests.setUp.<locals>.sendmail3   sJ    DI"DLDJ==

**     r   )
r   outtwisted.loggerr   r   patchr	   r   r   r   )r   r   r   logObserverr   s   `    r    setUpzOptionsTests.setUp#   s`    
 :>)$((3

8VV[%AB	

	 	

8Z2r!   c                     | j                  t        dt        d             | j                  t        t
        g       }| j                  |j                  d       y)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.
        stdinzSubject: foo

Hello, goodbye.
)zNo recipients specified.N)r$   sysr   assertRaises
SystemExitr
   assertEqualargsr   excs     r    test_unspecifiedRecipientsz'OptionsTests.test_unspecifiedRecipientsA   sC     	

3*S!TU
L"=#@Ar!   c                 t    | j                  t        t        dg      }| j                  |j                  d       y)z
        The I{-bp} option for listing queue information is unsupported and
        if it is passed to L{parseOptions}, L{SystemExit} is raised.
        z-bpzUnsupported option.Nr*   r+   r
   r,   r-   r.   s     r    test_listQueueInformationz&OptionsTests.test_listQueueInformationK   s/    
 
L5'B#;<r!   c                 t    | j                  t        t        dg      }| j                  |j                  d       y)z
        The I{-bs} option for using stdin and stdout as the SMTP transport
        is unsupported and if it is passed to L{parseOptions}, L{SystemExit}
        is raised.
        z-bsr2   Nr3   r.   s     r    test_stdioTransportz OptionsTests.test_stdioTransportS   /     
L5'B#;<r!   c                     | j                  t        t        dg      }| j                  |j                  d       | j                  t        t        dg      }| j                  |j                  d       y)z
        The I{-i} and I{-oi} options for ignoring C{"."} by itself on a line
        are unsupported and if either is passed to L{parseOptions},
        L{SystemExit} is raised.
        z-ir2   z-oiNr3   r.   s     r    test_ignoreFullStopz OptionsTests.test_ignoreFullStop\   sZ     
L4&A#;<
L5'B#;<r!   c                 t    | j                  t        t        dg      }| j                  |j                  d       y)z
        The I{-om} option for copying the sender if they appear in an alias
        expansion is unsupported and if it is passed to L{parseOptions},
        L{SystemExit} is raised.
        z-omr2   Nr3   r.   s     r    test_copyAliasedSenderz#OptionsTests.test_copyAliasedSenderg   r7   r!   c                    t               }| j                  t        d|       | j                  t        t
        d      }| j                  |j                  d       |j                         }| j                  |dt         d       y)z~
        The I{--version} option displays the version and raises
        L{SystemExit} with L{None} as the exit code.
        stdoutz	--versionNzmailmail version: 
)
r   r$   r)   r*   r+   r
   r,   codegetvaluer   )r   r"   systemExitCodedatas       r    test_versionzOptionsTests.test_versionp   sj    
 j

3#&**:|[Q,,d3||~!3G9B?@r!   c                     t        d      }| j                  t        d|       t        d      }| j	                  |j
                         y)zA
        The I{-odb} flag specifies background delivery.
        r>   r(   z-odbN)r   r$   r)   r
   
assertTrue
backgroundr   r(   os      r    test_backgroundDeliveryz$OptionsTests.test_backgroundDelivery}   s7     

3' %r!   c                     t        d      }| j                  t        d|       t        d      }| j	                  |j
                         y)zB
        The I{-odf} flags specifies foreground delivery.
        r>   r(   z-odfN)r   r$   r)   r
   assertFalserF   rG   s      r    test_foregroundDeliveryz$OptionsTests.test_foregroundDelivery   s9     

3' &r!   c                     t        d      }| j                  t        d|       t        d      }| j	                  t        |j                        d       y)zd
        The I{-t} flags specifies that recipients should be obtained
        from headers.
        zTo: Curly <invaliduser2@example.com>
Cc: Larry <invaliduser1@example.com>
Bcc: Moe <invaliduser3@example.com>

Oh, a wise guy?
r(   -t   N)r   r$   r)   r
   r,   lenr   rG   s      r    test_recipientsFromHeadersz'OptionsTests.test_recipientsFromHeaders   sG    
  
 	

3'QTTA&r!   c                     t        d      }| j                  t        d|       t        g d      }| j	                  |j
                  d       y)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 <invaliduser1@example.com>rN   rT   Nr   r$   r)   r
   r,   r   rG   s      r    test_setFromzOptionsTests.test_setFrom   s?    
 TU

3'IJ#EFr!   c                     t        d      }| j                  t        d|       t        g d      }| j	                  |j
                  d       y)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 <invaliduser4@example.com>
From: Shemp <invaliduser4@example.com>
r(   )rS   z"Groucho <invaliduser5@example.com>rN   zinvaliduser4@example.comNrU   rG   s      r    !test_overrideFromFlagByFromHeaderz.OptionsTests.test_overrideFromFlagByFromHeader   sE    
 7
 	

3'KL#=>r!   win32zImailmail.run() does not work on win32 due to lack of support for getuid()c                    d}t        d      }| j                  t        d|       | j                  t        d|       t        j                          | j                  | j                  j                  t        j                                | j                  | j                  j                  dg       | j                  | j                  j                         | j                  t        j                         y)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.
        ztest_mailmail.pyinvaliduser2@example.comz-oepr>   argvr(   r\   N)r   r$   r)   r	   runr,   r   r   getloginr   rE   printErrorsassertIsNonefailed)r   r]   r(   s      r    test_runErrorsToStderrz#OptionsTests.test_runErrorsToStderr   s     H

3%

3',,h.?.?.AB+E*FG001(//*r!   c                    t        d      }| j                  t        d|       | j                         }t	        j
                         }t	        j                         }t        |d      5 }|j                  dj                  ||             ddd       | j                  t        d|       d}| j                  t        d|       t        j                          | j                  | j                  j                         d	       | j                  | j                  j                         d
       | j                  | j                  j                         d       y# 1 sw Y   xY w)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)r   r$   r)   mktemposgetuidgetgidopenwriteformatr	   r^   assertRegexr"   r@   )r   r(   filenamemyUidmyGidfr]   s          r    test_readInvalidConfigz#OptionsTests.test_readInvalidConfig   s    

3';;=				(C  	AGG/ 06veU/C	2 	

8[(3G

3%HHF	
 	HHF	
 	HH!Q	
M	 	s   '"EEc                     ddl m} | j                         }t        |d      5 }|j	                  |       ddd        ||      S # 1 sw Y   xY w)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   )
loadConfigre   N)twisted.mail.scripts.mailmailru   rg   rk   rl   )r   configru   ro   rr   s        r    getConfigFromFilezOptionsTests.getConfigFromFile  sK     	=;;=(C  	AGGFO	 (##	 	s   AAc                    | j                  d      }| j                  |j                  d       | j                  d      }| j                  |j                  d       | j                  d      }| j                  |j                  d       | j                  |j                  d       | j                  d      }| j	                  d|j
                         | j                  |j
                  d   d	d
g       | j                  d      }| j                  |j                  dg       | j                  d      }| j                  |j                  ddg       | j                  d      }| j                  |j                  ddg       | j                  |j                  ddg       | j                  d      }| j                  |j                  dg       | j                  d      }| j                  |j                  dg       | j                  d      }| j                  |j                  ddg       | j                  |j                  ddg       y)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:passwordhost1host2usernamepasswordz+
[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)
rx   r,   	smarthostdomainassertNotIn
identities	allowUIDsdenyUIDs	allowGIDsdenyGIDs)r   rw   s     r    test_loadConfigzOptionsTests.test_loadConfig  s6   
 ''

 	));7''

 	6''
 	));76''
 	&"3"34**73j*5MN''
 	))B40''
 	2r(3''
 	))B842r(3''
 	))B40''
 	2$/''
 	))B842r(3r!   c                       fd} j                  t        d|       t        j                         }d|_        t        t                     }t        j                  ||       y)z
        L{twisted.mail.scripts.mailmail.senderror} sends mail back to the
        sender if an error occurs while sending mail to the recipient.
        c                     j                  |d       j                  |dg       j                  |j                         d       t               S )Nzpostmaster@
testsender
ValueError)rn   r,   r@   r   )r   r   	recipientr   r   s       r    r   z-OptionsTests.test_senderror.<locals>.sendmail}  sC    V]3Y7T]]_l;:r!   r   r   N)r$   r   r	   Optionsr   r   r   	senderror)r   r   optsfails   `   r    test_senderrorzOptionsTests.test_senderrorw  sK    	 	

4X.!"z|$4&r!   N)__name__
__module____qualname____doc__r   r   r&   r0   r4   r6   r9   r;   rC   rI   rL   rQ   rV   rX   r   r   rc   rs   rx   r   r    r!   r    r   r      s     "OM3<B==	==A&'' G? V+	+$ V4
	4
l$0W4r'r!   r   )r   rh   r)   ior   unittestr   twisted.copyrightr   twisted.internet.deferr   twisted.internet.testingr   twisted.mailr   twisted.mail.scriptsr	   rv   r
   twisted.python.failurer   twisted.python.runtimer   twisted.trial.unittestr   r   r   r!   r    <module>r      sA   

 
 
   % + 2  ) 6 * / +m'8 m'r!   