<s> from datetime import datetime , timedelta <EOL> from mock import patch , ANY <EOL> from django_webtest import WebTest <EOL> from django . contrib . admin import AdminSite <EOL> from django . contrib . messages . storage . fallback import FallbackStorage <EOL> from django . db . transaction import atomic <EOL> from django . test . utils import override_settings <EOL> from django . test . client import RequestFactory <EOL> from django import VERSION <EOL> from django . core . urlresolvers import reverse <EOL> from django . conf import settings <EOL> from django . utils . encoding import force_text <EOL> try : <EOL> from django . contrib . auth import get_user_model <EOL> User = get_user_model ( ) <EOL> except ImportError : <EOL> from django . contrib . auth . models import User <EOL> try : <EOL> from django . contrib . admin . utils import quote <EOL> except ImportError : <EOL> from django . contrib . admin . util import quote <EOL> from simple_history . models import HistoricalRecords <EOL> from simple_history . admin import SimpleHistoryAdmin <EOL> from . . models import Book , Person , Poll , State , Employee <EOL> today = datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:10> , <NUM_LIT:0> ) <EOL> tomorrow = today + timedelta ( days = <NUM_LIT:1> ) <EOL> def get_history_url ( obj , history_index = None , site = "<STR_LIT>" ) : <EOL> app , model = obj . _meta . app_label , obj . _meta . model_name <EOL> if history_index is not None : <EOL> history = obj . history . order_by ( '<STR_LIT>' ) [ history_index ] <EOL> return reverse ( <EOL> "<STR_LIT>" . format ( <EOL> site = site , app = app , model = model ) , <EOL> args = [ quote ( obj . pk ) , quote ( history . history_id ) ] , <EOL> ) <EOL> else : <EOL> return reverse ( "<STR_LIT>" . format ( <EOL> site = site , app = app , model = model ) , args = [ quote ( obj . pk ) ] ) <EOL> class AdminSiteTest ( WebTest ) : <EOL> def setUp ( self ) : <EOL> self . user = User . objects . create_superuser ( '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> def tearDown ( self ) : <EOL> try : <EOL> del HistoricalRecords . thread . request <EOL> except AttributeError : <EOL> pass <EOL> def login ( self , user = None ) : <EOL> if user is None : <EOL> user = self . user <EOL> form = self . app . get ( reverse ( '<STR_LIT>' ) ) . maybe_follow ( ) . form <EOL> form [ '<STR_LIT:username>' ] = user . username <EOL> form [ '<STR_LIT:password>' ] = '<STR_LIT>' <EOL> return form . submit ( ) <EOL> def test_history_list ( self ) : <EOL> model_name = self . user . _meta . model_name <EOL> self . assertEqual ( model_name , '<STR_LIT>' ) <EOL> self . login ( ) <EOL> poll = Poll ( question = "<STR_LIT>" , pub_date = today ) <EOL> poll . _history_user = self . user <EOL> poll . save ( ) <EOL> response = self . app . get ( get_history_url ( poll ) ) <EOL> self . assertIn ( get_history_url ( poll , <NUM_LIT:0> ) , response . unicode_normal_body ) <EOL> self . assertIn ( "<STR_LIT>" , response . unicode_normal_body ) <EOL> self . assertIn ( "<STR_LIT>" , response . unicode_normal_body ) <EOL> self . assertIn ( self . user . username , response . unicode_normal_body ) <EOL> def test_history_form_permission ( self ) : <EOL> self . login ( self . user ) <EOL> person = Person . objects . create ( name = '<STR_LIT>' ) <EOL> self . app . get ( get_history_url ( person , <NUM_LIT:0> ) , status = <NUM_LIT> ) <EOL> def test_invalid_history_form ( self ) : <EOL> self . login ( ) <EOL> poll = Poll . objects . create ( question = "<STR_LIT>" , pub_date = today ) <EOL> response = self . app . get ( get_history_url ( poll , <NUM_LIT:0> ) ) <EOL> response . form [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> response = response . form . submit ( ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertIn ( "<STR_LIT>" , response . unicode_normal_body ) <EOL> def test_history_form ( self ) : <EOL> self . login ( ) <EOL> poll = Poll . objects . create ( question = "<STR_LIT>" , pub_date = today ) <EOL> poll . question = "<STR_LIT>" <EOL> poll . save ( ) <EOL> response = self . app . get ( get_history_url ( poll , <NUM_LIT:0> ) ) <EOL> self . assertEqual ( response . form [ '<STR_LIT>' ] . value , "<STR_LIT>" ) <EOL> self . assertEqual ( response . form [ '<STR_LIT>' ] . value , "<STR_LIT>" ) <EOL> self . assertEqual ( response . form [ '<STR_LIT>' ] . value , "<STR_LIT>" ) <EOL> response . form [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> response . form [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> response = response . form . submit ( ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> if VERSION < ( <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:0> ) : <EOL> self . assertTrue ( response . headers [ '<STR_LIT:location>' ] <EOL> . endswith ( get_history_url ( poll ) ) ) <EOL> else : <EOL> self . assertTrue ( response . headers [ '<STR_LIT:location>' ] <EOL> . endswith ( reverse ( '<STR_LIT>' ) ) ) <EOL> response = self . app . get ( get_history_url ( poll , <NUM_LIT:1> ) ) <EOL> self . assertEqual ( response . form [ '<STR_LIT>' ] . value , "<STR_LIT>" ) <EOL> self . assertEqual ( response . form [ '<STR_LIT>' ] . value , "<STR_LIT>" ) <EOL> self . assertEqual ( response . form [ '<STR_LIT>' ] . value , "<STR_LIT>" ) <EOL> response = self . app . get ( get_history_url ( poll , <NUM_LIT:2> ) ) <EOL> self . assertEqual ( response . form [ '<STR_LIT>' ] . value , "<STR_LIT>" ) <EOL> self . assertEqual ( response . form [ '<STR_LIT>' ] . value , "<STR_LIT>" ) <EOL> self . assertEqual ( response . form [ '<STR_LIT>' ] . value , "<STR_LIT>" ) <EOL> poll = Poll . objects . get ( ) <EOL> self . assertEqual ( poll . question , "<STR_LIT>" ) <EOL> self . assertEqual ( poll . pub_date , tomorrow ) <EOL> self . assertEqual ( [ p . history_user for p in Poll . history . all ( ) ] , <EOL> [ self . user , None , None ] ) <EOL> def test_history_user_on_save_in_admin ( self ) : <EOL> self . login ( ) <EOL> add_page = self . app . get ( reverse ( '<STR_LIT>' ) ) <EOL> add_page . form [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> add_page . form [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> add_page . form [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> changelist_page = add_page . form . submit ( ) . follow ( ) <EOL> self . assertEqual ( Poll . history . get ( ) . history_user , self . user ) <EOL> change_page = changelist_page . click ( "<STR_LIT>" ) <EOL> change_page . form . submit ( ) <EOL> self . assertEqual ( [ p . history_user for p in Poll . history . all ( ) ] , <EOL> [ self . user , self . user ] ) <EOL> def test_underscore_in_pk ( self ) : <EOL> self . login ( ) <EOL> book = Book ( isbn = "<STR_LIT>" ) <EOL> book . _history_user = self . user <EOL> book . save ( ) <EOL> response = self . app . get ( get_history_url ( book ) ) <EOL> self . assertIn ( book . history . all ( ) [ <NUM_LIT:0> ] . revert_url ( ) , <EOL> response . unicode_normal_body ) <EOL> def test_historical_user_no_setter ( self ) : <EOL> """<STR_LIT>""" <EOL> self . login ( ) <EOL> add_page = self . app . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertRaises ( AttributeError , add_page . form . submit ) <EOL> def test_historical_user_with_setter ( self ) : <EOL> """<STR_LIT>""" <EOL> self . login ( ) <EOL> add_page = self . app . get ( reverse ( '<STR_LIT>' ) ) <EOL> add_page . form . submit ( ) <EOL> def test_history_user_not_saved ( self ) : <EOL> self . login ( ) <EOL> poll = Poll . objects . create ( question = "<STR_LIT>" , pub_date = today ) <EOL> historical_poll = poll . history . all ( ) [ <NUM_LIT:0> ] <EOL> self . assertIsNone ( <EOL> historical_poll . history_user , <EOL> "<STR_LIT>" , <EOL> ) <EOL> def test_middleware_saves_user ( self ) : <EOL> overridden_settings = { <EOL> '<STR_LIT>' : <EOL> settings . MIDDLEWARE_CLASSES <EOL> + [ '<STR_LIT>' ] , <EOL> } <EOL> with override_settings ( ** overridden_settings ) : <EOL> self . login ( ) <EOL> form = self . app . get ( reverse ( '<STR_LIT>' ) ) . form <EOL> form [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> form . submit ( ) <EOL> book = Book . objects . get ( ) <EOL> historical_book = book . history . all ( ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( historical_book . history_user , self . user , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def test_middleware_unsets_request ( self ) : <EOL> overridden_settings = { <EOL> '<STR_LIT>' : <EOL> settings . MIDDLEWARE_CLASSES <EOL> + [ '<STR_LIT>' ] , <EOL> } <EOL> with override_settings ( ** overridden_settings ) : <EOL> self . login ( ) <EOL> self . app . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertFalse ( hasattr ( HistoricalRecords . thread , '<STR_LIT>' ) ) <EOL> def test_rolled_back_user_does_not_lead_to_foreign_key_error ( self ) : <EOL> overridden_settings = { <EOL> '<STR_LIT>' : <EOL> settings . MIDDLEWARE_CLASSES <EOL> + [ '<STR_LIT>' ] , <EOL> } <EOL> with override_settings ( ** overridden_settings ) : <EOL> self . login ( ) <EOL> self . assertEqual ( <EOL> self . app . get ( reverse ( '<STR_LIT>' ) ) . status_code , <EOL> <NUM_LIT:200> , <EOL> ) <EOL> book = Book . objects . create ( isbn = "<STR_LIT>" ) <EOL> historical_book = book . history . all ( ) [ <NUM_LIT:0> ] <EOL> self . assertIsNone ( <EOL> historical_book . history_user , <EOL> "<STR_LIT>" , <EOL> ) <EOL> def test_middleware_anonymous_user ( self ) : <EOL> overridden_settings = { <EOL> '<STR_LIT>' : <EOL> settings . MIDDLEWARE_CLASSES <EOL> + [ '<STR_LIT>' ] , <EOL> } <EOL> with override_settings ( ** overridden_settings ) : <EOL> self . app . get ( reverse ( '<STR_LIT>' ) ) <EOL> poll = Poll . objects . create ( question = "<STR_LIT>" , pub_date = today ) <EOL> historical_poll = poll . history . all ( ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( historical_poll . history_user , None , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def test_other_admin ( self ) : <EOL> """<STR_LIT>""" <EOL> self . login ( ) <EOL> state = State . objects . create ( ) <EOL> history_url = get_history_url ( state , site = "<STR_LIT>" ) <EOL> self . app . get ( history_url ) <EOL> change_url = get_history_url ( state , <NUM_LIT:0> , site = "<STR_LIT>" ) <EOL> self . app . get ( change_url ) <EOL> def test_deleteting_user ( self ) : <EOL> """<STR_LIT>""" <EOL> self . login ( ) <EOL> poll = Poll ( question = "<STR_LIT>" , pub_date = today ) <EOL> poll . _history_user = self . user <EOL> poll . save ( ) <EOL> historical_poll = poll . history . all ( ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( historical_poll . history_user , self . user ) <EOL> self . user . delete ( ) <EOL> historical_poll = poll . history . all ( ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( historical_poll . history_user , None ) <EOL> def test_missing_one_to_one ( self ) : <EOL> """<STR_LIT>""" <EOL> self . login ( ) <EOL> manager = Employee . objects . create ( ) <EOL> employee = Employee . objects . create ( manager = manager ) <EOL> employee . manager = None <EOL> employee . save ( ) <EOL> manager . delete ( ) <EOL> response = self . app . get ( get_history_url ( employee , <NUM_LIT:0> ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> def test_history_deleted_instance ( self ) : <EOL> """<STR_LIT>""" <EOL> self . login ( ) <EOL> employee = Employee . objects . create ( ) <EOL> employee_pk = employee . pk <EOL> employee . delete ( ) <EOL> employee . pk = employee_pk <EOL> response = self . app . get ( get_history_url ( employee ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> def test_response_change ( self ) : <EOL> """<STR_LIT>""" <EOL> request = RequestFactory ( ) . post ( '<STR_LIT:/>' ) <EOL> request . POST = { '<STR_LIT>' : True } <EOL> request . session = '<STR_LIT>' <EOL> request . _messages = FallbackStorage ( request ) <EOL> request . path = '<STR_LIT>' <EOL> poll = Poll . objects . create ( question = "<STR_LIT>" , pub_date = today ) <EOL> poll . question = "<STR_LIT>" <EOL> poll . save ( ) <EOL> admin_site = AdminSite ( ) <EOL> admin = SimpleHistoryAdmin ( Poll , admin_site ) <EOL> with patch ( '<STR_LIT>' , True ) : <EOL> response = admin . response_change ( request , poll ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_response_change_change_history_setting_off ( self ) : <EOL> """<STR_LIT>""" <EOL> request = RequestFactory ( ) . post ( '<STR_LIT:/>' ) <EOL> request . POST = { '<STR_LIT>' : True } <EOL> request . session = '<STR_LIT>' <EOL> request . _messages = FallbackStorage ( request ) <EOL> request . path = '<STR_LIT>' <EOL> request . user = self . user <EOL> poll = Poll . objects . create ( question = "<STR_LIT>" , pub_date = today ) <EOL> poll . question = "<STR_LIT>" <EOL> poll . save ( ) <EOL> admin_site = AdminSite ( ) <EOL> admin = SimpleHistoryAdmin ( Poll , admin_site ) <EOL> response = admin . response_change ( request , poll ) <EOL> with patch ( <EOL> '<STR_LIT>' <EOL> ) as m_admin : <EOL> m_admin . return_value = '<STR_LIT>' <EOL> response = admin . response_change ( request , poll ) <EOL> self . assertEqual ( response , '<STR_LIT>' ) <EOL> def test_response_change_no_change_history ( self ) : <EOL> request = RequestFactory ( ) . post ( '<STR_LIT:/>' ) <EOL> request . session = '<STR_LIT>' <EOL> request . _messages = FallbackStorage ( request ) <EOL> request . user = self . user <EOL> poll = Poll . objects . create ( question = "<STR_LIT>" , pub_date = today ) <EOL> poll . question = "<STR_LIT>" <EOL> poll . save ( ) <EOL> admin_site = AdminSite ( ) <EOL> admin = SimpleHistoryAdmin ( Poll , admin_site ) <EOL> with patch ( <EOL> '<STR_LIT>' <EOL> ) as m_admin : <EOL> m_admin . return_value = '<STR_LIT>' <EOL> response = admin . response_change ( request , poll ) <EOL> self . assertEqual ( response , '<STR_LIT>' ) <EOL> def test_history_form_view_without_getting_history ( self ) : <EOL> request = RequestFactory ( ) . post ( '<STR_LIT:/>' ) <EOL> request . session = '<STR_LIT>' <EOL> request . _messages = FallbackStorage ( request ) <EOL> request . user = self . user <EOL> poll = Poll . objects . create ( question = "<STR_LIT>" , pub_date = today ) <EOL> poll . question = "<STR_LIT>" <EOL> poll . save ( ) <EOL> history = poll . history . all ( ) [ <NUM_LIT:0> ] <EOL> admin_site = AdminSite ( ) <EOL> admin = SimpleHistoryAdmin ( Poll , admin_site ) <EOL> with patch ( '<STR_LIT>' ) as mock_render : <EOL> admin . history_form_view ( request , poll . id , history . pk ) <EOL> context = { <EOL> '<STR_LIT>' : poll , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT:title>' : '<STR_LIT>' % force_text ( poll ) , <EOL> '<STR_LIT>' : ANY , <EOL> '<STR_LIT>' : poll . id , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : ANY , <EOL> '<STR_LIT>' : ANY , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : ANY , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : ANY , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : admin . has_add_permission ( request ) , <EOL> '<STR_LIT>' : admin . has_change_permission ( <EOL> request , poll ) , <EOL> '<STR_LIT>' : admin . has_delete_permission ( <EOL> request , poll ) , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : ANY , <EOL> '<STR_LIT>' : ANY , <EOL> '<STR_LIT>' : admin . save_as , <EOL> '<STR_LIT>' : admin . save_on_top , <EOL> '<STR_LIT>' : getattr ( admin_site , '<STR_LIT>' , None ) , <EOL> } <EOL> mock_render . assert_called_once_with ( <EOL> request , template_name = admin . object_history_form_template , <EOL> dictionary = context , current_app = admin_site . name ) <EOL> def test_history_form_view_getting_history ( self ) : <EOL> request = RequestFactory ( ) . post ( '<STR_LIT:/>' ) <EOL> request . session = '<STR_LIT>' <EOL> request . _messages = FallbackStorage ( request ) <EOL> request . user = self . user <EOL> request . POST = { '<STR_LIT>' : True } <EOL> poll = Poll . objects . create ( question = "<STR_LIT>" , pub_date = today ) <EOL> poll . question = "<STR_LIT>" <EOL> poll . save ( ) <EOL> history = poll . history . all ( ) [ <NUM_LIT:0> ] <EOL> admin_site = AdminSite ( ) <EOL> admin = SimpleHistoryAdmin ( Poll , admin_site ) <EOL> with patch ( '<STR_LIT>' ) as mock_render : <EOL> with patch ( '<STR_LIT>' , True ) : <EOL> admin . history_form_view ( request , poll . id , history . pk ) <EOL> context = { <EOL> '<STR_LIT>' : history . instance , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT:title>' : '<STR_LIT>' % force_text ( history . instance ) , <EOL> '<STR_LIT>' : ANY , <EOL> '<STR_LIT>' : poll . id , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : ANY , <EOL> '<STR_LIT>' : ANY , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : ANY , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : ANY , <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( <EOL> pk = poll . pk ) , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : admin . has_add_permission ( request ) , <EOL> '<STR_LIT>' : admin . has_change_permission ( <EOL> request , poll ) , <EOL> '<STR_LIT>' : admin . has_delete_permission ( <EOL> request , poll ) , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : ANY , <EOL> '<STR_LIT>' : ANY , <EOL> '<STR_LIT>' : admin . save_as , <EOL> '<STR_LIT>' : admin . save_on_top , <EOL> '<STR_LIT>' : getattr ( admin_site , '<STR_LIT>' , None ) , <EOL> } <EOL> mock_render . assert_called_once_with ( <EOL> request , template_name = admin . object_history_form_template , <EOL> dictionary = context , current_app = admin_site . name ) <EOL> def test_history_form_view_getting_history_with_setting_off ( self ) : <EOL> request = RequestFactory ( ) . post ( '<STR_LIT:/>' ) <EOL> request . session = '<STR_LIT>' <EOL> request . _messages = FallbackStorage ( request ) <EOL> request . user = self . user <EOL> request . POST = { '<STR_LIT>' : True } <EOL> poll = Poll . objects . create ( question = "<STR_LIT>" , pub_date = today ) <EOL> poll . question = "<STR_LIT>" <EOL> poll . save ( ) <EOL> history = poll . history . all ( ) [ <NUM_LIT:0> ] <EOL> admin_site = AdminSite ( ) <EOL> admin = SimpleHistoryAdmin ( Poll , admin_site ) <EOL> with patch ( '<STR_LIT>' ) as mock_render : <EOL> with patch ( '<STR_LIT>' , False ) : <EOL> admin . history_form_view ( request , poll . id , history . pk ) <EOL> context = { <EOL> '<STR_LIT>' : poll , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT:title>' : '<STR_LIT>' % force_text ( poll ) , <EOL> '<STR_LIT>' : ANY , <EOL> '<STR_LIT>' : poll . id , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : ANY , <EOL> '<STR_LIT>' : ANY , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : ANY , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : ANY , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : admin . has_add_permission ( request ) , <EOL> '<STR_LIT>' : admin . has_change_permission ( <EOL> request , poll ) , <EOL> '<STR_LIT>' : admin . has_delete_permission ( <EOL> request , poll ) , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : ANY , <EOL> '<STR_LIT>' : ANY , <EOL> '<STR_LIT>' : admin . save_as , <EOL> '<STR_LIT>' : admin . save_on_top , <EOL> '<STR_LIT>' : getattr ( admin_site , '<STR_LIT>' , None ) , <EOL> } <EOL> mock_render . assert_called_once_with ( <EOL> request , template_name = admin . object_history_form_template , <EOL> dictionary = context , current_app = admin_site . name ) </s>
