î
#dÝVÊ"  ã               @   sé   d  d l  m Z d  d l m Z d  d l m Z m Z d  d l m Z d  d l	 m
 Z
 d  d l m Z d  d l m Z d  d l m Z d  d	 l m Z e d
 d ƒ e d d d „  ƒ e d d d „  ƒ Gd d „  d e ƒ ƒ ƒ ƒ Z d S)é    )ÚTestCase)Úoverride_settings)ÚUserÚ
Permission)ÚContentType)Úreverse)Úescape)Úugettext_lazy)ÚCategory)Úswitch_user_dropdownZGRAPPELLI_SWITCH_USERTZGRAPPELLI_SWITCH_USER_ORIGINALc             C   s   |  j  S)N)Úis_superuser)Úuser© r   úG/tmp/pip-build-0jahl3lb/django-grappelli/grappelli/tests/test_switch.pyÚ<lambda>   s    r   ZGRAPPELLI_SWITCH_USER_TARGETc             C   s   | j  o | j S)N)Úis_staffr   )Úoriginal_userr   r   r   r   r      s    c               @   s^   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 S)ÚSwitchTestszgrappelli.tests.urlsc             C   s7  t  j j d d d ƒ |  _ t  j j d d d ƒ |  _ t  j j d d d	 ƒ |  _ d
 |  j _ |  j j ƒ  t  j j d d d ƒ |  _	 d
 |  j	 _ |  j	 j ƒ  t  j j d d d ƒ |  _
 d |  j
 _ |  j
 j ƒ  t j j t ƒ } t j j d | ƒ } | |  j _ x+ t d ƒ D] } t j j d d | ƒ qWd S)z/
        Create superusers and editors
        ÚSuperuser001zsuperuser001@example.comÚsuperuser001ÚSuperuser002zsuperuser002@example.comÚsuperuser002Ú	Editor001zeditor001@example.comÚ	editor001TÚ	Editor002zeditor002@example.comÚ	editor002ÚUser001zuser001@example.comÚuser001FÚcontent_typeéd   ÚnamezCategory No %sN)r   ÚobjectsZcreate_superuserZsuperuser_1Zsuperuser_2Zcreate_userZeditor_1r   ÚsaveZeditor_2Zuser_1r   Zget_for_modelr
   r   ÚfilterZuser_permissionsÚrangeÚcreate)Úselfr   ÚpermissionsÚir   r   r   ÚsetUp   s     zSwitchTests.setUpc             C   s  |  j  t j j ƒ  j ƒ  d ƒ |  j  t j j ƒ  j ƒ  d ƒ |  j j d d d d ƒ |  j j t	 d ƒ ƒ } |  j  | j
 d ƒ t | j ƒ } d	 } |  j  | | ƒ |  j j d d
 d d ƒ |  j j t	 d ƒ ƒ } |  j  | j
 d ƒ t | j ƒ } d	 } |  j  | | ƒ |  j j d d d d ƒ |  j j t	 d ƒ ƒ } |  j  | j
 d ƒ t | j ƒ } d } |  j  | | ƒ |  j j d d d d ƒ |  j j t	 d ƒ ƒ } |  j  | j
 d ƒ |  j j d d d d ƒ |  j j t	 d ƒ d d ƒ} |  j  | j
 d ƒ d S)z"
        Test login users
        é   r   Úusernamer   Úpasswordr   z#admin:grappelli_category_changelistéÈ   a   <li><a href="/grappelli/switch/user/3/?redirect=/admin/grappelli/category/" class="grp-switch-user-is-target">Editor001</a></li><li><a href="/grappelli/switch/user/4/?redirect=/admin/grappelli/category/" class="grp-switch-user-is-target">Editor002</a></li>r   r   r   r   Ú r   r   i“  r   r   ÚfollowTN)ÚassertEqualr   r!   ÚallÚcountr
   ÚclientÚloginÚgetr   Ústatus_coder   Úcontext)r&   ÚresponseÚtÚt_cmpr   r   r   Útest_switch_login0   s4    zSwitchTests.test_switch_loginc             C   s  t  j j d d ƒ } t  j j d d ƒ } |  j j d d d d ƒ |  j j d t d d | j g ƒt d	 ƒ f d
 d ƒ} |  j | j d ƒ |  j d d „  t	 | j
 d ƒ Dƒ t d ƒ g ƒ |  j |  j j j d d ƒ d ƒ |  j t |  j j d ƒ | j ƒ d S)zÔ
        Test switching from superuser001 to superuser002

        That should not work, because one superuser is not allowed to login
        as another superuser (given the standard grappelli settings)
        r+   r   r   r,   r   z%s?redirect=%sÚgrp_switch_userÚargsz#admin:grappelli_category_changelistr/   Tr-   c             S   s   g  |  ] } | j  ‘ q Sr   )Úmessage)Ú.0Úmr   r   r   ú
<listcomp>g   s   	 zESwitchTests.test_switch_superuser001_superuser002.<locals>.<listcomp>ÚmessageszPermission denied.r   NÚ_auth_user_id)r   r!   r5   r3   r4   r   Úidr0   r6   Úlistr7   Ú_ÚsessionÚintÚpk)r&   r   Útarget_userr8   r   r   r   Ú%test_switch_superuser001_superuser002Z   s    :3"z1SwitchTests.test_switch_superuser001_superuser002c             C   s¤  t  j j d d ƒ } t  j j d d ƒ } |  j j d d d d ƒ |  j j d t d d | j g ƒt d	 ƒ f d
 d ƒ} |  j | j d ƒ |  j |  j j	 j d d ƒ i | j d 6| j
 d 6ƒ |  j t |  j j	 d ƒ | j ƒ t | j ƒ } d } |  j | | ƒ |  j j d t d d | j g ƒt d	 ƒ f d
 d ƒ} |  j | j d ƒ |  j |  j j	 j d d ƒ d ƒ |  j t |  j j	 d ƒ | j ƒ d S)zZ
        Test switching from superuser001 to Editor001

        That should work.
        r+   r   r   r,   r   z%s?redirect=%sr<   r=   z#admin:grappelli_category_changelistr/   Tr-   r   NrD   rC   a…  <li><a href="/grappelli/switch/user/1/?redirect=/admin/grappelli/category/" class="grp-switch-user-is-original">Superuser001</a></li><li><a href="/grappelli/switch/user/3/?redirect=/admin/grappelli/category/" class="grp-switch-user-is-target">Editor001</a></li><li><a href="/grappelli/switch/user/4/?redirect=/admin/grappelli/category/" class="grp-switch-user-is-target">Editor002</a></li>)r   r!   r5   r3   r4   r   rD   r0   r6   rG   r+   rH   rI   r   r7   )r&   r   rJ   r8   r9   r:   r   r   r   Ú"test_switch_superuser001_editor001k   s    :6#:"z.SwitchTests.test_switch_superuser001_editor001c             C   s'  t  j j d d ƒ } t  j j d d ƒ } |  j j d d d d ƒ |  j j d t d d | j g ƒt d	 ƒ f d
 d ƒ} |  j | j d ƒ |  j d d „  t	 | j
 d ƒ Dƒ t d ƒ i d d 6t | j ƒ d 6g ƒ |  j |  j j j d d ƒ d ƒ |  j t |  j j d ƒ | j ƒ d S)zy
        Test switching from superuser001 to user001

        That should not work, because user001 is not found
        r+   r   r   r,   r   z%s?redirect=%sr<   r=   z#admin:grappelli_category_changelistr/   Tr-   c             S   s   g  |  ] } | j  ‘ q Sr   )r>   )r?   r@   r   r   r   rA   ’   s   	 z@SwitchTests.test_switch_superuser001_user001.<locals>.<listcomp>rB   z8%(name)s object with primary key %(key)r does not exist.r   r    Úkeyr   NrC   )r   r!   r5   r3   r4   r   rD   r0   r6   rE   r7   rF   r   rG   rH   rI   )r&   r   rJ   r8   r   r   r   Ú test_switch_superuser001_user001†   s    :N"z,SwitchTests.test_switch_superuser001_user001c             C   s  t  j j d d ƒ } t  j j d d ƒ } |  j j d d d d ƒ |  j j d t d d | j g ƒt d	 ƒ f d
 d ƒ} |  j | j d ƒ |  j d d „  t	 | j
 d ƒ Dƒ t d ƒ g ƒ |  j |  j j j d d ƒ d ƒ |  j t |  j j d ƒ | j ƒ d S)z~
        Test switching from editor001 to user001

        That should not work, because editor001 is not a superuser
        r+   r   r   r,   r   z%s?redirect=%sr<   r=   z#admin:grappelli_category_changelistr/   Tr-   c             S   s   g  |  ] } | j  ‘ q Sr   )r>   )r?   r@   r   r   r   rA   ¢   s   	 z=SwitchTests.test_switch_editor001_user001.<locals>.<listcomp>rB   zPermission denied.r   NrC   )r   r!   r5   r3   r4   r   rD   r0   r6   rE   r7   rF   rG   rH   rI   )r&   r   rJ   r8   r   r   r   Útest_switch_editor001_user001–   s    :3"z)SwitchTests.test_switch_editor001_user001N)
Ú__name__Ú
__module__Ú__qualname__Úurlsr)   r;   rK   rL   rN   rO   r   r   r   r   r      s   *r   N)Zdjango.testr   Zdjango.test.utilsr   Zdjango.contrib.auth.modelsr   r   Z"django.contrib.contenttypes.modelsr   Zdjango.core.urlresolversr   Zdjango.utils.htmlr   Zdjango.utils.translationr	   rF   Zgrappelli.tests.modelsr
   Zgrappelli.templatetags.grp_tagsr   r   r   r   r   r   Ú<module>   s   