From 280e82300c498f3694e1ee7b1e21612111086e87 Mon Sep 17 00:00:00 2001 From: Kushal Das Date: Fri, 12 Oct 2018 00:16:56 -0400 Subject: [PATCH 1/2] Fixes #11 Saves the downloaded submissions and replies Now we are explicitly closing NamedTemporaryFile and also marking it not to delete on save. --- securedrop_proxy/proxy.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/securedrop_proxy/proxy.py b/securedrop_proxy/proxy.py index 09c3067..2108d1a 100644 --- a/securedrop_proxy/proxy.py +++ b/securedrop_proxy/proxy.py @@ -96,11 +96,15 @@ def handle_non_json_response(self): res = Response(self._presp.status_code) - fh = tempfile.NamedTemporaryFile() + # Create a NamedTemporaryFile, we don't want + # to delete it after closing. + fh = tempfile.NamedTemporaryFile(delete=False) for c in self._presp.iter_content(10): fh.write(c) + fh.close() + res.headers = self._presp.headers self.on_save(fh, res, self.conf) From 0c0be3ec714875572d2fca2e548a4389b5c1232f Mon Sep 17 00:00:00 2001 From: redshiftzero Date: Fri, 12 Oct 2018 16:21:38 -0700 Subject: [PATCH 2/2] Regression test: Assert that file is not empty --- tests/test_main.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_main.py b/tests/test_main.py index 6bfa8b8..8a4a28e 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -83,6 +83,13 @@ def on_save(fh, res, conf): # The proxy should have created a filename in the response body self.assertIn('filename', response['body']) + # The file should not be empty + with open("/tmp/{}".format(self.fn)) as f: + saved_file = f.read() + + # We expect HTML content in the file from the test data + self.assertIn("", saved_file) + def test_error_response(self): test_input_json = """"foo": "bar", "baz": "bliff" }"""