desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'default: manage.py builtin commands succeed if settings are provided in the environment'
def test_builtin_with_environment(self):
args = ['sqlall', 'admin_scripts'] (out, err) = self.run_manage(args, 'settings') self.assertNoOutput(err) self.assertOutput(out, 'CREATE TABLE')
'default: manage.py builtin commands succeed if settings file (from argument) doesn\'t exist'
def test_builtin_with_bad_settings(self):
args = ['sqlall', '--settings=bad_settings', 'admin_scripts'] (out, err) = self.run_manage(args) self.assertNoOutput(out) self.assertOutput(err, "Could not import settings 'bad_settings'")
'default: manage.py builtin commands fail if settings file (from environment) doesn\'t exist'
def test_builtin_with_bad_environment(self):
args = ['sqlall', 'admin_scripts'] (out, err) = self.run_manage(args, 'bad_settings') self.assertNoOutput(err) self.assertOutput(out, 'CREATE TABLE')
'default: manage.py can execute user commands when default settings are appropriate'
def test_custom_command(self):
args = ['noargs_command'] (out, err) = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, 'EXECUTE:NoArgsCommand')
'default: manage.py can execute user commands when settings are provided as argument'
def test_custom_command_with_settings(self):
args = ['noargs_command', '--settings=settings'] (out, err) = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, 'EXECUTE:NoArgsCommand')
'default: manage.py can execute user commands when settings are provided in environment'
def test_custom_command_with_environment(self):
args = ['noargs_command'] (out, err) = self.run_manage(args, 'settings') self.assertNoOutput(err) self.assertOutput(out, 'EXECUTE:NoArgsCommand')
'fulldefault: manage.py builtin commands succeed when default settings are appropriate'
def test_builtin_command(self):
args = ['sqlall', 'admin_scripts'] (out, err) = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, 'CREATE TABLE')
'fulldefault: manage.py builtin commands succeed if settings are provided as argument'
def test_builtin_with_settings(self):
args = ['sqlall', '--settings=settings', 'admin_scripts'] (out, err) = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, 'CREATE TABLE')
'fulldefault: manage.py builtin commands succeed if settings are provided in the environment'
def test_builtin_with_environment(self):
args = ['sqlall', 'admin_scripts'] (out, err) = self.run_manage(args, 'settings') self.assertNoOutput(err) self.assertOutput(out, 'CREATE TABLE')
'fulldefault: manage.py builtin commands succeed if settings file (from argument) doesn\'t exist'
def test_builtin_with_bad_settings(self):
args = ['sqlall', '--settings=bad_settings', 'admin_scripts'] (out, err) = self.run_manage(args) self.assertNoOutput(out) self.assertOutput(err, "Could not import settings 'bad_settings'")
'fulldefault: manage.py builtin commands fail if settings file (from environment) doesn\'t exist'
def test_builtin_with_bad_environment(self):
args = ['sqlall', 'admin_scripts'] (out, err) = self.run_manage(args, 'bad_settings') self.assertNoOutput(err) self.assertOutput(out, 'CREATE TABLE')
'fulldefault: manage.py can execute user commands when default settings are appropriate'
def test_custom_command(self):
args = ['noargs_command'] (out, err) = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, 'EXECUTE:NoArgsCommand')
'fulldefault: manage.py can execute user commands when settings are provided as argument'
def test_custom_command_with_settings(self):
args = ['noargs_command', '--settings=settings'] (out, err) = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, 'EXECUTE:NoArgsCommand')
'fulldefault: manage.py can execute user commands when settings are provided in environment'
def test_custom_command_with_environment(self):
args = ['noargs_command'] (out, err) = self.run_manage(args, 'settings') self.assertNoOutput(err) self.assertOutput(out, 'EXECUTE:NoArgsCommand')
'minimal: manage.py builtin commands fail with an import error when no settings provided'
def test_builtin_command(self):
args = ['sqlall', 'admin_scripts'] (out, err) = self.run_manage(args) self.assertNoOutput(out) self.assertOutput(err, 'App with label admin_scripts could not be found')
'minimal: manage.py builtin commands fail if settings are provided as argument'
def test_builtin_with_settings(self):
args = ['sqlall', '--settings=settings', 'admin_scripts'] (out, err) = self.run_manage(args) self.assertNoOutput(out) self.assertOutput(err, 'App with label admin_scripts could not be found')
'minimal: manage.py builtin commands fail if settings are provided in the environment'
def test_builtin_with_environment(self):
args = ['sqlall', 'admin_scripts'] (out, err) = self.run_manage(args, 'settings') self.assertNoOutput(out) self.assertOutput(err, 'App with label admin_scripts could not be found')
'minimal: manage.py builtin commands fail if settings file (from argument) doesn\'t exist'
def test_builtin_with_bad_settings(self):
args = ['sqlall', '--settings=bad_settings', 'admin_scripts'] (out, err) = self.run_manage(args) self.assertNoOutput(out) self.assertOutput(err, "Could not import settings 'bad_settings'")
'minimal: manage.py builtin commands fail if settings file (from environment) doesn\'t exist'
def test_builtin_with_bad_environment(self):
args = ['sqlall', 'admin_scripts'] (out, err) = self.run_manage(args, 'bad_settings') self.assertNoOutput(out) self.assertOutput(err, 'App with label admin_scripts could not be found')
'minimal: manage.py can\'t execute user commands without appropriate settings'
def test_custom_command(self):
args = ['noargs_command'] (out, err) = self.run_manage(args) self.assertNoOutput(out) self.assertOutput(err, "Unknown command: 'noargs_command'")
'minimal: manage.py can\'t execute user commands, even if settings are provided as argument'
def test_custom_command_with_settings(self):
args = ['noargs_command', '--settings=settings'] (out, err) = self.run_manage(args) self.assertNoOutput(out) self.assertOutput(err, "Unknown command: 'noargs_command'")
'minimal: manage.py can\'t execute user commands, even if settings are provided in environment'
def test_custom_command_with_environment(self):
args = ['noargs_command'] (out, err) = self.run_manage(args, 'settings') self.assertNoOutput(out) self.assertOutput(err, "Unknown command: 'noargs_command'")
'alternate: manage.py builtin commands fail with an import error when no default settings provided'
def test_builtin_command(self):
args = ['sqlall', 'admin_scripts'] (out, err) = self.run_manage(args) self.assertNoOutput(out) self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'")
'alternate: manage.py builtin commands fail if settings are provided as argument but no defaults'
def test_builtin_with_settings(self):
args = ['sqlall', '--settings=alternate_settings', 'admin_scripts'] (out, err) = self.run_manage(args) self.assertNoOutput(out) self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'")
'alternate: manage.py builtin commands fail if settings are provided in the environment but no defaults'
def test_builtin_with_environment(self):
args = ['sqlall', 'admin_scripts'] (out, err) = self.run_manage(args, 'alternate_settings') self.assertNoOutput(out) self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'")
'alternate: manage.py builtin commands fail if settings file (from argument) doesn\'t exist'
def test_builtin_with_bad_settings(self):
args = ['sqlall', '--settings=bad_settings', 'admin_scripts'] (out, err) = self.run_manage(args) self.assertNoOutput(out) self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'")
'alternate: manage.py builtin commands fail if settings file (from environment) doesn\'t exist'
def test_builtin_with_bad_environment(self):
args = ['sqlall', 'admin_scripts'] (out, err) = self.run_manage(args, 'bad_settings') self.assertNoOutput(out) self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'")
'alternate: manage.py can\'t execute user commands'
def test_custom_command(self):
args = ['noargs_command'] (out, err) = self.run_manage(args) self.assertNoOutput(out) self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'")
'alternate: manage.py can\'t execute user commands, even if settings are provided as argument'
def test_custom_command_with_settings(self):
args = ['noargs_command', '--settings=alternate_settings'] (out, err) = self.run_manage(args) self.assertNoOutput(out) self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'")
'alternate: manage.py can\'t execute user commands, even if settings are provided in environment'
def test_custom_command_with_environment(self):
args = ['noargs_command'] (out, err) = self.run_manage(args, 'alternate_settings') self.assertNoOutput(out) self.assertOutput(err, "Can't find the file 'settings.py' in the directory containing './manage.py'")
'multiple: manage.py builtin commands fail with an import error when no settings provided'
def test_builtin_command(self):
args = ['sqlall', 'admin_scripts'] (out, err) = self.run_manage(args) self.assertNoOutput(out) self.assertOutput(err, 'App with label admin_scripts could not be found.')
'multiple: manage.py builtin commands succeed if settings are provided as argument'
def test_builtin_with_settings(self):
args = ['sqlall', '--settings=alternate_settings', 'admin_scripts'] (out, err) = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, 'CREATE TABLE')
'multiple: manage.py builtin commands fail if settings are provided in the environment'
def test_builtin_with_environment(self):
args = ['sqlall', 'admin_scripts'] (out, err) = self.run_manage(args, 'alternate_settings') self.assertNoOutput(out) self.assertOutput(err, 'App with label admin_scripts could not be found.')
'multiple: manage.py builtin commands fail if settings file (from argument) doesn\'t exist'
def test_builtin_with_bad_settings(self):
args = ['sqlall', '--settings=bad_settings', 'admin_scripts'] (out, err) = self.run_manage(args) self.assertNoOutput(out) self.assertOutput(err, "Could not import settings 'bad_settings'")
'multiple: manage.py builtin commands fail if settings file (from environment) doesn\'t exist'
def test_builtin_with_bad_environment(self):
args = ['sqlall', 'admin_scripts'] (out, err) = self.run_manage(args, 'bad_settings') self.assertNoOutput(out) self.assertOutput(err, 'App with label admin_scripts could not be found')
'multiple: manage.py can\'t execute user commands using default settings'
def test_custom_command(self):
args = ['noargs_command'] (out, err) = self.run_manage(args) self.assertNoOutput(out) self.assertOutput(err, "Unknown command: 'noargs_command'")
'multiple: manage.py can execute user commands if settings are provided as argument'
def test_custom_command_with_settings(self):
args = ['noargs_command', '--settings=alternate_settings'] (out, err) = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, 'EXECUTE:NoArgsCommand')
'multiple: manage.py can execute user commands if settings are provided in environment'
def test_custom_command_with_environment(self):
args = ['noargs_command'] (out, err) = self.run_manage(args, 'alternate_settings') self.assertNoOutput(out) self.assertOutput(err, "Unknown command: 'noargs_command'")
'import error: manage.py builtin commands shows useful diagnostic info when settings with import errors is provided'
def test_builtin_command(self):
args = ['sqlall', 'admin_scripts'] (out, err) = self.run_manage(args) self.assertNoOutput(out) self.assertOutput(err, 'ImportError: No module named foo42bar')
'manage.py validate reports an error on a non-existent app in INSTALLED_APPS'
def test_nonexistent_app(self):
self.write_settings('settings.py', apps=['admin_scriptz.broken_app'], sdict={'USE_I18N': False}) args = ['validate'] (out, err) = self.run_manage(args) self.assertNoOutput(out) self.assertOutput(err, 'No module named admin_scriptz')
'manage.py validate reports an ImportError if an app\'s models.py raises one on import'
def test_broken_app(self):
self.write_settings('settings.py', apps=['admin_scripts.broken_app']) args = ['validate'] (out, err) = self.run_manage(args) self.assertNoOutput(out) self.assertOutput(err, 'ImportError')
'manage.py validate does not raise an ImportError validating a complex app with nested calls to load_app'
def test_complex_app(self):
self.write_settings('settings.py', apps=['admin_scripts.complex_app', 'admin_scripts.simple_app'], sdict={'DEBUG': True}) args = ['validate'] (out, err) = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, '0 errors found')
'manage.py validate does not raise errors when an app imports a base class that itself has an abstract base'
def test_app_with_import(self):
self.write_settings('settings.py', apps=['admin_scripts.app_with_import', 'django.contrib.comments'], sdict={'DEBUG': True}) args = ['validate'] (out, err) = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, '0 errors found')
'--version is handled as a special case'
def test_version(self):
args = ['--version'] (out, err) = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, get_version().split('-')[0])
'--help is handled as a special case'
def test_help(self):
args = ['--help'] (out, err) = self.run_manage(args) if (sys.version_info < (2, 5)): self.assertOutput(out, 'usage: manage.py subcommand [options] [args]') else: self.assertOutput(out, 'Usage: manage.py subcommand [options] [args]') self.assertOutput(err, "Typ...
'-h is handled as a short form of --help'
def test_short_help(self):
args = ['-h'] (out, err) = self.run_manage(args) if (sys.version_info < (2, 5)): self.assertOutput(out, 'usage: manage.py subcommand [options] [args]') else: self.assertOutput(out, 'Usage: manage.py subcommand [options] [args]') self.assertOutput(err, "Type ...
'--help can be used on a specific command'
def test_specific_help(self):
args = ['sqlall', '--help'] (out, err) = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, 'Prints the CREATE TABLE, custom SQL and CREATE INDEX SQL statements for the given model module name(s).')
'User BaseCommands can execute when a label is provided'
def test_base_command(self):
args = ['base_command', 'testlabel'] (out, err) = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', '1'), ('option_b', '2'), ('option_c', '3'), ('pythonpath', None), ('settings', None),...
'User BaseCommands can execute when no labels are provided'
def test_base_command_no_label(self):
args = ['base_command'] (out, err) = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, "EXECUTE:BaseCommand labels=(), options=[('option_a', '1'), ('option_b', '2'), ('option_c', '3'), ('pythonpath', None), ('settings', None), ('traceback', None...
'User BaseCommands can execute when no labels are provided'
def test_base_command_multiple_label(self):
args = ['base_command', 'testlabel', 'anotherlabel'] (out, err) = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel', 'anotherlabel'), options=[('option_a', '1'), ('option_b', '2'), ('option_c', '3'), ('pythonpath', ...
'User BaseCommands can execute with options when a label is provided'
def test_base_command_with_option(self):
args = ['base_command', 'testlabel', '--option_a=x'] (out, err) = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', 'x'), ('option_b', '2'), ('option_c', '3'), ('pythonpath', None), ('sett...
'User BaseCommands can execute with multiple options when a label is provided'
def test_base_command_with_options(self):
args = ['base_command', 'testlabel', '-a', 'x', '--option_b=y'] (out, err) = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', 'x'), ('option_b', 'y'), ('option_c', '3'), ('pythonpath', None)...
'NoArg Commands can be executed'
def test_noargs(self):
args = ['noargs_command'] (out, err) = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, "EXECUTE:NoArgsCommand options=[('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
'NoArg Commands raise an error if an argument is provided'
def test_noargs_with_args(self):
args = ['noargs_command', 'argument'] (out, err) = self.run_manage(args) self.assertOutput(err, "Error: Command doesn't accept any arguments")
'User AppCommands can execute when a single app name is provided'
def test_app_command(self):
args = ['app_command', 'auth'] (out, err) = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, "EXECUTE:AppCommand app=<module 'django.contrib.auth.models'") self.assertOutput(out, os.sep.join(['django', 'contrib', 'auth', 'models.py'])) self.assertOutput(out, "'>, op...
'User AppCommands raise an error when no app name is provided'
def test_app_command_no_apps(self):
args = ['app_command'] (out, err) = self.run_manage(args) self.assertOutput(err, 'Error: Enter at least one appname.')
'User AppCommands raise an error when multiple app names are provided'
def test_app_command_multiple_apps(self):
args = ['app_command', 'auth', 'contenttypes'] (out, err) = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, "EXECUTE:AppCommand app=<module 'django.contrib.auth.models'") self.assertOutput(out, os.sep.join(['django', 'contrib', 'auth', 'models.py'])) self.assertOutput...
'User AppCommands can execute when a single app name is provided'
def test_app_command_invalid_appname(self):
args = ['app_command', 'NOT_AN_APP'] (out, err) = self.run_manage(args) self.assertOutput(err, 'App with label NOT_AN_APP could not be found')
'User AppCommands can execute when some of the provided app names are invalid'
def test_app_command_some_invalid_appnames(self):
args = ['app_command', 'auth', 'NOT_AN_APP'] (out, err) = self.run_manage(args) self.assertOutput(err, 'App with label NOT_AN_APP could not be found')
'User LabelCommands can execute when a label is provided'
def test_label_command(self):
args = ['label_command', 'testlabel'] (out, err) = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, "EXECUTE:LabelCommand label=testlabel, options=[('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]")
'User LabelCommands raise an error if no label is provided'
def test_label_command_no_label(self):
args = ['label_command'] (out, err) = self.run_manage(args) self.assertOutput(err, 'Enter at least one label')
'User LabelCommands are executed multiple times if multiple labels are provided'
def test_label_command_multiple_label(self):
args = ['label_command', 'testlabel', 'anotherlabel'] (out, err) = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, "EXECUTE:LabelCommand label=testlabel, options=[('pythonpath', None), ('settings', None), ('traceback', None), ('verbosity', '1')]") sel...
'Options passed after settings are correctly handled'
def test_setting_then_option(self):
args = ['base_command', 'testlabel', '--settings=alternate_settings', '--option_a=x'] (out, err) = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', 'x'), ('option_b', '2'), ('option_c', '3'), (...
'Short options passed after settings are correctly handled'
def test_setting_then_short_option(self):
args = ['base_command', 'testlabel', '--settings=alternate_settings', '--option_a=x'] (out, err) = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', 'x'), ('option_b', '2'), ('option_c', '3'), (...
'Options passed before settings are correctly handled'
def test_option_then_setting(self):
args = ['base_command', 'testlabel', '--option_a=x', '--settings=alternate_settings'] (out, err) = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', 'x'), ('option_b', '2'), ('option_c', '3'), (...
'Short options passed before settings are correctly handled'
def test_short_option_then_setting(self):
args = ['base_command', 'testlabel', '-a', 'x', '--settings=alternate_settings'] (out, err) = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', 'x'), ('option_b', '2'), ('option_c', '3'), ('pyth...
'Options are correctly handled when they are passed before and after a setting'
def test_option_then_setting_then_option(self):
args = ['base_command', 'testlabel', '--option_a=x', '--settings=alternate_settings', '--option_b=y'] (out, err) = self.run_manage(args) self.assertNoOutput(err) self.assertOutput(out, "EXECUTE:BaseCommand labels=('testlabel',), options=[('option_a', 'x'), ('option_b', 'y'), ('option_c...
'Can find a file in a STATICFILES_DIRS directory.'
def test_staticfiles_dirs(self):
self.assertFileContains('test.txt', 'Can we find')
'Can find a file in a subdirectory of a STATICFILES_DIRS directory.'
def test_staticfiles_dirs_subdir(self):
self.assertFileContains('subdir/test.txt', 'Can we find')
'File in STATICFILES_DIRS has priority over file in app.'
def test_staticfiles_dirs_priority(self):
self.assertFileContains('test/file.txt', 'STATICFILES_DIRS')
'Can find a file in an app static/ directory.'
def test_app_files(self):
self.assertFileContains('test/file1.txt', 'file1 in the app dir')
'Can find a file with non-ASCII character in an app static/ directory.'
def test_nonascii_filenames(self):
self.assertFileContains(u'test/fi\u015fier.txt', u'fi\u015fier in the app dir')
'Can find a file with capital letters.'
def test_camelcase_filenames(self):
self.assertFileContains(u'test/camelCase.txt', u'camelCase')
'Test that findstatic returns all candidate files if run without --first.'
def test_all_files(self):
_stdout = sys.stdout sys.stdout = StringIO() try: call_command('findstatic', 'test/file.txt', verbosity='0') sys.stdout.seek(0) lines = [l.strip() for l in sys.stdout.readlines()] finally: sys.stdout = _stdout self.assertEqual(len(lines), 3) self.assertTrue(('proj...
'Test that -i patterns are ignored.'
def test_ignore(self):
self.assertFileNotFound('test/test.ignoreme')
'Common ignore patterns (*~, .*, CVS) are ignored.'
def test_common_ignore_patterns(self):
self.assertFileNotFound('test/.hidden') self.assertFileNotFound('test/backup~') self.assertFileNotFound('test/CVS')
'With --no-default-ignore, common ignore patterns (*~, .*, CVS) are not ignored.'
def test_no_common_ignore_patterns(self):
self.assertFileContains('test/.hidden', 'should be ignored') self.assertFileContains('test/backup~', 'should be ignored') self.assertFileContains('test/CVS', 'should be ignored')
'Make sure no files were create in the destination directory.'
def test_no_files_created(self):
self.assertEqual(os.listdir(settings.STATIC_ROOT), [])
'When no prior CSRF cookie exists, check that the cookie is created and a token is inserted.'
def test_process_response_no_csrf_cookie(self):
req = self._get_GET_no_csrf_cookie_request() CsrfMiddleware().process_view(req, post_form_view, (), {}) resp = post_form_response() resp_content = resp.content resp2 = CsrfMiddleware().process_response(req, resp) csrf_cookie = resp2.cookies.get(settings.CSRF_COOKIE_NAME, False) self.assertNo...
'Check that a view decorated with \'csrf_view_exempt\' is still post-processed to add the CSRF token.'
def test_process_response_for_exempt_view(self):
req = self._get_GET_no_csrf_cookie_request() CsrfMiddleware().process_view(req, csrf_view_exempt(post_form_view), (), {}) resp = post_form_response() resp_content = resp.content resp2 = CsrfMiddleware().process_response(req, resp) csrf_cookie = resp2.cookies.get(settings.CSRF_COOKIE_NAME, False)...
'When no prior CSRF cookie exists, check that the cookie is created, even if only CsrfViewMiddleware is used.'
def test_process_response_no_csrf_cookie_view_only_get_token_used(self):
req = self._get_GET_no_csrf_cookie_request() CsrfViewMiddleware().process_view(req, token_view, (), {}) resp = token_view(req) resp2 = CsrfViewMiddleware().process_response(req, resp) csrf_cookie = resp2.cookies.get(settings.CSRF_COOKIE_NAME, False) self.assertNotEqual(csrf_cookie, False)
'Check that if get_token() is not called, the view middleware does not add a cookie.'
def test_process_response_get_token_not_used(self):
req = self._get_GET_no_csrf_cookie_request() CsrfViewMiddleware().process_view(req, non_token_view_using_request_processor, (), {}) resp = non_token_view_using_request_processor(req) resp2 = CsrfViewMiddleware().process_response(req, resp) csrf_cookie = resp2.cookies.get(settings.CSRF_COOKIE_NAME, F...
'Check that the token is inserted when a prior CSRF cookie exists'
def test_process_response_existing_csrf_cookie(self):
req = self._get_GET_csrf_cookie_request() CsrfMiddleware().process_view(req, post_form_view, (), {}) resp = post_form_response() resp_content = resp.content resp2 = CsrfMiddleware().process_response(req, resp) self.assertNotEqual(resp_content, resp2.content) self._check_token_present(resp2)
'Check the the post-processor does nothing for content-types not in _HTML_TYPES.'
def test_process_response_non_html(self):
req = self._get_GET_no_csrf_cookie_request() CsrfMiddleware().process_view(req, post_form_view, (), {}) resp = post_form_response_non_html() resp_content = resp.content resp2 = CsrfMiddleware().process_response(req, resp) self.assertEqual(resp_content, resp2.content)
'Check that no post processing is done for an exempt view'
def test_process_response_exempt_view(self):
req = self._get_GET_csrf_cookie_request() view = csrf_exempt(post_form_view) CsrfMiddleware().process_view(req, view, (), {}) resp = view(req) resp_content = resp.content resp2 = CsrfMiddleware().process_response(req, resp) self.assertEqual(resp_content, resp2.content)
'Check that if neither a CSRF cookie nor a session cookie are present, the middleware rejects the incoming request. This will stop login CSRF.'
def test_process_request_no_session_no_csrf_cookie(self):
req = self._get_POST_no_csrf_cookie_request() req2 = CsrfMiddleware().process_view(req, post_form_view, (), {}) self.assertEqual(403, req2.status_code)
'Check that if a CSRF cookie is present but no token, the middleware rejects the incoming request.'
def test_process_request_csrf_cookie_no_token(self):
req = self._get_POST_csrf_cookie_request() req2 = CsrfMiddleware().process_view(req, post_form_view, (), {}) self.assertEqual(403, req2.status_code)
'Check that if both a cookie and a token is present, the middleware lets it through.'
def test_process_request_csrf_cookie_and_token(self):
req = self._get_POST_request_with_token() req2 = CsrfMiddleware().process_view(req, post_form_view, (), {}) self.assertEqual(None, req2)
'When no CSRF cookie exists, but the user has a session, check that a token using the session cookie as a legacy CSRF cookie is accepted.'
def test_process_request_session_cookie_no_csrf_cookie_token(self):
orig_secret_key = settings.SECRET_KEY settings.SECRET_KEY = self._secret_key_for_session_test try: req = self._get_POST_session_request_with_token() req2 = CsrfMiddleware().process_view(req, post_form_view, (), {}) self.assertEqual(None, req2) finally: settings.SECRET_KEY...
'Check that if a session cookie is present but no token and no CSRF cookie, the request is rejected.'
def test_process_request_session_cookie_no_csrf_cookie_no_token(self):
req = self._get_POST_session_request_no_token() req2 = CsrfMiddleware().process_view(req, post_form_view, (), {}) self.assertEqual(403, req2.status_code)
'Check that if a CSRF cookie is present and no token, but the csrf_exempt decorator has been applied to the view, the middleware lets it through'
def test_process_request_csrf_cookie_no_token_exempt_view(self):
req = self._get_POST_csrf_cookie_request() req2 = CsrfMiddleware().process_view(req, csrf_exempt(post_form_view), (), {}) self.assertEqual(None, req2)
'Check that we can pass in the token in a header instead of in the form'
def test_csrf_token_in_header(self):
req = self._get_POST_csrf_cookie_request() req.META['HTTP_X_CSRFTOKEN'] = self._csrf_id req2 = CsrfMiddleware().process_view(req, post_form_view, (), {}) self.assertEqual(None, req2)
'Check that CsrfTokenNode works when no CSRF cookie is set'
def test_token_node_no_csrf_cookie(self):
req = self._get_GET_no_csrf_cookie_request() resp = token_view(req) self.assertEqual(u'', resp.content)
'Check that we get a new token if the csrf_cookie is the empty string'
def test_token_node_empty_csrf_cookie(self):
req = self._get_GET_no_csrf_cookie_request() req.COOKIES[settings.CSRF_COOKIE_NAME] = '' CsrfViewMiddleware().process_view(req, token_view, (), {}) resp = token_view(req) self.assertNotEqual(u'', resp.content)
'Check that CsrfTokenNode works when a CSRF cookie is set'
def test_token_node_with_csrf_cookie(self):
req = self._get_GET_csrf_cookie_request() CsrfViewMiddleware().process_view(req, token_view, (), {}) resp = token_view(req) self._check_token_present(resp)
'Check that get_token still works for a view decorated with \'csrf_view_exempt\'.'
def test_get_token_for_exempt_view(self):
req = self._get_GET_csrf_cookie_request() CsrfViewMiddleware().process_view(req, csrf_view_exempt(token_view), (), {}) resp = token_view(req) self._check_token_present(resp)
'Check that get_token works for a view decorated solely with requires_csrf_token'
def test_get_token_for_requires_csrf_token_view(self):
req = self._get_GET_csrf_cookie_request() resp = requires_csrf_token(token_view)(req) self._check_token_present(resp)
'Check that CsrfTokenNode works when a CSRF cookie is created by the middleware (when one was not already present)'
def test_token_node_with_new_csrf_cookie(self):
req = self._get_GET_no_csrf_cookie_request() CsrfViewMiddleware().process_view(req, token_view, (), {}) resp = token_view(req) resp2 = CsrfViewMiddleware().process_response(req, resp) csrf_cookie = resp2.cookies[settings.CSRF_COOKIE_NAME] self._check_token_present(resp, csrf_id=csrf_cookie.value...
'Check that CsrfResponseMiddleware finishes without error if the view middleware has not been called, as is the case if a request middleware returns a response.'
def test_response_middleware_without_view_middleware(self):
req = self._get_GET_no_csrf_cookie_request() resp = post_form_view(req) CsrfMiddleware().process_response(req, resp)
'Test that a POST HTTPS request with a bad referer is rejected'
def test_https_bad_referer(self):
req = self._get_POST_request_with_token() req._is_secure = True req.META['HTTP_HOST'] = 'www.example.com' req.META['HTTP_REFERER'] = 'https://www.evil.org/somepage' req2 = CsrfViewMiddleware().process_view(req, post_form_view, (), {}) self.assertNotEqual(None, req2) self.assertEqual(403, req...