🧪 test(api): email send_email, _get_smtp_config, test-email route#37
🧪 test(api): email send_email, _get_smtp_config, test-email route#37
Conversation
Mock aiosmtplib and fake DB session; cover smtp_from fallback and router branches. Made-with: Cursor
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Caution Review failedPull request was closed or merged during review Warning Ignoring CodeRabbit configuration file changes. For security, only the configuration from the base branch is applied for open source repositories. 📝 WalkthroughWalkthroughAdds unit tests for email functionality and a CodeRabbit configuration file. Tests exercise Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
apps/api/tests/unit/test_email_provider.py (1)
13-20: Avoid process-wide env mutation at import time in this test module.Setting defaults at module import can leak state across tests and makes outcomes depend on pre-existing CI/local env. Prefer an
autousefixture withmonkeypatchso values are scoped and restored per test run.Suggested refactor
-os.environ.setdefault("DATABASE_URL", "sqlite+aiosqlite:///:memory:") -os.environ.setdefault("REDIS_URL", "redis://localhost:6379/0") -os.environ.setdefault("JWT_SECRET", "test-secret-key-for-pytest") -os.environ.setdefault("STORAGE_BACKEND", "local") -os.environ.setdefault("STORAGE_LOCAL_PATH", "/tmp/lyranote-test-storage") -os.environ.setdefault("OPENAI_API_KEY", "sk-test") -os.environ.setdefault("DEBUG", "false") +@pytest.fixture(autouse=True) +def _env(monkeypatch: pytest.MonkeyPatch): + monkeypatch.setenv("DATABASE_URL", "sqlite+aiosqlite:///:memory:") + monkeypatch.setenv("REDIS_URL", "redis://localhost:6379/0") + monkeypatch.setenv("JWT_SECRET", "test-secret-key-for-pytest") + monkeypatch.setenv("STORAGE_BACKEND", "local") + monkeypatch.setenv("STORAGE_LOCAL_PATH", "/tmp/lyranote-test-storage") + monkeypatch.setenv("OPENAI_API_KEY", "sk-test") + monkeypatch.setenv("DEBUG", "false")🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@apps/api/tests/unit/test_email_provider.py` around lines 13 - 20, Module-level os.environ.setdefault calls cause process-wide env mutation at import; remove those top-level setdefault lines and instead add a pytest autouse fixture (e.g., env_autouse) that uses pytest's monkeypatch to set the same keys (DATABASE_URL, REDIS_URL, JWT_SECRET, STORAGE_BACKEND, STORAGE_LOCAL_PATH, OPENAI_API_KEY, DEBUG) for each test and automatically restores them after each run; update apps/api/tests/unit/test_email_provider.py (or a shared conftest.py) to define this fixture so state is scoped per test and no longer mutated at import time.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@apps/api/tests/unit/test_email_provider.py`:
- Around line 245-310: Move the tests that exercise
app.domains.config.router.test_email out of the provider-focused test file and
into the config-domain test module: create or update the config-domain unit test
file to contain the TestConfigTestEmailRoute class and its test_* methods (which
reference test_email, _ConfigFakeSession, and _CfgRow), update the import paths
inside those tests to import test_email from app.domains.config.router, and
remove these tests from apps/api/tests/unit/test_email_provider.py so
provider-focused tests only cover app.providers.email.send_email; ensure any
fixtures or helper classes (_CfgRow, _ConfigFakeSession) are available or moved
along with the tests.
- Around line 64-77: Modify the test_returns_false_when_from_and_username_empty
test so it triggers the from_addr-empty branch in send_email: supply
smtp_config["smtp_username"] as a whitespace string (e.g., " ") so .strip()
yields an empty username check to pass but set the from_addr argument to a
string that becomes empty after .strip() (e.g., " ") as well; this ensures
send_email's logic in the send_email function exercises the if not from_addr
branch and returns False while SMTP is not called (verify
smtp_cls.assert_not_called()).
---
Nitpick comments:
In `@apps/api/tests/unit/test_email_provider.py`:
- Around line 13-20: Module-level os.environ.setdefault calls cause process-wide
env mutation at import; remove those top-level setdefault lines and instead add
a pytest autouse fixture (e.g., env_autouse) that uses pytest's monkeypatch to
set the same keys (DATABASE_URL, REDIS_URL, JWT_SECRET, STORAGE_BACKEND,
STORAGE_LOCAL_PATH, OPENAI_API_KEY, DEBUG) for each test and automatically
restores them after each run; update apps/api/tests/unit/test_email_provider.py
(or a shared conftest.py) to define this fixture so state is scoped per test and
no longer mutated at import time.
🪄 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: defaults
Review profile: CHILL
Plan: Pro
Run ID: d1e87ff6-54c9-469a-a4e3-580cc992a909
📒 Files selected for processing (1)
apps/api/tests/unit/test_email_provider.py
Add .coderabbit.yaml (zh-CN, summary, path filters). Split config test-email handler tests into test_config_test_email.py; cover empty from_addr after strip. Made-with: Cursor
Mock aiosmtplib and fake DB session; cover smtp_from fallback and router branches.
Made-with: Cursor
Summary
Type of change
Related issue
Closes #
Changes
How to test
Screenshots (if applicable)
Checklist
./lyra lintand there are no type errorsfeat:,fix:, etc.)Summary by CodeRabbit