43 lines
1012 B
Python
43 lines
1012 B
Python
import pytest
|
|
|
|
from app.crud import user as user_crud
|
|
from app.models.user import UserRole, UserStatus
|
|
|
|
|
|
class _ScalarResult:
|
|
def __init__(self, value):
|
|
self._value = value
|
|
|
|
def scalar_one_or_none(self):
|
|
return self._value
|
|
|
|
|
|
class _FakeSession:
|
|
def __init__(self, existing_admin=None):
|
|
self.existing_admin = existing_admin
|
|
self.added = []
|
|
self.commit_count = 0
|
|
|
|
async def execute(self, _stmt):
|
|
return _ScalarResult(self.existing_admin)
|
|
|
|
def add(self, obj):
|
|
self.added.append(obj)
|
|
|
|
async def commit(self):
|
|
self.commit_count += 1
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_ensure_admin_exists_creates_protected_admin():
|
|
session = _FakeSession()
|
|
|
|
await user_crud.ensure_admin_exists(session)
|
|
|
|
assert len(session.added) == 1
|
|
admin = session.added[0]
|
|
assert admin.email == "admin@huapont.cn"
|
|
assert admin.role == UserRole.ADMIN
|
|
assert admin.status == UserStatus.ACTIVE
|
|
assert session.commit_count == 1
|