Skip to content

Commit

Permalink
fix: PDF and raw response (frappe#22402)
Browse files Browse the repository at this point in the history
  • Loading branch information
ankush authored Sep 13, 2023
1 parent 9244140 commit 909457d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
45 changes: 44 additions & 1 deletion frappe/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import sys
from contextlib import contextmanager
from random import choice
Expand All @@ -6,13 +7,14 @@
from unittest.mock import patch

import requests
from filetype import guess_mime
from semantic_version import Version
from werkzeug.test import TestResponse

import frappe
from frappe.installer import update_site_config
from frappe.tests.utils import FrappeTestCase, patch_hooks
from frappe.utils import get_site_url, get_test_client
from frappe.utils import cint, get_site_url, get_test_client

try:
_site = frappe.local.site
Expand Down Expand Up @@ -325,3 +327,44 @@ def before_request(*args, **kwargs):

def after_request(*args, **kwargs):
_test_REQ_HOOK["after_request"] = time()


class TestResponse(FrappeAPITestCase):
def test_generate_pdf(self):
response = self.get(
f"/api/method/frappe.utils.print_format.download_pdf",
{"sid": self.sid, "doctype": "User", "name": "Guest"},
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["content-type"], "application/pdf")
self.assertGreater(cint(response.headers["content-length"]), 0)

self.assertEqual(guess_mime(response.data), "application/pdf")

def test_binary_and_csv_response(self):
def download_template(file_type):
filters = json.dumps({})
fields = json.dumps({"User": ["name"]})
return self.post(
"/api/method/frappe.core.doctype.data_import.data_import.download_template",
{
"sid": self.sid,
"doctype": "User",
"export_fields": fields,
"export_filters": filters,
"file_type": file_type,
},
)

response = download_template("Excel")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["content-type"], "application/octet-stream")
self.assertGreater(cint(response.headers["content-length"]), 0)
self.assertEqual(
guess_mime(response.data), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
)

response = download_template("CSV")
self.assertEqual(response.status_code, 200)
self.assertIn("text/csv", response.headers["content-type"])
self.assertGreater(cint(response.headers["content-length"]), 0)
4 changes: 2 additions & 2 deletions frappe/utils/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ def as_json():
def as_pdf():
response = Response()
response.mimetype = "application/pdf"
response.headers.add("Content-Disposition", filename=frappe.response["filename"])
response.headers.add("Content-Disposition", None, filename=frappe.response["filename"])
response.data = frappe.response["filecontent"]
return response


def as_binary():
response = Response()
response.mimetype = "application/octet-stream"
response.headers.add("Content-Disposition", filename=frappe.response["filename"])
response.headers.add("Content-Disposition", None, filename=frappe.response["filename"])
response.data = frappe.response["filecontent"]
return response

Expand Down

0 comments on commit 909457d

Please sign in to comment.