<s> from datetime import datetime , timedelta <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from nose . tools import eq_ <EOL> from kitsune . access . tests import PermissionFactory <EOL> from kitsune . flagit . models import FlaggedObject <EOL> from kitsune . forums import POSTS_PER_PAGE <EOL> from kitsune . forums . events import NewPostEvent , NewThreadEvent <EOL> from kitsune . forums . models import Forum , Thread , Post <EOL> from kitsune . forums . tests import ForumTestCase , ForumFactory , ThreadFactory , PostFactory <EOL> from kitsune . sumo . templatetags . jinja_helpers import urlparams <EOL> from kitsune . sumo . urlresolvers import reverse <EOL> from kitsune . users . tests import UserFactory <EOL> YESTERDAY = datetime . now ( ) - timedelta ( days = <NUM_LIT:1> ) <EOL> class ForumModelTestCase ( ForumTestCase ) : <EOL> def test_forum_absolute_url ( self ) : <EOL> f = ForumFactory ( ) <EOL> eq_ ( '<STR_LIT>' % f . slug , <EOL> f . get_absolute_url ( ) ) <EOL> def test_thread_absolute_url ( self ) : <EOL> t = ThreadFactory ( ) <EOL> eq_ ( '<STR_LIT>' % ( t . forum . slug , t . id ) , <EOL> t . get_absolute_url ( ) ) <EOL> def test_post_absolute_url ( self ) : <EOL> t = ThreadFactory ( posts = [ ] ) <EOL> p1 = PostFactory ( thread = t , created = YESTERDAY ) <EOL> PostFactory . create_batch ( POSTS_PER_PAGE - <NUM_LIT:1> , created = YESTERDAY , thread = t ) <EOL> p2 = PostFactory ( thread = t ) <EOL> url = reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : p1 . thread . forum . slug , <EOL> '<STR_LIT>' : p1 . thread . id } ) <EOL> eq_ ( urlparams ( url , hash = '<STR_LIT>' % p1 . id ) , p1 . get_absolute_url ( ) ) <EOL> url = reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : p2 . thread . forum . slug , <EOL> '<STR_LIT>' : p2 . thread . id } ) <EOL> exp_ = urlparams ( url , hash = '<STR_LIT>' % p2 . id , page = <NUM_LIT:2> ) <EOL> eq_ ( exp_ , p2 . get_absolute_url ( ) ) <EOL> def test_post_page ( self ) : <EOL> t = ThreadFactory ( ) <EOL> page1 = PostFactory . create_batch ( POSTS_PER_PAGE , thread = t , created = YESTERDAY ) <EOL> p2 = PostFactory ( thread = t ) <EOL> for p in page1 : <EOL> eq_ ( <NUM_LIT:1> , p . page ) <EOL> eq_ ( <NUM_LIT:2> , p2 . page ) <EOL> def test_delete_post_removes_flag ( self ) : <EOL> """<STR_LIT>""" <EOL> p = PostFactory ( ) <EOL> u = UserFactory ( ) <EOL> FlaggedObject . objects . create ( <EOL> status = <NUM_LIT:0> , content_object = p , reason = '<STR_LIT>' , creator_id = u . id ) <EOL> eq_ ( <NUM_LIT:1> , FlaggedObject . objects . count ( ) ) <EOL> p . delete ( ) <EOL> eq_ ( <NUM_LIT:0> , FlaggedObject . objects . count ( ) ) <EOL> def test_thread_last_post_url ( self ) : <EOL> t = ThreadFactory ( ) <EOL> lp = t . last_post <EOL> f = t . forum <EOL> url = t . get_last_post_url ( ) <EOL> assert f . slug in url <EOL> assert str ( t . id ) in url <EOL> assert '<STR_LIT>' % lp . id in url <EOL> assert '<STR_LIT>' % lp . id in url <EOL> def test_last_post_updated ( self ) : <EOL> orig_post = PostFactory ( created = YESTERDAY ) <EOL> t = orig_post . thread <EOL> new_post = PostFactory ( thread = t , content = '<STR_LIT:test>' ) <EOL> f = Forum . objects . get ( id = t . forum_id ) <EOL> t = Thread . objects . get ( id = t . id ) <EOL> eq_ ( f . last_post . id , new_post . id ) <EOL> eq_ ( t . last_post . id , new_post . id ) <EOL> new_post . delete ( ) <EOL> f = Forum . objects . get ( id = f . id ) <EOL> t = Thread . objects . get ( id = t . id ) <EOL> eq_ ( f . last_post . id , orig_post . id ) <EOL> eq_ ( t . last_post . id , orig_post . id ) <EOL> def test_public_access ( self ) : <EOL> u = UserFactory ( ) <EOL> f = ForumFactory ( ) <EOL> assert f . allows_viewing_by ( u ) <EOL> assert f . allows_posting_by ( u ) <EOL> def test_access_restriction ( self ) : <EOL> """<STR_LIT>""" <EOL> f = ForumFactory ( ) <EOL> ct = ContentType . objects . get_for_model ( f ) <EOL> PermissionFactory ( codename = '<STR_LIT>' , content_type = ct , object_id = f . id ) <EOL> PermissionFactory ( codename = '<STR_LIT>' , content_type = ct , object_id = f . id ) <EOL> unprivileged_user = UserFactory ( ) <EOL> assert not f . allows_viewing_by ( unprivileged_user ) <EOL> assert not f . allows_posting_by ( unprivileged_user ) <EOL> def test_move_updates_last_posts ( self ) : <EOL> old_forum = ForumFactory ( ) <EOL> t1 = ThreadFactory ( forum = old_forum , posts = [ ] ) <EOL> p1 = PostFactory ( thread = t1 , created = YESTERDAY ) <EOL> t2 = ThreadFactory ( forum = old_forum , posts = [ ] ) <EOL> p2 = PostFactory ( thread = t2 ) <EOL> new_forum = ForumFactory ( ) <EOL> t3 = ThreadFactory ( forum = new_forum , posts = [ ] ) <EOL> p3 = PostFactory ( thread = t3 , created = YESTERDAY ) <EOL> eq_ ( p2 , Forum . objects . get ( id = old_forum . id ) . last_post ) <EOL> eq_ ( p3 , Forum . objects . get ( id = new_forum . id ) . last_post ) <EOL> t2 = Thread . objects . get ( id = t2 . id ) <EOL> t2 . forum = new_forum <EOL> t2 . save ( ) <EOL> eq_ ( p1 . id , Forum . objects . get ( id = old_forum . id ) . last_post_id ) <EOL> eq_ ( p2 . id , Forum . objects . get ( id = new_forum . id ) . last_post_id ) <EOL> p2 . delete ( ) <EOL> eq_ ( <NUM_LIT:1> , Forum . objects . filter ( id = old_forum . id ) . count ( ) ) <EOL> eq_ ( <NUM_LIT:1> , Forum . objects . filter ( id = new_forum . id ) . count ( ) ) <EOL> def test_delete_removes_watches ( self ) : <EOL> f = ForumFactory ( ) <EOL> NewThreadEvent . notify ( '<STR_LIT>' , f ) <EOL> assert NewThreadEvent . is_notifying ( '<STR_LIT>' , f ) <EOL> f . delete ( ) <EOL> assert not NewThreadEvent . is_notifying ( '<STR_LIT>' , f ) <EOL> def test_last_post_creator_deleted ( self ) : <EOL> """<STR_LIT>""" <EOL> post = PostFactory ( content = '<STR_LIT:test>' ) <EOL> forum = post . thread . forum <EOL> eq_ ( forum . last_post . id , post . id ) <EOL> post . author . delete ( ) <EOL> forum = Forum . objects . get ( id = forum . id ) <EOL> eq_ ( forum . last_post , None ) <EOL> class ThreadModelTestCase ( ForumTestCase ) : <EOL> def test_delete_thread_with_last_forum_post ( self ) : <EOL> t = ThreadFactory ( ) <EOL> f = t . forum <EOL> last_post = f . last_post <EOL> t = ThreadFactory ( title = '<STR_LIT:test>' , forum = f , posts = [ ] ) <EOL> p = PostFactory ( thread = t , content = '<STR_LIT:test>' , author = t . creator ) <EOL> f = Forum . objects . get ( id = f . id ) <EOL> eq_ ( f . last_post . id , p . id ) <EOL> t . delete ( ) <EOL> f = Forum . objects . get ( id = f . id ) <EOL> eq_ ( f . last_post . id , last_post . id ) <EOL> eq_ ( Thread . objects . filter ( pk = t . id ) . count ( ) , <NUM_LIT:0> ) <EOL> def test_delete_removes_watches ( self ) : <EOL> t = ThreadFactory ( ) <EOL> NewPostEvent . notify ( '<STR_LIT>' , t ) <EOL> assert NewPostEvent . is_notifying ( '<STR_LIT>' , t ) <EOL> t . delete ( ) <EOL> assert not NewPostEvent . is_notifying ( '<STR_LIT>' , t ) <EOL> def test_delete_last_and_only_post_in_thread ( self ) : <EOL> """<STR_LIT>""" <EOL> t = ThreadFactory ( ) <EOL> eq_ ( <NUM_LIT:1> , t . post_set . count ( ) ) <EOL> t . delete ( ) <EOL> eq_ ( <NUM_LIT:0> , Thread . objects . filter ( pk = t . id ) . count ( ) ) <EOL> class SaveDateTestCase ( ForumTestCase ) : <EOL> """<STR_LIT>""" <EOL> delta = timedelta ( milliseconds = <NUM_LIT> ) <EOL> def setUp ( self ) : <EOL> super ( SaveDateTestCase , self ) . setUp ( ) <EOL> self . user = UserFactory ( ) <EOL> self . thread = ThreadFactory ( ) <EOL> self . forum = self . thread . forum <EOL> def assertDateTimeAlmostEqual ( self , a , b , delta , msg = None ) : <EOL> """<STR_LIT>""" <EOL> diff = abs ( a - b ) <EOL> assert diff < abs ( delta ) , msg or '<STR_LIT>' % ( a , b ) <EOL> def test_save_thread_no_created ( self ) : <EOL> """<STR_LIT>""" <EOL> t = ThreadFactory ( forum = self . forum , title = '<STR_LIT:foo>' , creator = self . user ) <EOL> now = datetime . now ( ) <EOL> self . assertDateTimeAlmostEqual ( now , t . created , self . delta ) <EOL> def test_save_thread_created ( self ) : <EOL> created = datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:12> , <NUM_LIT:9> , <NUM_LIT> , <NUM_LIT> ) <EOL> t = Thread ( forum = self . forum , title = '<STR_LIT:foo>' , creator = self . user , created = created ) <EOL> eq_ ( created , t . created ) <EOL> def test_save_old_thread_created ( self ) : <EOL> """<STR_LIT>""" <EOL> t = ThreadFactory ( created = YESTERDAY ) <EOL> t = Thread . objects . get ( id = t . id ) <EOL> created = t . created <EOL> t . title = '<STR_LIT>' <EOL> t . save ( ) <EOL> t = Thread . objects . get ( id = t . id ) <EOL> eq_ ( created , t . created ) <EOL> def test_save_new_post_no_timestamps ( self ) : <EOL> p = PostFactory ( thread = self . thread , content = '<STR_LIT:bar>' , author = self . user ) <EOL> now = datetime . now ( ) <EOL> self . assertDateTimeAlmostEqual ( now , p . created , self . delta ) <EOL> self . assertDateTimeAlmostEqual ( now , p . updated , self . delta ) <EOL> def test_save_old_post_no_timestamps ( self ) : <EOL> """<STR_LIT>""" <EOL> created = datetime ( <NUM_LIT> , <NUM_LIT:5> , <NUM_LIT:4> , <NUM_LIT> , <NUM_LIT:4> , <NUM_LIT> ) <EOL> updated = datetime ( <NUM_LIT> , <NUM_LIT:5> , <NUM_LIT:4> , <NUM_LIT> , <NUM_LIT:4> , <NUM_LIT> ) <EOL> p = PostFactory ( thread = self . thread , created = created , updated = updated ) <EOL> eq_ ( updated , p . updated ) <EOL> p . content = '<STR_LIT>' <EOL> p . updated_by = self . user <EOL> p . save ( ) <EOL> now = datetime . now ( ) <EOL> self . assertDateTimeAlmostEqual ( now , p . updated , self . delta ) <EOL> eq_ ( created , p . created ) <EOL> def test_save_new_post_timestamps ( self ) : <EOL> created_ = datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:12> , <NUM_LIT:10> , <NUM_LIT:12> , <NUM_LIT:32> ) <EOL> p = Post ( thread = self . thread , content = '<STR_LIT:bar>' , author = self . user , <EOL> created = created_ , updated = created_ ) <EOL> p . save ( ) <EOL> eq_ ( created_ , p . created ) <EOL> eq_ ( created_ , p . updated ) <EOL> def test_content_parsed_sanity ( self ) : <EOL> """<STR_LIT>""" <EOL> p = PostFactory ( thread = self . thread , content = '<STR_LIT>' ) <EOL> eq_ ( '<STR_LIT>' , p . content_parsed ) </s>
