o
    bk2                     @   s   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 deZdS )zi
Tests for L{twisted.mail.scripts.mailmail}, the implementation of the
command line program I{mailmail}.
    N)StringIO)skipIf)version)Deferred)smtp)mailmail)parseOptions)Failure)platformType)MemoryReactor)TestCasec                   @   s   e Zd ZdZe Zdd Zdd Zdd Zdd	 Z	d
d Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zeedkddd Zeedkddd Zd d! Zd"d# Zd$d% Zd&S )'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                    sf   t   _ddlm}m} | j} td||d d _d _d _	 fdd} td| dS )	zz
        Override some things in mailmail, so that we capture C{stdout},
        and do not call L{reactor.stop}.
        r   )LoggertextFileLogObserver_log)observerNc                    s.   |  _ | _| _tj| |j|j|j jdS )N)reactor)	hostoptionsidentr   sendmailsendertobodymemoryReactor)r   r   r   self A/usr/lib/python3/dist-packages/twisted/mail/test/test_mailmail.pyr   3   s   z$OptionsTests.setUp.<locals>.sendmailr   )
r   outtwisted.loggerr   r   patchr   r   r   r   )r   r   r   logObserverr   r   r   r   setUp#   s   
zOptionsTests.setUpc                 C   s2   |  tdtd | ttg }| |jd dS )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   excr   r   r   test_unspecifiedRecipientsA   s   z'OptionsTests.test_unspecifiedRecipientsc                 C   "   |  ttdg}| |jd dS )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*   r   r   r   test_listQueueInformationK   s   z&OptionsTests.test_listQueueInformationc                 C   r-   )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-bsr.   Nr/   r*   r   r   r   test_stdioTransportS      z OptionsTests.test_stdioTransportc                 C   s@   |  ttdg}| |jd |  ttdg}| |jd dS )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-ir.   z-oiNr/   r*   r   r   r   test_ignoreFullStop\   s   z OptionsTests.test_ignoreFullStopc                 C   r-   )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-omr.   Nr/   r*   r   r   r   test_copyAliasedSenderg   r2   z#OptionsTests.test_copyAliasedSenderc                 C   sP   t  }| td| | ttd}| |jd | }| |dt	 d dS )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datar   r   r   test_versionp   s   zOptionsTests.test_versionc                 C   .   t d}| td| td}| |j dS )zA
        The I{-odb} flag specifies background delivery.
        r6   r$   z-odbN)r   r!   r%   r   
assertTrue
backgroundr   r$   or   r   r   test_backgroundDelivery}      z$OptionsTests.test_backgroundDeliveryc                 C   r<   )zB
        The I{-odf} flags specifies foreground delivery.
        r6   r$   z-odfN)r   r!   r%   r   assertFalser>   r?   r   r   r   test_foregroundDelivery   rB   z$OptionsTests.test_foregroundDeliveryc                 C   s4   t d}| td| td}| t|jd dS )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   r?   r   r   r   test_recipientsFromHeaders   s   z'OptionsTests.test_recipientsFromHeadersc                 C   4   t d}| td| tg d}| |jd dS )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>rE   rK   Nr   r!   r%   r   r(   r   r?   r   r   r   test_setFrom   s   zOptionsTests.test_setFromc                 C   rI   )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$   )rJ   z"Groucho <invaliduser5@example.com>rE   zinvaliduser4@example.comNrL   r?   r   r   r   !test_overrideFromFlagByFromHeader   s   z.OptionsTests.test_overrideFromFlagByFromHeaderwin32zImailmail.run() does not work on win32 due to lack of support for getuid()c                 C   st   d}t d}| td| | td| t  | | jjt  | | jj	dg | 
| jj | tj dS )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-oepr6   argvr$   rQ   N)r   r!   r%   r   runr(   r   r   getloginr   r=   printErrorsassertIsNonefailed)r   rR   r$   r   r   r   test_runErrorsToStderr   s   
z#OptionsTests.test_runErrorsToStderrc                 C   s   t d}| td| |  }t }t }t|d}|d	|| W d   n1 s0w   Y  | t
d| d}| td| t
  | | j d	 | | j d
 | | j d dS )z
        Error messages for illegal UID value, illegal GID value, and illegal
        identity entry will be sent to stderr.
        r6   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_CFGrP   rR   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   rS   assertRegexr   r8   )r   r$   filenamemyUidmyGidfrR   r   r   r   test_readInvalidConfig   s4   	
z#OptionsTests.test_readInvalidConfigc                 C   sV   ddl m} |  }t|d}|| W d   ||S 1 s"w   Y  ||S )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   )
loadConfigrY   N)twisted.mail.scripts.mailmailrh   r[   r_   r`   )r   configrh   rc   rf   r   r   r   getConfigFromFile  s   
zOptionsTests.getConfigFromFilec                 C   sN  |  d}| |jd |  d}| |jd |  d}| |jd | |jd |  d}| d|j | |jd d	d
g |  d}| |jdg |  d}| |jddg |  d}| |jddg | |jddg |  d}| |jdg |  d}| |j	dg |  d}| |jddg | |j	ddg dS )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)
rk   r(   	smarthostdomainassertNotIn
identities	allowUIDsdenyUIDs	allowGIDsdenyGIDs)r   rj   r   r   r   test_loadConfig  sX   zOptionsTests.test_loadConfigc                    sB    fdd}  td| t }d|_tt }t|| dS )z
        L{twisted.mail.scripts.mailmail.senderror} sends mail back to the
        sender if an error occurs while sending mail to the recipient.
        c                    s0     |d  |dg   | d t S )Nzpostmaster@
testsender
ValueError)rb   r(   r8   r   )r   r   	recipientr   r   r   r   r   }  s   z-OptionsTests.test_senderror.<locals>.sendmailr   r   N)r!   r   r   Optionsr   r	   r   	senderror)r   r   optsfailr   r   r   test_senderrorw  s   
zOptionsTests.test_senderrorN)__name__
__module____qualname____doc__r   r   r#   r,   r0   r1   r3   r4   r;   rA   rD   rH   rM   rN   r   r
   rX   rg   rk   r   r   r   r   r   r   r      s8    
				


6Yr   )r   r\   r%   ior   unittestr   twisted.copyrightr   twisted.internet.deferr   twisted.mailr   twisted.mail.scriptsr   ri   r   twisted.python.failurer	   twisted.python.runtimer
   twisted.test.proto_helpersr   twisted.trial.unittestr   r   r   r   r   r   <module>   s   