Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/routes/import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def import_csv():

try:
# Read file content
csv_content = file.read().decode("utf-8")
csv_content = file.read().decode("utf-8-sig")

# Create import record
import_record = DataImport(
Expand Down Expand Up @@ -703,7 +703,7 @@ def import_csv_clients_route():
# Read file content
file_bytes = file.read()
try:
csv_content = file_bytes.decode("utf-8")
csv_content = file_bytes.decode("utf-8-sig")
except UnicodeDecodeError:
# Try with different encodings
try:
Expand Down
40 changes: 40 additions & 0 deletions tests/test_import_csv_bom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""CSV imports accept files saved with a UTF-8 byte order mark (Excel "CSV UTF-8")."""

import io

import pytest

pytestmark = [pytest.mark.integration]

BOM = "".encode("utf-8")


def test_import_time_entries_csv_with_bom(admin_authenticated_client):
csv_bytes = BOM + (
b"project_name,client_name,task_name,start_time,end_time,duration_hours,notes,tags,billable\n"
b"BOM Project,BOM Client,,2024-01-01 09:00:00,2024-01-01 10:00:00,1.0,note,,true\n"
)
response = admin_authenticated_client.post(
"/api/import/csv",
data={"file": (io.BytesIO(csv_bytes), "entries.csv")},
content_type="multipart/form-data",
)

assert response.status_code == 200
summary = response.get_json()["summary"]
assert summary["successful"] == 1
assert summary["failed"] == 0


def test_import_clients_csv_with_bom(admin_authenticated_client):
csv_bytes = BOM + b"name,email\nBOM Client Ltd,billing@example.com\n"
response = admin_authenticated_client.post(
"/api/import/csv/clients",
data={"file": (io.BytesIO(csv_bytes), "clients.csv")},
content_type="multipart/form-data",
)

assert response.status_code == 200
summary = response.get_json()["summary"]
assert summary["successful"] == 1
assert summary["failed"] == 0
Loading