management: Refactor checkconfig code to live in library.

This makes it possible to call this from other management commands.
This commit is contained in:
Tim Abbott
2018-05-03 16:04:12 -07:00
parent fd06380701
commit 1ec0414786
3 changed files with 21 additions and 10 deletions

View File

@@ -3,6 +3,7 @@
import sys
from argparse import ArgumentParser
from django.conf import settings
from django.core.exceptions import MultipleObjectsReturned
from django.core.management.base import BaseCommand, CommandError
from typing import Any, Dict, Optional, Text, List
@@ -16,6 +17,16 @@ def is_integer_string(val: str) -> bool:
except ValueError:
return False
def check_config() -> None:
for (setting_name, default) in settings.REQUIRED_SETTINGS:
try:
if settings.__getattr__(setting_name) != default:
continue
except AttributeError:
pass
raise CommandError("Error: You must set %s in /etc/zulip/settings.py." % (setting_name,))
class ZulipBaseCommand(BaseCommand):
def add_realm_args(self, parser: ArgumentParser, required: bool=False,
help: Optional[str]=None) -> None:

View File

@@ -5,16 +5,10 @@ from typing import Any
from django.conf import settings
from django.core.management.base import BaseCommand
from zerver.lib.management import check_config
class Command(BaseCommand):
help = """Checks your Zulip Voyager Django configuration for issues."""
def handle(self, *args: Any, **options: Any) -> None:
for (setting_name, default) in settings.REQUIRED_SETTINGS:
try:
if settings.__getattr__(setting_name) != default:
continue
except AttributeError:
pass
print("Error: You must set %s in /etc/zulip/settings.py." % (setting_name,))
sys.exit(1)
check_config()

View File

@@ -11,7 +11,7 @@ from django.conf import settings
from django.core.management import call_command
from django.test import TestCase, override_settings
from zerver.lib.actions import do_create_user
from zerver.lib.management import ZulipBaseCommand, CommandError
from zerver.lib.management import ZulipBaseCommand, CommandError, check_config
from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import stdout_suppressed
from zerver.lib.test_runner import slow
@@ -20,6 +20,12 @@ from zerver.models import get_user_profile_by_email
from zerver.models import get_realm, UserProfile, Realm
from confirmation.models import RealmCreationKey, generate_realm_creation_url
class TestCheckConfig(ZulipTestCase):
def test_check_config(self) -> None:
with self.assertRaisesRegex(CommandError, "Error: You must set ZULIP_ADMINISTRATOR in /etc/zulip/settings.py."):
check_config()
class TestZulipBaseCommand(ZulipTestCase):
def setUp(self) -> None:
self.zulip_realm = get_realm("zulip")