feat(tests): Add comprehensive test suite for the Project API#7252
feat(tests): Add comprehensive test suite for the Project API#7252sriramveeraghanta merged 8 commits intomakeplane:previewfrom
Conversation
Introduces a new `workspace` fixture in `conftest.py` to provide a consistent and reusable setup for tests that require a workspace.
This commit introduces a comprehensive test suite for the project creation API endpoint. The suite covers a wide range of scenarios, including: - Successful creation and verification of side-effects (default states, project members, user properties). - Validation for invalid or missing data (400 Bad Request). - Permission checks for different user roles (e.g., guests are forbidden). - Authentication requirements (401 Unauthorized). - Uniqueness constraints for project names and identifiers (409 Conflict). - Successful creation with all optional fields populated. It leverages the `workspace`, `session_client` and `create_user` fixtures for a consistent test setup.
To avoid code duplication in upcoming tests, this commit introduces a `TestProjectBase` class. The `get_project_url` helper method is moved into this shared base class, and the existing `TestProjectAPIPost` class is updated to inherit from it. This ensures the URL generation logic is defined in a single place and preparing the suite for the upcoming GET tests.
This commit adds a suite for the GET method. It leverages the previously created `TestProjectBase` class for URL generation. The new test suite covers: - Listing projects: - Verifies that administrators see all projects. - Confirms guests only see projects they are members of. - Tests the separate detailed project list endpoint. - Retrieving a single project: - Checks for successful retrieval of a project by its ID. - Handles edge cases for non-existent and archived projects (404 Not Found). - Authentication: - Ensures authentication is required (401 Unauthorized).
Key scenarios tested for PATCH: - Successful partial updates by project administrators. - Forbidden access for non-admin members (403). - Conflict errors for duplicate names or identifiers on update (409). - Validation errors for invalid data (400). Key scenarios tested for DELETE: - Successful deletion by both project admins and workspace admins. - Forbidden access for non-admin members (403). - Authentication checks for unauthenticated users (401).
|
""" WalkthroughA new pytest fixture for creating workspaces was added to the test configuration, and a comprehensive contract test suite for the Project API endpoints within a workspace context was introduced. The tests cover project creation, retrieval, update, and deletion, verifying permissions, data validation, and correct API behavior. Changes
Sequence Diagram(s)sequenceDiagram
participant TestClient
participant API_Server
participant Database
TestClient->>API_Server: POST /api/workspace/ (create workspace)
API_Server->>Database: Insert Workspace
API_Server-->>TestClient: 201 Created + workspace ID
TestClient->>API_Server: POST /api/workspace/{slug}/project/ (create project)
API_Server->>Database: Insert Project, ProjectMember, IssueUserProperty
API_Server-->>TestClient: 201 Created / 400 Bad Request / 409 Conflict
TestClient->>API_Server: GET /api/workspace/{slug}/project/
API_Server->>Database: Query Projects (filter by permissions)
API_Server-->>TestClient: 200 OK + project list
TestClient->>API_Server: PATCH /api/workspace/{slug}/project/{id}/
API_Server->>Database: Update Project (if permitted)
API_Server-->>TestClient: 200 OK / 403 Forbidden / 409 Conflict
TestClient->>API_Server: DELETE /api/workspace/{slug}/project/{id}/
API_Server->>Database: Delete Project (if permitted)
API_Server-->>TestClient: 204 No Content / 403 Forbidden
Assessment against linked issues
Assessment against linked issues: Out-of-scope changesNo out-of-scope changes found. Suggested reviewers
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
✨ Finishing Touches
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (3)
apiserver/plane/tests/conftest.py (2)
6-6: Remove unused imports.The
patchandMagicMockimports fromunittest.mockare not used in this file and should be removed to keep the codebase clean.-from unittest.mock import patch, MagicMock
143-144: Fix line length violation.The assertion message exceeds the project's line length limit (96 > 88 characters).
- assert response.status_code == status.HTTP_201_CREATED, \ - f"Failed to create workspace. Status: {response.status_code}, Response: {response.data}" + assert response.status_code == status.HTTP_201_CREATED, ( + f"Failed to create workspace. Status: {response.status_code}, " + f"Response: {response.data}" + )apiserver/plane/tests/contract/app/test_project_app.py (1)
54-54: Remove debug print statement.The
print(url)statement should be removed as it's not needed in production test code.- print(url)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
apiserver/plane/tests/conftest.py(2 hunks)apiserver/plane/tests/contract/app/test_project_app.py(1 hunks)
🧰 Additional context used
🪛 Ruff (0.11.9)
apiserver/plane/tests/conftest.py
6-6: unittest.mock.patch imported but unused
Remove unused import
(F401)
6-6: unittest.mock.MagicMock imported but unused
Remove unused import
(F401)
144-144: Line too long (96 > 88)
(E501)
🪛 Pylint (3.3.7)
apiserver/plane/tests/contract/app/test_project_app.py
[refactor] 16-16: Too few public methods (1/2)
(R0903)
🔇 Additional comments (5)
apiserver/plane/tests/conftest.py (1)
125-148: Well-implemented workspace fixture!The workspace fixture is properly implemented with:
- Correct API endpoint usage via
reverse()- Proper error handling with descriptive assertion message
- Returns the actual model instance for test usage
- Good documentation explaining its purpose
This fixture will provide excellent reusability for the comprehensive test suite.
apiserver/plane/tests/contract/app/test_project_app.py (4)
16-42: Excellent URL helper method implementation!The
get_project_urlmethod is well-designed with:
- Clear documentation and parameter descriptions
- Flexible URL construction for different endpoint types
- Proper handling of optional parameters
- Good reasoning for avoiding
reverse()due to duplicate URL namesThis provides a clean abstraction for URL generation across all tests.
45-253: Comprehensive POST operation test coverage!The POST tests excellently cover:
- ✅ Empty data validation
- ✅ Valid project creation with all side effects (ProjectMember, IssueUserProperty, States)
- ✅ Project lead assignment scenarios
- ✅ Permission enforcement (guest forbidden, unauthenticated denied)
- ✅ Duplicate name/identifier conflict handling
- ✅ Required field validation
- ✅ Optional field handling
The tests verify both API responses and database state changes, which is crucial for contract testing.
255-403: Thorough GET operation test coverage!The GET tests comprehensively cover:
- ✅ Admin vs guest user access patterns
- ✅ Project visibility based on membership
- ✅ Unauthenticated access denial
- ✅ Detailed project listing
- ✅ Individual project retrieval
- ✅ 404 handling for non-existent projects
- ✅ Archived project exclusion
The permission testing ensures guests only see projects they belong to, which is critical for data security.
406-619: Complete PATCH/DELETE operation test suite!The PATCH/DELETE tests provide excellent coverage of:
- ✅ Successful partial updates with field verification
- ✅ Permission enforcement (only admins can update/delete)
- ✅ Duplicate name/identifier conflict handling
- ✅ Invalid data rejection
- ✅ Both project admin and workspace admin deletion rights
- ✅ Unauthenticated access denial
The tests properly verify database state changes and ensure proper authorization controls.
apiserver/plane/tests/conftest.py
Outdated
|
|
||
|
|
||
| @pytest.fixture | ||
| def workspace(session_client): |
There was a problem hiding this comment.
@mohamedhayballa22 I suggest you to directly create the workspace for this fixture using the Workspace model instead of the API.
Updates the `workspace` fixture to create the model instance directly via the ORM using the `Workspace` model instead of the API, as requested during code review.
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
apiserver/plane/tests/conftest.py (1)
4-4: Clean up unused imports flagged by static analysis.The static analysis correctly identifies several unused imports in this file. These should be removed to keep the codebase clean.
-from rest_framework import status -from unittest.mock import patch, MagicMock -from django.urls import reverseAlso applies to: 6-7
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
apiserver/plane/tests/conftest.py(2 hunks)apiserver/plane/tests/contract/app/test_project_app.py(1 hunks)
🧰 Additional context used
🪛 Pylint (3.3.7)
apiserver/plane/tests/contract/app/test_project_app.py
[refactor] 16-16: Too few public methods (1/2)
(R0903)
🪛 Ruff (0.11.9)
apiserver/plane/tests/conftest.py
4-4: rest_framework.status imported but unused
Remove unused import: rest_framework.status
(F401)
6-6: unittest.mock.patch imported but unused
Remove unused import
(F401)
6-6: unittest.mock.MagicMock imported but unused
Remove unused import
(F401)
7-7: django.urls.reverse imported but unused
Remove unused import: django.urls.reverse
(F401)
🔇 Additional comments (5)
apiserver/plane/tests/conftest.py (1)
125-142: Excellent workspace fixture implementation!The fixture correctly follows the previous review feedback by creating the workspace directly using the model instead of the API. The implementation properly:
- Creates a workspace with the authenticated user as owner
- Establishes workspace membership with admin role (20)
- Returns the workspace instance for use in tests
This provides a solid foundation for the comprehensive Project API tests.
apiserver/plane/tests/contract/app/test_project_app.py (4)
16-42: Well-designed URL helper method!The
get_project_url()method elegantly handles URL construction while avoiding thereverse()function issues with duplicate names. The method signature and logic clearly support all the different URL patterns needed for the tests.
45-252: Comprehensive POST operation test coverage!The test suite excellently covers all critical POST scenarios:
- Data validation (empty, missing fields, invalid data)
- Success cases with proper side effects (project creation, member roles, default states)
- Permission enforcement (guest forbidden, unauthenticated blocked)
- Conflict handling (duplicate names/identifiers)
- Edge cases (project lead assignment, optional fields)
The assertions verify both response codes and database state changes, ensuring complete validation of the API behavior.
254-402: Thorough GET operation test coverage!The test suite comprehensively validates GET operations:
- Authentication and authorization (admin vs guest vs unauthenticated)
- Data filtering (guests only see projects they belong to)
- Different endpoint variations (list, details, specific project)
- Edge cases (non-existent projects, archived projects)
The tests properly verify both access control and data correctness.
405-618: Complete PATCH and DELETE operation test coverage!The test suite thoroughly validates update and delete operations:
- Permission enforcement (only admins can modify/delete)
- Conflict detection (duplicate names/identifiers on updates)
- Data validation (invalid update data)
- Success scenarios with proper database state verification
- Authentication requirements
The tests ensure proper access control and data integrity for destructive operations.
Removes imports that I added while working on the test suite for the Project API but were ultimately not used. Note that other unused imports still exist from the state of the codebase when this branch was created/forked.
* feat(tests): Add reusable workspace fixture Introduces a new `workspace` fixture in `conftest.py` to provide a consistent and reusable setup for tests that require a workspace. * feat(tests): Add tests for project creation (POST) This commit introduces a comprehensive test suite for the project creation API endpoint. The suite covers a wide range of scenarios, including: - Successful creation and verification of side-effects (default states, project members, user properties). - Validation for invalid or missing data (400 Bad Request). - Permission checks for different user roles (e.g., guests are forbidden). - Authentication requirements (401 Unauthorized). - Uniqueness constraints for project names and identifiers (409 Conflict). - Successful creation with all optional fields populated. It leverages the `workspace`, `session_client` and `create_user` fixtures for a consistent test setup. * refactor(tests): Centralize project URL helper into a base class To avoid code duplication in upcoming tests, this commit introduces a `TestProjectBase` class. The `get_project_url` helper method is moved into this shared base class, and the existing `TestProjectAPIPost` class is updated to inherit from it. This ensures the URL generation logic is defined in a single place and preparing the suite for the upcoming GET tests. * feat(tests): Add tests for project listing and retrieval (GET) This commit adds a suite for the GET method. It leverages the previously created `TestProjectBase` class for URL generation. The new test suite covers: - Listing projects: - Verifies that administrators see all projects. - Confirms guests only see projects they are members of. - Tests the separate detailed project list endpoint. - Retrieving a single project: - Checks for successful retrieval of a project by its ID. - Handles edge cases for non-existent and archived projects (404 Not Found). - Authentication: - Ensures authentication is required (401 Unauthorized). * feat(tests): Add tests for project update (PATCH) and deletion (DELETE) Key scenarios tested for PATCH: - Successful partial updates by project administrators. - Forbidden access for non-admin members (403). - Conflict errors for duplicate names or identifiers on update (409). - Validation errors for invalid data (400). Key scenarios tested for DELETE: - Successful deletion by both project admins and workspace admins. - Forbidden access for non-admin members (403). - Authentication checks for unauthenticated users (401). * Remove unnecessary print statement * refactor(tests): Update workspace fixture to use ORM Updates the `workspace` fixture to create the model instance directly via the ORM using the `Workspace` model instead of the API, as requested during code review. * Refactor: Remove some unused imports Removes imports that I added while working on the test suite for the Project API but were ultimately not used. Note that other unused imports still exist from the state of the codebase when this branch was created/forked.
Description
This pull request introduces a comprehensive API contract test suite for the Project resource to improve the test coverage and long-term health of this component and the overall codebase.
This covers all CRUD operations (POST, GET, PATCH, DELETE) and includes extensive checks for:
To support this and future tests, this PR also introduces a reusable workspace fixture in conftest.py.
Type of Change
Test Scenarios
The changes in this PR consist entirely of new tests. All new and existing tests in the suite were run via pytest and passed successfully in the local development environment. The new tests themselves serve as the verification for the existing Project API functionality.
References
Closes #7229
Summary by CodeRabbit