Skip to content

Commit

Permalink
(PC-32112)[PRO] invoicetable: csv: 24 rows selected max
Browse files Browse the repository at this point in the history
Invoice details had a limit of 24: a user could not ask more than 24 at
a time. But for some (unknown) reason, csv details had none.

This commit fixes this: a user cannot ask for more than 24 csv/invoice
details files at a time.
  • Loading branch information
jbaudet-pass committed Dec 4, 2024
1 parent c835247 commit 0ea9004
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import Callable
from typing import Iterable

from pydantic.v1 import ConstrainedList
from pydantic.v1 import validator
from pydantic.v1.main import BaseModel
import pytz
Expand Down Expand Up @@ -300,8 +301,15 @@ class ReimbursementCsvQueryModel(BaseModel):
reimbursementPeriodEndingDate: str | None


class InvoiceList(ConstrainedList):
__args__ = (str,) # required by pydantic
item_type = str
unique_items = True
max_items = 24


class ReimbursementCsvByInvoicesModel(BaseModel):
invoicesReferences: list[str]
invoicesReferences: InvoiceList

@validator("invoicesReferences", pre=True)
def ensure_invoices_references_is_list(cls, v: list[str] | str) -> list[str]:
Expand Down
13 changes: 13 additions & 0 deletions api/tests/routes/pro/get_reimbursements_csv_by_invoices_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import csv
import datetime
from io import StringIO
import string

import pytest

Expand Down Expand Up @@ -245,3 +246,15 @@ def test_return_only_searched_invoice(client):
assert reader.fieldnames == ReimbursementDetails.CSV_HEADER
rows = list(reader)
assert len(rows) == 1


def test_too_many_invoices_searched_returns_an_error(client):
pro = users_factories.ProFactory()

client = client.with_session_auth(pro.email)
references = "invoicesReferences=" + "&invoicesReferences=".join(list(string.ascii_letters))

response = client.get(f"/v2/reimbursements/csv?{references}")

assert response.status_code == 400
assert response.json == {"invoicesReferences": ["ensure this value has at most 24 items"]}
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ export const InvoiceTable = ({ invoices }: InvoiceTableProps) => {
}

async function downloadCSVFiles(references: string[]) {
if (references.length > 24) {
notify.error(
'Vous ne pouvez pas télécharger plus de 24 documents en une fois.'
)
return
}
try {
logEvent(Events.CLICKED_INVOICES_DOWNLOAD, {
fileType: 'details',
Expand Down

0 comments on commit 0ea9004

Please sign in to comment.