Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

tests for pre and post consume script

jonaswinklerf37321128101
diff --git a/src/documents/consumer.py b/src/documents/consumer.py
index 134137e2f..5418e3b59 100755
--- a/src/documents/consumer.py
+++ b/src/documents/consumer.py
@@ -71,6 +71,11 @@ class Consumer(LoggingMixin):
if not settings.PRE_CONSUME_SCRIPT:
return
+ if not os.path.isfile(settings.PRE_CONSUME_SCRIPT):
+ raise ConsumerError(
+ f"Configured pre-consume script "
+ f"{settings.PRE_CONSUME_SCRIPT} does not exist.")
+
try:
Popen((settings.PRE_CONSUME_SCRIPT, self.path)).wait()
except Exception as e:
@@ -82,6 +87,11 @@ class Consumer(LoggingMixin):
if not settings.POST_CONSUME_SCRIPT:
return
+ if not os.path.isfile(settings.POST_CONSUME_SCRIPT):
+ raise ConsumerError(
+ f"Configured post-consume script "
+ f"{settings.POST_CONSUME_SCRIPT} does not exist.")
+
try:
Popen((
settings.POST_CONSUME_SCRIPT,
diff --git a/src/documents/tests/test_consumer.py b/src/documents/tests/test_consumer.py
index 5049c1dd2..a6861a541 100644
--- a/src/documents/tests/test_consumer.py
+++ b/src/documents/tests/test_consumer.py
@@ -468,6 +468,42 @@ class TestConsumer(DirectoriesMixin, TestCase):
self.assertTrue(os.path.isfile(dst))
+class PreConsumeTestCase(TestCase):
+
+ @mock.patch("documents.consumer.Popen")
+ @override_settings(PRE_CONSUME_SCRIPT=None)
+ def test_no_pre_consume_script(self, m):
+ c = Consumer()
+ c.path = "path-to-file"
+ c.run_pre_consume_script()
+ m.assert_not_called()
+
+ @mock.patch("documents.consumer.Popen")
+ @override_settings(PRE_CONSUME_SCRIPT="does-not-exist")
+ def test_pre_consume_script_not_found(self, m):
+ c = Consumer()
+ c.path = "path-to-file"
+ self.assertRaises(ConsumerError, c.run_pre_consume_script)
+
+ @mock.patch("documents.consumer.Popen")
+ def test_pre_consume_script(self, m):
+ with tempfile.NamedTemporaryFile() as script:
+ with override_settings(PRE_CONSUME_SCRIPT=script.name):
+ c = Consumer()
+ c.path = "path-to-file"
+ c.run_pre_consume_script()
+
+ m.assert_called_once()
+
+ args, kwargs = m.call_args
+
+ command = args[0]
+
+ self.assertEqual(command[0], script.name)
+ self.assertEqual(command[1], "path-to-file")
+
+
+
class PostConsumeTestCase(TestCase):
@mock.patch("documents.consumer.Popen")
@@ -483,36 +519,45 @@ class PostConsumeTestCase(TestCase):
m.assert_not_called()
+
+ @override_settings(POST_CONSUME_SCRIPT="does-not-exist")
+ def test_post_consume_script_not_found(self):
+ doc = Document.objects.create(title="Test", mime_type="application/pdf")
+
+ self.assertRaises(ConsumerError, Consumer().run_post_consume_script, doc)
+
@mock.patch("documents.consumer.Popen")
- @override_settings(POST_CONSUME_SCRIPT="script")
def test_post_consume_script_simple(self, m):
- doc = Document.objects.create(title="Test", mime_type="application/pdf")
+ with tempfile.NamedTemporaryFile() as script:
+ with override_settings(POST_CONSUME_SCRIPT=script.name):
+ doc = Document.objects.create(title="Test", mime_type="application/pdf")
- Consumer().run_post_consume_script(doc)
+ Consumer().run_post_consume_script(doc)
- m.assert_called_once()
+ m.assert_called_once()
@mock.patch("documents.consumer.Popen")
- @override_settings(POST_CONSUME_SCRIPT="script")
def test_post_consume_script_with_correspondent(self, m):
- c = Correspondent.objects.create(name="my_bank")
- doc = Document.objects.create(title="Test", mime_type="application/pdf", correspondent=c)
- tag1 = Tag.objects.create(name="a")
- tag2 = Tag.objects.create(name="b")
- doc.tags.add(tag1)
- doc.tags.add(tag2)
+ with tempfile.NamedTemporaryFile() as script:
+ with override_settings(POST_CONSUME_SCRIPT=script.name):
+ c = Correspondent.objects.create(name="my_bank")
+ doc = Document.objects.create(title="Test", mime_type="application/pdf", correspondent=c)
+ tag1 = Tag.objects.create(name="a")
+ tag2 = Tag.objects.create(name="b")
+ doc.tags.add(tag1)
+ doc.tags.add(tag2)
- Consumer().run_post_consume_script(doc)
+ Consumer().run_post_consume_script(doc)
- m.assert_called_once()
+ m.assert_called_once()
- args, kwargs = m.call_args
+ args, kwargs = m.call_args
- command = args[0]
+ command = args[0]
- self.assertEqual(command[0], "script")
- self.assertEqual(command[1], str(doc.pk))
- self.assertEqual(command[5], f"/api/documents/{doc.pk}/download/")
- self.assertEqual(command[6], f"/api/documents/{doc.pk}/thumb/")
- self.assertEqual(command[7], "my_bank")
- self.assertCountEqual(command[8].split(","), ["a", "b"])
+ self.assertEqual(command[0], script.name)
+ self.assertEqual(command[1], str(doc.pk))
+ self.assertEqual(command[5], f"/api/documents/{doc.pk}/download/")
+ self.assertEqual(command[6], f"/api/documents/{doc.pk}/thumb/")
+ self.assertEqual(command[7], "my_bank")
+ self.assertCountEqual(command[8].split(","), ["a", "b"])