<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 django . utils . timezone import now <EOL> from comments . models import ( Site , Thread , Comment ) <EOL> from datetime import timedelta , date <EOL> User = get_user_model ( ) <EOL> class AdminThreadTestCases ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . admin = User . objects . create_superuser ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:password>" <EOL> ) <EOL> self . client = Client ( ) <EOL> self . site = Site . objects . create ( ) <EOL> def test_get_threads_succeeds ( self ) : <EOL> self . client . login ( email = "<STR_LIT>" , password = "<STR_LIT:password>" ) <EOL> Thread . objects . create ( site = self . site , url = '<STR_LIT>' ) <EOL> resp = self . client . get ( reverse ( "<STR_LIT>" , args = [ self . site . id , ] ) ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> def test_get_threads_filter_1_day_success ( self ) : <EOL> time_period = <NUM_LIT:1> <EOL> t1 = Thread . objects . create ( site = self . site , url = '<STR_LIT>' ) <EOL> t2 = Thread . objects . create ( <EOL> site = self . site , <EOL> url = '<STR_LIT>' , <EOL> ) <EOL> t2 . created = now ( ) - timedelta ( days = time_period ) <EOL> t2 . save ( ) <EOL> self . client . login ( email = "<STR_LIT>" , password = "<STR_LIT:password>" ) <EOL> resp = self . client . post ( <EOL> reverse ( "<STR_LIT>" , args = [ self . site . id , ] ) , <EOL> { '<STR_LIT>' : '<STR_LIT>' } <EOL> ) <EOL> threads = resp . context [ '<STR_LIT>' ] . object_list <EOL> self . assertTrue ( t1 in threads ) <EOL> self . assertTrue ( t2 not in threads ) <EOL> def test_get_threads_filter_1_day_borderline_case ( self ) : <EOL> t1 = Thread . objects . create ( site = self . site , url = '<STR_LIT>' ) <EOL> t1 . created = date . today ( ) <EOL> t1 . save ( ) <EOL> self . client . login ( email = "<STR_LIT>" , password = "<STR_LIT:password>" ) <EOL> resp = self . client . post ( <EOL> reverse ( "<STR_LIT>" , args = [ self . site . id , ] ) , <EOL> { '<STR_LIT>' : '<STR_LIT>' } <EOL> ) <EOL> threads = resp . context [ '<STR_LIT>' ] . object_list <EOL> self . assertTrue ( t1 in threads ) <EOL> def test_get_threads_filter_1_week_success ( self ) : <EOL> time_period = <NUM_LIT:7> <EOL> t1 = Thread . objects . create ( site = self . site , url = '<STR_LIT>' ) <EOL> t2 = Thread . objects . create ( <EOL> site = self . site , <EOL> url = '<STR_LIT>' , <EOL> ) <EOL> t2 . created = now ( ) - timedelta ( days = time_period ) <EOL> t2 . save ( ) <EOL> self . client . login ( email = "<STR_LIT>" , password = "<STR_LIT:password>" ) <EOL> resp = self . client . post ( <EOL> reverse ( "<STR_LIT>" , args = [ self . site . id , ] ) , <EOL> { '<STR_LIT>' : '<STR_LIT>' } <EOL> ) <EOL> threads = resp . context [ '<STR_LIT>' ] . object_list <EOL> self . assertTrue ( t1 in threads ) <EOL> self . assertTrue ( t2 not in threads ) <EOL> def test_get_threads_filter_1_week_borderline_case ( self ) : <EOL> t1 = Thread . objects . create ( site = self . site , url = '<STR_LIT>' ) <EOL> t1 . created = date . today ( ) - timedelta ( date . today ( ) . weekday ( ) ) <EOL> t1 . save ( ) <EOL> self . client . login ( email = "<STR_LIT>" , password = "<STR_LIT:password>" ) <EOL> resp = self . client . post ( <EOL> reverse ( "<STR_LIT>" , args = [ self . site . id , ] ) , <EOL> { '<STR_LIT>' : '<STR_LIT>' } <EOL> ) <EOL> threads = resp . context [ '<STR_LIT>' ] . object_list <EOL> self . assertTrue ( t1 in threads ) <EOL> def test_get_threads_filter_1_month_success ( self ) : <EOL> time_period = now ( ) . day <EOL> t1 = Thread . objects . create ( site = self . site , url = '<STR_LIT>' ) <EOL> t2 = Thread . objects . create ( <EOL> site = self . site , <EOL> url = '<STR_LIT>' , <EOL> ) <EOL> t2 . created = now ( ) - timedelta ( days = time_period ) <EOL> t2 . save ( ) <EOL> self . client . login ( email = "<STR_LIT>" , password = "<STR_LIT:password>" ) <EOL> resp = self . client . post ( <EOL> reverse ( "<STR_LIT>" , args = [ self . site . id , ] ) , <EOL> { '<STR_LIT>' : '<STR_LIT>' } <EOL> ) <EOL> threads = resp . context [ '<STR_LIT>' ] . object_list <EOL> self . assertTrue ( t1 in threads ) <EOL> self . assertTrue ( t2 not in threads ) <EOL> def test_get_threads_filter_1_month_borderline ( self ) : <EOL> t1 = Thread . objects . create ( site = self . site , url = '<STR_LIT>' ) <EOL> t1 . created = date . today ( ) - timedelta ( date . today ( ) . day - <NUM_LIT:1> ) <EOL> t1 . save ( ) <EOL> self . client . login ( email = "<STR_LIT>" , password = "<STR_LIT:password>" ) <EOL> resp = self . client . post ( <EOL> reverse ( "<STR_LIT>" , args = [ self . site . id , ] ) , <EOL> { '<STR_LIT>' : '<STR_LIT>' } <EOL> ) <EOL> threads = resp . context [ '<STR_LIT>' ] . object_list <EOL> self . assertTrue ( t1 in threads ) <EOL> def test_get_threads_filter_all_time_success ( self ) : <EOL> time_period = <NUM_LIT:100> <EOL> t1 = Thread . objects . create ( site = self . site , url = '<STR_LIT>' ) <EOL> t2 = Thread . objects . create ( <EOL> site = self . site , <EOL> url = '<STR_LIT>' , <EOL> ) <EOL> t2 . created = now ( ) - timedelta ( weeks = time_period ) <EOL> t2 . save ( ) <EOL> self . client . login ( email = "<STR_LIT>" , password = "<STR_LIT:password>" ) <EOL> resp = self . client . post ( <EOL> reverse ( "<STR_LIT>" , args = [ self . site . id , ] ) , <EOL> { '<STR_LIT>' : '<STR_LIT>' } <EOL> ) <EOL> threads = resp . context [ '<STR_LIT>' ] . object_list <EOL> self . assertTrue ( t1 in threads ) <EOL> self . assertTrue ( t2 in threads ) <EOL> def test_get_threads_returns_all_threads_if_period_not_provided_post ( self ) : <EOL> t1 = Thread . objects . create ( site = self . site , url = '<STR_LIT>' ) <EOL> t2 = Thread . objects . create ( site = self . site , url = '<STR_LIT>' ) <EOL> self . client . login ( email = "<STR_LIT>" , password = "<STR_LIT:password>" ) <EOL> resp = self . client . post ( reverse ( "<STR_LIT>" , args = [ self . site . id , ] ) ) <EOL> threads = resp . context [ '<STR_LIT>' ] . object_list <EOL> self . assertTrue ( t1 in threads ) <EOL> self . assertTrue ( t2 in threads ) <EOL> def test_get_threads_returns_all_threads_if_period_not_provided_get ( self ) : <EOL> t1 = Thread . objects . create ( site = self . site , url = '<STR_LIT>' ) <EOL> t2 = Thread . objects . create ( site = self . site , url = '<STR_LIT>' ) <EOL> self . client . login ( email = "<STR_LIT>" , password = "<STR_LIT:password>" ) <EOL> resp = self . client . get ( reverse ( "<STR_LIT>" , args = [ self . site . id , ] ) ) <EOL> threads = resp . context [ '<STR_LIT>' ] . object_list <EOL> self . assertTrue ( t1 in threads ) <EOL> self . assertTrue ( t2 in threads ) <EOL> def test_get_threads_site_doesnt_exist_returns_404 ( self ) : <EOL> self . client . login ( email = "<STR_LIT>" , password = "<STR_LIT:password>" ) <EOL> Thread . objects . create ( site = self . site , url = '<STR_LIT>' ) <EOL> resp = self . client . get ( reverse ( "<STR_LIT>" , args = [ <NUM_LIT> , ] ) ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> def test_get_threads_returns_all_threads_if_no_id_token ( self ) : <EOL> self . client . login ( email = "<STR_LIT>" , password = "<STR_LIT:password>" ) <EOL> t = Thread . objects . create ( site = self . site , url = '<STR_LIT>' ) <EOL> resp = self . client . get ( reverse ( "<STR_LIT>" ) ) <EOL> threads = resp . context [ '<STR_LIT>' ] . object_list <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> self . assertTrue ( t in threads ) <EOL> def test_get_threads_returns_all_threads_by_latest_comment ( self ) : <EOL> self . client . login ( email = "<STR_LIT>" , password = "<STR_LIT:password>" ) <EOL> t1 = Thread . objects . create ( site = self . site , url = '<STR_LIT>' ) <EOL> t2 = Thread . objects . create ( site = self . site , url = '<STR_LIT>' ) <EOL> Comment . objects . create ( thread = t2 , user = self . admin ) <EOL> resp = self . client . get ( reverse ( "<STR_LIT>" ) ) <EOL> threads = resp . context [ '<STR_LIT>' ] . object_list <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> threads = resp . context [ '<STR_LIT>' ] . object_list <EOL> self . assertEqual ( len ( threads ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( threads [ <NUM_LIT:0> ] . id , t2 . id ) <EOL> self . assertEqual ( threads [ <NUM_LIT:1> ] . id , t1 . id ) <EOL> def test_get_threads_returns_all_threads_by_thread_date ( self ) : <EOL> self . client . login ( email = "<STR_LIT>" , password = "<STR_LIT:password>" ) <EOL> t1 = Thread . objects . create ( site = self . site , url = '<STR_LIT>' ) <EOL> t2 = Thread . objects . create ( site = self . site , url = '<STR_LIT>' ) <EOL> Comment . objects . create ( thread = t1 , user = self . admin ) <EOL> resp = self . client . get ( reverse ( "<STR_LIT>" ) , <EOL> data = { "<STR_LIT>" : "<STR_LIT>" } <EOL> ) <EOL> threads = resp . context [ '<STR_LIT>' ] . object_list <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> threads = resp . context [ '<STR_LIT>' ] . object_list <EOL> self . assertEqual ( len ( threads ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( threads [ <NUM_LIT:0> ] . id , t2 . id ) <EOL> self . assertEqual ( threads [ <NUM_LIT:1> ] . id , t1 . id ) <EOL> def test_get_threads_returns_threads_from_site_with_lowest_id_if_site_id_not_provided ( self ) : <EOL> self . site_2 = Site . objects . create ( ) <EOL> t1 = Thread . objects . create ( site = self . site , url = '<STR_LIT>' ) <EOL> t2 = Thread . objects . create ( site = self . site_2 , url = '<STR_LIT>' ) <EOL> self . client . login ( email = "<STR_LIT>" , password = "<STR_LIT:password>" ) <EOL> resp = self . client . get ( reverse ( "<STR_LIT>" ) ) <EOL> threads = resp . context [ '<STR_LIT>' ] . object_list <EOL> self . assertTrue ( t1 in threads ) <EOL> self . assertTrue ( t2 not in threads ) <EOL> def test_get_filtered_threads_admins_can_access ( self ) : <EOL> self . site_2 = Site . objects . create ( ) <EOL> t1 = Thread . objects . create ( site = self . site , url = '<STR_LIT>' ) <EOL> t2 = Thread . objects . create ( site = self . site_2 , url = '<STR_LIT>' ) <EOL> self . client . login ( email = "<STR_LIT>" , password = "<STR_LIT:password>" ) <EOL> self . admin . is_superuser = False <EOL> self . admin . save ( ) <EOL> self . assertFalse ( self . admin . get_threads ( ) ) <EOL> self . site . admins . add ( self . admin ) <EOL> num_of_threads = self . admin . get_threads ( ) . count ( ) <EOL> self . assertEqual ( num_of_threads , <NUM_LIT:1> ) <EOL> self . assertEqual ( <EOL> self . admin . get_threads ( ) [ num_of_threads - <NUM_LIT:1> ] . id , t1 . id ) <EOL> self . site_2 . admins . add ( self . admin ) <EOL> self . site . admins . add ( self . admin ) <EOL> num_of_threads = self . admin . get_threads ( ) . count ( ) <EOL> self . assertEqual ( num_of_threads , <NUM_LIT:2> ) <EOL> self . assertEqual ( <EOL> self . admin . get_threads ( ) [ num_of_threads - <NUM_LIT:2> ] . id , t1 . id ) <EOL> self . assertEqual ( <EOL> self . admin . get_threads ( ) [ num_of_threads - <NUM_LIT:1> ] . id , t2 . id ) <EOL> def test_superuser_can_get_all_threads ( self ) : <EOL> self . site_2 = Site . objects . create ( ) <EOL> t1 = Thread . objects . create ( site = self . site , url = '<STR_LIT>' ) <EOL> t2 = Thread . objects . create ( site = self . site_2 , url = '<STR_LIT>' ) <EOL> self . client . login ( email = "<STR_LIT>" , password = "<STR_LIT:password>" ) <EOL> self . site_2 . admins . add ( self . admin ) <EOL> self . site . admins . add ( self . admin ) <EOL> num_of_threads = self . admin . get_threads ( ) . count ( ) <EOL> self . assertEqual ( num_of_threads , <NUM_LIT:2> ) <EOL> self . assertEqual ( <EOL> self . admin . get_threads ( ) [ num_of_threads - <NUM_LIT:2> ] . id , t1 . id ) <EOL> self . assertEqual ( <EOL> self . admin . get_threads ( ) [ num_of_threads - <NUM_LIT:1> ] . id , t2 . id ) <EOL> def test_get_filtered_comments_admins_can_access ( self ) : <EOL> self . site_2 = Site . objects . create ( ) <EOL> t1 = Thread . objects . create ( site = self . site , url = '<STR_LIT>' ) <EOL> t2 = Thread . objects . create ( site = self . site_2 , url = '<STR_LIT>' ) <EOL> c1 = Comment . objects . create ( thread = t1 , hidden = True ) <EOL> c2 = Comment . objects . create ( thread = t2 , hidden = True ) <EOL> c3 = Comment . objects . create ( thread = t2 , hidden = True ) <EOL> self . client . login ( email = "<STR_LIT>" , password = "<STR_LIT:password>" ) <EOL> self . admin . is_superuser = False <EOL> self . admin . save ( ) <EOL> self . assertFalse ( self . admin . get_comments ( ) ) <EOL> self . site . admins . add ( self . admin ) <EOL> num_of_comments = self . admin . get_comments ( ) . count ( ) <EOL> self . assertEqual ( num_of_comments , <NUM_LIT:1> ) <EOL> self . assertEqual ( <EOL> self . admin . get_comments ( ) [ num_of_comments - <NUM_LIT:1> ] . id , c1 . id ) <EOL> self . site_2 . admins . add ( self . admin ) <EOL> num_of_comments = self . admin . get_comments ( ) . count ( ) <EOL> self . assertEqual ( num_of_comments , <NUM_LIT:3> ) <EOL> self . assertEqual ( <EOL> self . admin . get_comments ( ) [ num_of_comments - <NUM_LIT:3> ] . id , c1 . id ) <EOL> self . assertEqual ( <EOL> self . admin . get_comments ( ) [ num_of_comments - <NUM_LIT:2> ] . id , c2 . id ) <EOL> self . assertEqual ( <EOL> self . admin . get_comments ( ) [ num_of_comments - <NUM_LIT:1> ] . id , c3 . id ) <EOL> def test_superuser_can_get_all_comments ( self ) : <EOL> self . site_2 = Site . objects . create ( ) <EOL> t1 = Thread . objects . create ( site = self . site , url = '<STR_LIT>' ) <EOL> t2 = Thread . objects . create ( site = self . site_2 , url = '<STR_LIT>' ) <EOL> c1 = Comment . objects . create ( thread = t1 , hidden = True ) <EOL> c2 = Comment . objects . create ( thread = t2 , hidden = True ) <EOL> c3 = Comment . objects . create ( thread = t2 , hidden = True ) <EOL> num_of_comments = self . admin . get_comments ( ) . count ( ) <EOL> self . assertEqual ( num_of_comments , <NUM_LIT:3> ) <EOL> self . assertEqual ( <EOL> self . admin . get_comments ( ) [ num_of_comments - <NUM_LIT:3> ] . id , c1 . id ) <EOL> self . assertEqual ( <EOL> self . admin . get_comments ( ) [ num_of_comments - <NUM_LIT:2> ] . id , c2 . id ) <EOL> self . assertEqual ( <EOL> self . admin . get_comments ( ) [ num_of_comments - <NUM_LIT:1> ] . id , c3 . id ) </s>
