o
    ¯b  ã                   @   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   sF   |   ¡ | _t| jdƒ�}| d¡ W d   ƒ d S 1 sw   Y  d S )NÚwzsomeuser:	differentuser
)ÚmktempÚaliasFilenameÚopenÚwrite)ÚselfÚ	aliasFile© r   ú@/usr/lib/python3/dist-packages/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   ÚTCP4ServerEndpoint)r   ÚserviceÚoptionsr   r   r   Ú_endpointTest0   s   zOptionsTests._endpointTestc                 C   ó   |   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;   ó   zOptionsTests.test_endpointSMTPc                 C   r#   )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   r'   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   Ú_getEndpointsÚ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 ]
}|  ||| ¡ q$dS )zC
        Tests that the --auth option registers a checker.
        z--authzmemory:admin:admin:bob:passwordÚcredCheckersr   r   N)r   r   r   r   ÚcredentialInterfacesr    Ú
smtpPortalÚcheckers)r   r!   ÚcheckerÚ
interfacesÚregistered_checkersÚifacer   r   r   Ú	test_authw   s   
ÿzOptionsTests.test_authN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r"   r&   r)   r+   r0   r1   r4   r=   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   Úsucceed)r   Úfactoryr   r   r   ÚlistenŒ   s   
zSpyEndpoint.listen)r>   r?   r@   rA   rC   rF   r   r   r   r   rB   …   s    rB   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)	rB   r   r   ÚprivilegedStartServiceÚstartServiceÚ
addCleanupÚstopServicer   rC   )r   ÚkeyÚfactoryClassÚ	cleartextÚsecureÚconfigr    r   r   r   Ú_endpointServerTest–   s   z$MakeServiceTests._endpointServerTestc                 C   ó   |   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)rQ   r   ÚPOP3Factoryr   r   r   r   Ú	test_pop3§   ó   zMakeServiceTests.test_pop3c                 C   rR   )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)rQ   r   ÚSMTPFactoryr   r   r   r   Ú	test_smtp°   rU   zMakeServiceTests.test_smtpN)r>   r?   r@   rA   rQ   rT   rW   r   r   r   r   rG   ‘   s
    	rG   N)rA   Útwisted.internetr   r   Útwisted.mailr   Útwisted.mail.tapr   r   Útwisted.python.usager   Útwisted.trial.unittestr   r	   rB   rG   r   r   r   r   Ú<module>   s   v