U
    
W[‚2  ã                   @   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 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)Úversion)ÚDeferred)Úsmtp)Úmailmail)ÚparseOptions)ÚNativeStringIO)Ú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dd„ Zedkrˆde_dd„ Zedkržde_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   )ÚtextFileLogObserverÚLoggerZ_log)ZobserverNc                    s.   | ˆ _ |ˆ _|ˆ _tj| |j|j|jˆ jdS )N)Z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   1   s     ÿz$OptionsTests.setUp.<locals>.sendmailr   )
r   ÚoutZtwisted.loggerr   r   Úpatchr   r   r   r   )r   r   r   Z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_unspecifiedRecipients;   s
    
ÿz'OptionsTests.test_unspecifiedRecipientsc                 C   s"   |   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_listQueueInformationI   s    z&OptionsTests.test_listQueueInformationc                 C   s"   |   ttdg¡}|  |jd¡ dS )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_stdioTransportR   s    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   s"   |   ttdg¡}|  |jd¡ dS )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_copyAliasedSenderh   s    z#OptionsTests.test_copyAliasedSenderc                 C   sN   t ƒ }|  td|¡ |  ttd¡}|  |jd¡ | ¡ }|  |d 	t
¡¡ 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ÚgetvalueÚformatr   )r   r   ZsystemExitCodeÚdatar   r   r   Útest_versionr   s    ÿzOptionsTests.test_versionc                 C   s.   t dƒ}|  td|¡ tdƒ}|  |j¡ dS )zA
        The I{-odb} flag specifies background delivery.
        Ú
r   z-odbN)r   r   r   r   Ú
assertTrueÚ
background©r   r   Úor   r   r   Útest_backgroundDelivery   s    z$OptionsTests.test_backgroundDeliveryc                 C   s.   t dƒ}|  td|¡ tdƒ}|  |j¡ dS )zB
        The I{-odf} flags specifies foreground delivery.
        r3   r   z-odfN)r   r   r   r   ZassertFalser5   r6   r   r   r   Útest_foregroundDelivery‹   s    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   r6   r   r   r   Útest_recipientsFromHeaders•   s    ÿz'OptionsTests.test_recipientsFromHeadersc                 C   s6   t dƒ}|  td|¡ tdddgƒ}|  |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   ú-Fz Larry <invaliduser1@example.com>r:   N©r   r   r   r   r"   r   r6   r   r   r   Útest_setFrom¥   s    ÿzOptionsTests.test_setFromc                 C   s6   t dƒ}|  td|¡ tdddgƒ}|  |jd¡ dS )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   r>   z"Groucho <invaliduser5@example.com>r:   zinvaliduser4@example.comNr?   r6   r   r   r   Ú!test_overrideFromFlagByFromHeader²   s    ÿz.OptionsTests.test_overrideFromFlagByFromHeaderc                 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-oepr3   Úargvr   rC   N)r   r   r   r   Úrunr"   r   r   Úgetloginr   r4   ZprintErrorsZassertIsNoneZfailed)r   rD   r   r   r   r   Útest_runErrorsToStderr¿   s    z#OptionsTests.test_runErrorsToStderrZwin32zImailmail.run() does not work on win32 due to lack of support for getuid()c              	   C   s¸   t dƒ}|  td|¡ |  ¡ }t ¡ }t ¡ }t|dƒ}| d 	||¡¡ W 5 Q R X |  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.
        r3   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
Z	LOCAL_CFGrB   rD   z3Illegal UID in \[useraccess\] section: invaliduser1z3Illegal GID in \[groupaccess\] section: invalidgid1z,Illegal entry in \[identity\] section: funnyN)r   r   r   ÚmktempÚosÚgetuidÚgetgidÚopenÚwriter0   r   rE   ÚassertRegexr   r/   )r   r   ÚfilenameZmyUidZmyGidÚfrD   r   r   r   Útest_readInvalidConfig×   s.     ôÿÿÿz#OptionsTests.test_readInvalidConfigc              	   C   s<   ddl m} |  ¡ }t|dƒ}| |¡ W 5 Q R X ||ƒ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   )Ú
loadConfigrH   )Útwisted.mail.scripts.mailmailrS   rI   rM   rN   )r   ÚconfigrS   rP   rQ   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=localhostZ	localhostz'
[addresses]
default_domain=example.comzexample.comz;
[addresses]
smarthost=localhost
default_domain=example.comz1
[identity]
host1=invalid
host2=username:passwordZhost1Zhost2ZusernameZ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)
rV   r"   Z	smarthostZdomainZassertNotInZ
identitiesZ	allowUIDsZdenyUIDsZ	allowGIDsZdenyGIDs)r   rU   r   r   r   Útest_loadConfig&  s0    









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)rO   r"   r/   r   )r   r   Z	recipientr   r   r   r   r   q  s    z-OptionsTests.test_senderror.<locals>.sendmailr   r`   N)r   r   r   ZOptionsr   r   ra   Z	senderror)r   r   ZoptsZfailr   r   r   Útest_senderrorl  s    
zOptionsTests.test_senderrorN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   r   r&   r)   r*   r+   r,   r2   r8   r9   r=   r@   rA   rG   r	   ÚskiprR   rV   r_   rb   r   r   r   r   r      s0   	



ÿ1Fr   )rf   rJ   r   Ztwisted.copyrightr   Ztwisted.internet.deferr   Ztwisted.mailr   Ztwisted.mail.scriptsr   rT   r   Ztwisted.python.compatr   Ztwisted.python.failurer   Ztwisted.python.runtimer	   Ztwisted.test.proto_helpersr
   Ztwisted.trial.unittestr   r   r   r   r   r   Ú<module>   s   