]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Adds further testing to cover scripts with non-zero exit codes
authorTrenton H <797416+stumpylog@users.noreply.github.com>
Fri, 11 Nov 2022 16:58:49 +0000 (08:58 -0800)
committerTrenton H <797416+stumpylog@users.noreply.github.com>
Fri, 11 Nov 2022 16:58:49 +0000 (08:58 -0800)
src/documents/consumer.py
src/documents/tests/test_consumer.py

index 88d882350a10de3efa393a74b398d9b443cfec74..45722d01a4470b1b9875a9c6edfaa098d4981ccf 100644 (file)
@@ -163,7 +163,7 @@ class Consumer(LoggingMixin):
             # Raises exception on non-zero output
             completed_proc.check_returncode()
 
-        except Exception as e:  # pragma: nocover
+        except Exception as e:
             self._fail(
                 MESSAGE_PRE_CONSUME_SCRIPT_ERROR,
                 f"Error while executing pre-consume script: {e}",
@@ -237,7 +237,7 @@ class Consumer(LoggingMixin):
             # Raises exception on non-zero output
             completed_proc.check_returncode()
 
-        except Exception as e:  # pragma: nocover
+        except Exception as e:
             self._fail(
                 MESSAGE_POST_CONSUME_SCRIPT_ERROR,
                 f"Error while executing post-consume script: {e}",
index 4a3908f8e3f0e25394b76bd6492fb1a0eb272481..f6685ad02016ceee87cd9c6f1b4b4925a25517fa 100644 (file)
@@ -803,6 +803,15 @@ class TestConsumerCreatedDate(DirectoriesMixin, TestCase):
 
 
 class PreConsumeTestCase(TestCase):
+    def setUp(self) -> None:
+
+        # this prevents websocket message reports during testing.
+        patcher = mock.patch("documents.consumer.Consumer._send_progress")
+        self._send_progress = patcher.start()
+        self.addCleanup(patcher.stop)
+
+        return super().setUp()
+
     @mock.patch("documents.consumer.run")
     @override_settings(PRE_CONSUME_SCRIPT=None)
     def test_no_pre_consume_script(self, m):
@@ -868,8 +877,41 @@ class PreConsumeTestCase(TestCase):
                 mocked_log.assert_any_call("info", "This message goes to stdout")
                 mocked_log.assert_any_call("warning", "This message goes to stderr")
 
+    def test_script_exit_non_zero(self):
+        """
+        GIVEN:
+            - A script which exits with a non-zero exit code
+        WHEN:
+            - The script is executed as a pre-consume script
+        THEN:
+            - A ConsumerError is raised
+        """
+        with tempfile.NamedTemporaryFile(mode="w") as script:
+            # Write up a little script
+            with script.file as outfile:
+                outfile.write("#!/usr/bin/env bash\n")
+                outfile.write("exit 100\n")
+
+            # Make the file executable
+            st = os.stat(script.name)
+            os.chmod(script.name, st.st_mode | stat.S_IEXEC)
+
+            with override_settings(PRE_CONSUME_SCRIPT=script.name):
+                c = Consumer()
+                c.path = "path-to-file"
+                self.assertRaises(ConsumerError, c.run_pre_consume_script)
+
 
 class PostConsumeTestCase(TestCase):
+    def setUp(self) -> None:
+
+        # this prevents websocket message reports during testing.
+        patcher = mock.patch("documents.consumer.Consumer._send_progress")
+        self._send_progress = patcher.start()
+        self.addCleanup(patcher.stop)
+
+        return super().setUp()
+
     @mock.patch("documents.consumer.run")
     @override_settings(POST_CONSUME_SCRIPT=None)
     def test_no_post_consume_script(self, m):
@@ -930,3 +972,29 @@ class PostConsumeTestCase(TestCase):
                 self.assertEqual(command[6], f"/api/documents/{doc.pk}/thumb/")
                 self.assertEqual(command[7], "my_bank")
                 self.assertCountEqual(command[8].split(","), ["a", "b"])
+
+    def test_script_exit_non_zero(self):
+        """
+        GIVEN:
+            - A script which exits with a non-zero exit code
+        WHEN:
+            - The script is executed as a post-consume script
+        THEN:
+            - A ConsumerError is raised
+        """
+        with tempfile.NamedTemporaryFile(mode="w") as script:
+            # Write up a little script
+            with script.file as outfile:
+                outfile.write("#!/usr/bin/env bash\n")
+                outfile.write("exit -500\n")
+
+            # Make the file executable
+            st = os.stat(script.name)
+            os.chmod(script.name, st.st_mode | stat.S_IEXEC)
+
+            with override_settings(POST_CONSUME_SCRIPT=script.name):
+                c = Consumer()
+                doc = Document.objects.create(title="Test", mime_type="application/pdf")
+                c.path = "path-to-file"
+                with self.assertRaises(ConsumerError):
+                    c.run_post_consume_script(doc)