Skip to content

Commit c05a9c5

Browse files
committed
Block make reset with a safety setting
1 parent 5ab6637 commit c05a9c5

4 files changed

Lines changed: 34 additions & 2 deletions

File tree

api/app/settings/common.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,3 +1471,7 @@
14711471
PYLON_IDENTITY_VERIFICATION_SECRET = env.str("PYLON_IDENTITY_VERIFICATION_SECRET", None)
14721472

14731473
OSIC_UPDATE_BATCH_SIZE = env.int("OSIC_UPDATE_BATCH_SIZE", default=500)
1474+
1475+
# Allow the reset_local_database management command to run.
1476+
# This command flushes and seeds the database with test data.
1477+
ENABLE_LOCAL_DATABASE_RESET = False

api/app/settings/local.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@
1818
ENABLE_ADMIN_ACCESS_USER_PASS = True
1919

2020
SKIP_MIGRATION_TESTS = True
21+
22+
ENABLE_LOCAL_DATABASE_RESET = True

api/core/management/commands/reset_local_database.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import json
22
from typing import Any
33

4-
from django.core.management import BaseCommand, call_command
4+
from django.conf import settings
5+
from django.core.management import BaseCommand, CommandError, call_command
56
from django.urls import reverse
67
from rest_framework.test import APIClient
78

@@ -10,6 +11,11 @@ class Command(BaseCommand):
1011
help = "Resets and seeds the database with test data for local development"
1112

1213
def handle(self, *args: Any, **kwargs: Any) -> None:
14+
if not settings.ENABLE_LOCAL_DATABASE_RESET:
15+
raise CommandError(
16+
"This command is disabled. "
17+
"Set ENABLE_LOCAL_DATABASE_RESET to True in Django settings to enable it."
18+
)
1319
self.stdout.write("Flushing database...")
1420
call_command("flush", "--noinput", verbosity=0)
1521

api/tests/integration/core/test_reset_local_database.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from unittest.mock import MagicMock
22

33
import pytest
4-
from django.core.management import call_command
4+
from django.core.management import CommandError, call_command
5+
from pytest_django.fixtures import SettingsWrapper
56
from pytest_mock import MockerFixture
67

78
from environments.identities.models import Identity
@@ -21,6 +22,12 @@
2122
pytestmark = pytest.mark.django_db
2223

2324

25+
@pytest.fixture(autouse=True)
26+
def enable_local_database_reset(settings: SettingsWrapper) -> None:
27+
"""Enable the reset_local_database command for tests."""
28+
settings.ENABLE_LOCAL_DATABASE_RESET = True
29+
30+
2431
@pytest.fixture(autouse=True)
2532
def mock_reset_commands(mocker: MockerFixture) -> MagicMock:
2633
"""Mock flush/migrate/createcachetable to avoid resetting the test database."""
@@ -99,3 +106,16 @@ def test_reset_local_database__creates_expected_data() -> None:
99106
identifiers = list(Identity.objects.values_list("identifier", flat=True))
100107
assert "alice@example.com" in identifiers
101108
assert "bob@example.com" in identifiers
109+
110+
111+
def test_reset_local_database__raises_error_when_disabled(
112+
settings: SettingsWrapper,
113+
) -> None:
114+
# Given
115+
settings.ENABLE_LOCAL_DATABASE_RESET = False
116+
117+
# When / Then
118+
with pytest.raises(CommandError) as exc_info:
119+
call_command("reset_local_database")
120+
121+
assert "ENABLE_LOCAL_DATABASE_RESET" in str(exc_info.value)

0 commit comments

Comments
 (0)