o
    \"<f"                     @   s   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 d dlmZ d d	lmZ ed
dedd dedd deddG dd deZdS )    )
PermissionUser)ContentType)TestCase)override_settings)reverse)escape)gettext_lazy)switch_user_dropdown)CategoryT)GRAPPELLI_SWITCH_USERc                 C   s   | j S N)is_superuser)user r   R/var/www/html/kck/venv/lib/python3.10/site-packages/grappelli/tests/test_switch.py<lambda>   s    r   )GRAPPELLI_SWITCH_USER_ORIGINALc                 C   s   |j o|j S r   )is_staffr   )original_userr   r   r   r   r      s    )GRAPPELLI_SWITCH_USER_TARGETzgrappelli.tests.urls)ROOT_URLCONFc                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )SwitchTestsc                 C   s   t jddd| _t jddd| _t jddd	| _d
| j_| j  t jddd| _	d
| j	_| j	  t jddd| _
d| j
_| j
  tjt}tjj|d}| jj| tdD ]}tjjd| d q`d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_typed   zCategory No %s)nameN)r   objectscreate_superusersuperuser_1superuser_2create_usereditor_1r   saveeditor_2user_1r   get_for_modelr   r   filteruser_permissionssetrangecreate)selfr#   permissionsir   r   r   setUp   s"   


zSwitchTests.setUpc                 C   sh  |  tj  d |  tj  d | jjddd | jt	d}|  |j
d t|j}d}|  || | jjd	d
d | jt	d}|  |j
d t|j}d}|  || | jjddd | jt	d}|  |j
d t|j}d}|  || | jjddd | jt	d}|  |j
d | jjddd | jjt	ddd}|  |j
d dS )z"
        Test login users
           r$   r   r   usernamepassword#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"   TfollowN)assertEqualr   r&   allcountr   clientlogingetr   status_coder
   context)r5   responsett_cmpr   r   r   test_switch_login.   s4   


zSwitchTests.test_switch_loginc                 C      t jjdd}t jjdd}| jjddd | jjdtd|jgdtd	f d
d}| |jd | dd t	|j
d D tdg | | jjddd | 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:   %s?redirect=%sgrp_switch_userargsr=   Tr@   r>   c                 S      g | ]}|j qS r   message.0mr   r   r   
<listcomp>e       zESwitchTests.test_switch_superuser001_superuser002.<locals>.<listcomp>messagesPermission denied.r   N_auth_user_idr   r&   rG   rE   rF   r   idrB   rH   listrI   _sessionintpkr5   r   target_userrJ   r   r   r   %test_switch_superuser001_superuser002X   s   (&z1SwitchTests.test_switch_superuser001_superuser002c                 C   s$  t jjdd}t jjdd}| jjddd | jjdtd|jgdtd	f d
d}| |jd | | jj	dd|j|j
d | t| jj	d |j t|j}d}| || | jjdtd|jgdtd	f d
d}| |jd | | jj	ddd | t| jj	d |j dS )zZ
        Test switching from superuser001 to Editor001

        That should work.
        r   rO   r   r   r:   rP   rQ   rR   r=   Tr@   r>   r   N)r`   r;   r^   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&   rG   rE   rF   r   r`   rB   rH   rc   r;   rd   re   r
   rI   )r5   r   rg   rJ   rK   rL   r   r   r   "test_switch_superuser001_editor001i   s   ("
(z.SwitchTests.test_switch_superuser001_editor001c                 C   s   t jjdd}t jjdd}| jjddd | jjdtd|jgdtd	f d
d}| |jd | dd t	|j
d D tddt|jd g | | jjddd | t| jjd |j dS )zy
        Test switching from superuser001 to user001

        That should not work, because user001 is not found
        r   rO   r!   r   r:   rP   rQ   rR   r=   Tr@   r>   c                 S   rT   r   rU   rW   r   r   r   rZ      r[   z@SwitchTests.test_switch_superuser001_user001.<locals>.<listcomp>r\   z8%(name)s object with primary key %(key)r does not exist.r   )r%   keyr   Nr^   )r   r&   rG   rE   rF   r   r`   rB   rH   ra   rI   rb   r   rc   rd   re   rf   r   r   r    test_switch_superuser001_user001   s   (6z,SwitchTests.test_switch_superuser001_user001c                 C   rN   )z~
        Test switching from editor001 to user001

        That should not work, because editor001 is not a superuser
        r   rO   r!   r   r:   rP   rQ   rR   r=   Tr@   r>   c                 S   rT   r   rU   rW   r   r   r   rZ      r[   z=SwitchTests.test_switch_editor001_user001.<locals>.<listcomp>r\   r]   r   Nr^   r_   rf   r   r   r   test_switch_editor001_user001   s   (&z)SwitchTests.test_switch_editor001_user001N)	__name__
__module____qualname__r8   rM   rh   ri   rk   rl   r   r   r   r   r      s    *r   N)django.contrib.auth.modelsr   r   "django.contrib.contenttypes.modelsr   django.testr   django.test.utilsr   django.urlsr   django.utils.htmlr   django.utils.translationr	   rb   grappelli.templatetags.grp_tagsr
   grappelli.tests.modelsr   r   r   r   r   r   <module>   s   