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