<s> from django . contrib . auth . models import User <EOL> from django . utils . timezone import now <EOL> from rest_framework . test import APITestCase , APIRequestFactory <EOL> from rest_framework_tracking . models import APIRequestLog <EOL> from rest_framework . authtoken . models import Token <EOL> from views import MockLoggingView <EOL> import pytest <EOL> pytestmark = pytest . mark . django_db <EOL> class TestLoggingMixin ( APITestCase ) : <EOL> urls = '<STR_LIT>' <EOL> def test_nologging_no_log_created ( self ) : <EOL> self . client . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( APIRequestLog . objects . all ( ) . count ( ) , <NUM_LIT:0> ) <EOL> def test_logging_creates_log ( self ) : <EOL> self . client . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( APIRequestLog . objects . all ( ) . count ( ) , <NUM_LIT:1> ) <EOL> def test_log_path ( self ) : <EOL> self . client . get ( '<STR_LIT>' ) <EOL> log = APIRequestLog . objects . first ( ) <EOL> self . assertEqual ( log . path , '<STR_LIT>' ) <EOL> def test_log_ip_remote ( self ) : <EOL> request = APIRequestFactory ( ) . get ( '<STR_LIT>' ) <EOL> request . META [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> MockLoggingView . as_view ( ) ( request ) . render ( ) <EOL> log = APIRequestLog . objects . first ( ) <EOL> self . assertEqual ( log . remote_addr , '<STR_LIT>' ) <EOL> def test_log_ip_xforwarded ( self ) : <EOL> request = APIRequestFactory ( ) . get ( '<STR_LIT>' ) <EOL> request . META [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> MockLoggingView . as_view ( ) ( request ) . render ( ) <EOL> log = APIRequestLog . objects . first ( ) <EOL> self . assertEqual ( log . remote_addr , '<STR_LIT>' ) <EOL> def test_log_host ( self ) : <EOL> self . client . get ( '<STR_LIT>' ) <EOL> log = APIRequestLog . objects . first ( ) <EOL> self . assertEqual ( log . host , '<STR_LIT>' ) <EOL> def test_log_method ( self ) : <EOL> self . client . get ( '<STR_LIT>' ) <EOL> log = APIRequestLog . objects . first ( ) <EOL> self . assertEqual ( log . method , '<STR_LIT:GET>' ) <EOL> def test_log_status ( self ) : <EOL> self . client . get ( '<STR_LIT>' ) <EOL> log = APIRequestLog . objects . first ( ) <EOL> self . assertEqual ( log . status_code , <NUM_LIT:200> ) <EOL> def test_log_time_fast ( self ) : <EOL> self . client . get ( '<STR_LIT>' ) <EOL> log = APIRequestLog . objects . first ( ) <EOL> self . assertLessEqual ( log . response_ms , <NUM_LIT:7> ) <EOL> self . assertGreaterEqual ( ( now ( ) - log . requested_at ) . total_seconds ( ) , <NUM_LIT> ) <EOL> def test_log_time_slow ( self ) : <EOL> self . client . get ( '<STR_LIT>' ) <EOL> log = APIRequestLog . objects . first ( ) <EOL> self . assertGreaterEqual ( log . response_ms , <NUM_LIT:1000> ) <EOL> self . assertGreaterEqual ( ( now ( ) - log . requested_at ) . total_seconds ( ) , <NUM_LIT:1> ) <EOL> def test_log_anon_user ( self ) : <EOL> self . client . get ( '<STR_LIT>' ) <EOL> log = APIRequestLog . objects . first ( ) <EOL> self . assertEqual ( log . user , None ) <EOL> def test_log_auth_user ( self ) : <EOL> User . objects . create_user ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> user = User . objects . get ( username = '<STR_LIT>' ) <EOL> self . client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> self . client . get ( '<STR_LIT>' ) <EOL> log = APIRequestLog . objects . first ( ) <EOL> self . assertEqual ( log . user , user ) <EOL> def test_log_auth_inactive_user ( self ) : <EOL> user = User . objects . create_user ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> token = Token . objects . create ( user = user ) <EOL> token_header = '<STR_LIT>' % token . key <EOL> user . is_active = False <EOL> user . save ( ) <EOL> self . client . get ( '<STR_LIT>' , <EOL> HTTP_AUTHORIZATION = token_header ) <EOL> log = APIRequestLog . objects . first ( ) <EOL> self . assertIsNone ( log . user ) <EOL> self . assertIn ( "<STR_LIT>" , log . response ) <EOL> def test_log_unauth_fails ( self ) : <EOL> self . client . logout ( ) <EOL> self . client . get ( '<STR_LIT>' ) <EOL> log = APIRequestLog . objects . first ( ) <EOL> self . assertEqual ( log . response , '<STR_LIT>' ) <EOL> def test_log_params ( self ) : <EOL> self . client . get ( '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT:a>' , '<STR_LIT>' : '<STR_LIT:2>' } ) <EOL> log = APIRequestLog . objects . first ( ) <EOL> self . assertEqual ( log . query_params , str ( { u'<STR_LIT>' : u'<STR_LIT:a>' , u'<STR_LIT>' : u'<STR_LIT:2>' } ) ) <EOL> def test_log_data_empty ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . post ( '<STR_LIT>' ) <EOL> log = APIRequestLog . objects . first ( ) <EOL> self . assertEqual ( log . data , str ( { } ) ) <EOL> def test_log_data_json ( self ) : <EOL> self . client . post ( '<STR_LIT>' , { '<STR_LIT:key>' : <NUM_LIT:1> , '<STR_LIT>' : [ { '<STR_LIT:a>' : '<STR_LIT:b>' } ] } , format = '<STR_LIT>' ) <EOL> log = APIRequestLog . objects . first ( ) <EOL> expected_data = { u'<STR_LIT:key>' : <NUM_LIT:1> , u'<STR_LIT>' : [ { u'<STR_LIT:a>' : u'<STR_LIT:b>' } ] } <EOL> self . assertEqual ( log . data , str ( expected_data ) ) <EOL> def test_log_text_response ( self ) : <EOL> self . client . get ( '<STR_LIT>' ) <EOL> log = APIRequestLog . objects . first ( ) <EOL> self . assertEqual ( log . response , u'<STR_LIT>' ) <EOL> def test_log_json_get_response ( self ) : <EOL> self . client . get ( '<STR_LIT>' ) <EOL> log = APIRequestLog . objects . first ( ) <EOL> self . assertEqual ( log . response , u'<STR_LIT>' ) <EOL> def test_log_json_post_response ( self ) : <EOL> self . client . post ( '<STR_LIT>' , { } , format = '<STR_LIT>' ) <EOL> log = APIRequestLog . objects . first ( ) <EOL> self . assertEqual ( log . response , u'<STR_LIT>' ) <EOL> def test_log_status_validation_error ( self ) : <EOL> self . client . get ( '<STR_LIT>' ) <EOL> log = APIRequestLog . objects . first ( ) <EOL> self . assertEqual ( log . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( log . response , u'<STR_LIT>' ) <EOL> def test_log_request_404_error ( self ) : <EOL> self . client . get ( '<STR_LIT>' ) <EOL> log = APIRequestLog . objects . first ( ) <EOL> self . assertEqual ( log . status_code , <NUM_LIT> ) <EOL> self . assertIn ( '<STR_LIT>' , log . response ) </s>
