117 lines
7.8 KiB
Python
117 lines
7.8 KiB
Python
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from app.belki.applications import (MAX_APPLICATION_ATTACHMENTS, MAX_APPLICATION_FEATHERS,
|
|
ApplicationError, ApplicationStore, validate_application)
|
|
from app.belki.google_sheets_export import GoogleSheetsExportConfig, GoogleSheetsExportService
|
|
from app.belki.identity import IdentityDirectory
|
|
from app.belki.vk_bot import ApplicationDialog
|
|
|
|
|
|
class ApplicationTests(unittest.TestCase):
|
|
def test_kitten_requires_activity_screenshot(self) -> None:
|
|
with self.assertRaisesRegex(ApplicationError, "скриншот активности"):
|
|
validate_application({"type": "kitten", "name": "Бельчоночек", "character_id": 1676767})
|
|
|
|
def test_flying_squirrel_requires_previous_practice_comment(self) -> None:
|
|
with self.assertRaisesRegex(ApplicationError, "прошлой практики"):
|
|
validate_application({"type": "flying_squirrel", "name": "Муся", "character_id": 1337, "practiced_before": True})
|
|
|
|
def test_vk_link_accepts_a_domain_without_protocol(self) -> None:
|
|
for link in ("vk.com", "vk.ru", "https://vk.com", "https://vk.ru"):
|
|
with self.subTest(link=link):
|
|
application = validate_application({
|
|
"type": "flying_squirrel", "name": "Муся", "character_id": 1337,
|
|
"practiced_before": False, "vk_link": link,
|
|
})
|
|
expected = link if link.startswith("https://") else f"https://{link}"
|
|
self.assertEqual(application["vk_link"], expected)
|
|
|
|
def test_application_rejects_non_vk_or_insecure_profile_links(self) -> None:
|
|
for link in ("http://vk.com/id1", "https://vk.com.evil.test/id1", "https://example.test"):
|
|
with self.subTest(link=link), self.assertRaisesRegex(ApplicationError, "HTTPS-ссылку на страницу VK"):
|
|
validate_application({
|
|
"type": "flying_squirrel", "name": "Муся", "character_id": 1337,
|
|
"practiced_before": False, "vk_link": link,
|
|
})
|
|
|
|
def test_application_bounds_attachment_and_feather_fan_out(self) -> None:
|
|
too_many_attachments = [{"url": "https://img.example.test/photo.jpg"}] * (MAX_APPLICATION_ATTACHMENTS + 1)
|
|
with self.assertRaisesRegex(ApplicationError, "не более"):
|
|
validate_application({"type": "kitten", "name": "Муся", "character_id": 1337, "attachments": too_many_attachments})
|
|
too_many_feathers = [{"amount": 1, "color": "синее", "source": "запасы"}] * (MAX_APPLICATION_FEATHERS + 1)
|
|
with self.assertRaisesRegex(ApplicationError, "не более"):
|
|
validate_application({"type": "feather_donation", "name": "Муся", "character_id": 1337, "feathers": too_many_feathers})
|
|
|
|
def test_application_requires_https_attachment_and_practice_review_links(self) -> None:
|
|
with self.assertRaisesRegex(ApplicationError, "(?i)вложение.*https"):
|
|
validate_application({"type": "kitten", "name": "Муся", "character_id": 1337, "attachments": [{"url": "http://example.test/a.jpg"}]})
|
|
with self.assertRaisesRegex(ApplicationError, "(?i)комментарий.*https"):
|
|
validate_application({
|
|
"type": "flying_squirrel", "name": "Муся", "character_id": 1337,
|
|
"practiced_before": True, "practice_review_link": "http://example.test/review",
|
|
})
|
|
|
|
def test_feather_donation_is_saved_and_accepted(self) -> None:
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
store = ApplicationStore(sqlite_path=Path(directory) / "applications.sqlite3")
|
|
application = validate_application({
|
|
"type": "feather_donation", "name": "Белка", "character_id": 58775,
|
|
"feathers": [{"amount": 2, "color": "синие", "source": "личные запасы"}, {"amount": 1, "color": "чёрное", "source": "осмотр дупла"}],
|
|
})
|
|
application_id = store.create(application=application, actor_id=10, reply_peer_id=10, source="vk")
|
|
self.assertEqual(store.list(status="pending")[0]["application"]["feathers"][0]["color"], "синие")
|
|
accepted = store.accept(application_id)
|
|
self.assertEqual(accepted["status"], "accepted")
|
|
self.assertEqual(store.pending_notifications()[0]["reply_peer_id"], 10)
|
|
store.mark_notified(application_id)
|
|
self.assertEqual(store.pending_notifications(), [])
|
|
|
|
def test_export_outbox_collects_create_and_accept_events_while_disabled(self) -> None:
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
store = ApplicationStore(sqlite_path=Path(directory) / "applications.sqlite3")
|
|
application = validate_application({
|
|
"type": "feather_donation", "name": "Белка", "character_id": 58775,
|
|
"feathers": [{"amount": 1, "color": "красное", "source": "личные запасы"}],
|
|
})
|
|
application_id = store.create(application=application, actor_id=None, reply_peer_id=None, source="web")
|
|
store.accept(application_id)
|
|
events = store.pending_exports()
|
|
self.assertEqual([event["event_type"] for event in events], ["application_created", "application_accepted"])
|
|
self.assertEqual(events[0]["payload"]["schema_version"], 1)
|
|
exporter = GoogleSheetsExportService(store, GoogleSheetsExportConfig(enabled=False, url="https://example.test", token=""))
|
|
self.assertEqual(exporter.sync_pending(), 0)
|
|
self.assertEqual(len(store.pending_exports()), 2)
|
|
|
|
def test_vk_kitten_application_uses_profile_and_saves(self) -> None:
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
store = ApplicationStore(sqlite_path=Path(directory) / "applications.sqlite3")
|
|
identities = IdentityDirectory(db_path=Path(directory) / "identities.sqlite3")
|
|
identities.remember_profile(actor_id=55, name="Бельчоночек", character_id=1676767)
|
|
dialog = ApplicationDialog(identities, store)
|
|
self.assertIn("скриншот активности", dialog.handle(55, 55, "/бельчонок").lower())
|
|
dialog.queue_attachments(55, [{"type": "photo", "url": "https://example.test/activity.jpg"}])
|
|
self.assertIn("отправлена", dialog.handle(55, 55, "готово").lower())
|
|
self.assertEqual(store.list()[0]["application"]["type"], "kitten")
|
|
|
|
def test_vk_application_uses_keyboard_for_menu_and_branches(self) -> None:
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
store = ApplicationStore(sqlite_path=Path(directory) / "applications.sqlite3")
|
|
identities = IdentityDirectory(db_path=Path(directory) / "identities.sqlite3")
|
|
identities.remember_profile(actor_id=77, name="Муся", character_id=1337)
|
|
dialog = ApplicationDialog(identities, store)
|
|
dialog.handle(77, 77, "/заявка")
|
|
labels = [button["action"]["label"] for row in dialog.keyboard_for(77)["buttons"] for button in row]
|
|
self.assertEqual(labels[:4], ["Пожертвовать перо", "Стать бельчонком", "Стать летягой", "Стать куратором"])
|
|
dialog.handle(77, 77, "/летяга")
|
|
labels = [button["action"]["label"] for row in dialog.keyboard_for(77)["buttons"] for button in row]
|
|
self.assertIn("Нет ссылки VK", labels)
|
|
dialog.handle(77, 77, "нет")
|
|
labels = [button["action"]["label"] for row in dialog.keyboard_for(77)["buttons"] for button in row]
|
|
self.assertEqual(labels[:2], ["Да", "Нет"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|