<s> import mock <EOL> from django . db import transaction <EOL> from django . test import RequestFactory <EOL> from nose import tools as nt <EOL> from datetime import datetime , timedelta <EOL> from admin . common_auth . logs import OSFLogEntry <EOL> from admin . spam . forms import ConfirmForm <EOL> from tests . base import AdminTestCase <EOL> from tests . factories import CommentFactory , AuthUserFactory , ProjectFactory <EOL> from admin_tests . utilities import setup_view , setup_form_view <EOL> from admin_tests . factories import UserFactory <EOL> from admin . spam . views import SpamList , UserSpamList , SpamDetail <EOL> class TestSpamListView ( AdminTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestSpamListView , self ) . setUp ( ) <EOL> self . project = ProjectFactory ( is_public = True ) <EOL> self . user_1 = AuthUserFactory ( ) <EOL> self . user_2 = AuthUserFactory ( ) <EOL> self . project . add_contributor ( self . user_1 ) <EOL> self . project . add_contributor ( self . user_2 ) <EOL> self . project . save ( ) <EOL> self . user_1 . save ( ) <EOL> self . user_2 . save ( ) <EOL> date = datetime . utcnow ( ) <EOL> self . comment_1 = CommentFactory ( node = self . project , user = self . user_1 ) <EOL> self . comment_2 = CommentFactory ( node = self . project , user = self . user_1 ) <EOL> self . comment_3 = CommentFactory ( node = self . project , user = self . user_1 ) <EOL> self . comment_4 = CommentFactory ( node = self . project , user = self . user_1 ) <EOL> self . comment_5 = CommentFactory ( node = self . project , user = self . user_2 ) <EOL> self . comment_6 = CommentFactory ( node = self . project , user = self . user_2 ) <EOL> self . comment_1 . report_abuse ( <EOL> user = self . user_2 , <EOL> save = True , <EOL> category = '<STR_LIT>' , <EOL> date = date - timedelta ( seconds = <NUM_LIT:5> ) <EOL> ) <EOL> self . comment_2 . report_abuse ( <EOL> user = self . user_2 , <EOL> save = True , <EOL> category = '<STR_LIT>' , <EOL> date = date - timedelta ( seconds = <NUM_LIT:4> ) <EOL> ) <EOL> self . comment_3 . report_abuse ( <EOL> user = self . user_2 , <EOL> save = True , <EOL> category = '<STR_LIT>' , <EOL> date = date - timedelta ( seconds = <NUM_LIT:3> ) <EOL> ) <EOL> self . comment_4 . report_abuse ( <EOL> user = self . user_2 , <EOL> save = True , <EOL> category = '<STR_LIT>' , <EOL> date = date - timedelta ( seconds = <NUM_LIT:2> ) <EOL> ) <EOL> self . comment_5 . report_abuse ( <EOL> user = self . user_1 , <EOL> save = True , <EOL> category = '<STR_LIT>' , <EOL> date = date - timedelta ( seconds = <NUM_LIT:1> ) <EOL> ) <EOL> self . comment_6 . report_abuse ( user = self . user_1 , save = True , <EOL> category = '<STR_LIT>' ) <EOL> def test_get_spam ( self ) : <EOL> guid = self . user_1 . _id <EOL> request = RequestFactory ( ) . get ( '<STR_LIT>' ) <EOL> view = SpamList ( ) <EOL> view = setup_view ( view , request , user_id = guid ) <EOL> res = list ( view . get_queryset ( ) ) <EOL> nt . assert_equal ( len ( res ) , <NUM_LIT:6> ) <EOL> response_list = [ r . _id for r in res ] <EOL> should_be = [ <EOL> self . comment_6 . _id , <EOL> self . comment_5 . _id , <EOL> self . comment_4 . _id , <EOL> self . comment_3 . _id , <EOL> self . comment_2 . _id , <EOL> self . comment_1 . _id <EOL> ] <EOL> nt . assert_list_equal ( should_be , response_list ) <EOL> class TestSpamDetail ( AdminTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestSpamDetail , self ) . setUp ( ) <EOL> self . comment = CommentFactory ( ) <EOL> self . request = RequestFactory ( ) . post ( '<STR_LIT>' ) <EOL> self . request . user = UserFactory ( ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_add_log ( self , mock_success_url ) : <EOL> form_data = { '<STR_LIT>' : '<STR_LIT:2>' } <EOL> form = ConfirmForm ( data = form_data ) <EOL> nt . assert_true ( form . is_valid ( ) ) <EOL> view = SpamDetail ( ) <EOL> view = setup_form_view ( <EOL> view , self . request , form , spam_id = self . comment . _id ) <EOL> with transaction . atomic ( ) : <EOL> view . form_valid ( form ) <EOL> obj = OSFLogEntry . objects . latest ( field_name = '<STR_LIT>' ) <EOL> nt . assert_equal ( obj . object_id , self . comment . _id ) <EOL> class TestUserSpamListView ( AdminTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestUserSpamListView , self ) . setUp ( ) <EOL> self . project = ProjectFactory ( is_public = True ) <EOL> self . user_1 = AuthUserFactory ( ) <EOL> self . user_2 = AuthUserFactory ( ) <EOL> self . project . add_contributor ( self . user_1 ) <EOL> self . project . add_contributor ( self . user_2 ) <EOL> self . project . save ( ) <EOL> self . user_1 . save ( ) <EOL> self . user_2 . save ( ) <EOL> self . comment_1 = CommentFactory ( node = self . project , user = self . user_1 ) <EOL> self . comment_2 = CommentFactory ( node = self . project , user = self . user_1 ) <EOL> self . comment_3 = CommentFactory ( node = self . project , user = self . user_1 ) <EOL> self . comment_4 = CommentFactory ( node = self . project , user = self . user_1 ) <EOL> self . comment_5 = CommentFactory ( node = self . project , user = self . user_2 ) <EOL> self . comment_6 = CommentFactory ( node = self . project , user = self . user_2 ) <EOL> self . comment_1 . report_abuse ( user = self . user_2 , save = True , <EOL> category = '<STR_LIT>' ) <EOL> self . comment_2 . report_abuse ( user = self . user_2 , save = True , <EOL> category = '<STR_LIT>' ) <EOL> self . comment_3 . report_abuse ( user = self . user_2 , save = True , <EOL> category = '<STR_LIT>' ) <EOL> self . comment_4 . report_abuse ( user = self . user_2 , save = True , <EOL> category = '<STR_LIT>' ) <EOL> self . comment_5 . report_abuse ( user = self . user_1 , save = True , <EOL> category = '<STR_LIT>' ) <EOL> self . comment_6 . report_abuse ( user = self . user_1 , save = True , <EOL> category = '<STR_LIT>' ) <EOL> def test_get_user_spam ( self ) : <EOL> guid = self . user_1 . _id <EOL> request = RequestFactory ( ) . get ( '<STR_LIT>' ) <EOL> view = UserSpamList ( ) <EOL> view = setup_view ( view , request , user_id = guid ) <EOL> res = list ( view . get_queryset ( ) ) <EOL> nt . assert_equal ( len ( res ) , <NUM_LIT:4> ) </s>
