<s> from django . core . urlresolvers import reverse <EOL> from django . test import TestCase <EOL> from django . contrib . auth import get_user_model <EOL> from django . core import mail <EOL> from django . conf import settings <EOL> from django . test . utils import override_settings <EOL> from django . utils . encoding import force_text <EOL> from rest_framework import status <EOL> from . test_base import BaseAPITestCase <EOL> class APITestCase1 ( TestCase , BaseAPITestCase ) : <EOL> """<STR_LIT>""" <EOL> urls = '<STR_LIT>' <EOL> USERNAME = '<STR_LIT>' <EOL> PASS = '<STR_LIT>' <EOL> EMAIL = "<STR_LIT>" <EOL> NEW_PASS = '<STR_LIT>' <EOL> REGISTRATION_VIEW = '<STR_LIT>' <EOL> REGISTRATION_DATA = { <EOL> "<STR_LIT:username>" : USERNAME , <EOL> "<STR_LIT>" : PASS , <EOL> "<STR_LIT>" : PASS <EOL> } <EOL> REGISTRATION_DATA_WITH_EMAIL = REGISTRATION_DATA . copy ( ) <EOL> REGISTRATION_DATA_WITH_EMAIL [ '<STR_LIT:email>' ] = EMAIL <EOL> BASIC_USER_DATA = { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : EMAIL <EOL> } <EOL> USER_DATA = BASIC_USER_DATA . copy ( ) <EOL> USER_DATA [ '<STR_LIT>' ] = True <EOL> def setUp ( self ) : <EOL> self . init ( ) <EOL> def _generate_uid_and_token ( self , user ) : <EOL> result = { } <EOL> from django . utils . encoding import force_bytes <EOL> from django . contrib . auth . tokens import default_token_generator <EOL> from django . utils . http import urlsafe_base64_encode <EOL> result [ '<STR_LIT>' ] = urlsafe_base64_encode ( force_bytes ( user . pk ) ) <EOL> result [ '<STR_LIT>' ] = default_token_generator . make_token ( user ) <EOL> return result <EOL> def test_login ( self ) : <EOL> payload = { <EOL> "<STR_LIT:username>" : self . USERNAME , <EOL> "<STR_LIT:password>" : self . PASS <EOL> } <EOL> self . post ( self . login_url , data = payload , status_code = <NUM_LIT> ) <EOL> self . post ( self . password_change_url , status_code = <NUM_LIT> ) <EOL> user = get_user_model ( ) . objects . create_user ( self . USERNAME , '<STR_LIT>' , self . PASS ) <EOL> self . post ( self . login_url , data = payload , status_code = <NUM_LIT:200> ) <EOL> self . assertEqual ( '<STR_LIT:key>' in self . response . json . keys ( ) , True ) <EOL> self . token = self . response . json [ '<STR_LIT:key>' ] <EOL> self . post ( self . password_change_url , status_code = <NUM_LIT> ) <EOL> user . is_active = False <EOL> user . save ( ) <EOL> self . post ( self . login_url , data = payload , status_code = <NUM_LIT> ) <EOL> payload = { <EOL> "<STR_LIT:username>" : self . USERNAME + '<STR_LIT:?>' , <EOL> "<STR_LIT:password>" : self . PASS <EOL> } <EOL> self . post ( self . login_url , data = payload , status_code = <NUM_LIT> ) <EOL> self . post ( self . login_url , data = { } , status_code = <NUM_LIT> ) <EOL> @ override_settings ( REST_USE_JWT = True ) <EOL> def test_login_jwt ( self ) : <EOL> payload = { <EOL> "<STR_LIT:username>" : self . USERNAME , <EOL> "<STR_LIT:password>" : self . PASS <EOL> } <EOL> get_user_model ( ) . objects . create_user ( self . USERNAME , '<STR_LIT>' , self . PASS ) <EOL> self . post ( self . login_url , data = payload , status_code = <NUM_LIT:200> ) <EOL> self . assertEqual ( '<STR_LIT>' in self . response . json . keys ( ) , True ) <EOL> self . token = self . response . json [ '<STR_LIT>' ] <EOL> def test_login_by_email ( self ) : <EOL> settings . INSTALLED_APPS . remove ( '<STR_LIT>' ) <EOL> payload = { <EOL> "<STR_LIT:email>" : self . EMAIL . lower ( ) , <EOL> "<STR_LIT:password>" : self . PASS <EOL> } <EOL> self . post ( self . login_url , data = payload , status_code = <NUM_LIT> ) <EOL> self . post ( self . password_change_url , status_code = <NUM_LIT> ) <EOL> user = get_user_model ( ) . objects . create_user ( self . USERNAME , self . EMAIL , self . PASS ) <EOL> self . post ( self . login_url , data = payload , status_code = <NUM_LIT:200> ) <EOL> self . assertEqual ( '<STR_LIT:key>' in self . response . json . keys ( ) , True ) <EOL> self . token = self . response . json [ '<STR_LIT:key>' ] <EOL> payload = { <EOL> "<STR_LIT:email>" : self . EMAIL . upper ( ) , <EOL> "<STR_LIT:password>" : self . PASS <EOL> } <EOL> self . post ( self . login_url , data = payload , status_code = <NUM_LIT:200> ) <EOL> self . assertEqual ( '<STR_LIT:key>' in self . response . json . keys ( ) , True ) <EOL> self . token = self . response . json [ '<STR_LIT:key>' ] <EOL> user . is_active = False <EOL> user . save ( ) <EOL> self . post ( self . login_url , data = payload , status_code = <NUM_LIT> ) <EOL> payload = { <EOL> "<STR_LIT:email>" : '<STR_LIT:t>' + self . EMAIL , <EOL> "<STR_LIT:password>" : self . PASS <EOL> } <EOL> self . post ( self . login_url , data = payload , status_code = <NUM_LIT> ) <EOL> self . post ( self . login_url , data = { } , status_code = <NUM_LIT> ) <EOL> def test_password_change ( self ) : <EOL> login_payload = { <EOL> "<STR_LIT:username>" : self . USERNAME , <EOL> "<STR_LIT:password>" : self . PASS <EOL> } <EOL> get_user_model ( ) . objects . create_user ( self . USERNAME , '<STR_LIT>' , self . PASS ) <EOL> self . post ( self . login_url , data = login_payload , status_code = <NUM_LIT:200> ) <EOL> self . token = self . response . json [ '<STR_LIT:key>' ] <EOL> new_password_payload = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> self . post ( <EOL> self . password_change_url , <EOL> data = new_password_payload , <EOL> status_code = <NUM_LIT:200> <EOL> ) <EOL> self . post ( self . login_url , data = login_payload , status_code = <NUM_LIT> ) <EOL> login_payload [ '<STR_LIT:password>' ] = new_password_payload [ '<STR_LIT>' ] <EOL> self . post ( self . login_url , data = login_payload , status_code = <NUM_LIT:200> ) <EOL> new_password_payload = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> self . post ( <EOL> self . password_change_url , <EOL> data = new_password_payload , <EOL> status_code = <NUM_LIT> <EOL> ) <EOL> self . post ( self . password_change_url , data = { } , status_code = <NUM_LIT> ) <EOL> @ override_settings ( OLD_PASSWORD_FIELD_ENABLED = True ) <EOL> def test_password_change_with_old_password ( self ) : <EOL> login_payload = { <EOL> "<STR_LIT:username>" : self . USERNAME , <EOL> "<STR_LIT:password>" : self . PASS <EOL> } <EOL> get_user_model ( ) . objects . create_user ( self . USERNAME , '<STR_LIT>' , self . PASS ) <EOL> self . post ( self . login_url , data = login_payload , status_code = <NUM_LIT:200> ) <EOL> self . token = self . response . json [ '<STR_LIT:key>' ] <EOL> new_password_payload = { <EOL> "<STR_LIT>" : "<STR_LIT>" % self . PASS , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> self . post ( <EOL> self . password_change_url , <EOL> data = new_password_payload , <EOL> status_code = <NUM_LIT> <EOL> ) <EOL> new_password_payload = { <EOL> "<STR_LIT>" : self . PASS , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> self . post ( <EOL> self . password_change_url , <EOL> data = new_password_payload , <EOL> status_code = <NUM_LIT:200> <EOL> ) <EOL> self . post ( self . login_url , data = login_payload , status_code = <NUM_LIT> ) <EOL> login_payload [ '<STR_LIT:password>' ] = new_password_payload [ '<STR_LIT>' ] <EOL> self . post ( self . login_url , data = login_payload , status_code = <NUM_LIT:200> ) <EOL> def test_password_reset ( self ) : <EOL> user = get_user_model ( ) . objects . create_user ( self . USERNAME , self . EMAIL , self . PASS ) <EOL> mail_count = len ( mail . outbox ) <EOL> payload = { '<STR_LIT:email>' : self . EMAIL } <EOL> self . post ( self . password_reset_url , data = payload , status_code = <NUM_LIT:200> ) <EOL> self . assertEqual ( len ( mail . outbox ) , mail_count + <NUM_LIT:1> ) <EOL> url_kwargs = self . _generate_uid_and_token ( user ) <EOL> url = reverse ( '<STR_LIT>' ) <EOL> data = { <EOL> '<STR_LIT>' : self . NEW_PASS , <EOL> '<STR_LIT>' : self . NEW_PASS , <EOL> '<STR_LIT>' : force_text ( url_kwargs [ '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> self . post ( url , data = data , status_code = <NUM_LIT> ) <EOL> data = { <EOL> '<STR_LIT>' : self . NEW_PASS , <EOL> '<STR_LIT>' : self . NEW_PASS , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : url_kwargs [ '<STR_LIT>' ] <EOL> } <EOL> self . post ( url , data = data , status_code = <NUM_LIT> ) <EOL> data = { <EOL> '<STR_LIT>' : self . NEW_PASS , <EOL> '<STR_LIT>' : self . NEW_PASS , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> self . post ( url , data = data , status_code = <NUM_LIT> ) <EOL> data = { <EOL> '<STR_LIT>' : self . NEW_PASS , <EOL> '<STR_LIT>' : self . NEW_PASS , <EOL> '<STR_LIT>' : force_text ( url_kwargs [ '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : url_kwargs [ '<STR_LIT>' ] <EOL> } <EOL> url = reverse ( '<STR_LIT>' ) <EOL> self . post ( url , data = data , status_code = <NUM_LIT:200> ) <EOL> payload = { <EOL> "<STR_LIT:username>" : self . USERNAME , <EOL> "<STR_LIT:password>" : self . NEW_PASS <EOL> } <EOL> self . post ( self . login_url , data = payload , status_code = <NUM_LIT:200> ) <EOL> def test_password_reset_with_email_in_different_case ( self ) : <EOL> get_user_model ( ) . objects . create_user ( self . USERNAME , self . EMAIL . lower ( ) , self . PASS ) <EOL> mail_count = len ( mail . outbox ) <EOL> payload = { '<STR_LIT:email>' : self . EMAIL . upper ( ) } <EOL> self . post ( self . password_reset_url , data = payload , status_code = <NUM_LIT:200> ) <EOL> self . assertEqual ( len ( mail . outbox ) , mail_count + <NUM_LIT:1> ) <EOL> def test_password_reset_with_invalid_email ( self ) : <EOL> """<STR_LIT>""" <EOL> get_user_model ( ) . objects . create_user ( self . USERNAME , self . EMAIL , self . PASS ) <EOL> mail_count = len ( mail . outbox ) <EOL> payload = { '<STR_LIT:email>' : '<STR_LIT>' } <EOL> self . post ( self . password_reset_url , data = payload , status_code = <NUM_LIT:200> ) <EOL> self . assertEqual ( len ( mail . outbox ) , mail_count ) <EOL> def test_user_details ( self ) : <EOL> user = get_user_model ( ) . objects . create_user ( self . USERNAME , self . EMAIL , self . PASS ) <EOL> payload = { <EOL> "<STR_LIT:username>" : self . USERNAME , <EOL> "<STR_LIT:password>" : self . PASS <EOL> } <EOL> self . post ( self . login_url , data = payload , status_code = <NUM_LIT:200> ) <EOL> self . token = self . response . json [ '<STR_LIT:key>' ] <EOL> self . get ( self . user_url , status_code = <NUM_LIT:200> ) <EOL> self . patch ( self . user_url , data = self . BASIC_USER_DATA , status_code = <NUM_LIT:200> ) <EOL> user = get_user_model ( ) . objects . get ( pk = user . pk ) <EOL> self . assertEqual ( user . first_name , self . response . json [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( user . last_name , self . response . json [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( user . email , self . response . json [ '<STR_LIT:email>' ] ) <EOL> @ override_settings ( REST_USE_JWT = True ) <EOL> def test_user_details_using_jwt ( self ) : <EOL> user = get_user_model ( ) . objects . create_user ( self . USERNAME , self . EMAIL , self . PASS ) <EOL> payload = { <EOL> "<STR_LIT:username>" : self . USERNAME , <EOL> "<STR_LIT:password>" : self . PASS <EOL> } <EOL> self . post ( self . login_url , data = payload , status_code = <NUM_LIT:200> ) <EOL> self . token = self . response . json [ '<STR_LIT>' ] <EOL> self . get ( self . user_url , status_code = <NUM_LIT:200> ) <EOL> self . patch ( self . user_url , data = self . BASIC_USER_DATA , status_code = <NUM_LIT:200> ) <EOL> user = get_user_model ( ) . objects . get ( pk = user . pk ) <EOL> self . assertEqual ( user . email , self . response . json [ '<STR_LIT:email>' ] ) <EOL> def test_registration ( self ) : <EOL> user_count = get_user_model ( ) . objects . all ( ) . count ( ) <EOL> self . post ( self . register_url , data = { } , status_code = <NUM_LIT> ) <EOL> result = self . post ( self . register_url , data = self . REGISTRATION_DATA , status_code = <NUM_LIT> ) <EOL> self . assertIn ( '<STR_LIT:key>' , result . data ) <EOL> self . assertEqual ( get_user_model ( ) . objects . all ( ) . count ( ) , user_count + <NUM_LIT:1> ) <EOL> new_user = get_user_model ( ) . objects . latest ( '<STR_LIT:id>' ) <EOL> self . assertEqual ( new_user . username , self . REGISTRATION_DATA [ '<STR_LIT:username>' ] ) <EOL> self . _login ( ) <EOL> self . _logout ( ) <EOL> @ override_settings ( REST_USE_JWT = True ) <EOL> def test_registration_with_jwt ( self ) : <EOL> user_count = get_user_model ( ) . objects . all ( ) . count ( ) <EOL> self . post ( self . register_url , data = { } , status_code = <NUM_LIT> ) <EOL> result = self . post ( self . register_url , data = self . REGISTRATION_DATA , status_code = <NUM_LIT> ) <EOL> self . assertIn ( '<STR_LIT>' , result . data ) <EOL> self . assertEqual ( get_user_model ( ) . objects . all ( ) . count ( ) , user_count + <NUM_LIT:1> ) <EOL> self . _login ( ) <EOL> self . _logout ( ) <EOL> def test_registration_with_invalid_password ( self ) : <EOL> data = self . REGISTRATION_DATA . copy ( ) <EOL> data [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . post ( self . register_url , data = data , status_code = <NUM_LIT> ) <EOL> @ override_settings ( <EOL> ACCOUNT_EMAIL_VERIFICATION = '<STR_LIT>' , <EOL> ACCOUNT_EMAIL_REQUIRED = True <EOL> ) <EOL> def test_registration_with_email_verification ( self ) : <EOL> user_count = get_user_model ( ) . objects . all ( ) . count ( ) <EOL> mail_count = len ( mail . outbox ) <EOL> self . post ( <EOL> self . register_url , <EOL> data = { } , <EOL> status_code = status . HTTP_400_BAD_REQUEST <EOL> ) <EOL> result = self . post ( <EOL> self . register_url , <EOL> data = self . REGISTRATION_DATA_WITH_EMAIL , <EOL> status_code = status . HTTP_201_CREATED <EOL> ) <EOL> self . assertNotIn ( '<STR_LIT:key>' , result . data ) <EOL> self . assertEqual ( get_user_model ( ) . objects . all ( ) . count ( ) , user_count + <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( mail . outbox ) , mail_count + <NUM_LIT:1> ) <EOL> new_user = get_user_model ( ) . objects . latest ( '<STR_LIT:id>' ) <EOL> self . assertEqual ( new_user . username , self . REGISTRATION_DATA [ '<STR_LIT:username>' ] ) <EOL> payload = { <EOL> "<STR_LIT:username>" : self . USERNAME , <EOL> "<STR_LIT:password>" : self . PASS <EOL> } <EOL> self . post ( <EOL> self . login_url , <EOL> data = payload , <EOL> status = status . HTTP_400_BAD_REQUEST <EOL> ) <EOL> email_confirmation = new_user . emailaddress_set . get ( email = self . EMAIL ) . emailconfirmation_set . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> self . post ( <EOL> self . veirfy_email_url , <EOL> data = { "<STR_LIT:key>" : email_confirmation . key } , <EOL> status_code = status . HTTP_200_OK <EOL> ) <EOL> self . _login ( ) <EOL> self . _logout ( ) <EOL> @ override_settings ( ACCOUNT_LOGOUT_ON_GET = True ) <EOL> def test_logout_on_get ( self ) : <EOL> payload = { <EOL> "<STR_LIT:username>" : self . USERNAME , <EOL> "<STR_LIT:password>" : self . PASS <EOL> } <EOL> get_user_model ( ) . objects . create_user ( self . USERNAME , '<STR_LIT>' , self . PASS ) <EOL> self . post ( self . login_url , data = payload , status_code = <NUM_LIT:200> ) <EOL> self . get ( self . logout_url , status = status . HTTP_200_OK ) <EOL> @ override_settings ( ACCOUNT_LOGOUT_ON_GET = False ) <EOL> def test_logout_on_post_only ( self ) : <EOL> payload = { <EOL> "<STR_LIT:username>" : self . USERNAME , <EOL> "<STR_LIT:password>" : self . PASS <EOL> } <EOL> get_user_model ( ) . objects . create_user ( self . USERNAME , '<STR_LIT>' , self . PASS ) <EOL> self . post ( self . login_url , data = payload , status_code = status . HTTP_200_OK ) <EOL> self . get ( self . logout_url , status_code = status . HTTP_405_METHOD_NOT_ALLOWED ) </s>
