<s> from datetime import datetime , timedelta <EOL> from os import path <EOL> from django . core . urlresolvers import reverse <EOL> import jingo <EOL> from mock import Mock <EOL> from pyquery import PyQuery as pq <EOL> from olympia import amo <EOL> from olympia . amo . tests import TestCase <EOL> from olympia . addons . models import Addon , AddonUser <EOL> from olympia . bandwagon . models import Collection <EOL> from olympia . devhub . models import ActivityLog , AddonLog , BlogPost <EOL> from olympia . tags . models import Tag <EOL> from olympia . files . models import File <EOL> from olympia . reviews . models import Review <EOL> from olympia . users . models import UserProfile <EOL> from olympia . versions . models import Version <EOL> TESTS_DIR = path . dirname ( path . abspath ( __file__ ) ) <EOL> ATTACHMENTS_DIR = path . join ( TESTS_DIR , '<STR_LIT>' ) <EOL> class TestActivityLog ( TestCase ) : <EOL> fixtures = [ '<STR_LIT>' ] <EOL> def setUp ( self ) : <EOL> super ( TestActivityLog , self ) . setUp ( ) <EOL> u = UserProfile . objects . create ( username = '<STR_LIT>' ) <EOL> self . request = Mock ( ) <EOL> self . request . user = self . user = u <EOL> amo . set_user ( u ) <EOL> def tearDown ( self ) : <EOL> amo . set_user ( None ) <EOL> super ( TestActivityLog , self ) . tearDown ( ) <EOL> def test_basic ( self ) : <EOL> a = Addon . objects . get ( ) <EOL> amo . log ( amo . LOG [ '<STR_LIT>' ] , a ) <EOL> entries = ActivityLog . objects . for_addons ( a ) <EOL> assert len ( entries ) == <NUM_LIT:1> <EOL> assert entries [ <NUM_LIT:0> ] . arguments [ <NUM_LIT:0> ] == a <EOL> for x in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> assert x in unicode ( entries [ <NUM_LIT:0> ] ) <EOL> def test_no_user ( self ) : <EOL> amo . set_user ( None ) <EOL> count = ActivityLog . objects . count ( ) <EOL> amo . log ( amo . LOG . CUSTOM_TEXT , '<STR_LIT>' ) <EOL> assert count == ActivityLog . objects . count ( ) <EOL> def test_pseudo_objects ( self ) : <EOL> """<STR_LIT>""" <EOL> a = ActivityLog ( ) <EOL> a . arguments = [ ( Addon , <NUM_LIT> ) ] <EOL> assert a . arguments [ <NUM_LIT:0> ] == Addon . objects . get ( pk = <NUM_LIT> ) <EOL> def test_addon_logging_pseudo ( self ) : <EOL> """<STR_LIT>""" <EOL> a = Addon . objects . get ( ) <EOL> amo . log ( amo . LOG . CREATE_ADDON , ( Addon , a . id ) ) <EOL> assert AddonLog . objects . count ( ) == <NUM_LIT:1> <EOL> def test_addon_log ( self ) : <EOL> addon = Addon . objects . get ( ) <EOL> amo . log ( amo . LOG . CREATE_ADDON , ( Addon , addon . id ) ) <EOL> entries = ActivityLog . objects . for_addons ( addon ) <EOL> assert len ( entries ) == <NUM_LIT:1> <EOL> assert addon . get_url_path ( ) in unicode ( entries [ <NUM_LIT:0> ] ) <EOL> def test_addon_log_unlisted_addon ( self ) : <EOL> addon = Addon . objects . get ( ) <EOL> url_path = addon . get_url_path ( ) <EOL> addon . update ( is_listed = False ) <EOL> amo . log ( amo . LOG . CREATE_ADDON , ( Addon , addon . id ) ) <EOL> entries = ActivityLog . objects . for_addons ( addon ) <EOL> assert len ( entries ) == <NUM_LIT:1> <EOL> assert url_path not in unicode ( entries [ <NUM_LIT:0> ] ) <EOL> def test_fancy_rendering ( self ) : <EOL> """<STR_LIT>""" <EOL> a = ActivityLog . objects . create ( action = amo . LOG . ADD_REVIEW . id ) <EOL> u = UserProfile . objects . create ( ) <EOL> r = Review . objects . create ( user = u , addon_id = <NUM_LIT> ) <EOL> a . arguments = [ a , r ] <EOL> assert '<STR_LIT>' in a . to_string ( ) <EOL> a . action = amo . LOG . ADD_TO_COLLECTION . id <EOL> a . arguments = [ a , Collection . objects . create ( ) ] <EOL> assert '<STR_LIT>' in a . to_string ( ) <EOL> def test_bad_arguments ( self ) : <EOL> a = ActivityLog ( ) <EOL> a . arguments = [ ] <EOL> a . action = amo . LOG . ADD_USER_WITH_ROLE . id <EOL> assert a . to_string ( ) == '<STR_LIT>' <EOL> def test_json_failboat ( self ) : <EOL> a = Addon . objects . get ( ) <EOL> amo . log ( amo . LOG [ '<STR_LIT>' ] , a ) <EOL> entry = ActivityLog . objects . get ( ) <EOL> entry . _arguments = '<STR_LIT>' <EOL> entry . save ( ) <EOL> assert entry . arguments is None <EOL> def test_no_arguments ( self ) : <EOL> amo . log ( amo . LOG [ '<STR_LIT>' ] ) <EOL> entry = ActivityLog . objects . get ( ) <EOL> assert entry . arguments == [ ] <EOL> def test_output ( self ) : <EOL> amo . log ( amo . LOG [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> entry = ActivityLog . objects . get ( ) <EOL> assert unicode ( entry ) == '<STR_LIT>' <EOL> def test_user_log ( self ) : <EOL> request = self . request <EOL> amo . log ( amo . LOG [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> entries = ActivityLog . objects . for_user ( request . user ) <EOL> assert len ( entries ) == <NUM_LIT:1> <EOL> def test_user_log_as_argument ( self ) : <EOL> """<STR_LIT>""" <EOL> u = UserProfile ( username = '<STR_LIT>' ) <EOL> u . save ( ) <EOL> amo . log ( amo . LOG [ '<STR_LIT>' ] , <EOL> u , '<STR_LIT>' , Addon . objects . get ( ) ) <EOL> entries = ActivityLog . objects . for_user ( self . request . user ) <EOL> assert len ( entries ) == <NUM_LIT:1> <EOL> entries = ActivityLog . objects . for_user ( u ) <EOL> assert len ( entries ) == <NUM_LIT:1> <EOL> def test_version_log ( self ) : <EOL> version = Version . objects . all ( ) [ <NUM_LIT:0> ] <EOL> amo . log ( amo . LOG . REJECT_VERSION , version . addon , version , <EOL> user = self . request . user ) <EOL> entries = ActivityLog . objects . for_version ( version ) <EOL> assert len ( entries ) == <NUM_LIT:1> <EOL> assert version . get_url_path ( ) in unicode ( entries [ <NUM_LIT:0> ] ) <EOL> def test_version_log_unlisted_addon ( self ) : <EOL> version = Version . objects . all ( ) [ <NUM_LIT:0> ] <EOL> addon = version . addon <EOL> url_path = version . get_url_path ( ) <EOL> addon . update ( is_listed = False ) <EOL> amo . log ( amo . LOG . REJECT_VERSION , version . addon , version , <EOL> user = self . request . user ) <EOL> entries = ActivityLog . objects . for_version ( version ) <EOL> assert len ( entries ) == <NUM_LIT:1> <EOL> assert url_path not in unicode ( entries [ <NUM_LIT:0> ] ) <EOL> def test_version_log_transformer ( self ) : <EOL> addon = Addon . objects . get ( ) <EOL> version = addon . latest_version <EOL> amo . log ( amo . LOG . REJECT_VERSION , addon , version , <EOL> user = self . request . user ) <EOL> version_two = Version ( addon = addon , license = version . license , <EOL> version = '<STR_LIT>' ) <EOL> version_two . save ( ) <EOL> amo . log ( amo . LOG . REJECT_VERSION , addon , version_two , <EOL> user = self . request . user ) <EOL> versions = ( Version . objects . filter ( addon = addon ) . order_by ( '<STR_LIT>' ) <EOL> . transform ( Version . transformer_activity ) ) <EOL> assert len ( versions [ <NUM_LIT:0> ] . all_activity ) == <NUM_LIT:1> <EOL> assert len ( versions [ <NUM_LIT:1> ] . all_activity ) == <NUM_LIT:1> <EOL> def test_xss_arguments_and_escaping ( self ) : <EOL> addon = Addon . objects . get ( ) <EOL> addon . name = '<STR_LIT>' <EOL> addon . save ( ) <EOL> addon = addon . reload ( ) <EOL> au = AddonUser ( addon = addon , user = self . user ) <EOL> amo . log ( amo . LOG . CHANGE_USER_WITH_ROLE , au . user , au . get_role_display ( ) , <EOL> addon ) <EOL> log = ActivityLog . objects . get ( ) <EOL> env = jingo . get_env ( ) <EOL> log_expected = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> assert log . to_string ( ) == log_expected <EOL> assert env . from_string ( '<STR_LIT>' ) . render ( { '<STR_LIT>' : log } ) == ( <EOL> '<STR_LIT>' % log_expected ) <EOL> def test_tag_no_match ( self ) : <EOL> addon = Addon . objects . get ( ) <EOL> tag = Tag . objects . create ( tag_text = '<STR_LIT>' ) <EOL> amo . log ( amo . LOG . ADD_TAG , addon , tag ) <EOL> log = ActivityLog . objects . get ( ) <EOL> env = jingo . get_env ( ) <EOL> text = env . from_string ( '<STR_LIT>' ) . render ( { '<STR_LIT>' : log } ) <EOL> assert len ( pq ( text ) ( '<STR_LIT:a>' ) ) == <NUM_LIT:1> <EOL> def test_beta_signed_events_validation_passed ( self ) : <EOL> addon = Addon . objects . get ( ) <EOL> file_ = addon . versions . first ( ) . files . first ( ) <EOL> amo . log ( amo . LOG . BETA_SIGNED_VALIDATION_PASSED , file_ ) <EOL> log = ActivityLog . objects . beta_signed_events ( ) . first ( ) . to_string ( ) <EOL> link = pq ( log ) ( '<STR_LIT:a>' ) <EOL> assert len ( link ) == <NUM_LIT:1> <EOL> assert link [ <NUM_LIT:0> ] . attrib [ '<STR_LIT>' ] == reverse ( '<STR_LIT>' , args = [ file_ . pk ] ) <EOL> msg = '<STR_LIT>' . format ( <EOL> reverse ( '<STR_LIT>' , args = [ file_ . pk ] ) , file_ . filename ) <EOL> assert log == msg <EOL> def test_beta_signed_events_validation_failed ( self ) : <EOL> addon = Addon . objects . get ( ) <EOL> file_ = addon . versions . first ( ) . files . first ( ) <EOL> amo . log ( amo . LOG . BETA_SIGNED_VALIDATION_FAILED , file_ ) <EOL> log = ActivityLog . objects . beta_signed_events ( ) . first ( ) . to_string ( ) <EOL> link = pq ( log ) ( '<STR_LIT:a>' ) <EOL> assert len ( link ) == <NUM_LIT:1> <EOL> assert link [ <NUM_LIT:0> ] . attrib [ '<STR_LIT>' ] == reverse ( '<STR_LIT>' , args = [ file_ . pk ] ) <EOL> msg = '<STR_LIT>' . format ( <EOL> reverse ( '<STR_LIT>' , args = [ file_ . pk ] ) , file_ . filename ) <EOL> assert log == msg <EOL> class TestVersion ( TestCase ) : <EOL> fixtures = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def setUp ( self ) : <EOL> super ( TestVersion , self ) . setUp ( ) <EOL> self . addon = Addon . objects . get ( pk = <NUM_LIT> ) <EOL> self . version = Version . objects . get ( pk = <NUM_LIT> ) <EOL> self . file = File . objects . get ( pk = <NUM_LIT> ) <EOL> def test_version_delete_status_null ( self ) : <EOL> self . version . delete ( ) <EOL> assert self . addon . versions . count ( ) == <NUM_LIT:0> <EOL> assert Addon . objects . get ( pk = <NUM_LIT> ) . status == amo . STATUS_NULL <EOL> def _extra_version_and_file ( self , status ) : <EOL> version = Version . objects . get ( pk = <NUM_LIT> ) <EOL> version_two = Version ( addon = self . addon , <EOL> license = version . license , <EOL> version = '<STR_LIT>' ) <EOL> version_two . save ( ) <EOL> file_two = File ( status = status , version = version_two ) <EOL> file_two . save ( ) <EOL> return version_two , file_two <EOL> def test_version_delete_status ( self ) : <EOL> self . _extra_version_and_file ( amo . STATUS_PUBLIC ) <EOL> self . addon . status = amo . STATUS_BETA <EOL> self . addon . save ( ) <EOL> self . version . delete ( ) <EOL> assert self . addon . versions . count ( ) == <NUM_LIT:1> <EOL> assert Addon . objects . get ( id = <NUM_LIT> ) . status == amo . STATUS_BETA <EOL> def test_version_delete_status_unreviewed ( self ) : <EOL> self . _extra_version_and_file ( amo . STATUS_BETA ) <EOL> self . version . delete ( ) <EOL> assert self . addon . versions . count ( ) == <NUM_LIT:1> <EOL> assert Addon . objects . get ( id = <NUM_LIT> ) . status == amo . STATUS_UNREVIEWED <EOL> def test_file_delete_status_null ( self ) : <EOL> assert self . addon . versions . count ( ) == <NUM_LIT:1> <EOL> self . file . delete ( ) <EOL> assert self . addon . versions . count ( ) == <NUM_LIT:1> <EOL> assert Addon . objects . get ( pk = <NUM_LIT> ) . status == amo . STATUS_NULL <EOL> def test_file_delete_status_null_multiple ( self ) : <EOL> version_two , file_two = self . _extra_version_and_file ( amo . STATUS_NULL ) <EOL> self . file . delete ( ) <EOL> assert self . addon . status == amo . STATUS_PUBLIC <EOL> file_two . delete ( ) <EOL> assert self . addon . status == amo . STATUS_NULL <EOL> class TestActivityLogCount ( TestCase ) : <EOL> fixtures = [ '<STR_LIT>' ] <EOL> def setUp ( self ) : <EOL> super ( TestActivityLogCount , self ) . setUp ( ) <EOL> now = datetime . now ( ) <EOL> bom = datetime ( now . year , now . month , <NUM_LIT:1> ) <EOL> self . lm = bom - timedelta ( days = <NUM_LIT:1> ) <EOL> self . user = UserProfile . objects . get ( ) <EOL> amo . set_user ( self . user ) <EOL> def add_approve_logs ( self , count ) : <EOL> for x in range ( <NUM_LIT:0> , count ) : <EOL> amo . log ( amo . LOG [ '<STR_LIT>' ] , Addon . objects . get ( ) ) <EOL> def test_not_review_count ( self ) : <EOL> amo . log ( amo . LOG [ '<STR_LIT>' ] , Addon . objects . get ( ) ) <EOL> assert len ( ActivityLog . objects . monthly_reviews ( ) ) == <NUM_LIT:0> <EOL> def test_review_count ( self ) : <EOL> amo . log ( amo . LOG [ '<STR_LIT>' ] , Addon . objects . get ( ) ) <EOL> result = ActivityLog . objects . monthly_reviews ( ) <EOL> assert len ( result ) == <NUM_LIT:1> <EOL> assert result [ <NUM_LIT:0> ] [ '<STR_LIT>' ] == <NUM_LIT:1> <EOL> assert result [ <NUM_LIT:0> ] [ '<STR_LIT:user>' ] == self . user . pk <EOL> def test_review_count_few ( self ) : <EOL> self . add_approve_logs ( <NUM_LIT:5> ) <EOL> result = ActivityLog . objects . monthly_reviews ( ) <EOL> assert len ( result ) == <NUM_LIT:1> <EOL> assert result [ <NUM_LIT:0> ] [ '<STR_LIT>' ] == <NUM_LIT:5> <EOL> def test_review_last_month ( self ) : <EOL> log = amo . log ( amo . LOG [ '<STR_LIT>' ] , Addon . objects . get ( ) ) <EOL> log . update ( created = self . lm ) <EOL> assert len ( ActivityLog . objects . monthly_reviews ( ) ) == <NUM_LIT:0> <EOL> def test_not_total ( self ) : <EOL> amo . log ( amo . LOG [ '<STR_LIT>' ] , Addon . objects . get ( ) ) <EOL> assert len ( ActivityLog . objects . total_reviews ( ) ) == <NUM_LIT:0> <EOL> def test_total_few ( self ) : <EOL> self . add_approve_logs ( <NUM_LIT:5> ) <EOL> result = ActivityLog . objects . total_reviews ( ) <EOL> assert len ( result ) == <NUM_LIT:1> <EOL> assert result [ <NUM_LIT:0> ] [ '<STR_LIT>' ] == <NUM_LIT:5> <EOL> def test_total_last_month ( self ) : <EOL> log = amo . log ( amo . LOG [ '<STR_LIT>' ] , Addon . objects . get ( ) ) <EOL> log . update ( created = self . lm ) <EOL> result = ActivityLog . objects . total_reviews ( ) <EOL> assert len ( result ) == <NUM_LIT:1> <EOL> assert result [ <NUM_LIT:0> ] [ '<STR_LIT>' ] == <NUM_LIT:1> <EOL> assert result [ <NUM_LIT:0> ] [ '<STR_LIT:user>' ] == self . user . pk <EOL> def test_total_reviews_user_position ( self ) : <EOL> self . add_approve_logs ( <NUM_LIT:5> ) <EOL> result = ActivityLog . objects . total_reviews_user_position ( self . user ) <EOL> assert result == <NUM_LIT:1> <EOL> user = UserProfile . objects . create ( email = "<STR_LIT>" ) <EOL> result = ActivityLog . objects . total_reviews_user_position ( user ) <EOL> assert result is None <EOL> def test_monthly_reviews_user_position ( self ) : <EOL> self . add_approve_logs ( <NUM_LIT:5> ) <EOL> result = ActivityLog . objects . monthly_reviews_user_position ( self . user ) <EOL> assert result == <NUM_LIT:1> <EOL> user = UserProfile . objects . create ( email = "<STR_LIT>" ) <EOL> result = ActivityLog . objects . monthly_reviews_user_position ( user ) <EOL> assert result is None <EOL> def test_user_approve_reviews ( self ) : <EOL> self . add_approve_logs ( <NUM_LIT:3> ) <EOL> other = UserProfile . objects . create ( email = "<STR_LIT>" , username = "<STR_LIT:o>" ) <EOL> amo . set_user ( other ) <EOL> self . add_approve_logs ( <NUM_LIT:2> ) <EOL> result = ActivityLog . objects . user_approve_reviews ( self . user ) . count ( ) <EOL> assert result == <NUM_LIT:3> <EOL> result = ActivityLog . objects . user_approve_reviews ( other ) . count ( ) <EOL> assert result == <NUM_LIT:2> <EOL> another = UserProfile . objects . create ( <EOL> email = "<STR_LIT>" , username = "<STR_LIT:a>" ) <EOL> result = ActivityLog . objects . user_approve_reviews ( another ) . count ( ) <EOL> assert result == <NUM_LIT:0> <EOL> def test_current_month_user_approve_reviews ( self ) : <EOL> self . add_approve_logs ( <NUM_LIT:3> ) <EOL> ActivityLog . objects . update ( created = self . days_ago ( <NUM_LIT> ) ) <EOL> self . add_approve_logs ( <NUM_LIT:2> ) <EOL> result = ActivityLog . objects . current_month_user_approve_reviews ( <EOL> self . user ) . count ( ) <EOL> assert result == <NUM_LIT:2> <EOL> def test_log_admin ( self ) : <EOL> amo . log ( amo . LOG [ '<STR_LIT>' ] , Addon . objects . get ( ) ) <EOL> assert len ( ActivityLog . objects . admin_events ( ) ) == <NUM_LIT:1> <EOL> assert len ( ActivityLog . objects . for_developer ( ) ) == <NUM_LIT:0> <EOL> def test_log_not_admin ( self ) : <EOL> amo . log ( amo . LOG [ '<STR_LIT>' ] , Addon . objects . get ( ) ) <EOL> assert len ( ActivityLog . objects . admin_events ( ) ) == <NUM_LIT:0> <EOL> assert len ( ActivityLog . objects . for_developer ( ) ) == <NUM_LIT:1> <EOL> class TestBlogPosts ( TestCase ) : <EOL> def test_blog_posts ( self ) : <EOL> BlogPost . objects . create ( title = '<STR_LIT>' ) <EOL> bp = BlogPost . objects . all ( ) <EOL> assert bp . count ( ) == <NUM_LIT:1> <EOL> assert bp [ <NUM_LIT:0> ] . title == "<STR_LIT>" </s>
