Skip to content

fix(leads): Add file size validation to CSV import endpoint (LO-079) - #678

Open
Lakshayg136 wants to merge 1 commit into
Kuldeeep18:mainfrom
Lakshayg136:fix/LO-079-csv-size-validation
Open

fix(leads): Add file size validation to CSV import endpoint (LO-079)#678
Lakshayg136 wants to merge 1 commit into
Kuldeeep18:mainfrom
Lakshayg136:fix/LO-079-csv-size-validation

Conversation

@Lakshayg136

@Lakshayg136 Lakshayg136 commented Jul 13, 2026

Copy link
Copy Markdown

Pull Request

🔗 Related Issue

Closes #327


📝 Summary of Changes

  • Settings Configuration: Added MAX_CSV_UPLOAD_SIZE setting to backend/backend/settings.py (defaulting to 10MB) to configure the maximum allowable size for uploaded CSV files.
  • View Validation: Added validation inside import_csv in backend/leads/views.py to check file_obj.size against MAX_CSV_UPLOAD_SIZE before reading the file. If the file is oversized, it returns a 400 Bad Request response, preventing memory exhaustion.
  • Regression Testing: Added test_import_csv_oversized_file_rejected and test_import_csv_normal_size_file_accepted tests to backend/leads/tests.py to verify this behavior.

🏷️ Type of Change

  • 🐛 Bug fix
  • ✨ New feature
  • ♻️ Refactor
  • 📝 Documentation update
  • 🎨 UI / Style change
  • 🔧 Chore

🧪 Testing

The changes were verified locally using the backend test suite.

Steps to test:

  1. Navigate to the backend directory.
  2. Run the command: python manage.py test leads
  3. Verify that the 34 tests (including our new size-validation tests) execute and pass successfully.

📸 Screenshots (if applicable)

N/A (Backend logic changes only)


✅ Checklist

  • No merge conflicts
  • Changes follow the project guidelines
  • Documentation updated (if applicable)
  • Related issue linked
  • Changes tested locally (if applicable)

Summary by CodeRabbit

  • New Features

    • Added a configurable maximum file size for CSV uploads, defaulting to 10 MB.
    • CSV files exceeding the configured limit are rejected with a clear error message.
    • Valid CSV files continue to be accepted and processed asynchronously.
  • Bug Fixes

    • Prevented oversized uploads from creating unnecessary import jobs or entering processing queues.
  • Tests

    • Added coverage for both rejected oversized files and successfully accepted uploads.

@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds configurable CSV upload size limits, rejects oversized files before reading or queuing imports, and tests both rejected oversized uploads and accepted normal-sized uploads.

Changes

CSV Upload Limits

Layer / File(s) Summary
Upload size configuration
backend/backend/settings.py, backend/config/settings.py
Defines MAX_CSV_UPLOAD_SIZE from an environment variable with a 10MB default.
CSV import size enforcement
backend/leads/views.py, backend/leads/tests.py
Rejects files exceeding the configured limit with 400 BAD REQUEST before import processing, and tests rejection and successful acceptance responses.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: adding CSV import file size validation.
Linked Issues check ✅ Passed The PR adds the size limit setting, checks uploads before processing, returns 400 on oversize, and includes an oversized-file test.
Out of Scope Changes check ✅ Passed The changes stay focused on CSV upload size validation and related tests/settings.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit 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

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

@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.

🧹 Nitpick comments (1)
backend/leads/tests.py (1)

204-220: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Assert that oversized uploads create no import job.

The endpoint is required to reject the file before queuing or persisting work, but this test only checks the HTTP response. Add an assertion that no LeadImportJob exists for too_large.csv.

Suggested assertion
             self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
             self.assertIn("File size exceeds the limit", response.data['error'])
+            self.assertFalse(
+                LeadImportJob.objects.filter(
+                    organization=self.org_a,
+                    filename="too_large.csv",
+                ).exists()
+            )
🤖 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 `@backend/leads/tests.py` around lines 204 - 220, Extend
test_import_csv_oversized_file_rejected to assert that no LeadImportJob exists
for the rejected “too_large.csv” upload after the response is returned, while
preserving the existing status and error-message assertions.
🤖 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.

Nitpick comments:
In `@backend/leads/tests.py`:
- Around line 204-220: Extend test_import_csv_oversized_file_rejected to assert
that no LeadImportJob exists for the rejected “too_large.csv” upload after the
response is returned, while preserving the existing status and error-message
assertions.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: f8b4bb43-af05-41d4-98ea-691722cc8cd9

📥 Commits

Reviewing files that changed from the base of the PR and between 4a33158 and a1097a0.

📒 Files selected for processing (4)
  • backend/backend/settings.py
  • backend/config/settings.py
  • backend/leads/tests.py
  • backend/leads/views.py

@Lakshayg136

Lakshayg136 commented Jul 14, 2026

Copy link
Copy Markdown
Author

please review, add the gssoc Labels and merge it

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.

LO-079 [Easy - Bug]: Add File Size Validation for CSV Import Endpoint

1 participant