<s> """<STR_LIT>""" <EOL> from django . test import TestCase , RequestFactory <EOL> try : <EOL> from django . test import override_settings <EOL> except ImportError : <EOL> from django . test . utils import override_settings <EOL> from django . conf import settings <EOL> from django . contrib . auth . models import User , Group <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . core . urlresolvers import reverse <EOL> from django . utils . timezone import utc , localtime <EOL> from django . utils import timezone <EOL> import pytz <EOL> import json <EOL> from notifications import notify <EOL> from notifications . models import Notification <EOL> from notifications . utils import id2slug <EOL> class NotificationTest ( TestCase ) : <EOL> @ override_settings ( USE_TZ = True ) <EOL> @ override_settings ( TIME_ZONE = '<STR_LIT>' ) <EOL> def test_use_timezone ( self ) : <EOL> from_user = User . objects . create ( username = "<STR_LIT>" , password = "<STR_LIT>" , email = "<STR_LIT>" ) <EOL> to_user = User . objects . create ( username = "<STR_LIT:to>" , password = "<STR_LIT>" , email = "<STR_LIT>" ) <EOL> notify . send ( from_user , recipient = to_user , verb = '<STR_LIT>' , action_object = from_user ) <EOL> notification = Notification . objects . get ( recipient = to_user ) <EOL> delta = timezone . now ( ) . replace ( tzinfo = utc ) - localtime ( notification . timestamp , pytz . timezone ( settings . TIME_ZONE ) ) <EOL> self . assertTrue ( delta . seconds < <NUM_LIT> ) <EOL> @ override_settings ( USE_TZ = False ) <EOL> @ override_settings ( TIME_ZONE = '<STR_LIT>' ) <EOL> def test_disable_timezone ( self ) : <EOL> from_user = User . objects . create ( username = "<STR_LIT>" , password = "<STR_LIT>" , email = "<STR_LIT>" ) <EOL> to_user = User . objects . create ( username = "<STR_LIT>" , password = "<STR_LIT>" , email = "<STR_LIT>" ) <EOL> notify . send ( from_user , recipient = to_user , verb = '<STR_LIT>' , action_object = from_user ) <EOL> notification = Notification . objects . get ( recipient = to_user ) <EOL> delta = timezone . now ( ) - notification . timestamp <EOL> self . assertTrue ( delta . seconds < <NUM_LIT> ) <EOL> class NotificationManagersTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . message_count = <NUM_LIT:10> <EOL> self . from_user = User . objects . create ( username = "<STR_LIT>" , password = "<STR_LIT>" , email = "<STR_LIT>" ) <EOL> self . to_user = User . objects . create ( username = "<STR_LIT>" , password = "<STR_LIT>" , email = "<STR_LIT>" ) <EOL> self . to_group = Group . objects . create ( name = "<STR_LIT>" ) <EOL> self . to_group . user_set . add ( self . to_user ) <EOL> for i in range ( self . message_count ) : <EOL> notify . send ( self . from_user , recipient = self . to_user , verb = '<STR_LIT>' , action_object = self . from_user ) <EOL> notify . send ( self . from_user , recipient = self . to_group , verb = '<STR_LIT>' , action_object = self . from_user ) <EOL> self . message_count += <NUM_LIT:1> <EOL> def test_unread_manager ( self ) : <EOL> self . assertEqual ( Notification . objects . unread ( ) . count ( ) , self . message_count ) <EOL> n = Notification . objects . filter ( recipient = self . to_user ) . first ( ) <EOL> n . mark_as_read ( ) <EOL> self . assertEqual ( Notification . objects . unread ( ) . count ( ) , self . message_count - <NUM_LIT:1> ) <EOL> for n in Notification . objects . unread ( ) : <EOL> self . assertTrue ( n . unread ) <EOL> def test_read_manager ( self ) : <EOL> self . assertEqual ( Notification . objects . unread ( ) . count ( ) , self . message_count ) <EOL> n = Notification . objects . filter ( recipient = self . to_user ) . first ( ) <EOL> n . mark_as_read ( ) <EOL> self . assertEqual ( Notification . objects . read ( ) . count ( ) , <NUM_LIT:1> ) <EOL> for n in Notification . objects . read ( ) : <EOL> self . assertFalse ( n . unread ) <EOL> def test_mark_all_as_read_manager ( self ) : <EOL> self . assertEqual ( Notification . objects . unread ( ) . count ( ) , self . message_count ) <EOL> Notification . objects . filter ( recipient = self . to_user ) . mark_all_as_read ( ) <EOL> self . assertEqual ( Notification . objects . unread ( ) . count ( ) , <NUM_LIT:0> ) <EOL> @ override_settings ( NOTIFICATIONS_SOFT_DELETE = True ) <EOL> def test_mark_all_as_read_manager_with_soft_delete ( self ) : <EOL> to_delete = Notification . objects . filter ( recipient = self . to_user ) . order_by ( '<STR_LIT:id>' ) [ <NUM_LIT:0> ] <EOL> to_delete . deleted = True <EOL> to_delete . save ( ) <EOL> self . assertTrue ( Notification . objects . filter ( recipient = self . to_user ) . order_by ( '<STR_LIT:id>' ) [ <NUM_LIT:0> ] . unread ) <EOL> Notification . objects . filter ( recipient = self . to_user ) . mark_all_as_read ( ) <EOL> self . assertFalse ( Notification . objects . filter ( recipient = self . to_user ) . order_by ( '<STR_LIT:id>' ) [ <NUM_LIT:0> ] . unread ) <EOL> def test_mark_all_as_unread_manager ( self ) : <EOL> self . assertEqual ( Notification . objects . unread ( ) . count ( ) , self . message_count ) <EOL> Notification . objects . filter ( recipient = self . to_user ) . mark_all_as_read ( ) <EOL> self . assertEqual ( Notification . objects . unread ( ) . count ( ) , <NUM_LIT:0> ) <EOL> Notification . objects . filter ( recipient = self . to_user ) . mark_all_as_unread ( ) <EOL> self . assertEqual ( Notification . objects . unread ( ) . count ( ) , self . message_count ) <EOL> def test_mark_all_deleted_manager_without_soft_delete ( self ) : <EOL> self . assertRaises ( ImproperlyConfigured , Notification . objects . active ) <EOL> self . assertRaises ( ImproperlyConfigured , Notification . objects . active ) <EOL> self . assertRaises ( ImproperlyConfigured , Notification . objects . mark_all_as_deleted ) <EOL> self . assertRaises ( ImproperlyConfigured , Notification . objects . mark_all_as_active ) <EOL> @ override_settings ( NOTIFICATIONS_SOFT_DELETE = True ) <EOL> def test_mark_all_deleted_manager ( self ) : <EOL> n = Notification . objects . filter ( recipient = self . to_user ) . first ( ) <EOL> n . mark_as_read ( ) <EOL> self . assertEqual ( Notification . objects . read ( ) . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( Notification . objects . unread ( ) . count ( ) , self . message_count - <NUM_LIT:1> ) <EOL> self . assertEqual ( Notification . objects . active ( ) . count ( ) , self . message_count ) <EOL> self . assertEqual ( Notification . objects . deleted ( ) . count ( ) , <NUM_LIT:0> ) <EOL> Notification . objects . mark_all_as_deleted ( ) <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 . message_count ) <EOL> Notification . objects . mark_all_as_active ( ) <EOL> self . assertEqual ( Notification . objects . read ( ) . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( Notification . objects . unread ( ) . count ( ) , self . message_count - <NUM_LIT:1> ) <EOL> self . assertEqual ( Notification . objects . active ( ) . count ( ) , self . message_count ) <EOL> self . assertEqual ( Notification . objects . deleted ( ) . count ( ) , <NUM_LIT:0> ) <EOL> class NotificationTestPages ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . message_count = <NUM_LIT:10> <EOL> self . from_user = User . objects . create_user ( username = "<STR_LIT>" , password = "<STR_LIT>" , email = "<STR_LIT>" ) <EOL> self . to_user = User . objects . create_user ( username = "<STR_LIT:to>" , password = "<STR_LIT>" , email = "<STR_LIT>" ) <EOL> self . to_user . is_staff = True <EOL> self . to_user . save ( ) <EOL> for i in range ( self . message_count ) : <EOL> notify . send ( self . from_user , recipient = self . to_user , verb = '<STR_LIT>' , action_object = self . from_user ) <EOL> def logout ( self ) : <EOL> self . client . post ( reverse ( '<STR_LIT>' ) + '<STR_LIT>' , { } ) <EOL> def login ( self , username , password ) : <EOL> self . logout ( ) <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , { '<STR_LIT:username>' : username , '<STR_LIT:password>' : password } ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> return response <EOL> def test_all_messages_page ( self ) : <EOL> self . login ( '<STR_LIT:to>' , '<STR_LIT>' ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( len ( response . context [ '<STR_LIT>' ] ) , len ( self . to_user . notifications . all ( ) ) ) <EOL> def test_unread_messages_pages ( self ) : <EOL> self . login ( '<STR_LIT:to>' , '<STR_LIT>' ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( len ( response . context [ '<STR_LIT>' ] ) , len ( self . to_user . notifications . unread ( ) ) ) <EOL> self . assertEqual ( len ( response . context [ '<STR_LIT>' ] ) , self . message_count ) <EOL> for i , n in enumerate ( self . to_user . notifications . all ( ) ) : <EOL> if i % <NUM_LIT:3> == <NUM_LIT:0> : <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , args = [ id2slug ( n . id ) ] ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( len ( response . context [ '<STR_LIT>' ] ) , len ( self . to_user . notifications . unread ( ) ) ) <EOL> self . assertTrue ( len ( response . context [ '<STR_LIT>' ] ) < self . message_count ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertRedirects ( response , reverse ( '<STR_LIT>' ) ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( len ( response . context [ '<STR_LIT>' ] ) , len ( self . to_user . notifications . unread ( ) ) ) <EOL> self . assertEqual ( len ( response . context [ '<STR_LIT>' ] ) , <NUM_LIT:0> ) <EOL> def test_next_pages ( self ) : <EOL> self . login ( '<STR_LIT:to>' , '<STR_LIT>' ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) , data = { <EOL> "<STR_LIT>" : reverse ( '<STR_LIT>' ) , <EOL> } ) <EOL> self . assertRedirects ( response , reverse ( '<STR_LIT>' ) ) <EOL> slug = id2slug ( self . to_user . notifications . first ( ) . id ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , args = [ slug ] ) , data = { <EOL> "<STR_LIT>" : reverse ( '<STR_LIT>' ) , <EOL> } ) <EOL> self . assertRedirects ( response , reverse ( '<STR_LIT>' ) ) <EOL> slug = id2slug ( self . to_user . notifications . first ( ) . id ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , args = [ slug ] ) , { <EOL> "<STR_LIT>" : reverse ( '<STR_LIT>' ) , <EOL> } ) <EOL> self . assertRedirects ( response , reverse ( '<STR_LIT>' ) ) <EOL> def test_delete_messages_pages ( self ) : <EOL> self . login ( '<STR_LIT:to>' , '<STR_LIT>' ) <EOL> slug = id2slug ( self . to_user . notifications . first ( ) . id ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , args = [ slug ] ) ) <EOL> self . assertRedirects ( response , reverse ( '<STR_LIT>' ) ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( len ( response . context [ '<STR_LIT>' ] ) , len ( self . to_user . notifications . all ( ) ) ) <EOL> self . assertEqual ( len ( response . context [ '<STR_LIT>' ] ) , self . message_count - <NUM_LIT:1> ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( len ( response . context [ '<STR_LIT>' ] ) , len ( self . to_user . notifications . unread ( ) ) ) <EOL> self . assertEqual ( len ( response . context [ '<STR_LIT>' ] ) , self . message_count - <NUM_LIT:1> ) <EOL> @ override_settings ( NOTIFICATIONS_SOFT_DELETE = True ) <EOL> def test_soft_delete_messages_manager ( self ) : <EOL> self . login ( '<STR_LIT:to>' , '<STR_LIT>' ) <EOL> slug = id2slug ( self . to_user . notifications . first ( ) . id ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , args = [ slug ] ) ) <EOL> self . assertRedirects ( response , reverse ( '<STR_LIT>' ) ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( len ( response . context [ '<STR_LIT>' ] ) , len ( self . to_user . notifications . active ( ) ) ) <EOL> self . assertEqual ( len ( response . context [ '<STR_LIT>' ] ) , self . message_count - <NUM_LIT:1> ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( len ( response . context [ '<STR_LIT>' ] ) , len ( self . to_user . notifications . unread ( ) ) ) <EOL> self . assertEqual ( len ( response . context [ '<STR_LIT>' ] ) , self . message_count - <NUM_LIT:1> ) <EOL> def test_unread_count_api ( self ) : <EOL> self . login ( '<STR_LIT:to>' , '<STR_LIT>' ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> data = json . loads ( response . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( list ( data . keys ( ) ) , [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] , <NUM_LIT:10> ) <EOL> Notification . objects . filter ( recipient = self . to_user ) . mark_all_as_read ( ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> data = json . loads ( response . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( list ( data . keys ( ) ) , [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] , <NUM_LIT:0> ) <EOL> notify . send ( self . from_user , recipient = self . to_user , verb = '<STR_LIT>' , action_object = self . from_user ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> data = json . loads ( response . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( list ( data . keys ( ) ) , [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> def test_unread_list_api ( self ) : <EOL> self . login ( '<STR_LIT:to>' , '<STR_LIT>' ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> data = json . loads ( response . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( sorted ( list ( data . keys ( ) ) ) , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] , <NUM_LIT:10> ) <EOL> self . assertEqual ( len ( data [ '<STR_LIT>' ] ) , <NUM_LIT:5> ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) , data = { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> data = json . loads ( response . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( sorted ( list ( data . keys ( ) ) ) , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] , <NUM_LIT:10> ) <EOL> self . assertEqual ( len ( data [ '<STR_LIT>' ] ) , <NUM_LIT:10> ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) , data = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } ) <EOL> data = json . loads ( response . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( sorted ( list ( data . keys ( ) ) ) , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] , <NUM_LIT:10> ) <EOL> self . assertEqual ( len ( data [ '<STR_LIT>' ] ) , <NUM_LIT:5> ) <EOL> Notification . objects . filter ( recipient = self . to_user ) . mark_all_as_read ( ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> data = json . loads ( response . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( sorted ( list ( data . keys ( ) ) ) , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] , <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( data [ '<STR_LIT>' ] ) , <NUM_LIT:0> ) <EOL> notify . send ( self . from_user , recipient = self . to_user , verb = '<STR_LIT>' , action_object = self . from_user ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> data = json . loads ( response . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( sorted ( list ( data . keys ( ) ) ) , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( data [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_live_update_tags ( self ) : <EOL> from django . shortcuts import render <EOL> self . login ( '<STR_LIT:to>' , '<STR_LIT>' ) <EOL> self . factory = RequestFactory ( ) <EOL> request = self . factory . get ( '<STR_LIT>' ) <EOL> request . user = self . to_user <EOL> render ( request , '<STR_LIT>' , { '<STR_LIT>' : request } ) <EOL> def test_anon_user_gets_nothing ( self ) : <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> data = json . loads ( response . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] , <NUM_LIT:0> ) <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> data = json . loads ( response . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] , <NUM_LIT:0> ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] , [ ] ) </s>
