Skip to content

tests: Add comprehensive test suite for Telegram handlers - #718

Merged
T3ST3ST3R0N merged 10 commits into
devfrom
telegram/tests
Oct 10, 2026
Merged

T3ST3ST3R0N merged 10 commits into
devfrom
telegram/tests

Conversation

@ImMohammad20000

@ImMohammad20000 ImMohammad20000 commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Type of change

  • Tests / CI
  • Bug fix
  • New feature
  • Breaking change
  • Refactor / cleanup
  • Documentation

Checklist

  • I tested the change locally or explained why it cannot be tested.

  • I added or updated tests for behavior changes.

  • I did not include secrets, tokens, private keys, or unrelated changes.

  • I updated documentation, translations, or examples if needed.

  • I checked database migrations when models or schema changed.

Summary by CodeRabbit

  • Tests
    • Expanded automated coverage for Telegram admin bulk actions, confirmations, main menu actions, and user management flows.
    • Added reusable Telegram test doubles and async fixtures for state, messages, callbacks, inline queries, and dummy text lookups.
    • Improved test database isolation/cleanup: SQLite schema reset, plus targeted truncation for PostgreSQL and MySQL to keep test runs consistent.

@coderabbitai

coderabbitai Bot commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: bc28f7f6-e0df-4b09-8d8a-b9d6d79f2f2e

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Walkthrough

The PR adds shared Telegram test fakes and broad async coverage for admin, client, base, and error handlers. It also improves test database isolation across SQLite, PostgreSQL, and MySQL and removes stale local SQLite artifacts before migrations.

Changes

Telegram fixtures and local database cleanup

Layer / File(s) Summary
Telegram fixtures and local database cleanup
tests/api/__init__.py, tests/telegram/conftest.py
Adds reusable Telegram events, FSM state, user fixtures, and cleanup of local SQLite database artifacts before migrations.

Bulk admin action flows

Layer / File(s) Summary
Bulk admin action flows
tests/telegram/handlers/admin/test_bulk_actions.py
Covers bulk template creation, validation, username strategies, expiration operations, and data-limit updates.

Admin menu and user handlers

Layer / File(s) Summary
Admin menu and user handlers
tests/telegram/handlers/admin/test_confirm_action.py, tests/telegram/handlers/admin/test_main_menu.py, tests/telegram/handlers/admin/test_user.py
Tests admin confirmations, menu actions, FSM transitions, validation, user operations, templates, authorization, and callbacks.

Client, base, and error handlers

Layer / File(s) Summary
Client, base, and error handlers
tests/telegram/handlers/client/test_show_info.py, tests/telegram/handlers/test_base.py, tests/telegram/handlers/test_error_handler.py
Covers text/file responses, startup fallbacks, admin state cleanup, and truncated validation errors.

Cross-database test isolation

Layer / File(s) Summary
Cross-database test isolation
tests/test_record_usages.py
Uses SQLite schema recreation and PostgreSQL/MySQL table truncation during fixture setup and cleanup.

Estimated code review effort: 3 (Moderate) | ~25 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: a broad new test suite for Telegram handlers.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch telegram/tests


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

I’m a rabbit with fixtures in tow,
Testing each handler’s flow.
SQLite leaves clean,
While mocks hop unseen,
And every callback says “go!”

Comment @coderabbitai help to get the list of available commands.

@ImMohammad20000

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot added the Backend label Jul 22, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
tests/telegram/handlers/test_error_handler.py (1)

16-18: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Assert the cleanup and alert contracts, not only that calls occurred.

The message-path test does not verify old messages are deleted or state is cleared; the callback-path test would pass without show_alert=True or the required ... truncation suffix.

Proposed assertions
 async def test_handle_exception_value_error_message_path(monkeypatch, fake_state, fake_message):
     state = fake_state
+    state._state = "busy"
     state._data = {"messages_to_delete": [1, 2]}
 ...
     await error_handler.handle_exception(event, state)

     msg.answer.assert_awaited_once()
+    msg.bot.delete_messages.assert_awaited_once_with(msg.chat.id, [1, 2])
+    assert await state.get_state() is None
+    assert await state.get_value("messages_to_delete") == [msg.message_id + 1]
 ...
     callback.answer.assert_awaited_once()
     sent_text = callback.answer.await_args.args[0]
     assert len(sent_text) == 200
+    assert sent_text.endswith("...")
+    assert callback.answer.await_args.kwargs == {"show_alert": True}

Also applies to: 33-37

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/telegram/handlers/test_error_handler.py` around lines 16 - 18,
Strengthen the error-handler tests around handle_exception in both message and
callback paths: assert that prior messages are deleted, state is cleared, and
the user notification uses show_alert=True with the required “...” truncation
suffix. Verify the exact cleanup and alert arguments rather than only checking
that answer was awaited.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/api/__init__.py`:
- Around line 105-112: Update the database cleanup block in the test setup to
run only for SQLite URLs, parse the configured database path from DATABASE_URL,
and remove that exact database file plus its -shm and -wal sidecars. Stop
checking or deleting hardcoded CWD paths such as test.db, while preserving the
existing best-effort cleanup behavior.

---

Nitpick comments:
In `@tests/telegram/handlers/test_error_handler.py`:
- Around line 16-18: Strengthen the error-handler tests around handle_exception
in both message and callback paths: assert that prior messages are deleted,
state is cleared, and the user notification uses show_alert=True with the
required “...” truncation suffix. Verify the exact cleanup and alert arguments
rather than only checking that answer was awaited.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 3d75c352-6714-4b6b-86e7-d4c2e7178ed5

📥 Commits

Reviewing files that changed from the base of the PR and between 4a76381 and 403e956.

📒 Files selected for processing (10)
  • tests/api/__init__.py
  • tests/telegram/conftest.py
  • tests/telegram/handlers/admin/test_bulk_actions.py
  • tests/telegram/handlers/admin/test_confirm_action.py
  • tests/telegram/handlers/admin/test_main_menu.py
  • tests/telegram/handlers/admin/test_user.py
  • tests/telegram/handlers/client/test_show_info.py
  • tests/telegram/handlers/test_base.py
  • tests/telegram/handlers/test_error_handler.py
  • tests/test_record_usages.py

Comment thread tests/api/__init__.py Resolved
@ImMohammad20000 ImMohammad20000 changed the title test: Add comprehensive test suite for Telegram handlers tests: Add comprehensive test suite for Telegram handlers Jul 22, 2026
@coderabbitai coderabbitai Bot removed the Backend label Jul 22, 2026
ImMohammad20000 and others added 3 commits July 27, 2026 10:46
Take dev's tests/api/__init__.py and tests/test_record_usages.py, which both conflicted. This branch's two test-infra edits there (deleting the configured SQLite test file, and truncating tables on PostgreSQL and MySQL) are left out on purpose: the Telegram tests need neither, and they can return as their own PR. This PR now only adds the Telegram handler tests.
Current ruff flags the lambda that returns an empty dict; use dict, which does the same.
@T3ST3ST3R0N
T3ST3ST3R0N merged commit 65695ac into dev Oct 10, 2026
17 checks passed
@ImMohammad20000
ImMohammad20000 deleted the telegram/tests branch October 11, 2026 16:25
Free-Guy-IR added a commit to Free-Guy-IR/panel that referenced this pull request Nov 2, 2026
…ain bba95f274

Checked with git ls-remote on 2026-10-11 02:55 UTC. Panel dev moved from
d94e157 to 83fe90e with 22 commits; main is still v5.4.1.

- PasarGuard#879 (2d0ca81, loopback Xray client inbounds) is ported by design: the
  fork took the open pull request on 2026-09-25 as 2e7fc45 with migration
  bfd88026565b, the same listener rewrite.
- PasarGuard#934 (a5b28f3) moves from the port queue to a dated waiver until
  2026-11-09: the fork refuses to delete a core that nodes still use
  (W3-013, fix/node-health) instead of restarting those nodes.
- PasarGuard#993 (Hysteria2 FinalMask without a udp mask) is queued until 2026-10-18:
  the fork has the same FinalMask | dict field and the same non-null-safe
  udpHop reads, so a udpHop-only host can fail the whole subscription.
- PasarGuard#984, PasarGuard#985, PasarGuard#986, PasarGuard#989, PasarGuard#944, PasarGuard#864, PasarGuard#948, PasarGuard#951, PasarGuard#778, PasarGuard#774, PasarGuard#787, PasarGuard#994,
  PasarGuard#865, PasarGuard#875, PasarGuard#718, PasarGuard#967, PasarGuard#772, PasarGuard#662, PasarGuard#995 and PasarGuard#913 are queued as
  security: no until 2026-10-25, each with its reason; PasarGuard#913's node-removal
  half is already in the fork through sync_remove_users.
- The branch commits that upstream squash-merged (the three dashboard
  branches, telegram/tests, fix/core-editor-api-section-followup,
  fix/hwid-count-test-cleanup, fix/hysteria-finalmask-quic-only) follow
  their squash, by patch-id or by an equal tree.

Scripts main moved to bba95f274 (PasarGuard#35, PasarGuard#36, PasarGuard#37 after PasarGuard#34). The three
squashes are queued security: no until 2026-10-25, and their pre-squash
branch commits are waived as superseded (equal trees).

Against live upstream the panel gate now fails only on the 17 port-queue
commits classified security: yes, and the forks gate only on node
f72ac0ef (security: yes). No upstream commit is undecided.
Free-Guy-IR pushed a commit to Free-Guy-IR/panel that referenced this pull request Nov 4, 2026
…#718)

* test: add comprehensive test suite for Telegram handlers

* fix

* fix 2

* fix 3

* Delete the configured SQLite database path, not CWD test.db

* Delete the configured SQLite database path, not CWD test.db

* update telegram tests

* test(telegram): satisfy ruff PIE807 in the handler test fixture

Current ruff flags the lambda that returns an empty dict; use dict, which does the same.

---------

Co-authored-by: T3ST3ST3R0N <[email protected]>
(cherry picked from commit 65695ac)
Free-Guy-IR added a commit to Free-Guy-IR/panel that referenced this pull request Nov 4, 2026
…e in the PasarGuard#718 suite

The fork checks username uniqueness globally with get_existing_usernames
(b794f21) instead of the admin-scoped get_validated_user lookup, so the
three PasarGuard#718 cases that reach that check stub it too; their asserted
outcomes are unchanged.
Free-Guy-IR added a commit to Free-Guy-IR/panel that referenced this pull request Nov 4, 2026
…asarGuard#938 hunk

65695ac moves to Ported by design: its cherry-pick -x 357040fda is
unchanged and ea4c2512f stubs the fork's global username check in three
cases. 41eee93 stays in the port queue: 15 of its 16 hunks are on
main, the jwt.decode hunk must not be taken as it is (0e964a528 fails on
it), and the Ported by design check finds no fork commit whose lines are
all still on main.
Free-Guy-IR added a commit to Free-Guy-IR/panel that referenced this pull request Nov 4, 2026
Free-Guy-IR added a commit to Free-Guy-IR/panel that referenced this pull request Nov 4, 2026
…the B-AUTH-ROOTS picks by their r10/train-q SHAs

Rebasing port/b-auth-roots 7a35ccb0a onto r10/train-q gave its picks new SHAs: 357040fda is 66b8ee4, ea4c2512f is 380d02d and 0e964a528 is 6dd93c1. The ledger rows now name the commits that are in this history.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants