<s> from django . test import TestCase , override_settings <EOL> from django . contrib . auth import get_user_model <EOL> from notify . models import Notification <EOL> from notify . signals import notify <EOL> from django . utils import timezone <EOL> from django . core . urlresolvers import reverse <EOL> import json <EOL> from django . template import Template , Context , RequestContext <EOL> from django . test . client import RequestFactory <EOL> from django . contrib . auth . models import AnonymousUser <EOL> User = get_user_model ( ) <EOL> class NotificationManagerTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . recipient = User . objects . create ( username = '<STR_LIT>' , <EOL> email = '<STR_LIT>' , <EOL> password = '<STR_LIT>' ) <EOL> self . actor = User . objects . create ( username = '<STR_LIT>' , <EOL> email = '<STR_LIT>' , <EOL> password = '<STR_LIT>' ) <EOL> self . nf_count = <NUM_LIT:10> <EOL> self . assertEqual ( self . recipient . id , <NUM_LIT:1> ) <EOL> self . assertEqual ( self . actor . id , <NUM_LIT:2> ) <EOL> for x in range ( self . nf_count ) : <EOL> notify . send ( User , recipient = self . recipient , actor = self . actor , <EOL> verb = '<STR_LIT>' ) <EOL> def test_unread ( self ) : <EOL> self . assertEqual ( Notification . objects . unread ( ) . count ( ) , self . nf_count ) <EOL> nf = Notification . objects . filter ( recipient = self . recipient ) . first ( ) <EOL> nf . mark_as_read ( ) <EOL> self . assertEqual ( Notification . objects . unread ( ) . count ( ) , <EOL> self . nf_count - <NUM_LIT:1> ) <EOL> for n in Notification . objects . unread ( ) : <EOL> self . assertFalse ( n . read ) <EOL> def test_read ( self ) : <EOL> self . assertEqual ( Notification . objects . unread ( ) . count ( ) , self . nf_count ) <EOL> nf = Notification . objects . filter ( recipient = self . recipient ) . first ( ) <EOL> nf . mark_as_read ( ) <EOL> self . assertEqual ( Notification . objects . read ( ) . count ( ) , <NUM_LIT:1> ) <EOL> for n in Notification . objects . read ( ) : <EOL> self . assertTrue ( n . read ) <EOL> def test_read_all ( self ) : <EOL> self . assertEqual ( Notification . objects . unread ( ) . count ( ) , self . nf_count ) <EOL> recipient_nfs = Notification . objects . filter ( recipient = self . recipient ) <EOL> recipient_nfs . read_all ( ) <EOL> self . assertEqual ( Notification . objects . read ( ) . count ( ) , self . nf_count ) <EOL> self . assertEqual ( Notification . objects . unread ( ) . count ( ) , <NUM_LIT:0> ) <EOL> def test_unread_all ( self ) : <EOL> self . assertEqual ( Notification . objects . unread ( ) . count ( ) , self . nf_count ) <EOL> recipient_nfs = Notification . objects . filter ( recipient = self . recipient ) <EOL> recipient_nfs . read_all ( ) <EOL> self . assertEqual ( Notification . objects . read ( ) . count ( ) , self . nf_count ) <EOL> self . assertEqual ( Notification . objects . unread ( ) . count ( ) , <NUM_LIT:0> ) <EOL> recipient_nfs . unread_all ( ) <EOL> self . assertEqual ( Notification . objects . read ( ) . count ( ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( Notification . objects . unread ( ) . count ( ) , self . nf_count ) <EOL> def test_delete_all_soft ( self ) : <EOL> self . assertEqual ( Notification . objects . unread ( ) . count ( ) , self . nf_count ) <EOL> nf = Notification . objects . filter ( recipient = self . recipient ) . first ( ) <EOL> nf . mark_as_read ( ) <EOL> self . assertEqual ( Notification . objects . read ( ) . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( Notification . objects . unread ( ) . count ( ) , <EOL> self . nf_count - <NUM_LIT:1> ) <EOL> self . assertEqual ( Notification . objects . active ( ) . count ( ) , self . nf_count ) <EOL> self . assertEqual ( Notification . objects . deleted ( ) . count ( ) , <NUM_LIT:0> ) <EOL> Notification . objects . delete_all ( ) <EOL> self . assertEqual ( Notification . objects . read ( ) . count ( ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( Notification . objects . unread ( ) . count ( ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( Notification . objects . active ( ) . count ( ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( Notification . objects . deleted ( ) . count ( ) , self . nf_count ) <EOL> Notification . objects . active_all ( ) <EOL> self . assertEqual ( Notification . objects . read ( ) . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( Notification . objects . unread ( ) . count ( ) , <EOL> self . nf_count - <NUM_LIT:1> ) <EOL> self . assertEqual ( Notification . objects . active ( ) . count ( ) , self . nf_count ) <EOL> self . assertEqual ( Notification . objects . deleted ( ) . count ( ) , <NUM_LIT:0> ) <EOL> @ override_settings ( NOTIFY_SOFT_DELETE = False ) <EOL> def test_delete_all_hard ( self ) : <EOL> self . assertEqual ( Notification . objects . active ( ) . count ( ) , self . nf_count ) <EOL> Notification . objects . delete_all ( ) <EOL> self . assertEqual ( Notification . objects . active ( ) . count ( ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( Notification . objects . deleted ( ) . count ( ) , <NUM_LIT:0> ) <EOL> class NotificationTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . no_of_users = <NUM_LIT:10> <EOL> users = [ ] <EOL> for i in range ( self . no_of_users ) : <EOL> users . append ( <EOL> User ( username = '<STR_LIT>' % i , password = '<STR_LIT>' % i , <EOL> email = '<STR_LIT>' % i ) ) <EOL> User . objects . bulk_create ( users ) <EOL> self . actor = User . objects . create ( username = '<STR_LIT>' , <EOL> password = '<STR_LIT>' , <EOL> email = '<STR_LIT>' ) <EOL> self . recipient = User . objects . get ( username = '<STR_LIT>' ) <EOL> self . recipient_list = User . objects . filter ( <EOL> username__startswith = '<STR_LIT:u>' ) . order_by ( '<STR_LIT:id>' ) <EOL> def test_created_users ( self ) : <EOL> users = User . objects . all ( ) <EOL> self . assertEqual ( users . count ( ) , self . no_of_users + <NUM_LIT:1> ) <EOL> self . assertEqual ( self . recipient_list . count ( ) , self . no_of_users ) <EOL> def test_single_user_notify ( self ) : <EOL> notify . send ( User , recipient = self . recipient , actor = self . actor , <EOL> verb = '<STR_LIT>' ) <EOL> notification = Notification . objects . get ( pk = <NUM_LIT:1> ) <EOL> self . assertEqual ( notification . recipient_id , self . recipient . id ) <EOL> timedelta = timezone . now ( ) - notification . created <EOL> self . assertLessEqual ( timedelta . seconds , <NUM_LIT> ) <EOL> def test_multiple_user_notify ( self ) : <EOL> notify . send ( User , recipient_list = list ( self . recipient_list ) , <EOL> actor = self . actor , verb = '<STR_LIT>' ) <EOL> notifications = Notification . objects . filter ( verb__startswith = '<STR_LIT:u>' ) <EOL> self . assertEqual ( notifications . count ( ) , self . no_of_users ) <EOL> username_list = [ u . username for u in self . recipient_list ] <EOL> for nf in notifications : <EOL> self . assertIn ( nf . recipient . username , username_list ) <EOL> timedelta = timezone . now ( ) - nf . created <EOL> self . assertLessEqual ( timedelta . seconds , <NUM_LIT> ) <EOL> class NotificationViewTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . no_of_users = <NUM_LIT:10> <EOL> users = [ ] <EOL> for i in range ( self . no_of_users ) : <EOL> u = User ( username = '<STR_LIT>' % i , email = '<STR_LIT>' % i ) <EOL> u . set_password ( '<STR_LIT>' ) <EOL> users . append ( u ) <EOL> User . objects . bulk_create ( users ) <EOL> actor = User . objects . create ( username = '<STR_LIT>' , <EOL> email = '<STR_LIT>' ) <EOL> actor . set_password ( '<STR_LIT>' ) <EOL> actor . save ( ) <EOL> self . actor = User . objects . get ( username = "<STR_LIT>" ) <EOL> self . recipient = User . objects . get ( username = '<STR_LIT>' ) <EOL> self . recipient_list = User . objects . filter ( <EOL> username__startswith = '<STR_LIT:u>' ) . order_by ( '<STR_LIT:id>' ) <EOL> notify . send ( User , recipient_list = list ( self . recipient_list ) , <EOL> actor = self . actor , verb = '<STR_LIT>' ) <EOL> self . recipient_nf_count = <NUM_LIT:11> <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> notify . send ( User , recipient = self . recipient , actor_text = '<STR_LIT>' , <EOL> verb = '<STR_LIT>' % i ) <EOL> self . assertTrue ( self . client . login ( username = "<STR_LIT>" , <EOL> password = "<STR_LIT>" ) ) <EOL> def test_created_users ( self ) : <EOL> users = User . objects . all ( ) <EOL> self . assertEqual ( users . count ( ) , self . no_of_users + <NUM_LIT:1> ) <EOL> self . assertEqual ( self . recipient_list . count ( ) , self . no_of_users ) <EOL> self . assertEqual ( self . recipient . id , <NUM_LIT:1> ) <EOL> self . assertTrue ( self . recipient . is_active ) <EOL> def test_notifications_view ( self ) : <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( response . context [ '<STR_LIT>' ] . count ( ) , <EOL> self . recipient . notifications . count ( ) ) <EOL> self . assertEqual ( response . context [ '<STR_LIT>' ] . count ( ) , <EOL> self . recipient_nf_count ) <EOL> self . assertTemplateUsed ( response , '<STR_LIT>' ) <EOL> def test_mark_notification_view_non_ajax_without_next_parameter ( self ) : <EOL> self . assertEqual ( self . recipient . notifications . unread ( ) . count ( ) , <EOL> self . recipient_nf_count ) <EOL> first_nf = self . recipient . notifications . first ( ) <EOL> first_nf_id = first_nf . id <EOL> self . assertFalse ( first_nf . read ) <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> { '<STR_LIT:action>' : '<STR_LIT>' , '<STR_LIT:id>' : first_nf_id } ) <EOL> self . assertRedirects ( response , reverse ( '<STR_LIT>' ) ) <EOL> nf = Notification . objects . get ( pk = first_nf_id ) <EOL> self . assertTrue ( nf . read ) <EOL> self . assertEqual ( self . recipient . notifications . unread ( ) . count ( ) , <EOL> self . recipient_nf_count - <NUM_LIT:1> ) <EOL> response_2 = self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> { '<STR_LIT:action>' : '<STR_LIT>' , '<STR_LIT:id>' : first_nf_id } ) <EOL> self . assertRedirects ( response_2 , reverse ( '<STR_LIT>' ) ) <EOL> nf_2 = Notification . objects . get ( pk = first_nf_id ) <EOL> self . assertFalse ( nf_2 . read ) <EOL> self . assertEqual ( self . recipient . notifications . unread ( ) . count ( ) , <EOL> self . recipient_nf_count ) <EOL> def test_mark_notification_view_non_ajax_with_next_parameter ( self ) : <EOL> self . assertEqual ( self . recipient . notifications . unread ( ) . count ( ) , <EOL> self . recipient_nf_count ) <EOL> first_nf = self . recipient . notifications . first ( ) <EOL> first_nf_id = first_nf . id <EOL> self . assertFalse ( first_nf . read ) <EOL> ctx = { '<STR_LIT:action>' : '<STR_LIT>' , '<STR_LIT:id>' : first_nf_id , '<STR_LIT>' : '<STR_LIT>' } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , ctx ) <EOL> self . assertRedirects ( response , '<STR_LIT>' , <EOL> target_status_code = <NUM_LIT> , status_code = <NUM_LIT> ) <EOL> nf = Notification . objects . get ( pk = first_nf_id ) <EOL> self . assertTrue ( nf . read ) <EOL> self . assertEqual ( self . recipient . notifications . unread ( ) . count ( ) , <EOL> self . recipient_nf_count - <NUM_LIT:1> ) <EOL> ctx2 = { '<STR_LIT:action>' : '<STR_LIT>' , '<STR_LIT:id>' : first_nf_id , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> response_2 = self . client . post ( reverse ( '<STR_LIT>' ) , ctx2 ) <EOL> self . assertRedirects ( response_2 , '<STR_LIT>' , <EOL> target_status_code = <NUM_LIT> , status_code = <NUM_LIT> ) <EOL> nf_2 = Notification . objects . get ( pk = first_nf_id ) <EOL> self . assertFalse ( nf_2 . read ) <EOL> self . assertEqual ( self . recipient . notifications . unread ( ) . count ( ) , <EOL> self . recipient_nf_count ) <EOL> def test_mark_notification_view_ajax ( self ) : <EOL> self . assertEqual ( self . recipient . notifications . unread ( ) . count ( ) , <EOL> self . recipient_nf_count ) <EOL> first_nf = self . recipient . notifications . first ( ) <EOL> first_nf_id = first_nf . id <EOL> self . assertFalse ( first_nf . read ) <EOL> ctx = { '<STR_LIT:action>' : '<STR_LIT>' , '<STR_LIT:id>' : first_nf_id } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , ctx , <EOL> HTTP_X_REQUESTED_WITH = '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> nf = Notification . objects . get ( pk = first_nf_id ) <EOL> self . assertTrue ( nf . read ) <EOL> self . assertEqual ( self . recipient . notifications . unread ( ) . count ( ) , <EOL> self . recipient_nf_count - <NUM_LIT:1> ) <EOL> ctx = { '<STR_LIT:action>' : '<STR_LIT>' , '<STR_LIT:id>' : first_nf_id } <EOL> response_2 = self . client . post ( reverse ( '<STR_LIT>' ) , ctx , <EOL> HTTP_X_REQUESTED_WITH = '<STR_LIT>' ) <EOL> self . assertEqual ( response_2 . status_code , <NUM_LIT:200> ) <EOL> nf_2 = Notification . objects . get ( pk = first_nf_id ) <EOL> self . assertFalse ( nf_2 . read ) <EOL> self . assertEqual ( self . recipient . notifications . unread ( ) . count ( ) , <EOL> self . recipient_nf_count ) <EOL> def test_mark_notification_view_for_arbitrary_user ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( self . recipient . notifications . unread ( ) . count ( ) , <EOL> self . recipient_nf_count ) <EOL> self . assertNotEqual ( self . recipient . id , <NUM_LIT:2> ) <EOL> nf_of_other_user = Notification . objects . get ( recipient_id = <NUM_LIT:2> ) <EOL> self . assertFalse ( nf_of_other_user . read ) <EOL> ctx = { '<STR_LIT:action>' : '<STR_LIT>' , '<STR_LIT:id>' : nf_of_other_user . id } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , ctx ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> nf_of_other_user = Notification . objects . get ( recipient_id = <NUM_LIT:2> ) <EOL> self . assertFalse ( nf_of_other_user . read ) <EOL> self . assertEqual ( self . recipient . notifications . unread ( ) . count ( ) , <EOL> self . recipient_nf_count ) <EOL> def test_mark_all_notification_view_non_ajax ( self ) : <EOL> self . assertEqual ( self . recipient . notifications . unread ( ) . count ( ) , <EOL> self . recipient_nf_count ) <EOL> ctx = { '<STR_LIT:action>' : '<STR_LIT>' } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , ctx ) <EOL> self . assertRedirects ( response , reverse ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( self . recipient . notifications . unread ( ) . count ( ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( self . recipient . notifications . read ( ) . count ( ) , <EOL> self . recipient . notifications . count ( ) ) <EOL> ctx2 = { '<STR_LIT:action>' : '<STR_LIT>' } <EOL> response2 = self . client . post ( reverse ( '<STR_LIT>' ) , ctx2 ) <EOL> self . assertRedirects ( response2 , reverse ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( self . recipient . notifications . read ( ) . count ( ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( self . recipient . notifications . unread ( ) . count ( ) , <EOL> self . recipient . notifications . count ( ) ) <EOL> def test_mark_all_notification_view_ajax ( self ) : <EOL> self . assertEqual ( self . recipient . notifications . unread ( ) . count ( ) , <EOL> self . recipient_nf_count ) <EOL> ctx = { '<STR_LIT:action>' : '<STR_LIT>' } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , ctx , <EOL> HTTP_X_REQUESTED_WITH = '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( self . recipient . notifications . unread ( ) . count ( ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( self . recipient . notifications . read ( ) . count ( ) , <EOL> self . recipient . notifications . count ( ) ) <EOL> ctx2 = { '<STR_LIT:action>' : '<STR_LIT>' } <EOL> response2 = self . client . post ( reverse ( '<STR_LIT>' ) , ctx2 , <EOL> HTTP_X_REQUESTED_WITH = '<STR_LIT>' ) <EOL> self . assertEqual ( response2 . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( self . recipient . notifications . read ( ) . count ( ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( self . recipient . notifications . unread ( ) . count ( ) , <EOL> self . recipient . notifications . count ( ) ) <EOL> def test_soft_delete_notification_view_non_ajax ( self ) : <EOL> self . assertEqual ( self . recipient . notifications . active ( ) . count ( ) , <EOL> self . recipient_nf_count ) <EOL> first_nf = self . recipient . notifications . first ( ) <EOL> first_nf_id = first_nf . id <EOL> self . assertFalse ( first_nf . deleted ) <EOL> ctx = { '<STR_LIT:id>' : first_nf_id } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , ctx ) <EOL> self . assertRedirects ( response , reverse ( '<STR_LIT>' ) ) <EOL> nf = Notification . objects . get ( pk = first_nf_id ) <EOL> self . assertTrue ( nf . deleted ) <EOL> self . assertEqual ( self . recipient . notifications . active ( ) . count ( ) , <EOL> self . recipient_nf_count - <NUM_LIT:1> ) <EOL> def test_soft_delete_notification_view_ajax ( self ) : <EOL> self . assertEqual ( self . recipient . notifications . active ( ) . count ( ) , <EOL> self . recipient_nf_count ) <EOL> first_nf = self . recipient . notifications . first ( ) <EOL> first_nf_id = first_nf . id <EOL> self . assertFalse ( first_nf . deleted ) <EOL> ctx = { '<STR_LIT:id>' : first_nf_id } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , ctx , <EOL> HTTP_X_REQUESTED_WITH = '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> nf = Notification . objects . get ( pk = first_nf_id ) <EOL> self . assertTrue ( nf . deleted ) <EOL> self . assertEqual ( self . recipient . notifications . active ( ) . count ( ) , <EOL> self . recipient_nf_count - <NUM_LIT:1> ) <EOL> @ override_settings ( NOTIFY_SOFT_DELETE = False ) <EOL> def test_hard_delete_notification_view ( self ) : <EOL> self . assertEqual ( self . recipient . notifications . all ( ) . count ( ) , <EOL> self . recipient_nf_count ) <EOL> first_nf = self . recipient . notifications . first ( ) <EOL> first_nf_id = first_nf . id <EOL> ctx = { '<STR_LIT:id>' : first_nf_id } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , ctx ) <EOL> self . assertRedirects ( response , reverse ( '<STR_LIT>' ) ) <EOL> self . assertRaises ( Notification . DoesNotExist , <EOL> Notification . objects . get , <EOL> pk = first_nf_id ) <EOL> self . assertEqual ( self . recipient . notifications . all ( ) . count ( ) , <EOL> self . recipient_nf_count - <NUM_LIT:1> ) <EOL> def test_delete_notification_view_for_arbitrary_user ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertNotEqual ( self . recipient . id , <NUM_LIT:2> ) <EOL> nf_of_other_user = Notification . objects . get ( recipient_id = <NUM_LIT:2> ) <EOL> self . assertFalse ( nf_of_other_user . deleted ) <EOL> ctx = { '<STR_LIT:id>' : nf_of_other_user . id } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , ctx ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> nf_of_other_user = Notification . objects . get ( recipient_id = <NUM_LIT:2> ) <EOL> self . assertFalse ( nf_of_other_user . deleted ) <EOL> self . assertEqual ( self . recipient . notifications . active ( ) . count ( ) , <EOL> self . recipient_nf_count ) <EOL> def test_update_notifications_view ( self ) : <EOL> self . assertEqual ( self . recipient . notifications . active ( ) . count ( ) , <EOL> self . recipient_nf_count ) <EOL> nf = Notification . objects . get ( pk = <NUM_LIT:12> ) <EOL> self . assertEqual ( nf . recipient . username , '<STR_LIT>' ) <EOL> url = "<STR_LIT>" . format ( reverse ( '<STR_LIT>' ) ) <EOL> response = self . client . get ( url ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> resp = json . loads ( response . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertTrue ( resp [ '<STR_LIT:success>' ] ) <EOL> self . assertEqual ( resp [ '<STR_LIT>' ] , <EOL> self . recipient . notifications . unread ( ) . count ( ) ) <EOL> notifications = self . recipient . notifications . active ( ) . filter ( <EOL> id__gt = <NUM_LIT:12> ) <EOL> self . assertEqual ( resp [ '<STR_LIT>' ] , <EOL> notifications . count ( ) ) <EOL> for json_nf in resp [ '<STR_LIT>' ] : <EOL> self . assertIn ( json_nf [ '<STR_LIT:id>' ] , <EOL> [ nf . id for nf in notifications ] ) <EOL> def test_update_view_with_wrong_flag_value ( self ) : <EOL> nf = Notification . objects . get ( pk = <NUM_LIT:3> ) <EOL> self . assertNotEqual ( nf . recipient . username , '<STR_LIT>' ) <EOL> url = "<STR_LIT>" . format ( reverse ( '<STR_LIT>' ) ) <EOL> response = self . client . get ( url ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> resp = json . loads ( response . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertFalse ( resp [ '<STR_LIT:success>' ] ) <EOL> url2 = "<STR_LIT>" . format ( reverse ( '<STR_LIT>' ) ) <EOL> response2 = self . client . get ( url2 ) <EOL> self . assertEqual ( response2 . status_code , <NUM_LIT:200> ) <EOL> resp2 = json . loads ( response2 . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertTrue ( resp2 [ '<STR_LIT:success>' ] ) <EOL> def test_read_and_redirect ( self ) : <EOL> """<STR_LIT>""" <EOL> nf = Notification . objects . get ( pk = <NUM_LIT:1> ) <EOL> self . assertFalse ( nf . read ) <EOL> self . assertEqual ( nf . recipient . username , '<STR_LIT>' ) <EOL> target_page = reverse ( '<STR_LIT>' , args = [ nf . id ] ) <EOL> url = "<STR_LIT>" . format ( target_page ) <EOL> response = self . client . get ( url ) <EOL> self . assertRedirects ( response , "<STR_LIT>" , <EOL> target_status_code = <NUM_LIT> , status_code = <NUM_LIT> ) <EOL> nf_new = Notification . objects . get ( pk = <NUM_LIT:1> ) <EOL> self . assertTrue ( nf_new . read ) <EOL> def test_read_and_redirect_unsafe_url ( self ) : <EOL> """<STR_LIT>""" <EOL> nf = Notification . objects . get ( pk = <NUM_LIT:1> ) <EOL> self . assertFalse ( nf . read ) <EOL> self . assertEqual ( nf . recipient . username , '<STR_LIT>' ) <EOL> target_page = reverse ( '<STR_LIT>' , args = [ nf . id ] ) <EOL> url = "<STR_LIT>" . format ( target_page ) <EOL> response = self . client . get ( url ) <EOL> self . assertRedirects ( response , reverse ( '<STR_LIT>' ) , <EOL> target_status_code = <NUM_LIT:200> , status_code = <NUM_LIT> ) <EOL> nf_new = Notification . objects . get ( pk = <NUM_LIT:1> ) <EOL> self . assertTrue ( nf_new . read ) <EOL> def test_read_and_redirect_arbitrary_notification_id ( self ) : <EOL> """<STR_LIT>""" <EOL> nf = Notification . objects . get ( pk = <NUM_LIT:3> ) <EOL> self . assertFalse ( nf . read ) <EOL> self . assertNotEqual ( nf . recipient . username , '<STR_LIT>' ) <EOL> target_page = reverse ( '<STR_LIT>' , args = [ nf . id ] ) <EOL> url = "<STR_LIT>" . format ( target_page ) <EOL> response = self . client . get ( url ) <EOL> self . assertRedirects ( response , "<STR_LIT>" , <EOL> target_status_code = <NUM_LIT> , status_code = <NUM_LIT> ) <EOL> nf_new = Notification . objects . get ( pk = <NUM_LIT:3> ) <EOL> self . assertFalse ( nf_new . read ) <EOL> class NotificationTemplateTagTest ( TestCase ) : <EOL> RENDER_TEMPLATE = Template ( """<STR_LIT>""" ) <EOL> RENDER_TEMPLATE_FOR_BOX = Template ( """<STR_LIT>""" ) <EOL> JS_INCLUSION = Template ( """<STR_LIT>""" ) <EOL> USER_NOTIFICATIONS = Template ( """<STR_LIT>""" ) <EOL> def setUp ( self ) : <EOL> user = User ( username = '<STR_LIT:user>' , email = '<STR_LIT>' ) <EOL> user . set_password ( '<STR_LIT>' ) <EOL> user . save ( ) <EOL> self . user = User . objects . get ( username = '<STR_LIT:user>' ) <EOL> actor = User . objects . create_user ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> for x in range ( <NUM_LIT:10> ) : <EOL> notify . send ( User , recipient = self . user , actor = actor , <EOL> verb = '<STR_LIT>' , nf_type = '<STR_LIT>' ) <EOL> factory = RequestFactory ( ) <EOL> self . request = factory . get ( '<STR_LIT>' ) <EOL> def test_render_template_tag ( self ) : <EOL> notify . send ( User , recipient = self . user , <EOL> actor_text = '<STR_LIT>' , verb = '<STR_LIT>' ) <EOL> nf_list = Notification . objects . filter ( recipient = self . user ) . active ( ) <EOL> rendered = self . RENDER_TEMPLATE . render ( <EOL> Context ( { '<STR_LIT>' : nf_list } ) ) <EOL> self . assertIn ( '<STR_LIT>' , rendered ) <EOL> def test_render_template_tag_for_box ( self ) : <EOL> notify . send ( User , recipient = self . user , <EOL> actor_text = '<STR_LIT>' , verb = '<STR_LIT>' ) <EOL> nf_list = Notification . objects . filter ( recipient = self . user ) . active ( ) <EOL> rendered = self . RENDER_TEMPLATE_FOR_BOX . render ( <EOL> Context ( { '<STR_LIT>' : nf_list } ) ) <EOL> self . assertIn ( '<STR_LIT>' , rendered ) <EOL> self . assertIn ( '<STR_LIT>' , rendered ) <EOL> def test_render_template_tag_with_empty_notifications ( self ) : <EOL> nf_list = Notification . objects . filter ( recipient = self . user ) . none ( ) <EOL> rendered = self . RENDER_TEMPLATE . render ( <EOL> Context ( { '<STR_LIT>' : nf_list } ) ) <EOL> self . assertIn ( '<STR_LIT>' , rendered ) <EOL> def test_render_template_tag_without_context ( self ) : <EOL> rendered = self . RENDER_TEMPLATE . render ( Context ( { } ) ) <EOL> self . assertIn ( '<STR_LIT>' , rendered ) <EOL> def test_js_inclusion_tag ( self ) : <EOL> self . request . user = self . user <EOL> rendered = self . JS_INCLUSION . render ( RequestContext ( self . request ) ) <EOL> self . assertIn ( '<STR_LIT>' , rendered ) <EOL> def test_js_inclusion_tag_anonymous_user ( self ) : <EOL> self . request . user = AnonymousUser ( ) <EOL> rendered = self . JS_INCLUSION . render ( RequestContext ( self . request ) ) <EOL> self . assertNotIn ( '<STR_LIT>' , rendered ) <EOL> def test_user_notifications ( self ) : <EOL> self . request . user = self . user <EOL> rendered = self . USER_NOTIFICATIONS . render ( RequestContext ( self . request ) ) <EOL> self . assertIn ( '<STR_LIT>' , rendered ) <EOL> def test_user_notifications_for_anonymous_user ( self ) : <EOL> self . request . user = AnonymousUser ( ) <EOL> rendered = self . USER_NOTIFICATIONS . render ( RequestContext ( self . request ) ) <EOL> self . assertNotIn ( '<STR_LIT>' , rendered ) </s>
