Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] report_csv: Assign 'reportname' variable before accessing it #773

Merged
merged 3 commits into from
Jul 14, 2023
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
5 changes: 4 additions & 1 deletion report_csv/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import logging

from werkzeug.exceptions import InternalServerError
from werkzeug.urls import url_decode

from odoo.http import (
Expand Down Expand Up @@ -54,6 +55,7 @@ def report_routes(self, reportname, docids=None, converter=None, **data):
def report_download(self, data, context=None):
requestcontent = json.loads(data)
url, report_type = requestcontent[0], requestcontent[1]
reportname = ""
try:
if report_type == "csv":
reportname = url.split("/report/csv/")[1].split("?")[0]
Expand Down Expand Up @@ -102,4 +104,5 @@ def report_download(self, data, context=None):
_logger.exception("Error while generating report %s", reportname)
se = _serialize_exception(e)
error = {"code": 200, "message": "Odoo Server Error", "data": se}
return request.make_response(html_escape(json.dumps(error)))
res = request.make_response(html_escape(json.dumps(error)))
raise InternalServerError(response=res) from e
60 changes: 60 additions & 0 deletions report_csv/tests/test_report.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# Copyright 2019 Creu Blanca
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
import json
import logging
from io import StringIO
from unittest import mock

from odoo import http
from odoo.tests import common
from odoo.tools import mute_logger

from odoo.addons.web.controllers.report import ReportController

_logger = logging.getLogger(__name__)
try:
Expand All @@ -12,6 +18,14 @@
_logger.debug("Can not import csv.")


class TestCsvException(Exception):
def __init__(self, message):
"""
:param message: exception message and frontend modal content
"""
super().__init__(message)


class TestReport(common.TransactionCase):
def setUp(self):
super().setUp()
Expand Down Expand Up @@ -55,3 +69,49 @@ def test_id_retrieval(self):
# Typical call from render
objs = self.csv_report._get_objs_for_report(self.docs.ids, {})
self.assertEqual(objs, self.docs)


class TestCsvReport(common.HttpCase):
"""
Some tests calling controller
"""

def setUp(self):
super().setUp()
self.report_object = self.env["ir.actions.report"]
self.csv_report = self.env["report.report_csv.abstract"].with_context(
active_model="res.partner"
)
self.report_name = "report_csv.partner_csv"
self.report = self.report_object._get_report_from_name(self.report_name)
self.docs = self.env["res.company"].search([], limit=1).partner_id
self.session = self.authenticate("admin", "admin")

def test_csv(self):
filename = self.get_report_headers().headers.get("Content-Disposition")
self.assertTrue(".csv" in filename)

@mute_logger("odoo.addons.web.controllers.report")
def test_pdf_error(self):
with mock.patch.object(
ReportController, "report_routes"
) as route_patch, self.assertLogs(
"odoo.addons.report_csv.controllers.main", level=logging.ERROR
) as cm:
route_patch.side_effect = TestCsvException("Test")
self.get_report_headers(
suffix="/report/pdf/test/10", f_type="qweb-pdf"
).headers.get("Content-Disposition")
[msg] = cm.output
self.assertIn("Error while generating report", msg)

def get_report_headers(
self, suffix="/report/csv/report_csv.partner_csv/1", f_type="csv"
):
return self.url_open(
url="/report/download",
data={
"data": json.dumps([suffix, f_type]),
"csrf_token": http.Request.csrf_token(self),
},
)