test: 全面迁移 pytest 并加入格式门禁
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import unittest
|
||||
import pytest
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
@@ -10,22 +10,24 @@ from web.backend.routers.accounts import list_accounts, set_account_tag
|
||||
from web.backend.schemas import AccountTag
|
||||
|
||||
|
||||
class AccountSensitiveFieldsTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
class TestAccountSensitiveFields:
|
||||
def setup_method(self):
|
||||
self.engine = create_engine("sqlite://")
|
||||
Base.metadata.create_all(self.engine)
|
||||
self.session = sessionmaker(bind=self.engine)()
|
||||
self.admin = User(username="admin", password_hash="hash", role="super_admin")
|
||||
self.session.add(self.admin)
|
||||
self.session.add(Account(
|
||||
username="account",
|
||||
password="account-password",
|
||||
email="account@example.com",
|
||||
email_password="email-password",
|
||||
))
|
||||
self.session.add(
|
||||
Account(
|
||||
username="account",
|
||||
password="account-password",
|
||||
email="account@example.com",
|
||||
email_password="email-password",
|
||||
)
|
||||
)
|
||||
self.session.commit()
|
||||
|
||||
def tearDown(self):
|
||||
def teardown_method(self):
|
||||
self.session.close()
|
||||
Base.metadata.drop_all(self.engine)
|
||||
self.engine.dispose()
|
||||
@@ -44,9 +46,9 @@ class AccountSensitiveFieldsTests(unittest.TestCase):
|
||||
)
|
||||
|
||||
item = result["items"][0]
|
||||
self.assertIsNone(item.password)
|
||||
self.assertIsNone(item.email)
|
||||
self.assertIsNone(item.email_password)
|
||||
assert item.password is None
|
||||
assert item.email is None
|
||||
assert item.email_password is None
|
||||
|
||||
def test_admin_can_explicitly_request_sensitive_fields(self):
|
||||
result = list_accounts(
|
||||
@@ -62,34 +64,42 @@ class AccountSensitiveFieldsTests(unittest.TestCase):
|
||||
)
|
||||
|
||||
item = result["items"][0]
|
||||
self.assertEqual(item.password, "account-password")
|
||||
self.assertEqual(item.email, "account@example.com")
|
||||
self.assertEqual(item.email_password, "email-password")
|
||||
assert item.password == "account-password"
|
||||
assert item.email == "account@example.com"
|
||||
assert item.email_password == "email-password"
|
||||
|
||||
def test_support_can_only_change_tags_on_assigned_accounts(self):
|
||||
support = User(username="support", password_hash="hash", role="support")
|
||||
other_support = User(username="other-support", password_hash="hash", role="support")
|
||||
other_support = User(
|
||||
username="other-support", password_hash="hash", role="support"
|
||||
)
|
||||
self.session.add_all([support, other_support])
|
||||
self.session.commit()
|
||||
assigned = Account(
|
||||
username="assigned", password="password", email="assigned@example.com",
|
||||
email_password="mail-password", assigned_to=support.id,
|
||||
username="assigned",
|
||||
password="password",
|
||||
email="assigned@example.com",
|
||||
email_password="mail-password",
|
||||
assigned_to=support.id,
|
||||
)
|
||||
other = Account(
|
||||
username="other", password="password", email="other@example.com",
|
||||
email_password="mail-password", assigned_to=other_support.id,
|
||||
username="other",
|
||||
password="password",
|
||||
email="other@example.com",
|
||||
email_password="mail-password",
|
||||
assigned_to=other_support.id,
|
||||
)
|
||||
self.session.add_all([assigned, other])
|
||||
self.session.commit()
|
||||
|
||||
set_account_tag(assigned.id, AccountTag(tag="客服组"), db=self.session, current=support)
|
||||
set_account_tag(
|
||||
assigned.id, AccountTag(tag="客服组"), db=self.session, current=support
|
||||
)
|
||||
self.session.refresh(assigned)
|
||||
self.assertEqual(assigned.tag, "客服组")
|
||||
assert assigned.tag == "客服组"
|
||||
|
||||
with self.assertRaises(HTTPException) as context:
|
||||
set_account_tag(other.id, AccountTag(tag="越权"), db=self.session, current=support)
|
||||
self.assertEqual(context.exception.status_code, 404)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
with pytest.raises(HTTPException) as context:
|
||||
set_account_tag(
|
||||
other.id, AccountTag(tag="越权"), db=self.session, current=support
|
||||
)
|
||||
assert context.value.status_code == 404
|
||||
|
||||
Reference in New Issue
Block a user