<s> import json <EOL> import os <EOL> import tempfile <EOL> from django . conf import settings <EOL> from nose . tools import assert_equal , assert_false , assert_true <EOL> import desktop . conf <EOL> from desktop . conf import AUDIT_EVENT_LOG_DIR <EOL> from desktop . lib . django_test_util import make_logged_in_client <EOL> from desktop . lib . test_utils import add_permission <EOL> def test_view_perms ( ) : <EOL> c = make_logged_in_client ( ) <EOL> response = c . get ( "<STR_LIT>" ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code ) <EOL> response = c . get ( "<STR_LIT>" ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code ) <EOL> c = make_logged_in_client ( '<STR_LIT:user>' , is_superuser = False ) <EOL> add_permission ( '<STR_LIT:user>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> response = c . get ( "<STR_LIT>" ) <EOL> assert_equal ( <NUM_LIT> , response . status_code ) <EOL> response = c . get ( "<STR_LIT>" ) <EOL> assert_equal ( <NUM_LIT> , response . status_code ) <EOL> response = c . get ( "<STR_LIT>" ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code , response . content ) <EOL> def test_ensure_safe_method_middleware ( ) : <EOL> try : <EOL> c = make_logged_in_client ( ) <EOL> response = c . get ( "<STR_LIT>" ) <EOL> assert_equal ( <NUM_LIT:200> , response . status_code ) <EOL> done = desktop . conf . HTTP_ALLOWED_METHODS . set_for_testing ( [ ] ) <EOL> response = c . get ( "<STR_LIT>" ) <EOL> assert_equal ( <NUM_LIT> , response . status_code ) <EOL> finally : <EOL> done ( ) <EOL> def test_audit_logging_middleware_enable ( ) : <EOL> c = make_logged_in_client ( username = '<STR_LIT>' , is_superuser = False ) <EOL> with tempfile . NamedTemporaryFile ( "<STR_LIT>" ) as log_tmp : <EOL> log_path = log_tmp . name <EOL> reset = AUDIT_EVENT_LOG_DIR . set_for_testing ( log_path ) <EOL> settings . MIDDLEWARE_CLASSES . append ( '<STR_LIT>' ) <EOL> try : <EOL> response = c . get ( "<STR_LIT>" ) <EOL> assert_true ( '<STR_LIT>' in response , response ) <EOL> audit = open ( log_path ) . readlines ( ) <EOL> for line in audit : <EOL> audit_json = json . loads ( line ) <EOL> audit_record = audit_json . values ( ) [ <NUM_LIT:0> ] <EOL> assert_equal ( '<STR_LIT>' , audit_record [ '<STR_LIT:user>' ] , audit_record ) <EOL> assert_equal ( '<STR_LIT>' , audit_record [ '<STR_LIT:url>' ] , audit_record ) <EOL> finally : <EOL> settings . MIDDLEWARE_CLASSES . pop ( ) <EOL> reset ( ) <EOL> def test_audit_logging_middleware_disable ( ) : <EOL> c = make_logged_in_client ( username = '<STR_LIT>' , is_superuser = False ) <EOL> reset = AUDIT_EVENT_LOG_DIR . set_for_testing ( '<STR_LIT>' ) <EOL> try : <EOL> response = c . get ( "<STR_LIT>" ) <EOL> assert_false ( '<STR_LIT>' in response , response ) <EOL> finally : <EOL> reset ( ) <EOL> def test_ensure_safe_redirect_middleware ( ) : <EOL> done = [ ] <EOL> settings . MIDDLEWARE_CLASSES . append ( '<STR_LIT>' ) <EOL> try : <EOL> c = make_logged_in_client ( ) <EOL> response = c . post ( "<STR_LIT>" , { <EOL> '<STR_LIT:username>' : '<STR_LIT:test>' , <EOL> '<STR_LIT:password>' : '<STR_LIT:test>' , <EOL> } ) <EOL> assert_equal ( <NUM_LIT> , response . status_code ) <EOL> done . append ( desktop . conf . REDIRECT_WHITELIST . set_for_testing ( '<STR_LIT>' ) ) <EOL> response = c . post ( "<STR_LIT>" , { <EOL> '<STR_LIT:username>' : '<STR_LIT:test>' , <EOL> '<STR_LIT:password>' : '<STR_LIT:test>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ) <EOL> assert_equal ( <NUM_LIT> , response . status_code ) <EOL> done . append ( desktop . conf . REDIRECT_WHITELIST . set_for_testing ( '<STR_LIT>' ) ) <EOL> response = c . post ( "<STR_LIT>" , { <EOL> '<STR_LIT:username>' : '<STR_LIT:test>' , <EOL> '<STR_LIT:password>' : '<STR_LIT:test>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ) <EOL> assert_equal ( <NUM_LIT> , response . status_code ) <EOL> done . append ( desktop . conf . REDIRECT_WHITELIST . set_for_testing ( '<STR_LIT>' ) ) <EOL> response = c . post ( "<STR_LIT>" , { <EOL> '<STR_LIT:username>' : '<STR_LIT:test>' , <EOL> '<STR_LIT:password>' : '<STR_LIT:test>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ) <EOL> assert_equal ( <NUM_LIT> , response . status_code ) <EOL> finally : <EOL> settings . MIDDLEWARE_CLASSES . pop ( ) <EOL> for finish in done : <EOL> finish ( ) </s>
