U
    P|j  ã                   @   sz   d Z ddlmZmZ ddlmZ ddlmZmZ ddl	m
Z
 ddlmZ G dd„ deƒZG d	d
„ d
ƒZG dd„ deƒZdS )z 
Tests for L{twisted.mail.tap}.
é    )ÚdeferÚ	endpoints)Ú	protocols)ÚOptionsÚmakeService)Ú
UsageError)ÚTestCasec                   @   sh   e Zd Z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S )ÚOptionsTestszK
    Tests for the command line option parser used for I{twistd mail}.
    c              	   C   s0   |   ¡ | _t| jdƒ�}| d¡ W 5 Q R X d S )NÚwzsomeuser:	differentuser
)ÚmktempÚaliasFilenameÚopenÚwrite)ÚselfZ	aliasFile© r   úB/tmp/pip-unpacked-wheel-audpp6lv/twisted/mail/test/test_options.pyÚsetUp   s    
zOptionsTests.setUpc                 C   s   |   ttƒ jd| jg¡ dS )zi
        Test that adding an aliases(5) file before adding a domain raises a
        UsageError.
        ú	--aliasesN)ÚassertRaisesr   r   ÚparseOptionsr   ©r   r   r   r   ÚtestAliasesWithoutDomain   s
      ÿz%OptionsTests.testAliasesWithoutDomainc                 C   s   t ƒ  ddd| jg¡ dS )zƒ
        Test that adding an aliases(5) file to an IAliasableDomain at least
        doesn't raise an unhandled exception.
        z--maildirdbmdomainzexample.com=example.comr   N)r   r   r   r   r   r   r   ÚtestAliases"   s    üÿzOptionsTests.testAliasesc                 C   sF   t ƒ }| d| dg¡ |  t|| ƒd¡ |  || d tj¡ dS )z¹
        Use L{Options} to parse a single service configuration parameter and
        verify that an endpoint of the correct type is added to the list for
        that service.
        z--ztcp:1234é   r   N©r   r   ÚassertEqualÚlenÚassertIsInstancer   ZTCP4ServerEndpoint)r   ÚserviceÚoptionsr   r   r   Ú_endpointTest0   s    zOptionsTests._endpointTestc                 C   s   |   d¡ dS )z˜
        When I{--smtp} is given a TCP endpoint description as an argument, a
        TCPServerEndpoint is added to the list of SMTP endpoints.
        ÚsmtpN©r    r   r   r   r   Útest_endpointSMTP;   s    zOptionsTests.test_endpointSMTPc                 C   s   |   d¡ dS )z˜
        When I{--pop3} is given a TCP endpoint description as an argument, a
        TCPServerEndpoint is added to the list of POP3 endpoints.
        Úpop3Nr"   r   r   r   r   Útest_endpointPOP3B   s    zOptionsTests.test_endpointPOP3c                 C   sh   t ƒ }| g ¡ |  t|d ƒd¡ |  |d d tj¡ |  t|d ƒd¡ |  |d d tj¡ dS )zO
        POP3 and SMTP each listen on a TCP4ServerEndpoint by default.
        r$   r   r   r!   Nr   ©r   r   r   r   r   Útest_protoDefaultsI   s    
zOptionsTests.test_protoDefaultsc                 C   sx   t ƒ }| dg¡ |  | dd¡g ¡ |  | dd¡g ¡ t ƒ }| dg¡ |  | dd¡g ¡ |  | dd¡g ¡ dS )zg
        The I{--no-pop3} and I{--no-smtp} options disable POP3 and SMTP
        respectively.
        ú	--no-pop3Nr$   r!   ú	--no-smtp)r   r   r   Z_getEndpointsZassertNotEqualr&   r   r   r   Útest_protoDisableV   s    zOptionsTests.test_protoDisablec                 C   s   t ƒ }|  t|jddg¡ dS )zI
        If all protocols are disabled, L{UsageError} is raised.
        r(   r)   N)r   r   r   r   r&   r   r   r   Útest_allProtosDisabledErrore   s      ÿz(OptionsTests.test_allProtosDisabledErrorc                 C   s,   t ƒ }|  t|jdg¡}|  dt|ƒ¡ dS )zu
        If I{--esmtp} is given without I{--hostname}, L{Options.parseOptions}
        raises L{UsageError}.
        z--esmtpz--esmtp requires --hostnameN)r   r   r   r   r   Ústr)r   r   Úexcr   r   r   Útest_esmtpWithoutHostnamen   s    z&OptionsTests.test_esmtpWithoutHostnamec                 C   sb   t ƒ }| ddg¡ |  t|d ƒd¡ |d d }|j}|jjj}|D ]}|  ||| ¡ qHdS )zC
        Tests that the --auth option registers a checker.
        z--authzmemory:admin:admin:bob:passwordZcredCheckersr   r   N)r   r   r   r   ZcredentialInterfacesr   Z
smtpPortalZcheckers)r   r   ÚcheckerZ
interfacesZregistered_checkersZifacer   r   r   Ú	test_authw   s    
zOptionsTests.test_authN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r    r#   r%   r'   r*   r+   r.   r0   r   r   r   r   r	      s   			r	   c                   @   s   e Zd ZdZdZdd„ ZdS )ÚSpyEndpointzG
    SpyEndpoint remembers what factory it is told to listen with.
    Nc                 C   s   || _ t d ¡S )N)ÚlisteningWithr   Zsucceed)r   Úfactoryr   r   r   ÚlistenŒ   s    zSpyEndpoint.listen)r1   r2   r3   r4   r6   r8   r   r   r   r   r5   …   s   r5   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚMakeServiceTestsz3
    Tests for L{twisted.mail.tap.makeService}
    c                 C   sb   t ƒ }t ƒ }tƒ }||g||< t|ƒ}| ¡  | ¡  |  |j¡ |  |j|¡ |  |j|¡ dS )zá
        Configure a service with two endpoints for the protocol associated with
        C{key} and verify that when the service is started a factory of type
        C{factoryClass} is used to listen on each of them.
        N)	r5   r   r   ZprivilegedStartServiceZstartServiceZ
addCleanupZstopServicer   r6   )r   ÚkeyZfactoryClassZ	cleartextÚsecureÚconfigr   r   r   r   Ú_endpointServerTest–   s    z$MakeServiceTests._endpointServerTestc                 C   s   |   dtj¡ dS )zþ
        If one or more endpoints is included in the configuration passed to
        L{makeService} for the C{"pop3"} key, a service for starting a POP3
        server is constructed for each of them and attached to the returned
        service.
        r$   N)r=   r   ZPOP3Factoryr   r   r   r   Ú	test_pop3§   s    zMakeServiceTests.test_pop3c                 C   s   |   dtj¡ dS )zÿ
        If one or more endpoints is included in the configuration passed to
        L{makeService} for the C{"smtp"} key, a service for starting an SMTP
        server is constructed for each of them and attached to the returned
        service.
        r!   N)r=   r   ZSMTPFactoryr   r   r   r   Ú	test_smtp°   s    zMakeServiceTests.test_smtpN)r1   r2   r3   r4   r=   r>   r?   r   r   r   r   r9   ‘   s   	r9   N)r4   Ztwisted.internetr   r   Ztwisted.mailr   Ztwisted.mail.tapr   r   Ztwisted.python.usager   Ztwisted.trial.unittestr   r	   r5   r9   r   r   r   r   Ú<module>   s   v