U
    |®Þhw  ã                   @   sh   d dl mZ d dlmZ d dlmZmZmZmZ G dd„ deƒZ	G dd„ deeƒZ
G dd	„ d	eeƒZd
S )é    )ÚTestCase)ÚWagtailTestUtils)ÚWhitelisterÚallow_without_attributesÚattribute_ruleÚ	check_urlc                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚTestCheckUrlc                 C   s(   dD ]}|d }|   tt|ƒƒ¡ qd S )N)Ú ÚhttpÚhttpsÚftpÚmailtoÚtelz://www.example.com)Z
assertTrueÚboolr   )ÚselfÚ
url_schemeÚurl© r   úd/var/www/dating/data/www/fatepal.com/env/lib/python3.8/site-packages/wagtail/tests/test_whitelist.pyÚtest_allowed_url_schemes   s    z%TestCheckUrl.test_allowed_url_schemesc                 C   s   |   ttdƒƒ¡ d S )Nzinvalid://url©ZassertFalser   r   ©r   r   r   r   Útest_disallowed_url_scheme   s    z'TestCheckUrl.test_disallowed_url_schemec                 C   s   |   ttdƒƒ¡ dS )z 
        Some URL parsers do not parse 'jav	ascript:' as a valid scheme.
        Browsers, however, do. The checker needs to catch these crafty schemes
        zjav	ascript:alert('XSS')Nr   r   r   r   r   Ú!test_crafty_disallowed_url_scheme   s    z.TestCheckUrl.test_crafty_disallowed_url_schemeN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r   r      s   r   c                   @   sD   e 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S )ÚTestAttributeRulec                 C   s   |   d¡| _d S )Nú<b foo="bar">baz</b>)Úget_soupÚsoupr   r   r   r   ÚsetUp   s    zTestAttributeRule.setUpc                 C   s0   | j j}tddiƒ}||ƒ |  t|ƒd¡ dS )zi
        Test that attribute_rule() drops attributes for
        which no rule has been defined.
        ÚsnowmanZbarbecueú
<b>baz</b>N©r    Úbr   ÚassertEqualÚstr©r   ÚtagÚfnr   r   r   Útest_no_rule_for_attr!   s    z'TestAttributeRule.test_no_rule_for_attrc                 C   s0   | j j}tddiƒ}||ƒ |  t|ƒd¡ dS )zx
        Test that attribute_rule() does not change attributes
        when the corresponding rule returns True
        ÚfooTr   Nr$   r(   r   r   r   Útest_rule_true_for_attr+   s    z)TestAttributeRule.test_rule_true_for_attrc                 C   s0   | j j}tddiƒ}||ƒ |  t|ƒd¡ dS )zo
        Test that attribute_rule() drops attributes
        when the corresponding rule returns False
        r,   Fr#   Nr$   r(   r   r   r   Útest_rule_false_for_attr5   s    z*TestAttributeRule.test_rule_false_for_attrc                 C   s0   | j j}tdtiƒ}||ƒ |  t|ƒd¡ dS )ú¯
        Test that when the rule returns a callable,
        attribute_rule() replaces the attribute with
        the result of calling the callable on the attribute.
        r,   z<b foo="3">baz</b>N)r    r%   r   Úlenr&   r'   r(   r   r   r   Útest_callable_called_on_attr?   s    z.TestAttributeRule.test_callable_called_on_attrc                 C   s4   | j j}tddd„ iƒ}||ƒ |  t|ƒd¡ dS )r/   r,   c                 S   s   d S ©Nr   )Úxr   r   r   Ú<lambda>Q   ó    z>TestAttributeRule.test_callable_returns_None.<locals>.<lambda>r#   Nr$   r(   r   r   r   Útest_callable_returns_NoneJ   s    z,TestAttributeRule.test_callable_returns_Nonec                 C   s,   |   d¡}|j}t|ƒ |  t|ƒd¡ dS )zS
        Test that attribute_rule() with will drop all
        attributes.
        z/<b foo="bar" baz="quux" snowman="barbecue"></b>z<b></b>N)r   r%   r   r&   r'   ©r   r    r)   r   r   r   Útest_allow_without_attributesU   s    ÿz/TestAttributeRule.test_allow_without_attributesN)
r   r   r   r!   r+   r-   r.   r1   r6   r8   r   r   r   r   r      s   


r   c                   @   sT   e 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S )ÚTestWhitelisterc                 C   s   t ƒ | _d S r2   )r   Úwhitelisterr   r   r   r   r!   c   s    zTestWhitelister.setUpc                 C   s4   |   d¡}|j}| j d|j¡ |  t|ƒd¡ dS )zL
        Unknown node should remove a node from the parent document
        z<foo><bar>baz</bar>quux</foo>r	   z<foo>quux</foo>N)r   r,   r:   Zclean_unknown_nodeÚbarr&   r'   r7   r   r   r   Útest_clean_unknown_nodef   s    
z'TestWhitelister.test_clean_unknown_nodec                 C   s2   |   d¡}|j}| j ||¡ |  t|ƒd¡ dS )zj
        <b> tags are allowed without attributes. This remains true
        when tags are nested.
        z#<b><b class="delete me">foo</b></b>z<b><b>foo</b></b>N©r   r%   r:   Zclean_tag_noder&   r'   r7   r   r   r   Ú1test_clean_tag_node_cleans_nested_recognised_nodeo   s    
zATestWhitelister.test_clean_tag_node_cleans_nested_recognised_nodec                 C   s2   |   d¡}|j}| j ||¡ |  t|ƒd¡ dS )zA
        <foo> tags should be removed, even when nested.
        z<b><foo>bar</foo></b>ú
<b>bar</b>Nr=   r7   r   r   r   Ú6test_clean_tag_node_disallows_nested_unrecognised_nodey   s    
zFTestWhitelister.test_clean_tag_node_disallows_nested_unrecognised_nodec                 C   s4   |   d¡}|jj}| j ||¡ |  t|ƒd¡ d S ©Nr?   r;   )r   r%   Ústringr:   Zclean_string_noder&   r'   ©r   r    rB   r   r   r   Ú#test_clean_string_node_does_nothing‚   s    
z3TestWhitelister.test_clean_string_node_does_nothingc                 C   s4   |   d¡}|jj}| j ||¡ |  t|ƒd¡ d S rA   )r   r%   rB   r:   Z
clean_noder&   r'   rC   r   r   r   Ú1test_clean_node_does_not_change_navigable_stringsˆ   s    
zATestWhitelister.test_clean_node_does_not_change_navigable_stringsc                 C   s    d}| j  |¡}|  |d¡ dS )zf
        Whitelister.clean should remove disallowed tags and attributes from
        a string
        z7<b foo="bar">snowman <barbecue>Yorkshire</barbecue></b>ú<b>snowman Yorkshire</b>N©r:   Úcleanr&   ©r   rB   Zcleaned_stringr   r   r   Ú
test_cleanŽ   s    zTestWhitelister.test_cleanc                 C   s    d}| j  |¡}|  |d¡ d S )NzD<b>snowman Yorkshire<!--[if gte mso 10]>MS word junk<![endif]--></b>rF   rG   rI   r   r   r   Útest_clean_comments—   s    z#TestWhitelister.test_clean_commentsc                 C   s    d}| j  |¡}|  |d¡ d S )Nz:<img alt="Arthur &quot;two sheds&quot; Jackson" sheds="2">z1<img alt="Arthur &quot;two sheds&quot; Jackson"/>rG   rI   r   r   r   Útest_quotingœ   s     ÿzTestWhitelister.test_quotingN)r   r   r   r!   r<   r>   r@   rD   rE   rJ   rK   rL   r   r   r   r   r9   b   s   	
		r9   N)Zdjango.testr   Zwagtail.test.utilsr   Zwagtail.whitelistr   r   r   r   r   r   r9   r   r   r   r   Ú<module>   s
   E