Skip to content

Commit

Permalink
Merge pull request #21 from logikal-io/make-hosts-configurable
Browse files Browse the repository at this point in the history
Make hosts configurable
  • Loading branch information
GergelyKalmar authored Jun 27, 2024
2 parents 88c5b04 + 1d9098e commit 2676ff3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 6 additions & 2 deletions django_logikal/management/commands/syncdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@
from django.core.management.base import BaseCommand, CommandError, CommandParser
from django.db import connections
from factory import random as factory_random
from logikal_utils.project import tool_config
from logikal_utils.random import DEFAULT_RANDOM_SEED

from django_logikal.local_data import LocalData

DEFAULT_ALLOWED_HOSTS = ('127.0.0.1', 'localhost', 'postgres')


class Command(BaseCommand):
help = ' '.join(__doc__.splitlines()[0:4])
Expand All @@ -40,8 +43,9 @@ def add_arguments(self, parser: CommandParser) -> None:
def handle(self, *_args: Any, **options: Any) -> None:
connection = connections['default']
database = connection.settings_dict
if database['HOST'] not in ('127.0.0.1', 'postgres'):
raise CommandError('Only the local database can be synchronized')
config = tool_config('django_logikal')
if database['HOST'] not in config.get('allowed_syncdb_hosts', DEFAULT_ALLOWED_HOSTS):
raise CommandError(f'Unallowed database host "{database["HOST"]}"')

database_url = f'{database["HOST"]}:{database["PORT"]}/{database["NAME"]}'
self.stdout.write(f'Synchronizing database {self.style.ERROR(database_url)}')
Expand Down
6 changes: 3 additions & 3 deletions tests/django_logikal/test_dynamic_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ def test_syncdb_cancelled(mocker: MockerFixture) -> None:
syncdb.Command().handle()


def test_syncdb_non_local_error(mocker: MockerFixture) -> None:
def test_syncdb_unallowed_error(mocker: MockerFixture) -> None:
connections = mocker.patch('django_logikal.management.commands.syncdb.connections')
connections.__getitem__.return_value.settings_dict = {'HOST': 'non-local'}
with raises(CommandError, match='local database'):
connections.__getitem__.return_value.settings_dict = {'HOST': 'unallowed'}
with raises(CommandError, match='Unallowed database'):
syncdb.Command().handle()


Expand Down

0 comments on commit 2676ff3

Please sign in to comment.