o
    ÌÉ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)Ú
assertTrueÚboolr   )ÚselfÚ
url_schemeÚurl© r   ú\/var/www/html/ndineBlogger/venv/lib/python3.10/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©Ú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   ó0   | j j}tddiƒ}||ƒ |  t|ƒd¡ dS )zi
        Test that attribute_rule() drops attributes for
        which no rule has been defined.
        ÚsnowmanÚbarbecueú
<b>baz</b>N©r"   Úbr   ÚassertEqualÚstr©r   ÚtagÚfnr   r   r   Útest_no_rule_for_attr!   ó   z'TestAttributeRule.test_no_rule_for_attrc                 C   r$   )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+   r0   z)TestAttributeRule.test_rule_true_for_attrc                 C   r$   )zo
        Test that attribute_rule() drops attributes
        when the corresponding rule returns False
        r1   Fr'   Nr(   r,   r   r   r   Útest_rule_false_for_attr5   r0   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.
        r1   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 )r4   r1   c                 S   s   d S ©Nr   )Úxr   r   r   Ú<lambda>Q   s    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/   r2   r3   r6   r:   r<   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 r7   )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!   r1   r>   Úclean_unknown_nodeÚbarr*   r+   r;   r   r   r   Útest_clean_unknown_nodef   s   
z'TestWhitelister.test_clean_unknown_nodec                 C   ó2   |   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>   Úclean_tag_noder*   r+   r;   r   r   r   Ú1test_clean_tag_node_cleans_nested_recognised_nodeo   s   
zATestWhitelister.test_clean_tag_node_cleans_nested_recognised_nodec                 C   rB   )zA
        <foo> tags should be removed, even when nested.
        z<b><foo>bar</foo></b>ú
<b>bar</b>NrC   r;   r   r   r   Ú6test_clean_tag_node_disallows_nested_unrecognised_nodey   s   
zFTestWhitelister.test_clean_tag_node_disallows_nested_unrecognised_nodec                 C   ó4   |   d¡}|jj}| j ||¡ |  t|ƒd¡ d S ©NrF   r@   )r!   r)   Ústringr>   Úclean_string_noder*   r+   ©r   r"   rJ   r   r   r   Ú#test_clean_string_node_does_nothing‚   ó   
z3TestWhitelister.test_clean_string_node_does_nothingc                 C   rH   rI   )r!   r)   rJ   r>   Ú
clean_noder*   r+   rL   r   r   r   Ú1test_clean_node_does_not_change_navigable_stringsˆ   rN   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   rJ   Úcleaned_stringr   r   r   Ú
test_cleanŽ   s   zTestWhitelister.test_cleanc                 C   ó    d}| j  |¡}|  |d¡ d S )NzD<b>snowman Yorkshire<!--[if gte mso 10]>MS word junk<![endif]--></b>rQ   rR   rT   r   r   r   Útest_clean_comments—   s   z#TestWhitelister.test_clean_commentsc                 C   rW   )Nz:<img alt="Arthur &quot;two sheds&quot; Jackson" sheds="2">z1<img alt="Arthur &quot;two sheds&quot; Jackson"/>rR   rT   r   r   r   Útest_quotingœ   s
   ÿzTestWhitelister.test_quotingN)r   r   r   r#   rA   rE   rG   rM   rP   rV   rX   rY   r   r   r   r   r=   b   s    	
		r=   N)Údjango.testr   Úwagtail.test.utilsr   Úwagtail.whitelistr   r   r   r   r   r   r=   r   r   r   r   Ú<module>   s    E