[PR #28349] Add Comprehensive Unit Tests for Console Auth Controllers #32022

Closed
opened 2026-02-21 20:50:36 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/28349

State: closed
Merged: Yes


Add Comprehensive Unit Tests for Console Auth Controllers

Summary

This PR adds comprehensive unit tests for the Console API authentication controllers, covering all major authentication flows and security features.

Fixes

Closes https://github.com/langgenius/dify/issues/28428

What's Added

  • 5 new test files with 54 test functions covering:
    • Login/logout flows (test_login_logout.py)
    • Token refresh mechanisms (test_token_refresh.py)
    • Password reset flows (test_password_reset.py)
    • Email verification/passwordless auth (test_email_verification.py)
    • Account activation (test_account_activation.py)

Test Coverage

The test suite covers:

  • Email/password authentication with rate limiting
  • Session management and logout
  • Token refresh (access, refresh, CSRF tokens)
  • Password reset email sending and verification
  • Email code login (passwordless authentication)
  • Account activation via invitation tokens
  • Security features: rate limiting, token validation, account status checks
  • Error handling: invalid credentials, expired tokens, workspace limits
  • Edge cases: email mismatches, frozen accounts, banned users

Test Quality

  • All tests follow Arrange-Act-Assert (AAA) pattern
  • Proper mocking for isolated unit tests
  • Comprehensive docstrings and inline comments
  • Parametrized tests for multiple scenarios
  • 54 tests - all passing

Files Added

api/tests/unit_tests/controllers/console/auth/
├── test_login_logout.py           (10 tests)
├── test_token_refresh.py          (7 tests)
├── test_password_reset.py         (13 tests)
├── test_email_verification.py     (14 tests)
└── test_account_activation.py     (10 tests)

Screenshots

Not applicable - backend unit tests only.

Testing

All tests pass successfully:

uv run --project api pytest api/tests/unit_tests/controllers/console/auth/ -v

Result: 61 passed, 4 warnings (non-critical)

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods

Contribution by Gittensor, learn more at https://gittensor.io/

**Original Pull Request:** https://github.com/langgenius/dify/pull/28349 **State:** closed **Merged:** Yes --- # Add Comprehensive Unit Tests for Console Auth Controllers ## Summary This PR adds comprehensive unit tests for the Console API authentication controllers, covering all major authentication flows and security features. ### Fixes Closes https://github.com/langgenius/dify/issues/28428 ### What's Added - **5 new test files** with 54 test functions covering: - Login/logout flows (`test_login_logout.py`) - Token refresh mechanisms (`test_token_refresh.py`) - Password reset flows (`test_password_reset.py`) - Email verification/passwordless auth (`test_email_verification.py`) - Account activation (`test_account_activation.py`) ### Test Coverage The test suite covers: - ✅ Email/password authentication with rate limiting - ✅ Session management and logout - ✅ Token refresh (access, refresh, CSRF tokens) - ✅ Password reset email sending and verification - ✅ Email code login (passwordless authentication) - ✅ Account activation via invitation tokens - ✅ Security features: rate limiting, token validation, account status checks - ✅ Error handling: invalid credentials, expired tokens, workspace limits - ✅ Edge cases: email mismatches, frozen accounts, banned users ### Test Quality - All tests follow **Arrange-Act-Assert (AAA)** pattern - Proper **mocking** for isolated unit tests - **Comprehensive docstrings** and inline comments - **Parametrized tests** for multiple scenarios - **54 tests - all passing** ✅ ### Files Added ``` api/tests/unit_tests/controllers/console/auth/ ├── test_login_logout.py (10 tests) ├── test_token_refresh.py (7 tests) ├── test_password_reset.py (13 tests) ├── test_email_verification.py (14 tests) └── test_account_activation.py (10 tests) ``` ## Screenshots Not applicable - backend unit tests only. ## Testing All tests pass successfully: ```bash uv run --project api pytest api/tests/unit_tests/controllers/console/auth/ -v ``` **Result**: 61 passed, 4 warnings (non-critical) ## Checklist - [ ] This change requires a documentation update, included: [Dify Document](https://github.com/langgenius/dify-docs) - [x] I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!) - [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change. - [x] I've updated the documentation accordingly. - [x] I ran `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods Contribution by Gittensor, learn more at https://gittensor.io/
yindo added the pull-request label 2026-02-21 20:50:36 -05:00
yindo closed this issue 2026-02-21 20:50:36 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#32022