
    .~h(                         d dl 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 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       G d de             Zy)    N)settings)AnonymousUser)SessionMiddleware)mail)DatabaseError)TransactionTestCase)RequestFactory)override_settings)reverse)patch)RegistrationView)RegistrationForm)RegistrationProfile)	UserModelztest_app.urls_default   )ROOT_URLCONFACCOUNT_ACTIVATION_DAYSc                       e Zd ZdZeZeZ ed      d        Z	 ed      d        Z
d Zd Zd	 Z ed
      d        Zd Z ed      d        Zd Zd Zd Zd Zy)DefaultBackendViewTestsa  
    Test the default registration backend.

    Running these tests successfully will require two templates to be
    created for the sending of activation emails; details on these
    templates and their contexts may be found in the documentation for
    the default backend.

    T)REGISTRATION_OPENc                     | j                   j                  t        d            }| j                  d|j                         y)zv
        The setting ``REGISTRATION_OPEN`` appropriately controls
        whether registration is permitted.

        registration_register   N)clientgetr   assertEqualstatus_codeselfresps     k/var/www/peopleoo.sandbox-dev.co.uk/venv/lib/python3.12/site-packages/registration/tests/default_backend.pytest_registration_openz.DefaultBackendViewTests.test_registration_open%   s2     {{w'>?@d../    Fc                    | j                   j                  t        d            }| j                  |t        d             | j                   j	                  t        d      ddddd      }| j                  |t        d             y )Nr   registration_disallowedbobbob@example.comsecretusernameemail	password1	password2data)r   r   r   assertRedirectspostr   s     r!   test_registration_closedz0DefaultBackendViewTests.test_registration_closed/   s|    
 {{w'>?@T7+D#EF{{(? @27/@3;3;&=   >
 	T7+D#EFr#   c                     | j                   j                  t        d            }| j                  d|j                         | j                  |d       | j                  |j                  d   t               y)z
        HTTP ``GET`` to the registration view uses the appropriate
        template and populates a registration form into the context.

        r   r   z#registration/registration_form.htmlformN)	r   r   r   r   r   assertTemplateUsedassertIsInstancecontextr   r   s     r!   test_registration_getz-DefaultBackendViewTests.test_registration_get>   s`     {{w'>?@d../ E	Gdll624DEr#   c                 @   | j                   j                  t        d      ddddd      }| j                  |t        d             t	               j
                  j                  d      }| j                  |j                  d             | j                  |j                  d       | j                  |j                         | j                  | j                  j
                  j                         d	       | j                  t        t         j"                        d	       y
)z
        Registration creates a new inactive account and a new profile
        with activation key, populates the correct account data and
        sends an activation email.

        r   r&   r'   r(   r)   r.   registration_completer*      N)r   r1   r   r0   r   objectsr   
assertTruecheck_passwordr   r+   assertFalse	is_activeregistration_profilecountlenr   outboxr   r    new_users      r!   test_registrationz)DefaultBackendViewTests.test_registrationJ   s     {{(? @27/@3;3;&=   >
 	T7+B#CD;&&**E*://9:):; 	++, 	22::@@BAFT[[)1-r#   c                 V    G d d| j                         }t               }|j                         }|j                  dddddd      }t	               |_        d	 }t        |      }|j                  |        ||       t               j                  j                  d
       | j                  | j                  j                  j                         d       | j                  t        t        j                         d       | j                  |j"                  j                  d      d       y)z
        Overridden Registration view does not send an activation email if the
        associated class variable is set to ``False``

        c                       e Zd ZdZy)SDefaultBackendViewTests.test_registration_no_email.<locals>.RegistrationNoEmailViewFN)__name__
__module____qualname__SEND_ACTIVATION_EMAIL r#   r!   RegistrationNoEmailViewrK   k   s    $)!r#   rQ   /r&   r'   r(   r)   r.   c                      y )NrP   )requests    r!   dummy_get_responsezNDefaultBackendViewTests.test_registration_no_email.<locals>.dummy_get_responsew   s    r#   r;   r<   r   registration_emailN)registration_viewr	   as_viewr1   r   userr   process_requestr   r=   r   r   rB   rC   rD   r   rE   session)r   rQ   request_factoryviewrT   rU   
middlewares          r!   test_registration_no_emailz2DefaultBackendViewTests.test_registration_no_emaile   s    	*d&<&< 	* )*&..0!&&s&!!	2#& $
 %	 ''9:
""7+W/22::@@BAFT[[)1-,,-ABDUVr#   )zdjango.contrib.authregistration)INSTALLED_APPSc                 B   | j                   j                  t        d      ddddd      }| j                  d|j                         t               j                  j                  d      }| j                  |j                  d             | j                  |j                  d       | j                  |j                         | j                  | j                  j                  j                         d	       | j                  t        t         j"                        d	       y
)z
        Registration still functions properly when
        ``django.contrib.sites`` is not installed; the fallback will
        be a ``RequestSite`` instance.

        r   r&   r'   r(   r)   r.   i.  r;   r<   N)r   r1   r   r   r   r   r=   r   r>   r?   r+   r@   rA   rB   rC   rD   r   rE   rF   s      r!   test_registration_no_sitesz2DefaultBackendViewTests.test_registration_no_sites   s     {{(? @27/@3;3;&=   >
 	d../;&&**E*://9:):;++,22::@@BAFT[[)1-r#   c                 <   | j                   j                  t        d      ddddd      }| j                  d|j                         | j                  |j                  d	   j                                | j                  d
t        t        j                               y)z7
        Registering with invalid data fails.

        r   r&   r'   r(   	notsecretr)   r.   r   r4   r   N)r   r1   r   r   r   r@   r7   is_validrD   r   rE   r   s     r!   test_registration_failurez1DefaultBackendViewTests.test_registration_failure   s    
 {{(? @27/@3;3>&@   A
 	d../f-6689C,-r#   z<registration.models.RegistrationManager.create_inactive_userc                 8   t               |_        ddddd}| j                  t               5  | j                  j	                  t        d      |       ddd       t               j                  j                  d      j                         rJ y# 1 sw Y   >xY w)	zs
        User is not created beforehand if an exception occurred at
        creating registration profile.
        r&   r'   r(   r)   r   r.   Nr;   )
r   side_effectassertRaisesr   r1   r   r   r=   filterexists)r   create_inactive_user
valid_datas      r!   test_registration_exceptionz3DefaultBackendViewTests.test_registration_exception   s     ,9?("'0#+#+-
 }- 	.KKW%<=",  .	. ;&&--u-=DDFFFF	. 	.s   'BBc                 B   | j                   j                  t        d      ddddd      }| j                  j                  j                  d      }| j                   j                  t        dd	d
|j                  i            }| j                  |t        d             y)z?
        Activation of an account functions properly.

        r   r&   r'   r(   r)   r.   user__usernameregistration_activaterP   activation_keyargskwargs registration_activation_completeN)r   r1   r   rB   r=   r   rt   r0   r   r    profiles      r!   test_activationz'DefaultBackendViewTests.test_activation   s    
 {{(? @27/@3;3;&=   > ++3377u7M{{+,g.D.DEGH 	T7+M#NOr#   c                    | j                   j                  t        d      ddddd      }| j                  j                  j                  d      }|j                  }|xj                  t        j                  t        j                        z  c_        |j                          | j                   j                  t        d	d
d|j                  i            }| j                  d|j                         | j!                  |d       t#               j                  j                  d      }| j%                  |j&                         y)z9
        An expired account can't be activated.

        r   r&   r'   r(   r)   r.   rq   )daysrs   rP   rt   ru   r   zregistration/activate.htmlr;   N)r   r1   r   rB   r=   r   rY   date_joineddatetime	timedeltar   r   savert   r   r   r5   r   r@   rA   )r   r    rz   rY   s       r!   test_activation_expiredz/DefaultBackendViewTests.test_activation_expired   s   
 {{(? @27/@3;3;&=   > ++3377u7M||H..113 	3		{{+,g.D.DEGH
 	d../&BC{""&&&6(r#   c                    | j                   j                  t        d      ddddd      }| j                  j                  j                  d      }| j                   j                  t        d      d	|j                  j                  i      }| j                  |d
       | j                  |j                  d	   |j                  j                         y)z8
        Resend activation functions properly.

        r   r&   r'   r(   r)   r.   rq   registration_resend_activationr+   ,registration/resend_activation_complete.htmlN)r   r1   r   rB   r=   r   rY   r+   r5   r   r7   ry   s      r!   test_resend_activationz.DefaultBackendViewTests.test_resend_activation   s    
 {{(? @27/@3;3;&=   > ++3377u7M{{(H I&-w||/A/A%B   D N	Pg.0B0BCr#   c                 x    | j                   j                  t        d      ddi      }| j                  |d       y)zP
        Calling resend with an invalid email shows the same template.

        r   r+   zinvalid@example.comr.   r   N)r   r1   r   r5   r   s     r!   $test_resend_activation_invalid_emailz<DefaultBackendViewTests.test_resend_activation_invalid_email   sC    
 {{(H I&-/D%E   G N	Pr#   N)rL   rM   rN   __doc__r   rB   r   rW   r
   r"   r2   r8   rH   r_   rc   rg   r   ro   r{   r   r   r   rP   r#   r!   r   r      s     /(.0 /0 /G 0G
F.6W> ?..0. IJG KGP&)6D&Pr#   r   )r   django.confr   django.contrib.auth.modelsr   "django.contrib.sessions.middlewarer   django.corer   	django.dbr   django.testr   django.test.clientr	   django.test.utilsr
   django.urlsr   mockr   #registration.backends.default.viewsr   registration.formsr   registration.modelsr   registration.usersr   r   rP   r#   r!   <module>r      s_       4 @  # + - /   @ / 3 (  7+,.pP1 pP.pPr#   