<s> from django . test import TestCase <EOL> from django . test import Client <EOL> from django . contrib . auth import get_user_model <EOL> from django . core . urlresolvers import reverse <EOL> from datetime import datetime , timedelta <EOL> import json <EOL> from comments . models import Comment , Site , Thread <EOL> User = get_user_model ( ) <EOL> class AdminUsertestCases ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . admin = User . objects . create_superuser ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:password>" <EOL> ) <EOL> self . user = User . objects . create_user ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:password>" , <EOL> ) <EOL> self . user_hidden = User . objects . create_user ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:password>" , <EOL> ) <EOL> self . site = Site . objects . create ( domain = '<STR_LIT>' ) <EOL> self . user_hidden . hidden . add ( self . site ) <EOL> self . user_hidden . created = datetime . today ( ) - timedelta ( <NUM_LIT:1> ) <EOL> self . user_hidden . save ( ) <EOL> self . client = Client ( ) <EOL> self . client . login ( email = "<STR_LIT>" , password = "<STR_LIT:password>" ) <EOL> def test_get_users_returns_all_users_except_admin ( self ) : <EOL> thread = Thread . objects . create ( site = self . site ) <EOL> Comment . objects . create ( thread = thread , user = self . user ) <EOL> Comment . objects . create ( thread = thread , user = self . user_hidden ) <EOL> Comment . objects . create ( thread = thread , user = self . admin ) <EOL> resp = self . client . get ( reverse ( "<STR_LIT>" , <EOL> args = [ self . site . id ] ) ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> users = resp . context [ '<STR_LIT>' ] . object_list <EOL> self . assertTrue ( self . user in users ) <EOL> self . assertTrue ( self . user_hidden in users ) <EOL> self . assertTrue ( self . admin not in users ) <EOL> def test_get_hidden_users_returns_hidden_users ( self ) : <EOL> thread = Thread . objects . create ( site = self . site ) <EOL> Comment . objects . create ( thread = thread , user = self . user ) <EOL> Comment . objects . create ( thread = thread , user = self . user_hidden ) <EOL> resp = self . client . get ( reverse ( "<STR_LIT>" , <EOL> args = [ self . site . id ] ) , { "<STR_LIT>" : True } ) <EOL> self . assertTrue ( resp . status_code , <NUM_LIT:200> ) <EOL> users = resp . context [ '<STR_LIT>' ] . object_list <EOL> self . assertTrue ( self . user_hidden in users ) <EOL> self . assertFalse ( self . user in users ) <EOL> def test_user_bulk_actions_delete_successfully_deletes_user_comments ( self ) : <EOL> thread = Thread . objects . create ( site = self . site ) <EOL> Comment . objects . create ( thread = thread , user = self . user ) <EOL> Comment . objects . create ( thread = thread , user = self . user_hidden ) <EOL> self . assertEqual ( Comment . objects . count ( ) , <NUM_LIT:2> ) <EOL> resp = self . client . post ( <EOL> reverse ( "<STR_LIT>" ) , <EOL> { <EOL> "<STR_LIT>" : self . site . id , <EOL> "<STR_LIT:action>" : [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ self . user . id , self . user_hidden . id ] <EOL> } <EOL> ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> users = User . objects . all ( ) <EOL> self . assertEqual ( users . count ( ) , <NUM_LIT:3> ) <EOL> self . assertFalse ( Comment . objects . count ( ) ) <EOL> def test_user_bulk_actions_delete_successfully_deletes_user_comments_not_admin ( self ) : <EOL> resp = self . client . post ( <EOL> reverse ( "<STR_LIT>" ) , <EOL> { <EOL> "<STR_LIT>" : self . site . id , <EOL> "<STR_LIT:action>" : [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ self . user . id , self . admin . id ] <EOL> } <EOL> ) <EOL> users = User . objects . all ( ) <EOL> self . assertEqual ( users . count ( ) , <NUM_LIT:3> ) <EOL> thread = Thread . objects . create ( site = self . site ) <EOL> Comment . objects . create ( thread = thread , user = self . user ) <EOL> Comment . objects . create ( thread = thread , user = self . admin ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> users = User . objects . all ( ) <EOL> self . assertEqual ( users . count ( ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( Comment . objects . filter ( user = self . admin ) . count ( ) , <NUM_LIT:1> ) <EOL> def test_user_bulk_actions_hide_successfully_hides_users ( self ) : <EOL> users = User . objects . filter ( hidden = True ) <EOL> self . assertEqual ( users . count ( ) , <NUM_LIT:1> ) <EOL> resp = self . client . post ( <EOL> reverse ( "<STR_LIT>" ) , <EOL> { <EOL> "<STR_LIT>" : self . site . id , <EOL> "<STR_LIT:action>" : [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ self . user . id ] <EOL> } <EOL> ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> users = User . objects . filter ( hidden = True ) <EOL> self . assertEqual ( users . count ( ) , <NUM_LIT:2> ) <EOL> self . assertTrue ( self . user in users ) <EOL> def test_user_bulk_actions_hide_successfully_hides_user_not_admin ( self ) : <EOL> site2 = Site . objects . create ( domain = '<STR_LIT>' ) <EOL> users = Site . objects . get ( id = site2 . id ) . hidden_users . all ( ) <EOL> self . assertEqual ( users . count ( ) , <NUM_LIT:0> ) <EOL> self . assertTrue ( self . admin not in users ) <EOL> resp = self . client . post ( <EOL> reverse ( "<STR_LIT>" ) , <EOL> { <EOL> "<STR_LIT>" : site2 . id , <EOL> "<STR_LIT:action>" : [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ self . user . id , self . admin . id ] <EOL> } <EOL> ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> users = Site . objects . get ( id = site2 . id ) . hidden_users . all ( ) <EOL> self . assertEqual ( users . count ( ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( self . admin not in users ) <EOL> def test_user_bulk_actions_hide_hidden_user_doesnt_change_status ( self ) : <EOL> resp = self . client . post ( <EOL> reverse ( "<STR_LIT>" ) , <EOL> { <EOL> "<STR_LIT>" : self . site . id , <EOL> "<STR_LIT:action>" : [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ self . user_hidden . id ] <EOL> } <EOL> ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> users = User . objects . filter ( hidden = True ) <EOL> self . assertEqual ( users . count ( ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( self . user_hidden in users ) <EOL> def test_user_hide_not_ajax_call_fails ( self ) : <EOL> """<STR_LIT>""" <EOL> r = self . client . post ( <EOL> reverse ( '<STR_LIT>' , args = ( <EOL> self . site . id , self . user . id , ) ) , <EOL> ) <EOL> self . assertEqual ( r . status_code , <NUM_LIT> ) <EOL> def test_user_hide_succeeds ( self ) : <EOL> """<STR_LIT>""" <EOL> r = self . client . post ( <EOL> reverse ( '<STR_LIT>' , args = ( <EOL> self . site . id , self . user . id , ) ) , <EOL> HTTP_X_REQUESTED_WITH = '<STR_LIT>' <EOL> ) <EOL> self . assertEqual ( r . status_code , <NUM_LIT:200> ) <EOL> user = User . objects . get ( id = self . user . id ) <EOL> self . assertTrue ( user . hidden . filter ( id = self . site . id ) ) <EOL> def test_user_hide_admin_fails ( self ) : <EOL> """<STR_LIT>""" <EOL> r = self . client . post ( <EOL> reverse ( '<STR_LIT>' , args = ( <EOL> self . site . id , self . admin . id , ) ) , <EOL> HTTP_X_REQUESTED_WITH = '<STR_LIT>' <EOL> ) <EOL> self . assertEqual ( r . status_code , <NUM_LIT> ) <EOL> user = User . objects . get ( id = self . admin . id ) <EOL> self . assertFalse ( user . hidden . filter ( id = self . site . id ) ) <EOL> def test_user_hide_hidden_user_doesnt_change_state ( self ) : <EOL> """<STR_LIT>""" <EOL> r = self . client . post ( <EOL> reverse ( '<STR_LIT>' , args = ( <EOL> self . site . id , self . user_hidden . id , ) ) , <EOL> HTTP_X_REQUESTED_WITH = '<STR_LIT>' <EOL> ) <EOL> self . assertEqual ( r . status_code , <NUM_LIT:200> ) <EOL> user = User . objects . get ( id = self . user_hidden . id ) <EOL> self . assertTrue ( user . hidden . filter ( id = self . site . id ) ) <EOL> def test_user_hide_returns_404_for_nonexisting_user ( self ) : <EOL> """<STR_LIT>""" <EOL> r = self . client . post ( <EOL> reverse ( '<STR_LIT>' , args = ( self . site . id , <NUM_LIT> , ) ) , <EOL> HTTP_X_REQUESTED_WITH = '<STR_LIT>' <EOL> ) <EOL> self . assertEqual ( r . status_code , <NUM_LIT> ) <EOL> def test_user_unhide_not_ajax_call_fails ( self ) : <EOL> """<STR_LIT>""" <EOL> r = self . client . post ( <EOL> reverse ( '<STR_LIT>' , args = ( <EOL> self . site . id , self . user . id , ) ) , <EOL> ) <EOL> self . assertEqual ( r . status_code , <NUM_LIT> ) <EOL> def test_user_unhide_succeeds ( self ) : <EOL> """<STR_LIT>""" <EOL> r = self . client . post ( <EOL> reverse ( '<STR_LIT>' , args = ( <EOL> self . site . id , self . user_hidden . id , ) ) , <EOL> HTTP_X_REQUESTED_WITH = '<STR_LIT>' <EOL> ) <EOL> self . assertEqual ( r . status_code , <NUM_LIT:200> ) <EOL> user = User . objects . get ( id = self . user_hidden . id ) <EOL> self . assertFalse ( user . hidden . filter ( id = self . site . id ) ) <EOL> def test_user_unhide_admin_fails ( self ) : <EOL> """<STR_LIT>""" <EOL> r = self . client . post ( <EOL> reverse ( '<STR_LIT>' , args = ( <EOL> self . site . id , self . admin . id , ) ) , <EOL> HTTP_X_REQUESTED_WITH = '<STR_LIT>' <EOL> ) <EOL> self . assertEqual ( r . status_code , <NUM_LIT> ) <EOL> user = User . objects . get ( id = self . admin . id ) <EOL> self . assertFalse ( user . hidden . filter ( id = self . site . id ) ) <EOL> def test_user_unhide_returns_404_for_nonexisting_user ( self ) : <EOL> """<STR_LIT>""" <EOL> r = self . client . post ( <EOL> reverse ( '<STR_LIT>' , args = ( self . site . id , <NUM_LIT> , ) ) , <EOL> HTTP_X_REQUESTED_WITH = '<STR_LIT>' <EOL> ) <EOL> self . assertEqual ( r . status_code , <NUM_LIT> ) <EOL> def test_admin_changes_password_successfully ( self ) : <EOL> """<STR_LIT>""" <EOL> pass_1 = self . user . password <EOL> resp = self . client . post ( <EOL> reverse ( "<STR_LIT>" , args = [ self . user . id ] ) , <EOL> data = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> HTTP_X_REQUESTED_WITH = '<STR_LIT>' <EOL> ) <EOL> pass_2 = User . objects . get ( id = self . user . id ) . password <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> self . assertNotEqual ( pass_1 , pass_2 ) <EOL> content = json . loads ( resp . content ) <EOL> self . assertEqual ( content [ '<STR_LIT>' ] , self . user . id ) <EOL> def test_admin_changes_password_passwords_different_doesnt_change_password ( self ) : <EOL> """<STR_LIT>""" <EOL> pass_1 = self . user . password <EOL> resp = self . client . post ( <EOL> reverse ( "<STR_LIT>" , args = [ self . user . id ] ) , <EOL> data = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> HTTP_X_REQUESTED_WITH = '<STR_LIT>' <EOL> ) <EOL> pass_2 = User . objects . get ( id = self . user . id ) . password <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( pass_1 , pass_2 ) <EOL> content = json . loads ( resp . content ) <EOL> self . assertEqual ( content [ '<STR_LIT>' ] , self . user . id ) <EOL> def test_admin_password_change_not_provided_password2_doesnt_change_password ( self ) : <EOL> """<STR_LIT>""" <EOL> pass_1 = self . user . password <EOL> resp = self . client . post ( <EOL> reverse ( "<STR_LIT>" , args = [ self . user . id ] ) , <EOL> data = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> HTTP_X_REQUESTED_WITH = '<STR_LIT>' <EOL> ) <EOL> pass_2 = User . objects . get ( id = self . user . id ) . password <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( pass_1 , pass_2 ) <EOL> content = json . loads ( resp . content ) <EOL> self . assertEqual ( content [ '<STR_LIT>' ] , self . user . id ) <EOL> def test_admin_password_change_not_provided_password1_doesnt_change_password ( self ) : <EOL> """<STR_LIT>""" <EOL> pass_1 = self . user . password <EOL> resp = self . client . post ( <EOL> reverse ( "<STR_LIT>" , args = [ self . user . id ] ) , <EOL> data = { <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> HTTP_X_REQUESTED_WITH = '<STR_LIT>' <EOL> ) <EOL> pass_2 = User . objects . get ( id = self . user . id ) . password <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( pass_1 , pass_2 ) <EOL> content = json . loads ( resp . content ) <EOL> self . assertEqual ( content [ '<STR_LIT>' ] , self . user . id ) <EOL> def test_admin_password_change_nonexistent_user_returns_404 ( self ) : <EOL> """<STR_LIT>""" <EOL> pass_1 = self . user . password <EOL> resp = self . client . post ( <EOL> reverse ( "<STR_LIT>" , args = [ <NUM_LIT> ] ) , <EOL> data = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> HTTP_X_REQUESTED_WITH = '<STR_LIT>' <EOL> ) <EOL> pass_2 = User . objects . get ( id = self . user . id ) . password <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( pass_1 , pass_2 ) <EOL> def test_change_admins_password_fails ( self ) : <EOL> """<STR_LIT>""" <EOL> pass_1 = self . user . password <EOL> resp = self . client . post ( <EOL> reverse ( "<STR_LIT>" , args = [ self . admin . id ] ) , <EOL> data = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> HTTP_X_REQUESTED_WITH = '<STR_LIT>' <EOL> ) <EOL> pass_2 = User . objects . get ( id = self . user . id ) . password <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( pass_1 , pass_2 ) </s>
