ó
ø¢TQc           @  s!  d  d l  m Z d  d l m Z d  d l m Z d  d l m Z m Z m	 Z	 m
 Z
 d  d l m Z d  d l m Z 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 e f d „  ƒ  YZ e d e e f d „  ƒ  Yƒ Z e d d ƒ d e e f d „  ƒ  Yƒ Z e d d ƒ d e e f d „  ƒ  Yƒ Z e d d ƒ d e f d „  ƒ  Yƒ Z  d e f d „  ƒ  YZ! d e f d „  ƒ  YZ" e d e f d „  ƒ  Yƒ Z# d  e f d! „  ƒ  YZ$ e e d" g  ƒ d# e f d$ „  ƒ  Yƒ ƒ Z% e d% e f d& „  ƒ  Yƒ Z& d' S((   iÿÿÿÿ(   t   unicode_literals(   t   date(   t   settings(   t   Usert   Groupt
   Permissiont   AnonymousUser(   t   skipIfCustomUser(   t   ExtensionUsert   CustomPermissionsUsert
   CustomUser(   t   ContentType(   t   ImproperlyConfigured(   t   authenticate(   t   TestCase(   t   override_settingst   BaseModelBackendTestc           B  sJ   e  Z d  Z d Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   uñ   
    A base class for tests that need to validate the ModelBackend
    with different User models. Subclasses should define a class
    level UserModel attribute, and a create_users() method to
    construct two users for test purposes.
    u)   django.contrib.auth.backends.ModelBackendc         C  s)   t  j |  _ |  j f t  _ |  j ƒ  d  S(   N(   R   t   AUTHENTICATION_BACKENDSt	   curr_autht   backendt   create_users(   t   self(    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyt   setUp   s    c         C  s   |  j  t _ t j j ƒ  d  S(   N(   R   R   R   R   t   objectst   clear_cache(   R   (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyt   tearDown   s    c         C  s  |  j  j j d |  j j ƒ } |  j | j d ƒ t ƒ t | _	 | j
 ƒ  |  j | j d ƒ t ƒ t | _ | j
 ƒ  |  j | j d ƒ t ƒ t | _	 t | _ | j
 ƒ  |  j | j d ƒ t ƒ t | _	 t | _ t | _ | j
 ƒ  |  j | j d ƒ t ƒ d  S(   Nt   pku	   auth.test(   t	   UserModelt   _default_managert   gett   userR   t   assertEqualt   has_permt   Falset   Truet   is_stafft   savet   is_superusert	   is_active(   R   R   (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyt   test_has_perm$   s"    	
	
		
			
c         C  s5  |  j  j j d |  j j ƒ } t j j t ƒ } t	 j j
 d d d | d d ƒ } | j j | ƒ | j ƒ  |  j  j j d |  j j ƒ } |  j | j ƒ  t d g ƒ k t ƒ |  j | j ƒ  t g  ƒ ƒ |  j | j d ƒ t ƒ |  j | j d ƒ t ƒ t	 j j
 d d	 d | d d	 ƒ } | j j | ƒ | j ƒ  t	 j j
 d d
 d | d d
 ƒ } | j j | ƒ | j ƒ  |  j  j j d |  j j ƒ } |  j | j ƒ  t d d d g ƒ ƒ |  j | j d ƒ t ƒ |  j | j d ƒ t ƒ |  j | j d d g ƒ t ƒ t	 j j
 d d d | d d ƒ } t j j
 d d ƒ } | j j | ƒ | j ƒ  | j j | ƒ |  j  j j d |  j j ƒ } t d d d d g ƒ } |  j | j ƒ  | ƒ |  j | j ƒ  t d g ƒ ƒ |  j | j d d g ƒ t ƒ t ƒ  } |  j | j d ƒ t ƒ |  j | j d d g ƒ t ƒ d  S(   NR   t   nameu   testt   content_typet   codenameu	   auth.testu   Groupu   authu   test2u   test3u
   auth.test2u
   auth.test3u
   test_groupu   auth.test_group(   R   R   R   R   R   R   R   t   get_for_modelR   R   t   createt   user_permissionst   addR$   R   t   get_all_permissionst   setR"   t   get_group_permissionst   has_module_permsR!   R    t	   has_permst   permissionst   groupsR   (   R   R   R)   t   permt   groupt   exp(    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyt   test_custom_perms7   sD    !
%!
!
%!
	c         C  sâ   |  j  j j d |  j j ƒ } t j j t ƒ } t	 j j
 d d d | d d ƒ } | j j | ƒ | j ƒ  |  j | j d d ƒ t ƒ |  j | j d ƒ t g  ƒ ƒ |  j | j d ƒ t ƒ |  j | j ƒ  t d g ƒ ƒ d S(	   u   Regressiontest for #12462R   R(   u   testR)   R*   u	   auth.testu   objectN(   R   R   R   R   R   R   R   R+   R   R   R,   R-   R.   R$   R   R    R!   R/   R0   R"   (   R   R   R)   R6   (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyt   test_has_no_object_perm^   s    !
c         C  sM   |  j  j j d |  j j ƒ } |  j t | j ƒ  ƒ t t j	 j
 ƒ  ƒ ƒ d S(   u,   A superuser has all permissions. Refs #14795R   N(   R   R   R   t	   superuserR   R   t   lenR/   R   R   t   all(   R   R   (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyt"   test_get_all_superuser_permissionsk   s    (
   t   __name__t
   __module__t   __doc__R   R   R   R'   R9   R:   R>   (    (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyR      s   				'	t   ModelBackendTestc           B  s   e  Z d  Z e Z d „  Z RS(   uB   
    Tests for the ModelBackend using the default User model.
    c         C  sL   t  j j d d d d d d ƒ |  _ t  j j d d d d d d ƒ |  _ d  S(   Nt   usernameu   testt   emailu   test@example.comt   passwordu   test2u   test2@example.com(   R   R   t   create_userR   t   create_superuserR;   (   R   (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyR   x   s    (   R?   R@   RA   R   R   R   (    (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyRB   q   s   t   AUTH_USER_MODELu   auth.ExtensionUsert   ExtensionUserModelBackendTestc           B  s   e  Z d  Z e Z d „  Z RS(   u÷  
    Tests for the ModelBackend using the custom ExtensionUser model.

    This isn't a perfect test, because both the User and ExtensionUser are
    synchronized to the database, which wouldn't ordinary happen in
    production. As a result, it doesn't catch errors caused by the non-
    existence of the User table.

    The specific problem is queries on .filter(groups__user) et al, which
    makes an implicit assumption that the user model is called 'User'. In
    production, the auth.User table won't exist, so the requested join
    won't exist either; in testing, the auth.User *does* exist, and
    so does the join. However, the join table won't contain any useful
    data; for testing, we check that the data we expect actually does exist.
    c         C  sp   t  j j d d d d d d d t d d d	 ƒ ƒ |  _ t  j j d d
 d d d d d t d d d ƒ ƒ |  _ d  S(   NRC   u   testRD   u   test@example.comRE   t   date_of_birthiÖ  i   i   u   test2u   test2@example.comi¸  i   i   (   R   R   RF   R   R   RG   R;   (   R   (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyR   ™   s    (   R?   R@   RA   R   R   R   (    (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyRI   …   s   u   auth.CustomPermissionsUsert%   CustomPermissionsUserModelBackendTestc           B  s   e  Z d  Z e Z d „  Z RS(   u  
    Tests for the ModelBackend using the CustomPermissionsUser model.

    As with the ExtensionUser test, this isn't a perfect test, because both
    the User and CustomPermissionsUser are synchronized to the database,
    which wouldn't ordinary happen in production.
    c      
   C  sd   t  j j d d d d d t d d d ƒ ƒ |  _ t  j j d d	 d d d t d
 d d ƒ ƒ |  _ d  S(   NRD   u   test@example.comRE   u   testRJ   iÖ  i   i   u   test2@example.comi¸  i   i   (   R	   R   RF   R   R   RG   R;   (   R   (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyR   ´   s    (   R?   R@   RA   R	   R   R   (    (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyRK   ¨   s   u   auth.CustomUsert&   CustomUserModelBackendAuthenticateTestc           B  s   e  Z d  Z d „  Z RS(   uz   
    Tests that the model backend can accept a credentials kwarg labeled with
    custom user model's USERNAME_FIELD.
    c      
   C  sV   t  j j d d d d d t d d d ƒ ƒ } t d d d d ƒ } |  j | | ƒ d  S(	   NRD   u   test@example.comRE   u   testRJ   iÖ  i   i   (   R
   R   RF   R   R   R   (   R   t	   test_usert   authenticated_user(    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyt   test_authenticateÈ   s    (   R?   R@   RA   RO   (    (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyRL   Á   s   t   TestObjc           B  s   e  Z RS(    (   R?   R@   (    (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyRP   Ó   s   t   SimpleRowlevelBackendc           B  s5   e  Z d d  „ Z d „  Z d d „ Z d d „ Z RS(   c         C  si   | s
 d  St  | t ƒ re | j d k r, t S| j ƒ  rH | d k rH t S| j re | d k re t Sn  t S(   Nu   test2u   anonu   inactive(   t
   isinstanceRP   RC   R"   t   is_anonymousR&   R!   (   R   R   R6   t   obj(    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyR    Ø   s    c         C  s%   | j  ƒ  r | j r t S| d k S(   Nu   app1(   RS   R&   R!   (   R   R   t	   app_label(    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyR2   å   s    c         C  sW   | s
 g  St  | t ƒ s  d g S| j ƒ  r3 d g S| j d k rL d d g Sd g Sd  S(   Nu   noneu   anonu   test2u   simpleu   advanced(   RR   RP   RS   RC   (   R   R   RT   (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyR/   ê   s    
c         C  s]   | s
 d  St  | t ƒ s  d g Sd g  | j j ƒ  D] } | j ^ q3 k rR d g Sd g Sd  S(   Nu   noneu
   test_groupu
   group_perm(   RR   RP   R5   R=   R(   (   R   R   RT   R7   (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyR1   ø   s    +N(   R?   R@   t   NoneR    R2   R/   R1   (    (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyRQ   ×   s   	t   RowlevelBackendTestc           B  sA   e  Z d  Z d Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   uG   
    Tests for auth backend that supports object level permissions
    u=   django.contrib.auth.tests.auth_backends.SimpleRowlevelBackendc         C  s}   t  j |  _ t |  j ƒ |  j f t  _ t j j d d d ƒ |  _ t j j d d d ƒ |  _	 t j j d d d ƒ |  _
 d  S(   Nu   testu   test@example.comu   test2u   test2@example.comu   test3u   test3@example.com(   R   R   R   t   tupleR   R   R   RF   t   user1t   user2t   user3(   R   (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyR     s
    c         C  s   |  j  t _ t j j ƒ  d  S(   N(   R   R   R   R   R   R   (   R   (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyR     s    c         C  sø   |  j  |  j j d t ƒ  ƒ t ƒ |  j  |  j j d t ƒ  ƒ t ƒ |  j  |  j j d ƒ t ƒ |  j  |  j j d d g t ƒ  ƒ t ƒ |  j  |  j j d t ƒ  ƒ t ƒ |  j  |  j j d t ƒ  ƒ t ƒ |  j  |  j j d d g t ƒ  ƒ t ƒ d  S(   Nu   permu   simpleu   advancedu   anon(	   R   RY   R    RP   R!   RZ   R"   R3   R[   (   R   (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyR'     s    ""(""c         C  sv   |  j  |  j j t ƒ  ƒ t d g ƒ ƒ |  j  |  j j t ƒ  ƒ t d d g ƒ ƒ |  j  |  j j ƒ  t g  ƒ ƒ d  S(   Nu   simpleu   advanced(   R   RY   R/   RP   R0   RZ   (   R   (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyt   test_get_all_permissions#  s    (+c         C  sT   t  j j d d ƒ } |  j j j | ƒ |  j |  j j t ƒ  ƒ t	 d g ƒ ƒ d  S(   NR(   u
   test_groupu
   group_perm(
   R   R   R,   R[   R5   R.   R   R1   RP   R0   (   R   R7   (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyt   test_get_group_permissions(  s    (	   R?   R@   RA   R   R   R   R'   R\   R]   (    (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyRW     s   					t   AnonymousUserBackendTestc           B  sJ   e  Z d  Z d Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   u8   
    Tests for AnonymousUser delegating to backend.
    u=   django.contrib.auth.tests.auth_backends.SimpleRowlevelBackendc         C  s+   t  j |  _ |  j f t  _ t ƒ  |  _ d  S(   N(   R   R   R   R   R   RY   (   R   (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyR   5  s    c         C  s   |  j  t _ d  S(   N(   R   R   R   (   R   (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyR   :  s    c         C  sH   |  j  |  j j d t ƒ  ƒ t ƒ |  j  |  j j d t ƒ  ƒ t ƒ d  S(   Nu   permu   anon(   R   RY   R    RP   R!   R"   (   R   (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyR'   =  s    "c         C  sQ   |  j  |  j j d g t ƒ  ƒ t ƒ |  j  |  j j d d g t ƒ  ƒ t ƒ d  S(   Nu   anonu   perm(   R   RY   R3   RP   R"   R!   (   R   (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyt   test_has_permsA  s    %c         C  s<   |  j  |  j j d ƒ t ƒ |  j  |  j j d ƒ t ƒ d  S(   Nu   app1u   app2(   R   RY   R2   R"   R!   (   R   (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyt   test_has_module_permsE  s    c         C  s,   |  j  |  j j t ƒ  ƒ t d g ƒ ƒ d  S(   Nu   anon(   R   RY   R/   RP   R0   (   R   (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyR\   I  s    (
   R?   R@   RA   R   R   R   R'   R_   R`   R\   (    (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyR^   .  s   					R   t   NoBackendsTestc           B  s    e  Z d  Z d „  Z d „  Z RS(   uU   
    Tests that an appropriate error is raised if no auth backends are provided.
    c         C  s   t  j j d d d ƒ |  _ d  S(   Nu   testu   test@example.com(   R   R   RF   R   (   R   (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyR   S  s    c         C  s&   |  j  t |  j j d t ƒ  f ƒ d  S(   Nu   perm(   t   assertRaisesR   R   R    RP   (   R   (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyt   test_raises_exceptionV  s    (   R?   R@   RA   R   Rc   (    (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyRa   M  s   	t   InActiveUserBackendTestc           B  s8   e  Z d  Z d Z d „  Z d „  Z d „  Z d „  Z RS(   u#   
    Tests for a inactive user
    u=   django.contrib.auth.tests.auth_backends.SimpleRowlevelBackendc         C  sS   t  j |  _ |  j f t  _ t j j d d d ƒ |  _ t |  j _	 |  j j
 ƒ  d  S(   Nu   testu   test@example.com(   R   R   R   R   R   R   RF   RY   R!   R&   R$   (   R   (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyR   a  s
    c         C  s   |  j  t _ d  S(   N(   R   R   R   (   R   (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyR   h  s    c         C  sH   |  j  |  j j d t ƒ  ƒ t ƒ |  j  |  j j d t ƒ  ƒ t ƒ d  S(   Nu   permu   inactive(   R   RY   R    RP   R!   R"   (   R   (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyR'   k  s    "c         C  s<   |  j  |  j j d ƒ t ƒ |  j  |  j j d ƒ t ƒ d  S(   Nu   app1u   app2(   R   RY   R2   R!   (   R   (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyR`   o  s    (   R?   R@   RA   R   R   R   R'   R`   (    (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyRd   Z  s   			N('   t
   __future__R    t   datetimeR   t   django.confR   t   django.contrib.auth.modelsR   R   R   R   t   django.contrib.auth.tests.utilsR   t%   django.contrib.auth.tests.custom_userR   R	   R
   t"   django.contrib.contenttypes.modelsR   t   django.core.exceptionsR   t   django.contrib.authR   t   django.testR   t   django.test.utilsR   t   objectR   RB   RI   RK   RL   RP   RQ   RW   R^   Ra   Rd   (    (    (    s@   ../Django//lib/python/django/contrib/auth/tests/auth_backends.pyt   <module>   s:   "b".(