<s> from __future__ import unicode_literals <EOL> import datetime <EOL> from django . test import TestCase , RequestFactory <EOL> from django . core . urlresolvers import reverse <EOL> from django . core . cache import cache <EOL> from django . contrib . auth import get_user_model , HASH_SESSION_KEY <EOL> from django . core import mail <EOL> from django . utils . translation import ugettext as _ <EOL> from django . utils import timezone <EOL> from django . test . utils import override_settings <EOL> from djconfig . utils import override_djconfig <EOL> from . . core . tests import utils <EOL> from . forms import UserProfileForm , EmailChangeForm , UserForm , EmailCheckForm <EOL> from . . comment . like . models import CommentLike <EOL> from . . topic . models import Topic <EOL> from . . comment . models import Comment <EOL> from . . comment . bookmark . models import CommentBookmark <EOL> from . utils . tokens import UserActivationTokenGenerator , UserEmailChangeTokenGenerator <EOL> from . utils . email import send_activation_email , send_email_change_email , sender <EOL> from . utils import email <EOL> User = get_user_model ( ) <EOL> class UserViewTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> cache . clear ( ) <EOL> self . user = utils . create_user ( ) <EOL> self . user2 = utils . create_user ( ) <EOL> self . category = utils . create_category ( ) <EOL> self . topic = utils . create_topic ( self . category , user = self . user2 ) <EOL> self . topic2 = utils . create_topic ( self . category ) <EOL> def test_user_views_denied_to_non_logged_users ( self ) : <EOL> """<STR_LIT>""" <EOL> pk = self . user . pk <EOL> slug = self . user . st . slug <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : pk , '<STR_LIT>' : slug } ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : pk , '<STR_LIT>' : slug } ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : pk , '<STR_LIT>' : slug } ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : "<STR_LIT:foo>" } ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_profile_topics ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . login ( self ) <EOL> response = self . client . get ( reverse ( "<STR_LIT>" , kwargs = { '<STR_LIT>' : self . user2 . pk , <EOL> '<STR_LIT>' : self . user2 . st . slug } ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( list ( response . context [ '<STR_LIT>' ] ) , [ self . topic , ] ) <EOL> self . assertEqual ( response . context [ '<STR_LIT>' ] , self . user2 ) <EOL> def test_profile_topics_order ( self ) : <EOL> """<STR_LIT>""" <EOL> Topic . objects . all ( ) . delete ( ) <EOL> category = utils . create_category ( ) <EOL> topic_a = utils . create_topic ( category = category , user = self . user2 ) <EOL> topic_b = utils . create_topic ( category = category , user = self . user2 ) <EOL> topic_c = utils . create_topic ( category = category , user = self . user2 ) <EOL> Topic . objects . filter ( pk = topic_a . pk ) . update ( date = timezone . now ( ) - datetime . timedelta ( days = <NUM_LIT:10> ) ) <EOL> Topic . objects . filter ( pk = topic_c . pk ) . update ( date = timezone . now ( ) - datetime . timedelta ( days = <NUM_LIT:5> ) ) <EOL> utils . login ( self ) <EOL> response = self . client . get ( reverse ( "<STR_LIT>" , kwargs = { '<STR_LIT>' : self . user2 . pk , <EOL> '<STR_LIT>' : self . user2 . st . slug } ) ) <EOL> self . assertEqual ( list ( response . context [ '<STR_LIT>' ] ) , [ topic_b , topic_c , topic_a ] ) <EOL> def test_profile_topics_bookmarks ( self ) : <EOL> """<STR_LIT>""" <EOL> bookmark = CommentBookmark . objects . create ( topic = self . topic , user = self . user ) <EOL> utils . login ( self ) <EOL> response = self . client . get ( reverse ( "<STR_LIT>" , <EOL> kwargs = { '<STR_LIT>' : self . user2 . pk , '<STR_LIT>' : self . user2 . st . slug } ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( list ( response . context [ '<STR_LIT>' ] ) , [ self . topic , ] ) <EOL> self . assertEqual ( response . context [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . bookmark , bookmark ) <EOL> @ override_djconfig ( topics_per_page = <NUM_LIT:1> ) <EOL> def test_profile_topics_paginate ( self ) : <EOL> """<STR_LIT>""" <EOL> topic = utils . create_topic ( self . category , user = self . user2 ) <EOL> utils . login ( self ) <EOL> response = self . client . get ( reverse ( "<STR_LIT>" , kwargs = { '<STR_LIT>' : self . user2 . pk , <EOL> '<STR_LIT>' : self . user2 . st . slug } ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( list ( response . context [ '<STR_LIT>' ] ) , [ topic , ] ) <EOL> def test_profile_topics_dont_show_removed_or_private ( self ) : <EOL> """<STR_LIT>""" <EOL> Topic . objects . all ( ) . delete ( ) <EOL> category = utils . create_category ( ) <EOL> category_removed = utils . create_category ( is_removed = True ) <EOL> subcategory = utils . create_category ( parent = category_removed ) <EOL> subcategory_removed = utils . create_category ( parent = category , is_removed = True ) <EOL> utils . create_private_topic ( user = self . user2 ) <EOL> utils . create_topic ( category = category , user = self . user2 , is_removed = True ) <EOL> utils . create_topic ( category = category_removed , user = self . user2 ) <EOL> utils . create_topic ( category = subcategory , user = self . user2 ) <EOL> utils . create_topic ( category = subcategory_removed , user = self . user2 ) <EOL> utils . login ( self ) <EOL> response = self . client . get ( reverse ( "<STR_LIT>" , kwargs = { '<STR_LIT>' : self . user2 . pk , <EOL> '<STR_LIT>' : self . user2 . st . slug } ) ) <EOL> self . assertEqual ( list ( response . context [ '<STR_LIT>' ] ) , [ ] ) <EOL> def test_profile_topics_invalid_slug ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . login ( self ) <EOL> response = self . client . get ( reverse ( "<STR_LIT>" , kwargs = { '<STR_LIT>' : self . user2 . pk , <EOL> '<STR_LIT>' : "<STR_LIT>" } ) ) <EOL> expected_url = reverse ( "<STR_LIT>" , kwargs = { '<STR_LIT>' : self . user2 . pk , <EOL> '<STR_LIT>' : self . user2 . st . slug } ) <EOL> self . assertRedirects ( response , expected_url , status_code = <NUM_LIT> ) <EOL> def test_profile_comments ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . login ( self ) <EOL> comment = utils . create_comment ( user = self . user2 , topic = self . topic ) <EOL> utils . create_comment ( user = self . user , topic = self . topic ) <EOL> response = self . client . get ( reverse ( "<STR_LIT>" , kwargs = { '<STR_LIT>' : self . user2 . pk , <EOL> '<STR_LIT>' : self . user2 . st . slug } ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( list ( response . context [ '<STR_LIT>' ] ) , [ comment , ] ) <EOL> self . assertEqual ( response . context [ '<STR_LIT>' ] , self . user2 ) <EOL> def test_profile_comments_order ( self ) : <EOL> """<STR_LIT>""" <EOL> comment_a = utils . create_comment ( user = self . user2 , topic = self . topic ) <EOL> comment_b = utils . create_comment ( user = self . user2 , topic = self . topic ) <EOL> comment_c = utils . create_comment ( user = self . user2 , topic = self . topic ) <EOL> Comment . objects . filter ( pk = comment_a . pk ) . update ( date = timezone . now ( ) - datetime . timedelta ( days = <NUM_LIT:10> ) ) <EOL> Comment . objects . filter ( pk = comment_c . pk ) . update ( date = timezone . now ( ) - datetime . timedelta ( days = <NUM_LIT:5> ) ) <EOL> utils . login ( self ) <EOL> response = self . client . get ( reverse ( "<STR_LIT>" , kwargs = { '<STR_LIT>' : self . user2 . pk , <EOL> '<STR_LIT>' : self . user2 . st . slug } ) ) <EOL> self . assertEqual ( list ( response . context [ '<STR_LIT>' ] ) , [ comment_b , comment_c , comment_a ] ) <EOL> @ override_djconfig ( comments_per_page = <NUM_LIT:1> ) <EOL> def test_profile_comments_paginate ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . create_comment ( user = self . user2 , topic = self . topic ) <EOL> comment = utils . create_comment ( user = self . user2 , topic = self . topic ) <EOL> utils . login ( self ) <EOL> response = self . client . get ( reverse ( "<STR_LIT>" , kwargs = { '<STR_LIT>' : self . user2 . pk , <EOL> '<STR_LIT>' : self . user2 . st . slug } ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( list ( response . context [ '<STR_LIT>' ] ) , [ comment , ] ) <EOL> def test_profile_comments_dont_show_removed_or_private ( self ) : <EOL> """<STR_LIT>""" <EOL> category = utils . create_category ( ) <EOL> category_removed = utils . create_category ( is_removed = True ) <EOL> subcategory = utils . create_category ( parent = category_removed ) <EOL> subcategory_removed = utils . create_category ( parent = category , is_removed = True ) <EOL> topic_a = utils . create_private_topic ( user = self . user2 ) <EOL> topic_b = utils . create_topic ( category = category , is_removed = True ) <EOL> topic_c = utils . create_topic ( category = category_removed ) <EOL> topic_d = utils . create_topic ( category = subcategory ) <EOL> topic_e = utils . create_topic ( category = subcategory_removed ) <EOL> utils . create_comment ( user = self . user2 , topic = topic_a . topic ) <EOL> utils . create_comment ( user = self . user2 , topic = topic_b ) <EOL> utils . create_comment ( user = self . user2 , topic = topic_c ) <EOL> utils . create_comment ( user = self . user2 , topic = topic_d ) <EOL> utils . create_comment ( user = self . user2 , topic = topic_e ) <EOL> utils . login ( self ) <EOL> response = self . client . get ( reverse ( "<STR_LIT>" , kwargs = { '<STR_LIT>' : self . user2 . pk , <EOL> '<STR_LIT>' : self . user2 . st . slug } ) ) <EOL> self . assertEqual ( list ( response . context [ '<STR_LIT>' ] ) , [ ] ) <EOL> def test_profile_comments_invalid_slug ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . login ( self ) <EOL> response = self . client . get ( reverse ( "<STR_LIT>" , kwargs = { '<STR_LIT>' : self . user2 . pk , <EOL> '<STR_LIT>' : "<STR_LIT>" } ) ) <EOL> expected_url = reverse ( "<STR_LIT>" , kwargs = { '<STR_LIT>' : self . user2 . pk , <EOL> '<STR_LIT>' : self . user2 . st . slug } ) <EOL> self . assertRedirects ( response , expected_url , status_code = <NUM_LIT> ) <EOL> def test_profile_likes ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . login ( self ) <EOL> comment = utils . create_comment ( user = self . user , topic = self . topic ) <EOL> comment2 = utils . create_comment ( user = self . user2 , topic = self . topic ) <EOL> like = CommentLike . objects . create ( user = self . user2 , comment = comment ) <EOL> CommentLike . objects . create ( user = self . user , comment = comment2 ) <EOL> response = self . client . get ( reverse ( "<STR_LIT>" , kwargs = { '<STR_LIT>' : self . user2 . pk , <EOL> '<STR_LIT>' : self . user2 . st . slug } ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( list ( response . context [ '<STR_LIT>' ] ) , [ like . comment , ] ) <EOL> self . assertEqual ( response . context [ '<STR_LIT>' ] , self . user2 ) <EOL> def test_profile_likes_order ( self ) : <EOL> """<STR_LIT>""" <EOL> comment_a = utils . create_comment ( user = self . user , topic = self . topic ) <EOL> comment_b = utils . create_comment ( user = self . user , topic = self . topic ) <EOL> comment_c = utils . create_comment ( user = self . user , topic = self . topic ) <EOL> like_a = CommentLike . objects . create ( user = self . user2 , comment = comment_a ) <EOL> CommentLike . objects . create ( user = self . user2 , comment = comment_b ) <EOL> like_c = CommentLike . objects . create ( user = self . user2 , comment = comment_c ) <EOL> CommentLike . objects . filter ( pk = like_a . pk ) . update ( date = timezone . now ( ) - datetime . timedelta ( days = <NUM_LIT:10> ) ) <EOL> CommentLike . objects . filter ( pk = like_c . pk ) . update ( date = timezone . now ( ) - datetime . timedelta ( days = <NUM_LIT:5> ) ) <EOL> utils . login ( self ) <EOL> response = self . client . get ( reverse ( "<STR_LIT>" , kwargs = { '<STR_LIT>' : self . user2 . pk , <EOL> '<STR_LIT>' : self . user2 . st . slug } ) ) <EOL> self . assertEqual ( list ( response . context [ '<STR_LIT>' ] ) , [ comment_b , comment_c , comment_a ] ) <EOL> def test_profile_likes_dont_show_removed_or_private ( self ) : <EOL> """<STR_LIT>""" <EOL> category = utils . create_category ( ) <EOL> category_removed = utils . create_category ( is_removed = True ) <EOL> subcategory = utils . create_category ( parent = category_removed ) <EOL> subcategory_removed = utils . create_category ( parent = category , is_removed = True ) <EOL> topic_a = utils . create_private_topic ( user = self . user2 ) <EOL> topic_b = utils . create_topic ( category = category , is_removed = True ) <EOL> topic_c = utils . create_topic ( category = category_removed ) <EOL> topic_d = utils . create_topic ( category = subcategory ) <EOL> topic_e = utils . create_topic ( category = subcategory_removed ) <EOL> comment_a = utils . create_comment ( user = self . user2 , topic = topic_a . topic ) <EOL> comment_b = utils . create_comment ( user = self . user , topic = topic_b ) <EOL> comment_c = utils . create_comment ( user = self . user , topic = topic_c ) <EOL> comment_d = utils . create_comment ( user = self . user , topic = topic_d ) <EOL> comment_e = utils . create_comment ( user = self . user , topic = topic_e ) <EOL> CommentLike . objects . create ( user = self . user2 , comment = comment_a ) <EOL> CommentLike . objects . create ( user = self . user2 , comment = comment_b ) <EOL> CommentLike . objects . create ( user = self . user2 , comment = comment_c ) <EOL> CommentLike . objects . create ( user = self . user2 , comment = comment_d ) <EOL> CommentLike . objects . create ( user = self . user2 , comment = comment_e ) <EOL> utils . login ( self ) <EOL> response = self . client . get ( reverse ( "<STR_LIT>" , kwargs = { '<STR_LIT>' : self . user2 . pk , <EOL> '<STR_LIT>' : self . user2 . st . slug } ) ) <EOL> self . assertEqual ( list ( response . context [ '<STR_LIT>' ] ) , [ ] ) <EOL> def test_profile_likes_invalid_slug ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . login ( self ) <EOL> response = self . client . get ( reverse ( "<STR_LIT>" , kwargs = { '<STR_LIT>' : self . user2 . pk , <EOL> '<STR_LIT>' : "<STR_LIT>" } ) ) <EOL> expected_url = reverse ( "<STR_LIT>" , kwargs = { '<STR_LIT>' : self . user2 . pk , <EOL> '<STR_LIT>' : self . user2 . st . slug } ) <EOL> self . assertRedirects ( response , expected_url , status_code = <NUM_LIT> ) <EOL> @ override_djconfig ( comments_per_page = <NUM_LIT:1> ) <EOL> def test_profile_likes_paginate ( self ) : <EOL> """<STR_LIT>""" <EOL> comment = utils . create_comment ( user = self . user2 , topic = self . topic ) <EOL> comment2 = utils . create_comment ( user = self . user2 , topic = self . topic ) <EOL> CommentLike . objects . create ( user = self . user2 , comment = comment ) <EOL> like = CommentLike . objects . create ( user = self . user2 , comment = comment2 ) <EOL> utils . login ( self ) <EOL> response = self . client . get ( reverse ( "<STR_LIT>" , kwargs = { '<STR_LIT>' : self . user2 . pk , <EOL> '<STR_LIT>' : self . user2 . st . slug } ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( list ( response . context [ '<STR_LIT>' ] ) , [ like . comment , ] ) <EOL> def test_profile_update ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . login ( self ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> form_data = { '<STR_LIT>' : '<STR_LIT:foo>' , '<STR_LIT>' : '<STR_LIT:bar>' , <EOL> '<STR_LIT:location>' : '<STR_LIT>' , '<STR_LIT>' : self . user . st . timezone } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> form_data ) <EOL> expected_url = reverse ( '<STR_LIT>' ) <EOL> self . assertRedirects ( response , expected_url , status_code = <NUM_LIT> ) <EOL> def test_profile_password_change ( self ) : <EOL> """<STR_LIT>""" <EOL> user = utils . create_user ( password = "<STR_LIT:foo>" ) <EOL> utils . login ( self , user = user , password = "<STR_LIT:foo>" ) <EOL> form_data = { '<STR_LIT>' : '<STR_LIT:foo>' , <EOL> '<STR_LIT>' : '<STR_LIT:bar>' , <EOL> '<STR_LIT>' : '<STR_LIT:bar>' } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> form_data ) <EOL> expected_url = reverse ( "<STR_LIT>" ) <EOL> self . assertRedirects ( response , expected_url , status_code = <NUM_LIT> ) <EOL> utils . login ( self , user = user , password = "<STR_LIT:bar>" ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> def test_profile_password_change_re_login ( self ) : <EOL> """<STR_LIT>""" <EOL> user = utils . create_user ( password = "<STR_LIT:foo>" ) <EOL> utils . login ( self , user = user , password = "<STR_LIT:foo>" ) <EOL> old_hash = self . client . session [ HASH_SESSION_KEY ] <EOL> form_data = { '<STR_LIT>' : '<STR_LIT:foo>' , <EOL> '<STR_LIT>' : '<STR_LIT:bar>' , <EOL> '<STR_LIT>' : '<STR_LIT:bar>' } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , form_data ) <EOL> expected_url = reverse ( "<STR_LIT>" ) <EOL> self . assertRedirects ( response , expected_url , status_code = <NUM_LIT> ) <EOL> self . assertNotEqual ( old_hash , self . client . session [ HASH_SESSION_KEY ] ) <EOL> def test_email_change_confirm ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . login ( self ) <EOL> new_email = "<STR_LIT>" <EOL> token = UserEmailChangeTokenGenerator ( ) . generate ( self . user , new_email ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : token } ) ) <EOL> expected_url = reverse ( "<STR_LIT>" ) <EOL> self . assertRedirects ( response , expected_url , status_code = <NUM_LIT> ) <EOL> self . assertEqual ( User . objects . get ( pk = self . user . pk ) . email , new_email ) <EOL> def test_email_change_confirm_invalid ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . login ( self ) <EOL> old_email = "<STR_LIT>" <EOL> token = UserEmailChangeTokenGenerator ( ) . generate ( self . user , old_email ) <EOL> new_email = "<STR_LIT>" <EOL> User . objects . filter ( pk = self . user . pk ) . update ( email = new_email ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : token } ) ) <EOL> expected_url = reverse ( "<STR_LIT>" ) <EOL> self . assertRedirects ( response , expected_url , status_code = <NUM_LIT> ) <EOL> self . assertEqual ( User . objects . get ( pk = self . user . pk ) . email , new_email ) <EOL> def test_email_change_duplicated ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . login ( self ) <EOL> utils . create_user ( email = "<STR_LIT>" ) <EOL> new_email = "<STR_LIT>" <EOL> old_email = self . user . email <EOL> token = UserEmailChangeTokenGenerator ( ) . generate ( self . user , new_email ) <EOL> self . client . get ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : token } ) ) <EOL> self . assertEqual ( User . objects . get ( pk = self . user . pk ) . email , old_email ) <EOL> @ override_settings ( ST_UNIQUE_EMAILS = False ) <EOL> def test_email_change_duplicated_allowed ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . login ( self ) <EOL> utils . create_user ( email = "<STR_LIT>" ) <EOL> new_email = "<STR_LIT>" <EOL> token = UserEmailChangeTokenGenerator ( ) . generate ( self . user , new_email ) <EOL> self . client . get ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : token } ) ) <EOL> self . assertEqual ( User . objects . get ( pk = self . user . pk ) . email , new_email ) <EOL> def test_profile_email_change ( self ) : <EOL> """<STR_LIT>""" <EOL> user = utils . create_user ( password = "<STR_LIT:foo>" ) <EOL> utils . login ( self , user = user , password = "<STR_LIT:foo>" ) <EOL> form_data = { '<STR_LIT:password>' : '<STR_LIT:foo>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> form_data ) <EOL> expected_url = reverse ( "<STR_LIT>" ) <EOL> self . assertRedirects ( response , expected_url , status_code = <NUM_LIT> ) <EOL> self . assertEquals ( len ( mail . outbox ) , <NUM_LIT:1> ) <EOL> self . assertIn ( _ ( "<STR_LIT>" ) , mail . outbox [ <NUM_LIT:0> ] . subject ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> class UserFormTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> cache . clear ( ) <EOL> self . user = utils . create_user ( ) <EOL> def test_profile ( self ) : <EOL> """<STR_LIT>""" <EOL> form_data = { '<STR_LIT>' : '<STR_LIT:foo>' , '<STR_LIT>' : '<STR_LIT:bar>' , <EOL> '<STR_LIT:location>' : '<STR_LIT>' , '<STR_LIT>' : self . user . st . timezone } <EOL> form = UserProfileForm ( data = form_data , instance = self . user . st ) <EOL> self . assertEqual ( form . is_valid ( ) , True ) <EOL> form = UserForm ( data = form_data , instance = self . user ) <EOL> self . assertEqual ( form . is_valid ( ) , True ) <EOL> def test_email_change ( self ) : <EOL> """<STR_LIT>""" <EOL> user = utils . create_user ( password = "<STR_LIT:foo>" ) <EOL> form_data = { '<STR_LIT:email>' : '<STR_LIT>' , '<STR_LIT:password>' : '<STR_LIT:foo>' } <EOL> form = EmailChangeForm ( data = form_data , user = user ) <EOL> self . assertEqual ( form . is_valid ( ) , True ) <EOL> def test_email_change_invalid ( self ) : <EOL> """<STR_LIT>""" <EOL> user = utils . create_user ( password = "<STR_LIT:foo>" , email = "<STR_LIT>" ) <EOL> form_data = { '<STR_LIT:email>' : '<STR_LIT>' , '<STR_LIT:password>' : '<STR_LIT>' } <EOL> form = EmailChangeForm ( data = form_data , user = user ) <EOL> self . assertEqual ( form . is_valid ( ) , False ) <EOL> self . assertNotIn ( '<STR_LIT:email>' , form . cleaned_data ) <EOL> self . assertNotIn ( '<STR_LIT:password>' , form . cleaned_data ) <EOL> def test_email_change_email_duplication ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . create_user ( email = "<STR_LIT>" ) <EOL> user = utils . create_user ( password = "<STR_LIT:foo>" ) <EOL> form_data = { '<STR_LIT:email>' : '<STR_LIT>' , '<STR_LIT:password>' : '<STR_LIT:foo>' } <EOL> form = EmailChangeForm ( data = form_data , user = user ) <EOL> self . assertEqual ( form . is_valid ( ) , False ) <EOL> self . assertNotIn ( '<STR_LIT:email>' , form . cleaned_data ) <EOL> @ override_settings ( ST_UNIQUE_EMAILS = False ) <EOL> def test_email_change_email_duplication_allowed ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . create_user ( email = "<STR_LIT>" ) <EOL> user = utils . create_user ( password = "<STR_LIT:foo>" ) <EOL> form_data = { '<STR_LIT:email>' : '<STR_LIT>' , '<STR_LIT:password>' : '<STR_LIT:foo>' } <EOL> form = EmailChangeForm ( data = form_data , user = user ) <EOL> self . assertEqual ( form . is_valid ( ) , True ) <EOL> @ override_settings ( ST_CASE_INSENSITIVE_EMAILS = True ) <EOL> def test_email_change_email_case_insensitive ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . create_user ( email = "<STR_LIT>" ) <EOL> user = utils . create_user ( password = "<STR_LIT:foo>" ) <EOL> form_data = { '<STR_LIT:email>' : '<STR_LIT>' , '<STR_LIT:password>' : '<STR_LIT:foo>' } <EOL> form = EmailChangeForm ( data = form_data , user = user ) <EOL> self . assertEqual ( form . is_valid ( ) , False ) <EOL> @ override_settings ( ST_CASE_INSENSITIVE_EMAILS = False ) <EOL> def test_email_change_email_case_sensitive ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . create_user ( email = "<STR_LIT>" ) <EOL> user = utils . create_user ( password = "<STR_LIT:foo>" ) <EOL> form_data = { '<STR_LIT:email>' : '<STR_LIT>' , '<STR_LIT:password>' : '<STR_LIT:foo>' } <EOL> form = EmailChangeForm ( data = form_data , user = user ) <EOL> self . assertEqual ( form . is_valid ( ) , True ) <EOL> def test_email_check ( self ) : <EOL> """<STR_LIT>""" <EOL> form_data = { '<STR_LIT:email>' : '<STR_LIT>' , } <EOL> form = EmailCheckForm ( form_data ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> utils . create_user ( email = "<STR_LIT>" ) <EOL> form_data [ '<STR_LIT:email>' ] = "<STR_LIT>" <EOL> form = EmailCheckForm ( form_data ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> @ override_settings ( ST_UNIQUE_EMAILS = False ) <EOL> def test_email_check_non_unique ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . create_user ( email = "<STR_LIT>" ) <EOL> form_data = { '<STR_LIT:email>' : '<STR_LIT>' , } <EOL> form = EmailCheckForm ( form_data ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> @ override_settings ( ST_CASE_INSENSITIVE_EMAILS = True ) <EOL> def test_email_check_case_insensitive ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . create_user ( email = "<STR_LIT>" ) <EOL> form_data = { '<STR_LIT:email>' : '<STR_LIT>' , } <EOL> form = EmailCheckForm ( form_data ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> @ override_settings ( ST_CASE_INSENSITIVE_EMAILS = False ) <EOL> def test_email_check_case_sensitive ( self ) : <EOL> """<STR_LIT>""" <EOL> utils . create_user ( email = "<STR_LIT>" ) <EOL> form_data = { '<STR_LIT:email>' : '<STR_LIT>' , } <EOL> form = EmailCheckForm ( form_data ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> class UserModelTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> cache . clear ( ) <EOL> def test_user_superuser ( self ) : <EOL> """<STR_LIT>""" <EOL> user = User ( is_superuser = True ) <EOL> user . save ( ) <EOL> self . assertTrue ( user . st . is_administrator ) <EOL> self . assertTrue ( user . st . is_moderator ) <EOL> def test_user_administrator ( self ) : <EOL> """<STR_LIT>""" <EOL> user = User ( ) <EOL> user . save ( ) <EOL> user . st . is_administrator = True <EOL> user . st . save ( ) <EOL> self . assertTrue ( user . st . is_moderator ) <EOL> class UtilsUserTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> cache . clear ( ) <EOL> self . user = utils . create_user ( ) <EOL> def test_user_activation_token_generator ( self ) : <EOL> """<STR_LIT>""" <EOL> self . user . st . is_verified = False <EOL> activation_token = UserActivationTokenGenerator ( ) <EOL> token = activation_token . generate ( self . user ) <EOL> self . assertTrue ( activation_token . is_valid ( self . user , token ) ) <EOL> self . assertFalse ( activation_token . is_valid ( self . user , "<STR_LIT>" ) ) <EOL> self . user . st . is_verified = True <EOL> self . assertFalse ( activation_token . is_valid ( self . user , token ) ) <EOL> user2 = utils . create_user ( ) <EOL> self . assertFalse ( activation_token . is_valid ( user2 , token ) ) <EOL> def test_user_email_change_token_generator ( self ) : <EOL> """<STR_LIT>""" <EOL> new_email = "<STR_LIT>" <EOL> email_change_token = UserEmailChangeTokenGenerator ( ) <EOL> token = email_change_token . generate ( self . user , new_email ) <EOL> self . assertTrue ( email_change_token . is_valid ( self . user , token ) ) <EOL> self . assertFalse ( email_change_token . is_valid ( self . user , "<STR_LIT>" ) ) <EOL> self . assertTrue ( email_change_token . is_valid ( self . user , token ) ) <EOL> self . assertEqual ( email_change_token . get_email ( ) , new_email ) <EOL> user2 = utils . create_user ( ) <EOL> self . assertFalse ( email_change_token . is_valid ( user2 , token ) ) <EOL> self . user . email = "<STR_LIT>" <EOL> self . assertFalse ( email_change_token . is_valid ( self . user , token ) ) <EOL> def test_user_activation_email ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _monkey_sender_called = False <EOL> def monkey_sender ( request , subject , template_name , context , email ) : <EOL> self . assertEqual ( request , req ) <EOL> self . assertEqual ( email , [ self . user . email , ] ) <EOL> activation_token = UserActivationTokenGenerator ( ) <EOL> token = activation_token . generate ( self . user ) <EOL> self . assertDictEqual ( context , { '<STR_LIT>' : token , '<STR_LIT>' : self . user . pk } ) <EOL> self . assertEqual ( subject , _ ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( template_name , '<STR_LIT>' ) <EOL> self . _monkey_sender_called = True <EOL> req = RequestFactory ( ) . get ( '<STR_LIT:/>' ) <EOL> org_sender , email . sender = email . sender , monkey_sender <EOL> try : <EOL> send_activation_email ( req , self . user ) <EOL> self . assertTrue ( self . _monkey_sender_called ) <EOL> finally : <EOL> email . sender = org_sender <EOL> def test_user_activation_email_complete ( self ) : <EOL> """<STR_LIT>""" <EOL> req = RequestFactory ( ) . get ( '<STR_LIT:/>' ) <EOL> send_activation_email ( req , self . user ) <EOL> self . assertEquals ( len ( mail . outbox ) , <NUM_LIT:1> ) <EOL> def test_email_change_email ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _monkey_sender_called = False <EOL> def monkey_sender ( request , subject , template_name , context , email ) : <EOL> self . assertEqual ( request , req ) <EOL> self . assertEqual ( email , [ self . user . email , ] ) <EOL> change_token = UserEmailChangeTokenGenerator ( ) <EOL> token = change_token . generate ( self . user , new_email ) <EOL> self . assertDictEqual ( context , { '<STR_LIT>' : token , } ) <EOL> self . assertEqual ( subject , _ ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( template_name , '<STR_LIT>' ) <EOL> self . _monkey_sender_called = True <EOL> req = RequestFactory ( ) . get ( '<STR_LIT:/>' ) <EOL> new_email = "<STR_LIT>" <EOL> org_sender , email . sender = email . sender , monkey_sender <EOL> try : <EOL> send_email_change_email ( req , self . user , new_email ) <EOL> self . assertTrue ( self . _monkey_sender_called ) <EOL> finally : <EOL> email . sender = org_sender <EOL> def test_email_change_email_complete ( self ) : <EOL> """<STR_LIT>""" <EOL> req = RequestFactory ( ) . get ( '<STR_LIT:/>' ) <EOL> send_email_change_email ( req , self . user , "<STR_LIT>" ) <EOL> self . assertEquals ( len ( mail . outbox ) , <NUM_LIT:1> ) <EOL> def test_sender ( self ) : <EOL> """<STR_LIT>""" <EOL> class SiteMock : <EOL> name = "<STR_LIT:foo>" <EOL> domain = "<STR_LIT>" <EOL> def monkey_get_current_site ( request ) : <EOL> return SiteMock <EOL> def monkey_render_to_string ( template , data ) : <EOL> self . assertEquals ( template , template_name ) <EOL> self . assertDictEqual ( data , { '<STR_LIT>' : self . user . pk , <EOL> '<STR_LIT>' : token , <EOL> '<STR_LIT>' : SiteMock . name , <EOL> '<STR_LIT>' : SiteMock . domain , <EOL> '<STR_LIT>' : '<STR_LIT>' if req . is_secure ( ) else '<STR_LIT:http>' } ) <EOL> return "<STR_LIT>" <EOL> req = RequestFactory ( ) . get ( '<STR_LIT:/>' ) <EOL> token = "<STR_LIT>" <EOL> subject = SiteMock . name <EOL> template_name = "<STR_LIT>" <EOL> context = { '<STR_LIT>' : self . user . pk , '<STR_LIT>' : token } <EOL> org_site , email . get_current_site = email . get_current_site , monkey_get_current_site <EOL> org_render_to_string , email . render_to_string = email . render_to_string , monkey_render_to_string <EOL> try : <EOL> sender ( req , subject , template_name , context , [ self . user . email , ] ) <EOL> finally : <EOL> email . get_current_site = org_site <EOL> email . render_to_string = org_render_to_string <EOL> self . assertEquals ( len ( mail . outbox ) , <NUM_LIT:1> ) <EOL> self . assertEquals ( mail . outbox [ <NUM_LIT:0> ] . subject , SiteMock . name ) <EOL> self . assertEquals ( mail . outbox [ <NUM_LIT:0> ] . body , "<STR_LIT>" ) <EOL> self . assertEquals ( mail . outbox [ <NUM_LIT:0> ] . from_email , "<STR_LIT>" ) <EOL> self . assertEquals ( mail . outbox [ <NUM_LIT:0> ] . to , [ self . user . email , ] ) <EOL> @ override_settings ( DEFAULT_FROM_EMAIL = '<STR_LIT>' ) <EOL> def test_sender_from_email ( self ) : <EOL> """<STR_LIT>""" <EOL> class SiteMock : <EOL> name = "<STR_LIT:foo>" <EOL> domain = "<STR_LIT>" <EOL> def monkey_get_current_site ( * args , ** kw ) : <EOL> return SiteMock <EOL> def monkey_render_to_string ( * args , ** kw ) : <EOL> return "<STR_LIT>" <EOL> req = RequestFactory ( ) . get ( '<STR_LIT:/>' ) <EOL> token = "<STR_LIT>" <EOL> subject = SiteMock . name <EOL> template_name = "<STR_LIT>" <EOL> context = { '<STR_LIT>' : self . user . pk , '<STR_LIT>' : token } <EOL> org_site , email . get_current_site = email . get_current_site , monkey_get_current_site <EOL> org_render_to_string , email . render_to_string = email . render_to_string , monkey_render_to_string <EOL> try : <EOL> sender ( req , subject , template_name , context , [ self . user . email , ] ) <EOL> finally : <EOL> email . get_current_site = org_site <EOL> email . render_to_string = org_render_to_string <EOL> self . assertEquals ( len ( mail . outbox ) , <NUM_LIT:1> ) <EOL> self . assertEquals ( mail . outbox [ <NUM_LIT:0> ] . from_email , "<STR_LIT>" ) </s>
