38 lines
1.0 KiB
Python
38 lines
1.0 KiB
Python
import pytest
|
|
from django.core.management import call_command
|
|
from django.core.management.base import CommandError
|
|
from django.test import override_settings
|
|
|
|
from accounts.models import User
|
|
|
|
|
|
@pytest.mark.django_db
|
|
@override_settings(DEBUG=True)
|
|
def test_init_local_admin_创建可登录管理员(client):
|
|
call_command(
|
|
"init_local_admin",
|
|
username="test_local_admin",
|
|
password="LocalAdmin2026!",
|
|
)
|
|
|
|
user = User.objects.get(username="test_local_admin")
|
|
assert user.is_staff is True
|
|
assert user.is_superuser is True
|
|
assert client.login(
|
|
username="test_local_admin",
|
|
password="LocalAdmin2026!",
|
|
)
|
|
|
|
|
|
@pytest.mark.django_db
|
|
@override_settings(DEBUG=False)
|
|
def test_init_local_admin_生产环境拒绝执行():
|
|
with pytest.raises(CommandError, match="仅允许"):
|
|
call_command(
|
|
"init_local_admin",
|
|
username="forbidden_admin",
|
|
password="LocalAdmin2026!",
|
|
)
|
|
|
|
assert not User.objects.filter(username="forbidden_admin").exists()
|