Last active
August 4, 2016 18:38
-
-
Save smcoll/5950470707677f250d95e3a59458760e to your computer and use it in GitHub Desktop.
test for output of `manage.py check --deploy`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from subprocess import check_call | |
from tempfile import TemporaryFile | |
from django.test import TestCase | |
class SecurityHealthCheck(TestCase): | |
""" Run the deploy check on each settings file for a live deployment. If there are any warnings, fail and report. | |
NOTE: In Django 1.10 we can set `--fail-level` and exit on the error code instead of a text search: | |
https://docs.djangoproject.com/en/1.10/ref/django-admin/#cmdoption-check--fail-level | |
""" | |
def get_check_output_for_settings(self, settings_file): | |
with TemporaryFile() as output: | |
args = [sys.executable, './manage.py', 'check', '--deploy', '--settings', settings_file] | |
check_call(args, stdout=output) | |
output.seek(0) | |
return output.read() | |
def test_security_check_on_production_settings(self): | |
val = self.get_check_output_for_settings('foo.settings.production') | |
if 'System check identified no issues' not in val: | |
self.fail("There are issues:\n %s" % val) | |
def test_security_check_on_staging_settings(self): | |
val = self.get_check_output_for_settings('foo.settings.staging') | |
if 'System check identified no issues' not in val: | |
self.fail("There are issues:\n %s" % val) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment